diff --git a/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php b/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php index e4cdb0ba..1fddf3f6 100644 --- a/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php +++ b/DependencyInjection/CompilerPass/CommunityManagerCompilerPass.php @@ -26,9 +26,9 @@ class CommunityManagerCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - $config = $container->getParameter('sulu_community.config'); + $webspacesConfig = $container->getParameter('sulu_community.webspaces_config'); - foreach ($config[Configuration::WEBSPACES] as $webspaceKey => $webspaceConfig) { + foreach ($webspacesConfig as $webspaceKey => $webspaceConfig) { // Set firewall by webspace key if ($webspaceConfig[Configuration::FIREWALL] === null) { $webspaceConfig[Configuration::FIREWALL] = $webspaceKey; diff --git a/DependencyInjection/CompilerPass/CommunityValidatorCompilerPass.php b/DependencyInjection/CompilerPass/CommunityValidatorCompilerPass.php index 887ea3e6..4c44518e 100644 --- a/DependencyInjection/CompilerPass/CommunityValidatorCompilerPass.php +++ b/DependencyInjection/CompilerPass/CommunityValidatorCompilerPass.php @@ -34,9 +34,9 @@ public function process(ContainerBuilder $container) } // Create Validator References - $config = $container->getParameter('sulu_community.config'); + $webspacesConfig = $container->getParameter('sulu_community.webspaces_config'); - foreach ($config[Configuration::WEBSPACES] as $webspaceKey => $webspaceConfig) { + foreach ($webspacesConfig as $webspaceKey => $webspaceConfig) { // Get Completion Validator $validatorId = $webspaceConfig[Configuration::TYPE_COMPLETION][Configuration::SERVICE]; diff --git a/DependencyInjection/Configuration.php b/DependencyInjection/Configuration.php index f60214b3..9aa39dad 100644 --- a/DependencyInjection/Configuration.php +++ b/DependencyInjection/Configuration.php @@ -69,6 +69,10 @@ class Configuration implements ConfigurationInterface const EMAIL_USER_TEMPLATE = 'user_template'; const DELETE_USER = 'delete_user'; + // Other configurations + const LAST_LOGIN = 'last_login'; + const REFRESH_INTERVAL = 'refresh_interval'; + /** * {@inheritdoc} */ @@ -79,6 +83,12 @@ public function getConfigTreeBuilder() $rootNode ->children() + ->arrayNode(self::LAST_LOGIN) + ->canBeEnabled() + ->children() + ->integerNode(self::REFRESH_INTERVAL)->defaultValue(600)->end() + ->end() + ->end() ->arrayNode('webspaces') ->prototype('array') ->children() diff --git a/DependencyInjection/SuluCommunityExtension.php b/DependencyInjection/SuluCommunityExtension.php index 7dfb75b6..a57e64b8 100644 --- a/DependencyInjection/SuluCommunityExtension.php +++ b/DependencyInjection/SuluCommunityExtension.php @@ -32,11 +32,24 @@ public function load(array $configs, ContainerBuilder $container) $configuration = new Configuration(); $config = $this->processConfiguration($configuration, $configs); - $container->setParameter('sulu_community.config', $config); + $container->setParameter('sulu_community.webspaces_config', $config[Configuration::WEBSPACES]); + + $lastLoginEnabled = $config[Configuration::LAST_LOGIN]['enabled']; $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); $loader->load('services.xml'); $loader->load('validator.xml'); + + if ($lastLoginEnabled) { + $lastLoginRefreshInterval = $config[Configuration::LAST_LOGIN][Configuration::REFRESH_INTERVAL]; + + $container->setParameter( + 'sulu_community.last_login.refresh_interval', + $lastLoginRefreshInterval + ); + + $loader->load('last-login.xml'); + } } /** diff --git a/EventListener/LastLoginListener.php b/EventListener/LastLoginListener.php new file mode 100644 index 00000000..6d921faf --- /dev/null +++ b/EventListener/LastLoginListener.php @@ -0,0 +1,95 @@ +tokenStorage = $tokenStorage; + $this->entityManager = $entityManager; + $this->interval = (int) $interval; + } + + /** + * Update the last login in specific interval. + * + * @param GetResponseEvent $event + */ + public function onRequest(GetResponseEvent $event) + { + if (!$event->isMasterRequest()) { + return; + } + + if (!$this->interval) { + return; + } + + // Check token authentication availability + if ($this->tokenStorage->getToken()) { + $user = $this->tokenStorage->getToken()->getUser(); + + if ($user instanceof BaseUser && !$this->isActiveNow($user)) { + $user->setLastLogin(new \DateTime()); + $this->entityManager->flush($user); + } + } + } + + /** + * Check if user was active shortly. + * + * @param BaseUser $user + * + * @return bool + */ + private function isActiveNow(BaseUser $user) + { + $delay = new \DateTime($this->interval . ' seconds ago'); + + return $user->getLastLogin() > $delay; + } +} diff --git a/Resources/config/last-login.xml b/Resources/config/last-login.xml new file mode 100644 index 00000000..9e21f710 --- /dev/null +++ b/Resources/config/last-login.xml @@ -0,0 +1,15 @@ + + + + + + + %sulu_community.last_login.refresh_interval% + + + + + + diff --git a/UPGRADE.md b/UPGRADE.md index 770bbf89..3e0092df 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,12 @@ # Upgrade +## 0.3.0 + +### Parameter `sulu_community.config` was removed + +The whole config as parameter is not longer available the webspaces config +you can get over the `sulu_community.webspaces_config` parameter. + ## 0.2.0 ### Avatar title will use username instead of fullname