/** * Método que verifica se o usuario está logado * @param type $e */ public function validaAutenticacao($e) { $authenticateService = new \Zend\Authentication\AuthenticationService(); $authenticateService->setStorage(new \Zend\Authentication\Storage\Session("Semente")); $sessao = new \Zend\Session\Container("Semente"); $controller = $e->getTarget(); $em = $controller->getServiceLocator()->get('ZeDbManager'); $rotaAcessada = $controller->getEvent()->getRouteMatch()->getMatchedRouteName(); //erro 404: http://pensadores.local:8080/colunas/pensando-cabeca/100 /** Liberando rota para não precisar de autenticação */ $rota_livre = in_array($rotaAcessada, ['acesso/login', 'acesso/logout', 'acesso/nao-autorizado']); if ($rota_livre) { return true; } else { if (!$authenticateService->hasIdentity()) { $controller->redirect()->toRoute("acesso/login"); } else { $controlador = $controller->params()->fromRoute('controller'); $action = $controller->params()->fromRoute('action'); $user = $authenticateService->getIdentity()['cliente']; $esta_autorizado = TRUE; if (!$esta_autorizado) { return $controller->redirect()->toRoute("acesso/nao-autorizado", array('controlador' => $controlador, 'acao' => $action)); } } } }
function coreAuth(&$adapter, &$authService) { $dbAdapter = new \Zend\Db\Adapter\Adapter(array('driver' => 'Pdo', 'username' => 'kevin', 'password' => '123456', 'dsn' => 'mysql:dbname=bd_grupos;host=192.168.1.50', 'driver_options' => array())); $adapter = new \Zend\Authentication\AuthenticationService(); $authService = new \Zend\Authentication\Adapter\DbTable($dbAdapter, 'ta_usuario', 'va_nombre', 'va_contrasena'); $adapter->setStorage(new \Zend\Authentication\Storage\Session('Auth')); $adapter->setAdapter($authService); }
public function getServiceConfig() { return array('factories' => array('auth-storage' => function ($sm) { return new \Sticks\Storage\Auth('user_auth'); }, 'auth-service' => function ($sm) { $doctrineAdapter = $adapter = new \Custom\Auth\Adapter\Doctrine(null, '\\Sticks\\Model\\User', 'email', 'password', 'md5'); $authService = new \Zend\Authentication\AuthenticationService(); $authService->setAdapter($doctrineAdapter); $authService->setStorage($sm->get('auth-storage')); //$authService->setStorage(new \Zend\Authentication\Storage\Session('vasabi-auth')); return $authService; }, 'user-session' => function ($sm) { })); }
/** * Registers Slim Auth services on the given container. * * @param Container $pimple A container instance */ public function register(Container $pimple) { // This must be set to true or Slim Auth will not work. // @see https://github.com/marcelbonnet/slim-auth/issues/37 $pimple['settings']['determineRouteBeforeAppMiddleware'] = true; $pimple['auth'] = function ($c) { $auth = new \Zend\Authentication\AuthenticationService(); $auth->setAdapter($c->get('authAdapter')); if ($c->has('authStorage')) { $auth->setStorage($c->get('authStorage')); } return $auth; }; $pimple['redirectHandler'] = function ($c) { $redirectNotAuthenticated = '/login'; $redirectNotAuthorized = '/403'; if (isset($c['redirectNotAuthenticated'])) { $redirectNotAuthenticated = $c['redirectNotAuthenticated']; } if (isset($c['redirectNotAuthorized'])) { $redirectNotAuthorized = $c['redirectNotAuthorized']; } return new \marcelbonnet\Slim\Auth\Handlers\RedirectHandler($redirectNotAuthenticated, $redirectNotAuthorized); }; $pimple['throwHttpExceptionHandler'] = function ($c) { return new \marcelbonnet\Slim\Auth\Handlers\ThrowHttpExceptionHandler(); }; $pimple['slimAuthRedirectMiddleware'] = function ($c) { return new \marcelbonnet\Slim\Auth\Middleware\Authorization($c->get('auth'), $c->get('acl'), $c->get('redirectHandler')); }; $pimple['slimAuthThrowHttpExceptionMiddleware'] = function ($c) { return new \marcelbonnet\Slim\Auth\Middleware\Authorization($c->get('auth'), $c->get('acl'), $c->get('throwHttpExceptionHandler')); }; $pimple['authenticator'] = function ($c) { return new \marcelbonnet\Slim\Auth\Authenticator($c->get('auth')); }; }
* for the session recorded in the database * * the user and the session entities are never stored in * the container/session, only de-referenced when needed by the * entity manager using their surrogate keys */ $container = $sm->get('Acl\\Authentication\\Storage\\Session'); $sessionPrototype = $sm->get('Acl\\Entity\\Session'); $entityManager = $sm->get('Acl\\Entity\\Manager'); $storage = new \Acl\Model\Authentication\DoctrineSessionStorage(); $storage->setContainer($container)->setSessionPrototype($sessionPrototype)->setEntityManager($entityManager); return $storage; }, 'Acl\\Authentication\\Service' => function ($sm) { $storage = $sm->get('Acl\\Authentication\\Storage\\Doctrine'); $service = new \Zend\Authentication\AuthenticationService(); $service->setStorage($storage); return $service; }, 'Acl\\Authorization\\UserAttributeEvaluator' => function ($sm) { $em = $sm->get('Acl\\EntityManager'); $evaluator = new \Acl\Model\Authorization\UserAttributeEvaluator(); $evaluator->setEntityManager($em); return $evaluator; }, 'Acl\\Authorization\\UserAttributeEvaluatorListener' => function ($sm) { $evaluator = $sm->get('Acl\\Authorization\\UserAttributeEvaluator'); $authService = $sm->get('Acl\\Authentication\\Service'); $routeForwardingContainer = $sm->get('Acl\\Authentication\\Storage\\RouteForwarding'); $listener = new \Acl\Model\Authorization\UserAttributeEvaluatorListener(); $listener->setAuthenticationService($authService)->setUserAttributeEvaluator($evaluator)->setRouteForwardingContainer($routeForwardingContainer); return $listener; }, 'Acl\\View\\CurrentUserListener' => function ($sm) { $view = $sm->get('Acl\\DefaultViewModel');
if (isset($config['map_auth_data_to_adapter_callback'])) { $options->setMapAuthDataToAdapterCallback($config['map_auth_data_to_adapter_callback']); } if (isset($config['fetch_entity_from_identity_callback'])) { $options->setFetchEntityFromIdentityCallback($config['fetch_entity_from_identity_callback']); } } return $options; }, 'deit_authentication_events' => function ($sm) { return new \Zend\EventManager\EventManager(); }, 'deit_authentication_form' => function ($sm) { $form = new \DeitAuthenticationModule\Form\Authentication(); return $form; }, 'deit_authentication_service' => function ($sm) { $service = new \Zend\Authentication\AuthenticationService(); $service->setStorage($sm->get('deit_authentication_storage'))->setAdapter($sm->get('deit_authentication_adapter')); return $service; }, 'deit_authentication_storage' => function ($sm) { $storage = new \Zend\Authentication\Storage\Session(); return $storage; })), 'controllers' => array('invokables' => array('DeitAuthenticationModule\\Controller\\Authentication' => 'DeitAuthenticationModule\\Controller\\AuthenticationController')), 'controller_plugins' => array('factories' => array('identity' => function ($sm) { $sm = $sm->getServiceLocator(); $plugin = new \DeitAuthenticationModule\Controller\Plugin\Identity(); $plugin->setAuthenticationService($sm->get('deit_authentication_service')); return $plugin; })), 'view_helpers' => array('factories' => array('identity' => function ($sm) { $sm = $sm->getServiceLocator(); $plugin = new \DeitAuthenticationModule\View\Helper\Identity(); $plugin->setAuthenticationService($sm->get('deit_authentication_service')); return $plugin; })), 'view_manager' => array('template_map' => array('deit-authentication-module/log-in' => __DIR__ . '/../view/deit-authentication-module/log-in.phtml')));
// replace request with our own $container['request'] = function ($c) { return \MartynBiz\Slim3Controller\Http\Request::createFromEnvironment($c->get('environment')); }; // replace reponse with our own $container['response'] = function ($c) { $headers = new \Slim\Http\Headers(['Content-Type' => 'text/html; charset=UTF-8']); $response = new \MartynBiz\Slim3Controller\Http\Response(200, $headers); return $response->withProtocolVersion($c->get('settings')['httpVersion']); }; $container['auth'] = function ($c) { // we're using Zend's AuthenticationService here $authService = new \Zend\Authentication\AuthenticationService(); // even though SessionStorage is the default container, we want it to use // this app's object and namespace $authService->setStorage(new \Zend\Authentication\Storage\Session('crsrc')); // create an instance of our AuthInterface implemented class // pass in our User model for getCurrentUser method return new \Wordup\Auth\Auth($authService, $c['model.user']); }; $container['flash'] = function ($c) { $storage = new \Zend\Session\Container('crsrc_flash_messages'); return new \MartynBiz\FlashMessage\Flash($storage); }; $container['cache'] = function ($c) { // we wanna set the prefix so not to clash with other apps $backend = new \Predis\Client(null, array('prefix' => 'wordup:')); $adapter = new \Desarrolla2\Cache\Adapter\Predis($backend); return new \Desarrolla2\Cache\Cache($adapter); }; // Models