/** * Authorizes user * * @param array $data * @return boolean **/ public function auth($data) { $form = $this->getForm(); $form->setData($data); if (!$form->isValid()) { $this->addMessage('Stop it!', self::MSG_ERROR); return false; } $adapter = new ObjectRepository(array('objectManager' => $this->em, 'identityClass' => 'Blog\\Model\\User', 'identityProperty' => 'email', 'credentialProperty' => 'password', 'credentialCallable' => 'Blog\\Model\\User::hashPassword')); $adapter->setIdentityValue($data['email']); $adapter->setCredentialValue($data['password']); $result = $this->getAuthService()->authenticate($adapter); if (!$result->isValid()) { //Probably should not give users too much info //$form->get('email')->setMessages($result->getMessages()); $this->addMessage('Stop it!', self::MSG_ERROR); return false; } $this->addMessage('Successfully logged in.', self::MSG_NOTICE); return true; }
/** * Authenticate user by webservice * @param string $usuario * @param string $senha * @return array */ public function autentica(string $login, string $senha) { $adapter = new ObjectRepository(); $auth = new AuthenticationService(); $adapter->setOptions(array('object_manager' => Conn::getConn(), 'identity_class' => 'MyClasses\\Entities\\AclUsuario', 'identity_property' => 'login', 'credential_property' => 'senha')); $adapter->setIdentityValue($login); $adapter->setCredentialValue(sha1($senha)); $resultado = $auth->authenticate($adapter); if ($resultado->isValid()) { foreach ($resultado->getIdentity()->getEquipes() as $equipe) { $equipes[] = array($equipe->getId(), mb_convert_encoding($equipe->getPerfil(), 'UTF-8')); } return array(0 => array(0 => array($resultado->getIdentity()->getId(), mb_convert_encoding($resultado->getIdentity()->getNome(), 'UTF-8'))), 1 => $equipes); /* return base64_encode( gzcompress( serialize($resultado->getIdentity()))); */ } else { return array(0 => "login invalido"); } //throw new Exception('Exception with some special chars: Äßö'); }
/** * Faz a autenticação dos usuários * * @param array $params * @return array */ public function authenticate($params) { if (!isset($params['userName']) || !isset($params['password'])) { throw new \Exception("Parâmetros inválidos"); } $password = sha1(md5($params['password'])); $entityManager = $this->getEntityManager(); $adapter = new DoctrineAdapter(array('objectManager' => $entityManager, 'identityClass' => 'Core\\Entity\\System\\Users', 'identityProperty' => 'userName', 'credentialProperty' => 'password')); $adapter->setIdentityValue($params['userName']); $adapter->setCredentialValue($password); $authService = new AuthenticationService(); $authService->setAdapter($adapter); $authResult = $authService->authenticate(); if (!$authResult->isValid()) { throw new \Exception("Login ou senha inválidos"); } else { $user = $this->getEntityManager()->getRepository('Core\\Entity\\Sistem\\Users')->findOneBy(array('userName' => $params['userName'], 'password' => $password)); $session = $this->getServiceManager()->get('Session'); $session->offsetSet('sysUserData', $user); return true; } }
public function testWillNotCastAuthCredentialValue() { $objectRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $adapter = new ObjectRepositoryAdapter(); $entity = new IdentityObject(); $entity->setPassword(0); $adapter->setOptions(array('object_repository' => $objectRepository, 'credential_property' => 'password', 'identity_property' => 'username')); $adapter->setIdentity('a username'); $adapter->setCredential('00000'); $objectRepository->expects($this->once())->method('findOneBy')->with($this->equalTo(array('username' => 'a username')))->will($this->returnValue($entity)); $this->assertFalse($adapter->authenticate()->isValid()); }
public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound() { $this->setExpectedException('Zend\\Authentication\\Adapter\\Exception\\UnexpectedValueException'); $objectRepository = $this->getMock('Doctrine\\Common\\Persistence\\ObjectRepository'); $objectRepository->expects($this->once())->method('findOneBy')->with($this->equalTo(array('username' => 'a username')))->will($this->returnValue(new \stdClass())); $adapter = new ObjectRepositoryAdapter(); $adapter->setOptions(array('object_repository' => $objectRepository, 'credential_property' => 'password', 'identity_property' => 'username')); $adapter->setIdentityValue('a username'); $adapter->setCredentialValue('a password'); $adapter->authenticate(); }
/** * Constructor * * @param array|AuthenticationOptions $options */ public function __construct($options = array()) { parent::__construct($options); }