Add cache decorator for UserModel
This commit is contained in:
parent
aafa1de4d5
commit
ddeb89e2c6
|
|
@ -31,7 +31,7 @@ class AuthenticationMiddleware extends Base implements MiddlewareInterface
|
|||
$this->sessionStorage->scope = 'API';
|
||||
|
||||
if ($this->isUserAuthenticated($username, $password)) {
|
||||
$this->userSession->initialize($this->userModel->getByUsername($username));
|
||||
$this->userSession->initialize($this->userCacheDecorator->getByUsername($username));
|
||||
} elseif (! $this->isAppAuthenticated($username, $password)) {
|
||||
$this->logger->error('API authentication failure for '.$username);
|
||||
throw new AuthenticationFailureException('Wrong credentials');
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ class ReverseProxyAuth extends Base implements PreAuthenticationProviderInterfac
|
|||
$username = $this->request->getRemoteUser();
|
||||
|
||||
if (! empty($username)) {
|
||||
$userProfile = $this->userModel->getByUsername($username);
|
||||
$userProfile = $this->userCacheDecorator->getByUsername($username);
|
||||
$this->userInfo = new ReverseProxyUserProvider($username, $userProfile ?: array());
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -109,7 +109,7 @@ class PasswordResetController extends BaseController
|
|||
$token = $this->passwordResetModel->create($username);
|
||||
|
||||
if ($token !== false) {
|
||||
$user = $this->userModel->getByUsername($username);
|
||||
$user = $this->userCacheDecorator->getByUsername($username);
|
||||
|
||||
$this->emailClient->send(
|
||||
$user['email'],
|
||||
|
|
|
|||
|
|
@ -58,7 +58,8 @@ use Pimple\Container;
|
|||
* @property \Kanboard\Core\Paginator $paginator
|
||||
* @property \Kanboard\Core\Template $template
|
||||
* @property \Kanboard\Decorator\MetadataCacheDecorator $userMetadataCacheDecorator
|
||||
* @property \Kanboard\Decorator\columnRestrictionCacheDecorator $columnRestrictionCacheDecorator
|
||||
* @property \Kanboard\Decorator\UserCacheDecorator $userCacheDecorator
|
||||
* @property \Kanboard\Decorator\ColumnRestrictionCacheDecorator $columnRestrictionCacheDecorator
|
||||
* @property \Kanboard\Decorator\ColumnMoveRestrictionCacheDecorator $columnMoveRestrictionCacheDecorator
|
||||
* @property \Kanboard\Decorator\ProjectRoleRestrictionCacheDecorator $projectRoleRestrictionCacheDecorator
|
||||
* @property \Kanboard\Model\ActionModel $actionModel
|
||||
|
|
|
|||
|
|
@ -88,7 +88,7 @@ class Markdown extends Parsedown
|
|||
{
|
||||
if (! $this->isPublicLink && preg_match('/^@([^\s,!:?]+)/', $Excerpt['text'], $matches)) {
|
||||
$username = rtrim($matches[1], '.');
|
||||
$user = $this->container['userModel']->getByUsername($username);
|
||||
$user = $this->container['userCacheDecorator']->getByUsername($username);
|
||||
|
||||
if (! empty($user)) {
|
||||
$url = $this->container['helper']->url->href('UserViewController', 'profile', array('user_id' => $user['id']));
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
<?php
|
||||
|
||||
namespace Kanboard\Decorator;
|
||||
|
||||
use Kanboard\Core\Cache\CacheInterface;
|
||||
use Kanboard\Model\UserModel;
|
||||
|
||||
/**
|
||||
* Class UserCacheDecorator
|
||||
*
|
||||
* @package Kanboard\Decorator
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class UserCacheDecorator
|
||||
{
|
||||
protected $cachePrefix = 'user_model:';
|
||||
|
||||
/**
|
||||
* @var CacheInterface
|
||||
*/
|
||||
protected $cache;
|
||||
|
||||
/**
|
||||
* @var UserModel
|
||||
*/
|
||||
private $userModel;
|
||||
|
||||
/**
|
||||
* UserCacheDecorator constructor.
|
||||
*
|
||||
* @param CacheInterface $cache
|
||||
* @param UserModel $userModel
|
||||
*/
|
||||
public function __construct(CacheInterface $cache, UserModel $userModel)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->userModel = $userModel;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a specific user by the username
|
||||
*
|
||||
* @access public
|
||||
* @param string $username Username
|
||||
* @return array
|
||||
*/
|
||||
public function getByUsername($username)
|
||||
{
|
||||
$key = $this->cachePrefix.$username;
|
||||
$user = $this->cache->get($key);
|
||||
|
||||
if ($user === null) {
|
||||
$user = $this->userModel->getByUsername($username);
|
||||
$this->cache->set($key, $user);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ use Kanboard\Decorator\ColumnMoveRestrictionCacheDecorator;
|
|||
use Kanboard\Decorator\ColumnRestrictionCacheDecorator;
|
||||
use Kanboard\Decorator\MetadataCacheDecorator;
|
||||
use Kanboard\Decorator\ProjectRoleRestrictionCacheDecorator;
|
||||
use Kanboard\Decorator\UserCacheDecorator;
|
||||
use Pimple\Container;
|
||||
use Pimple\ServiceProviderInterface;
|
||||
|
||||
|
|
@ -40,6 +41,13 @@ class CacheProvider implements ServiceProviderInterface
|
|||
$container['cacheDriver'] = $container['memoryCache'];
|
||||
}
|
||||
|
||||
$container['userCacheDecorator'] = function($c) {
|
||||
return new UserCacheDecorator(
|
||||
$c['memoryCache'],
|
||||
$c['userModel']
|
||||
);
|
||||
};
|
||||
|
||||
$container['userMetadataCacheDecorator'] = function($c) {
|
||||
return new MetadataCacheDecorator(
|
||||
$c['cacheDriver'],
|
||||
|
|
|
|||
Loading…
Reference in New Issue