Skip to content

Instantly share code, notes, and snippets.

@eminetto
Created November 4, 2012 13:58
Show Gist options
  • Save eminetto/4012039 to your computer and use it in GitHub Desktop.
Save eminetto/4012039 to your computer and use it in GitHub Desktop.
<?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;
}
}
@miguelsmuller
Copy link

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:

  1. Mudei public function authenticate($params) para public function authenticate(Array $params = null)

  2. Dentro dessa mesma função eu adicionei:

if ($params == null) {
    throw new \Exception("Parâmetros inválidos");
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment