Created
November 4, 2012 13:58
-
-
Save eminetto/4012039 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Admin\Service; | |
use Core\Service\Service; | |
use Zend\Authentication\AuthenticationService; | |
use Zend\Authentication\Adapter\DbTable as AuthAdapter; | |
use Zend\Db\Sql\Select; | |
/** | |
* Serviço responsável pela autenticação da aplicação | |
* | |
* @category Admin | |
* @package Service | |
* @author Elton Minetto<[email protected]> | |
*/ | |
class Auth extends Service | |
{ | |
/** | |
* Adapter usado para a autenticação | |
* @var Zend\Db\Adapter\Adapter | |
*/ | |
private $dbAdapter; | |
/** | |
* Construtor da classe | |
* | |
* @return void | |
*/ | |
public function __construct($dbAdapter = null) | |
{ | |
$this->dbAdapter = $dbAdapter; | |
} | |
/** | |
* 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 = md5($params['password']); | |
$auth = new AuthenticationService(); | |
$authAdapter = new AuthAdapter($this->dbAdapter); | |
$authAdapter | |
->setTableName('users') | |
->setIdentityColumn('username') | |
->setCredentialColumn('password') | |
->setIdentity($params['username']) | |
->setCredential($password); | |
$result = $auth->authenticate($authAdapter); | |
if (! $result->isValid()) { | |
throw new \Exception("Login ou senha inválidos"); | |
} | |
//salva o user na sessão | |
$session = $this->getServiceManager()->get('Session'); | |
$session->offsetSet('user', $authAdapter->getResultRowObject()); | |
return true; | |
} | |
/** | |
* Faz o logout do sistema | |
* | |
* @return void | |
*/ | |
public function logout() { | |
$auth = new AuthenticationService(); | |
$session = $this->getServiceManager()->get('Session'); | |
$session->offsetUnset('user'); | |
$auth->clearIdentity(); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A função authenticate não tava passando no testAuthenticateWithoutParams eu mudei aqui, ta passando nos testes agora mas eu não sei se tá certo.
A mudança que eu fiz foram 2:
Mudei
public function authenticate($params)
parapublic function authenticate(Array $params = null)
Dentro dessa mesma função eu adicionei: