diff options
-rw-r--r-- | app/Api/Middleware/AuthenticationMiddleware.php | 2 | ||||
-rw-r--r-- | app/Auth/ReverseProxyAuth.php | 2 | ||||
-rw-r--r-- | app/Controller/PasswordResetController.php | 2 | ||||
-rw-r--r-- | app/Core/Base.php | 3 | ||||
-rw-r--r-- | app/Core/Markdown.php | 2 | ||||
-rw-r--r-- | app/Decorator/UserCacheDecorator.php | 59 | ||||
-rw-r--r-- | app/ServiceProvider/CacheProvider.php | 8 |
7 files changed, 73 insertions, 5 deletions
diff --git a/app/Api/Middleware/AuthenticationMiddleware.php b/app/Api/Middleware/AuthenticationMiddleware.php index c4fa874a..174dc467 100644 --- a/app/Api/Middleware/AuthenticationMiddleware.php +++ b/app/Api/Middleware/AuthenticationMiddleware.php @@ -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'); diff --git a/app/Auth/ReverseProxyAuth.php b/app/Auth/ReverseProxyAuth.php index 02afc302..bf71e71e 100644 --- a/app/Auth/ReverseProxyAuth.php +++ b/app/Auth/ReverseProxyAuth.php @@ -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; } diff --git a/app/Controller/PasswordResetController.php b/app/Controller/PasswordResetController.php index a1780ed9..6189f946 100644 --- a/app/Controller/PasswordResetController.php +++ b/app/Controller/PasswordResetController.php @@ -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'], diff --git a/app/Core/Base.php b/app/Core/Base.php index e7ccafaa..881cccbd 100644 --- a/app/Core/Base.php +++ b/app/Core/Base.php @@ -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 diff --git a/app/Core/Markdown.php b/app/Core/Markdown.php index 5d723bad..4487bf2a 100644 --- a/app/Core/Markdown.php +++ b/app/Core/Markdown.php @@ -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'])); diff --git a/app/Decorator/UserCacheDecorator.php b/app/Decorator/UserCacheDecorator.php new file mode 100644 index 00000000..1cfe31c9 --- /dev/null +++ b/app/Decorator/UserCacheDecorator.php @@ -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; + } +} diff --git a/app/ServiceProvider/CacheProvider.php b/app/ServiceProvider/CacheProvider.php index e93dd502..af8a8e7a 100644 --- a/app/ServiceProvider/CacheProvider.php +++ b/app/ServiceProvider/CacheProvider.php @@ -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'], |