Example #1
0
 public function onBootstrap(MvcEvent $e)
 {
     $e->getApplication()->getServiceManager()->get('translator');
     $eventManager = $e->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     // Add translation
     $translator = $e->getApplication()->getServiceManager()->get('translator');
     if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
         $setLang = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
     } else {
         $setLang = '';
     }
     $translator->setLocale(\Locale::acceptFromHttp($setLang))->setFallbackLocale('en_US');
     // Add ACL information to the Navigation view helper
     $sm = $e->getApplication()->getServiceManager();
     $authorize = $sm->get('BjyAuthorize\\Service\\Authorize');
     $acl = $authorize->getAcl();
     $role = $authorize->getIdentity();
     \Zend\View\Helper\Navigation::setDefaultAcl($acl);
     \Zend\View\Helper\Navigation::setDefaultRole($role);
     $e->getApplication()->getEventManager()->getSharedManager()->attach('Zend\\Mvc\\Application', 'dispatch.error', function ($e) use($sm) {
         if ($e->getParam('exception')) {
             $sm->get('Zend\\Log\\Logger')->crit($e->getParam('exception'));
         }
     });
 }
Example #2
0
 public function onBootstrap(MvcEvent $e)
 {
     $app = $e->getApplication();
     $eventManager = $app->getEventManager();
     $sm = $app->getServiceManager();
     // initialize SessionManager
     $this->bootstrapSession($e);
     // Attach UserListener for role and UserProfile handling
     $listener = $sm->get(UserListener::class);
     $eventManager->attach($listener);
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'initTranslator'), -100);
     $eventManager->attach(MvcEvent::EVENT_ROUTE, array($this, 'setUserLanguage'));
     // Enable BjyAuthorize when not in console mode
     if (!\Zend\Console\Console::isConsole()) {
         // Add ACL information to the Navigation view helper
         $authorize = $sm->get(Authorize::class);
         $acl = $authorize->getAcl();
         $role = $authorize->getIdentity();
         Navigation::setDefaultAcl($acl);
         Navigation::setDefaultRole($role);
     }
     /* @var $translator \Zend\I18n\Translator\Translator */
     $translator = $e->getApplication()->getServiceManager()->get('MvcTranslator');
     // add Db Loader factory
     $translator->getPluginManager()->setFactory(I18n\Translator\Loader\Db::class, I18n\Translator\Loader\Factory\DbFactory::class);
     if ($e->getRouter() instanceof \Zend\Mvc\Router\Http\TranslatorAwareTreeRouteStack) {
         $e->getRouter()->setTranslator($translator);
     }
 }
Example #3
0
 public function onBootstrap(MvcEvent $e)
 {
     $eventManager = $e->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $request = $e->getApplication()->getServiceManager()->get('Request');
     $moduleRouteListener->attach($eventManager);
     $adapter = $e->getApplication()->getServiceManager()->get('Zend\\Db\\Adapter\\Adapter');
     $config = $e->getApplication()->getServiceManager()->get('Configuration');
     $locale = null;
     $eventManager->attach(new UserRegisterListener($adapter));
     $eventManager->attach(new LogListener());
     // Add ACL information to the Navigation view helper
     $authorize = $e->getApplication()->getServiceManager()->get('BjyAuthorize\\Service\\Authorize');
     $acl = $authorize->getAcl();
     $role = $authorize->getIdentity();
     \Zend\View\Helper\Navigation::setDefaultAcl($acl);
     \Zend\View\Helper\Navigation::setDefaultRole($role);
     // translating system
     $sessionConfig = new SessionConfig();
     $sessionConfig->setOptions($config['session']);
     $sessionManager = new SessionManager($sessionConfig);
     $sessionManager->start();
     $session = new Container('base');
     // Get the visitor language selection
     $translator = $e->getApplication()->getServiceManager()->get('translator');
     // get the locale from the cookie
     $headCookie = $request->getHeaders()->get('Cookie');
     if (!empty($headCookie) && array_key_exists('locale', get_object_vars($headCookie))) {
         $locale = $headCookie->locale;
     }
     if (empty($locale)) {
         // if there is not set any cookie
         $locale = $session->offsetGet('locale');
         // Get the locale from the session
         if (empty($locale)) {
             // if there is not any session set yet
             $headers = $request->getHeaders();
             if ($headers->has('Accept-Language')) {
                 $locales = $headers->get('Accept-Language')->getPrioritized();
                 $first = array_shift($locales);
                 $locale = $first->getLanguage();
                 if (!empty($locale) && 2 == strlen($locale)) {
                     $locale .= "_" . strtoupper($locale);
                 }
             }
             if (empty($locale)) {
                 // if the browser has no locale set, we have to get the default INTL global locale setting
                 $locale = \Locale::getPrimaryLanguage(\Locale::getDefault());
                 // Gets the default locale value from the INTL global 'default_locale'
             }
         }
     } else {
         #\Zend\Debug\debug::dump("Cookie set with locale: $locale");
     }
     $translator->setLocale(\Locale::acceptFromHttp($locale));
     $translator->setLocale($locale)->setFallbackLocale('en_US');
     \Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
     \Zend\Validator\AbstractValidator::setDefaultTranslatorTextDomain();
 }
Example #4
0
 public function onBootstrap(MvcEvent $e)
 {
     $eventManager = $e->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     $sm = $e->getApplication()->getServiceManager();
     // Add ACL information to the Navigation view helper
     $authorize = $sm->get('BjyAuthorizeServiceAuthorize');
     $acl = $authorize->getAcl();
     $role = $authorize->getIdentity();
     Navigation::setDefaultAcl($acl);
     Navigation::setDefaultRole($role);
 }
Example #5
0
 public function onBootstrap(EventInterface $e)
 {
     /* @var $application \Zend\Mvc\Application */
     $application = $e->getTarget();
     $eventManager = $application->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     //todo Delete this hack (avoid unit tests) after update BjyAuthorize module to 2.0
     if (\Zend\Console\Console::isConsole()) {
         return;
     }
     $sm = $e->getApplication()->getServiceManager();
     // Add ACL information to the Navigation view helper
     $authorize = $sm->get('BjyAuthorizeServiceAuthorize');
     $acl = $authorize->getAcl();
     $role = $authorize->getIdentity();
     ZendViewHelperNavigation::setDefaultAcl($acl);
     ZendViewHelperNavigation::setDefaultRole($role);
     $services = $application->getServiceManager();
     $zfcServiceEvents = $services->get('zfcuser_user_service')->getEventManager();
     $zfcServiceEvents->attach('register', function ($e) use($services) {
         $zfcUser = $e->getParam('user');
         $em = $services->get('doctrine.entitymanager.orm_default');
         $configAuth = $services->get('BjyAuthorize\\Config');
         $providerConfig = $configAuth['role_providers']['BjyAuthorize\\Provider\\Role\\ObjectRepositoryProvider'];
         $criteria = array('roleId' => $configAuth['authenticated_role']);
         $defaultUserRole = $em->getRepository($providerConfig['role_entity_class'])->findOneBy($criteria);
         if ($defaultUserRole !== null) {
             $zfcUser->addRole($defaultUserRole);
         }
     });
     $application->getEventManager()->getSharedManager()->attach('ZfcUserAdmin\\Form\\EditUser', 'init', function ($e) {
         // $form is a ZfcUser\Form\Register
         $form = $e->getTarget();
         $sm = $form->getServiceManager();
         $om = $sm->get('Doctrine\\ORM\\EntityManager');
         //$form->setHydrator(new \DoctrineORMModule\Stdlib\Hydrator\DoctrineEntity($om, 'OpsWay\TocatUser\Entity\User'));
         $form->add(array('name' => 'roles', 'type' => 'DoctrineModule\\Form\\Element\\ObjectMultiCheckbox', 'options' => array('label' => 'Assign Roles', 'object_manager' => $om, 'target_class' => Entity\Role::class, 'property' => 'roleId')));
         $form->add(array('name' => 'groups', 'type' => 'DoctrineModule\\Form\\Element\\ObjectSelect', 'options' => array('label' => 'Assign Groups', 'object_manager' => $om, 'target_class' => Entity\Group::class, 'property' => 'name'), 'attributes' => array('multiple' => true)));
     });
     $application->getEventManager()->getSharedManager()->attach('ZfcUserAdmin\\Service\\User', 'edit', function ($e) {
         $zfcUser = $e->getParam('user');
         $post = $e->getParam('data');
         $em = $e->getParam('form')->getServiceManager()->get('doctrine.entitymanager.orm_default');
         $listRoles = $em->getRepository(Entity\Role::class)->findBy(array('id' => $post['roles']));
         $zfcUser->updateRoles($listRoles);
         $listGroup = $em->getRepository(Entity\Group::class)->findBy(array('id' => $post['groups']));
         $zfcUser->updateGroups($listGroup);
     });
 }
Example #6
0
 public function onBootstrap($e)
 {
     $app = $e->getApplication();
     $em = $app->getEventManager();
     $sm = $app->getServiceManager();
     $list = $this->whitelist;
     $auth = $sm->get('zfcuser_auth_service');
     # global check access
     $em->attach(MvcEvent::EVENT_ROUTE, function ($e) use($list, $auth) {
         $match = $e->getRouteMatch();
         # No route match, this is a 404
         if (!$match instanceof RouteMatch) {
             return;
         }
         # Route is whitelisted
         $name = $match->getMatchedRouteName();
         if (in_array($name, $list)) {
             return;
         }
         # User is authenticated
         if ($auth->hasIdentity()) {
             return;
         }
         # Redirect to the user login page
         $router = $e->getRouter();
         $url = $router->assemble(array(), array('name' => 'zfcuser/login'));
         $response = $e->getResponse();
         $response->getHeaders()->addHeaderLine('Location', $url);
         $response->setStatusCode(302);
         return $response;
     }, -100);
     # if not auth change Template
     if (!$auth->hasIdentity()) {
         $em->attach(MvcEvent::EVENT_ROUTE, function ($e) {
             $vm = $e->getViewModel();
             $vm->setTemplate('layout/blank');
         });
     }
     # Add ACL information to the Navigation view helper
     $authorize = $sm->get('BjyAuthorizeServiceAuthorize');
     $acl = $authorize->getAcl();
     $role = $authorize->getIdentity();
     Navigation::setDefaultAcl($acl);
     Navigation::setDefaultRole($role);
 }
Example #7
0
 public function onBootstrap(MvcEvent $e)
 {
     $eventManager = $e->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     $adapter = $e->getApplication()->getServiceManager()->get('Zend\\Db\\Adapter\\Adapter');
     $sm = $e->getApplication()->getServiceManager();
     $config = $e->getApplication()->getServiceManager()->get('Configuration');
     $settings = $e->getApplication()->getServiceManager()->get('SettingsService');
     $eventManager->attach(new UserRegisterListener($adapter));
     $eventManager->attach(new LogListener());
     // Add ACL information to the Navigation view helper
     $authorize = $e->getApplication()->getServiceManager()->get('BjyAuthorize\\Service\\Authorize');
     $acl = $authorize->getAcl();
     $role = $authorize->getIdentity();
     \Zend\View\Helper\Navigation::setDefaultAcl($acl);
     \Zend\View\Helper\Navigation::setDefaultRole($role);
     // translating system
     $sessionConfig = new SessionConfig();
     $sessionConfig->setOptions($config['session']);
     $sessionManager = new SessionManager($sessionConfig);
     $sessionManager->start();
     $session = new Container('base');
     // Get the visitor language selection
     $translator = $e->getApplication()->getServiceManager()->get('translator');
     $locale = $session->offsetGet('locale');
     // Get the locale
     if (empty($locale)) {
         $locale = \Locale::getPrimaryLanguage(\Locale::getDefault());
         // Get the locale
     }
     if (!empty($locale) && 2 == strlen($locale)) {
         $locale .= "_" . strtoupper($locale);
     }
     $translator->setLocale($locale)->setFallbackLocale('en_US');
     $isCompress = $settings->getValueByParameter('Base', 'iscompressed');
     if ($isCompress) {
         $eventManager->getSharedManager()->attach('Zend\\Mvc\\Application', 'finish', array($this, 'compressHtml'), 1002);
     }
 }
Example #8
0
 public function onBootstrap(MvcEvent $e)
 {
     $eventManager = $e->getApplication()->getEventManager();
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     /* Translator */
     $translator = $e->getApplication()->getServiceManager()->get('translator');
     $translator->addTranslationFile('phpArray', './vendor/zendframework/zend-i18n-resources/languages/pt_BR/Zend_Validate.php', 'default', 'pt_BR');
     \Zend\Validator\AbstractValidator::setDefaultTranslator($translator);
     /* Injeta o ACL e o usuário no Navigator */
     $auth = $e->getApplication()->getServiceManager()->get('Admin\\Service\\Auth');
     $acl = $auth->getAcl();
     $role = $auth->getRole();
     \Zend\View\Helper\Navigation::setDefaultAcl($acl);
     \Zend\View\Helper\Navigation::setDefaultRole($role);
     // Remove itens do menu
     if ($role == 'admin') {
         $container = $e->getApplication()->getServiceManager()->get('navigation');
         $home = $container->findBy('route', 'home');
         $entrar = $container->findBy('uri', '/admin/auth');
         $container->removePage($home);
         $container->removePage($entrar);
     }
 }
Example #9
0
 public function onBootstrap($e)
 {
     $eventManager = $e->getApplication()->getEventManager();
     $eventManager->getSharedManager()->attach('Zend\\Mvc\\Controller\\AbstractActionController', 'dispatch', function ($e) {
         $controller = $e->getTarget();
         $controllerClass = get_class($controller);
         $moduleNamespace = substr($controllerClass, 0, strpos($controllerClass, '\\'));
         $config = $e->getApplication()->getServiceManager()->get('config');
         if (isset($config['module_layouts'][$moduleNamespace])) {
             $controller->layout($config['module_layouts'][$moduleNamespace]);
         }
     }, 100);
     $moduleRouteListener = new ModuleRouteListener();
     $moduleRouteListener->attach($eventManager);
     $this->bootstrapSession($e);
     $application = $e->getApplication();
     $sm = $application->getServiceManager();
     #$sm   = $e->getApplication()->getServiceManager();
     $auth = $sm->get('BjyAuthorize\\Service\\Authorize');
     if (!\Zend\Console\Console::isConsole()) {
         $acl = $auth->getAcl();
         $role = $auth->getIdentity();
         \Zend\View\Helper\Navigation::setDefaultAcl($acl);
         \Zend\View\Helper\Navigation::setDefaultRole($role);
     }
     $sharedManager = $application->getEventManager()->getSharedManager();
     $sharedManager->attach('Zend\\Mvc\\Application', 'dispatch.error', function ($e) use($sm) {
         if ($e->getParam('exception')) {
             $sm->get('Logger')->crit($e->getParam('exception'));
             $auth = $sm->get('zfcuser_auth_service');
             if (!$auth->hasIdentity()) {
                 $url = $e->getRouter()->assemble(array(), array('name' => 'zfcuser/login'));
                 $response = $e->getResponse();
                 $response->getHeaders()->addHeaderLine('Location', $url);
                 $response->setStatusCode(302);
                 $response->sendHeaders();
                 // When an MvcEvent Listener returns a Response object,
                 // It automatically short-circuit the Application running
                 // -> true only for Route Event propagation see Zend\Mvc\Application::run
                 // To avoid additional processing
                 // we can attach a listener for Event Route with a high priority
                 $stopCallBack = function ($event) use($response) {
                     $event->stopPropagation();
                     return $response;
                 };
                 //Attach the "break" as a listener with a high priority
                 $e->getApplication()->getEventManager()->attach(MvcEvent::EVENT_ROUTE, $stopCallBack, -10000);
                 return $response;
             }
             $emailService = $sm->get('ErsBase\\Service\\EmailService');
             $emailService->sendExceptionEmail($e->getParam('exception'));
         }
     });
     $zfcAuthEvents = $sm->get('ZfcUser\\Authentication\\Adapter\\AdapterChain')->getEventManager();
     $zfcAuthEvents->attach('authenticate.success', function ($authEvent) use($sm) {
         $loginService = $sm->get('PreReg\\Service\\LoginService');
         $user_id = $authEvent->getIdentity();
         $loginService->setUserId($user_id);
         $loginService->onLogin();
         error_log('this is on the login');
         return true;
     });
     $zfcAuthEvents->attach('logout', function ($authEvent) use($sm) {
         $loginService = $sm->get('PreReg\\Service\\LoginService');
         #$user_id = $authEvent->getIdentity();
         #$loginService->setUserId($user_id);
         $loginService->onLogout();
         return true;
     });
 }
 /**
  * 
  * @param MvcEvent $e
  */
 public function onRender(MvcEvent $e)
 {
     Navigation::setDefaultAcl($e->getApplication()->getServiceManager()->get('Acl'));
     Navigation::setDefaultRole($this->getRole($e));
 }