diff options
Diffstat (limited to 'app')
-rw-r--r-- | app/Auth/Database.php | 14 | ||||
-rw-r--r-- | app/Auth/GitHub.php | 14 | ||||
-rw-r--r-- | app/Auth/Google.php | 14 | ||||
-rw-r--r-- | app/Auth/Ldap.php | 11 | ||||
-rw-r--r-- | app/Auth/RememberMe.php | 10 | ||||
-rw-r--r-- | app/Auth/ReverseProxy.php | 12 | ||||
-rw-r--r-- | app/Controller/Base.php | 2 | ||||
-rw-r--r-- | app/Event/AuthEvent.php | 27 | ||||
-rw-r--r-- | app/ServiceProvider/EventDispatcherProvider.php | 6 | ||||
-rw-r--r-- | app/Subscriber/AuthSubscriber.php | 27 | ||||
-rw-r--r-- | app/Subscriber/BootstrapSubscriber.php | 22 | ||||
-rw-r--r-- | app/common.php | 3 |
12 files changed, 97 insertions, 65 deletions
diff --git a/app/Auth/Database.php b/app/Auth/Database.php index 47dc8e6e..bdb2aeb6 100644 --- a/app/Auth/Database.php +++ b/app/Auth/Database.php @@ -3,7 +3,7 @@ namespace Auth; use Model\User; -use Core\Request; +use Event\AuthEvent; /** * Database authentication @@ -33,18 +33,8 @@ class Database extends Base $user = $this->db->table(User::TABLE)->eq('username', $username)->eq('is_ldap_user', 0)->findOne(); if ($user && password_verify($password, $user['password'])) { - - // Update user session $this->user->updateSession($user); - - // Update login history - $this->lastLogin->create( - self::AUTH_NAME, - $user['id'], - Request::getIpAddress(), - Request::getUserAgent() - ); - + $this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id'])); return true; } diff --git a/app/Auth/GitHub.php b/app/Auth/GitHub.php index 034f9260..a785c494 100644 --- a/app/Auth/GitHub.php +++ b/app/Auth/GitHub.php @@ -2,7 +2,7 @@ namespace Auth; -use Core\Request; +use Event\AuthEvent; use OAuth\Common\Storage\Session; use OAuth\Common\Consumer\Credentials; use OAuth\Common\Http\Uri\UriFactory; @@ -35,18 +35,8 @@ class GitHub extends Base $user = $this->user->getByGitHubId($github_id); if ($user) { - - // Create the user session $this->user->updateSession($user); - - // Update login history - $this->lastLogin->create( - self::AUTH_NAME, - $user['id'], - Request::getIpAddress(), - Request::getUserAgent() - ); - + $this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id'])); return true; } diff --git a/app/Auth/Google.php b/app/Auth/Google.php index 587ecde1..f779cfe5 100644 --- a/app/Auth/Google.php +++ b/app/Auth/Google.php @@ -2,7 +2,7 @@ namespace Auth; -use Core\Request; +use Event\AuthEvent; use OAuth\Common\Storage\Session; use OAuth\Common\Consumer\Credentials; use OAuth\Common\Http\Uri\UriFactory; @@ -36,18 +36,8 @@ class Google extends Base $user = $this->user->getByGoogleId($google_id); if ($user) { - - // Create the user session $this->user->updateSession($user); - - // Update login history - $this->lastLogin->create( - self::AUTH_NAME, - $user['id'], - Request::getIpAddress(), - Request::getUserAgent() - ); - + $this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id'])); return true; } diff --git a/app/Auth/Ldap.php b/app/Auth/Ldap.php index 82307e8c..b3c998f9 100644 --- a/app/Auth/Ldap.php +++ b/app/Auth/Ldap.php @@ -2,7 +2,7 @@ namespace Auth; -use Core\Request; +use Event\AuthEvent; /** * LDAP model @@ -55,14 +55,7 @@ class Ldap extends Base // We open the session $this->user->updateSession($user); - - // Update login history - $this->lastLogin->create( - self::AUTH_NAME, - $user['id'], - Request::getIpAddress(), - Request::getUserAgent() - ); + $this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id'])); return true; } diff --git a/app/Auth/RememberMe.php b/app/Auth/RememberMe.php index cb8a9b44..9f2bb13a 100644 --- a/app/Auth/RememberMe.php +++ b/app/Auth/RememberMe.php @@ -3,6 +3,7 @@ namespace Auth; use Core\Request; +use Event\AuthEvent; use Core\Security; /** @@ -103,12 +104,9 @@ class RememberMe extends Base $this->user->updateSession($this->user->getById($record['user_id'])); $this->acl->isRememberMe(true); - // Update last login infos - $this->lastLogin->create( - self::AUTH_NAME, - $this->acl->getUserId(), - Request::getIpAddress(), - Request::getUserAgent() + $this->container['dispatcher']->dispatch( + 'auth.success', + new AuthEvent(self::AUTH_NAME, $this->acl->getUserId()) ); return true; diff --git a/app/Auth/ReverseProxy.php b/app/Auth/ReverseProxy.php index 23e71a22..9d766a5b 100644 --- a/app/Auth/ReverseProxy.php +++ b/app/Auth/ReverseProxy.php @@ -2,7 +2,7 @@ namespace Auth; -use Core\Request; +use Event\AuthEvent; /** * ReverseProxy backend @@ -37,16 +37,8 @@ class ReverseProxy extends Base $user = $this->user->getByUsername($login); } - // Create the user session $this->user->updateSession($user); - - // Update login history - $this->lastLogin->create( - self::AUTH_NAME, - $user['id'], - Request::getIpAddress(), - Request::getUserAgent() - ); + $this->container['dispatcher']->dispatch('auth.success', new AuthEvent(self::AUTH_NAME, $user['id'])); return true; } diff --git a/app/Controller/Base.php b/app/Controller/Base.php index 8330f6d5..19eb7f12 100644 --- a/app/Controller/Base.php +++ b/app/Controller/Base.php @@ -9,6 +9,7 @@ use Core\Response; use Core\Template; use Core\Session; use Model\LastLogin; +use Symfony\Component\EventDispatcher\Event; /** * Base controller @@ -140,6 +141,7 @@ abstract class Base { // Start the session $this->session->open(BASE_URL_DIRECTORY); + $this->container['dispatcher']->dispatch('session.bootstrap', new Event); // HTTP secure headers $this->response->csp(array('style-src' => "'self' 'unsafe-inline'")); diff --git a/app/Event/AuthEvent.php b/app/Event/AuthEvent.php new file mode 100644 index 00000000..ec1bec99 --- /dev/null +++ b/app/Event/AuthEvent.php @@ -0,0 +1,27 @@ +<?php + +namespace Event; + +use Symfony\Component\EventDispatcher\Event as BaseEvent; + +class AuthEvent extends BaseEvent +{ + private $auth_name; + private $user_id; + + public function __construct($auth_name, $user_id) + { + $this->auth_name = $auth_name; + $this->user_id = $user_id; + } + + public function getUserId() + { + return $this->user_id; + } + + public function getAuthType() + { + return $this->auth_name; + } +} diff --git a/app/ServiceProvider/EventDispatcherProvider.php b/app/ServiceProvider/EventDispatcherProvider.php index 90210ba6..fd0f7a84 100644 --- a/app/ServiceProvider/EventDispatcherProvider.php +++ b/app/ServiceProvider/EventDispatcherProvider.php @@ -4,18 +4,22 @@ namespace ServiceProvider; use Pimple\Container; use Pimple\ServiceProviderInterface; +use Symfony\Component\EventDispatcher\EventDispatcher; +use Subscriber\AuthSubscriber; +use Subscriber\BootstrapSubscriber; use Subscriber\NotificationSubscriber; use Subscriber\ProjectActivitySubscriber; use Subscriber\ProjectDailySummarySubscriber; use Subscriber\ProjectModificationDateSubscriber; use Subscriber\WebhookSubscriber; -use Symfony\Component\EventDispatcher\EventDispatcher; class EventDispatcherProvider implements ServiceProviderInterface { public function register(Container $container) { $container['dispatcher'] = new EventDispatcher; + $container['dispatcher']->addSubscriber(new BootstrapSubscriber($container)); + $container['dispatcher']->addSubscriber(new AuthSubscriber($container)); $container['dispatcher']->addSubscriber(new ProjectActivitySubscriber($container)); $container['dispatcher']->addSubscriber(new ProjectDailySummarySubscriber($container)); $container['dispatcher']->addSubscriber(new ProjectModificationDateSubscriber($container)); diff --git a/app/Subscriber/AuthSubscriber.php b/app/Subscriber/AuthSubscriber.php new file mode 100644 index 00000000..161a7afd --- /dev/null +++ b/app/Subscriber/AuthSubscriber.php @@ -0,0 +1,27 @@ +<?php + +namespace Subscriber; + +use Core\Request; +use Event\AuthEvent; +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class AuthSubscriber extends Base implements EventSubscriberInterface +{ + public static function getSubscribedEvents() + { + return array( + 'auth.success' => array('onSuccess', 0), + ); + } + + public function onSuccess(AuthEvent $event) + { + $this->lastLogin->create( + $event->getAuthType(), + $event->getUserId(), + Request::getIpAddress(), + Request::getUserAgent() + ); + } +} diff --git a/app/Subscriber/BootstrapSubscriber.php b/app/Subscriber/BootstrapSubscriber.php new file mode 100644 index 00000000..30b8f168 --- /dev/null +++ b/app/Subscriber/BootstrapSubscriber.php @@ -0,0 +1,22 @@ +<?php + +namespace Subscriber; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; + +class BootstrapSubscriber extends Base implements EventSubscriberInterface +{ + public static function getSubscribedEvents() + { + return array( + 'session.bootstrap' => array('setup', 0), + 'api.bootstrap' => array('setup', 0), + ); + } + + public function setup() + { + $this->container['config']->setupTranslations(); + $this->container['config']->setupTimezone(); + } +} diff --git a/app/common.php b/app/common.php index 14d8f8ba..12132189 100644 --- a/app/common.php +++ b/app/common.php @@ -15,6 +15,3 @@ $container->register(new ServiceProvider\DatabaseProvider); $container->register(new ServiceProvider\ModelProvider); $container->register(new ServiceProvider\EventDispatcherProvider); $container->register(new ServiceProvider\MailerProvider); - -$container['config']->setupTranslations(); -$container['config']->setupTimezone(); |