<?php
namespace Moto\Authentication; use Moto; use Moto\Json\Server\Exception; use Zend\Authentication\Adapter\AdapterInterface; use Zend\Authentication\Result; class Adapter implements AdapterInterface { protected $_login = null; protected $_password = null; protected $_dataBaseAdapter = null; public function __construct($login, $password) { $this->_login = $login; $this->_password = $password; } public function setDataBaseAdapter($adapter) { $this->_dataBaseAdapter = $adapter; } public function authenticate() { $result = new \stdClass(); $result->code = Result::FAILURE; $result->identity = null; $result->messages = array(); try { $user = Moto\Database\Models\User::where('email', $this->_login)->first(); if ($user) { if (is_array($user)) $user = (object)$user; if (!$user->enabled) { throw new \Exception('ERROR.ACCESS_DENIED', Result::FAILURE); } if (!$user->isValidPassword($this->_password)) { throw new \Exception('ERROR.ACCESS_DENIED', Result::FAILURE); } $user->salt = null; $result->code = Result::SUCCESS; $result->identity = $user; } else { $result->messages[] = 'ERROR.ACCESS_DENIED'; } } catch (\Exception $e) { $result->code = Result::FAILURE; $result->messages[] = $e->getMessage(); } return new Result($result->code, $result->identity, $result->messages); } }