123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- <?php
- /*
- * This file is part of the Cygnite package.
- *
- * (c) Sanjoy Dey <[email protected]>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Application\Components\Authentication;
- use Cygnite\Auth\AuthManager;
- use Cygnite\Auth\AuthInterface;
- use Cygnite\Common\UrlManager\Url;
- use Cygnite\Foundation\Application;
- use Cygnite\Common\SessionManager\Session;
- use Cygnite\Auth\Exception\InvalidCredentialException;
- class Auth extends AuthManager implements AuthInterface
- {
- public static $user = array();
- public static $msg = 'Welcome! ';
- public $username;
- public $valid = false;
- public $attempt = 0;
- protected $item = array();
- protected $errors = array();
- private $credential = array();
- private $table;
- /**
- * We will make Auth instance and return singleton
- * instance to the user
- *
- * @return object
- */
- public static function make()
- {
- $app = self::getContainer();
- $auth = __CLASS__;
- return $app->singleton(
- 'auth',
- function ($c) use ($auth) {
- return new $auth;
- }
- );
- }
- /**
- * Get user credentials
- *
- * @return array|null
- */
- public function getCredential()
- {
- return !empty($this->credential) ? $this->credential : null;
- }
- /**
- * Set User Credentials to authentication
- *
- * @param $credential
- */
- public function setCredential($credential)
- {
- $this->credential = $credential;
- }
- /**
- * We will validate user and return boolean value
- *
- * $input = array('email' => '[email protected]', 'password' => 'xyz@324', 'status' => 1);
- * $auth->verify($input);
- *
- * @param $user
- * @param null $password
- * @param bool $status
- * @throws \Exception
- * @return bool
- */
- public function verify($user, $password = null, $status = false)
- {
- $this->table = $this->table();
- $credential = array();
- if (is_array($user)) {
- $credential = $this->credential($user)->getCredential();
- } else {
- $credential = $this->credential($user, $password, $status)->getCredential();
- }
- /**
- | Get user information from model
- | to verify against user input
- */
- $userInfo = $this->setWhere()->findAll();
- if ($userInfo->count() > 0) {
- /*
- | Validate user against password
- | if user validated return true
- */
- if (trim($userInfo[0]->password) == trim($credential['password'])) {
- $this->valid = true;
- self::$user = $userInfo;
- $this->attempt = 0;
- return true;
- } else {
- return $this->setFailure('password');
- } // password validation end
- } else {
- return $this->setFailure('user');
- } // no user found
- }
- /**
- * Login user with user credentials
- *
- * @throws \Cygnite\Auth\Exception\InvalidCredentialException
- * @return boolean
- */
- public function login()
- {
- if ($this->valid) {
- return $this->createSession();
- } else {
- $credential = $this->getCredential();
- if (empty($credential)) {
- throw new InvalidCredentialException('Please set credential using Auth::setCredential($credential) to login.');
- }
- if ($valid = $this->verify($credential)) {
- return ($valid) ? $this->createSession() : $valid;
- }
- }
- }
- /**
- * Check user logged in or not
- *
- * @return boolean
- */
- public function isLoggedIn()
- {
- //If user has valid session, and such is logged in
- if (Session::has('auth:' . trim($this->table))) {
- $session = Session::get('auth:' . trim($this->table));
- return (isset($session['isLoggedIn']) && $session['isLoggedIn'] == true) ? true : false;
- }
- return false;
- }
- public function rememberMe()
- {
- }
- /**
- * Return number of un-successful attempt by user
- *
- * @return int
- */
- public function attempts()
- {
- return $this->attempt;
- }
- /**
- * Magic Method for handling dynamic data access.
- */
- public function __get($key)
- {
- return $this->item[$key];
- }
- /**
- * Magic Method for handling the dynamic setting of data.
- */
- public function __set($key, $value)
- {
- $this->item[$key] = $value;
- }
- /**
- * We will destroy current user session and return to
- * application base url
- */
- public function logout($redirect = true)
- {
- Session::delete();
- ($redirect) ? Url::redirectTo(Url::getBase()) : '';
- }
- public function userInfo()
- {
- if (Session::has('auth:' . trim($this->table))) {
- $user = Session::get('auth:' . trim($this->table));
- return $user;
- }
- }
- /**
- * Set user credentials into array
- *
- * @param $user
- * @param null $password
- * @param bool $status
- * @return $this
- */
- protected function credential($user, $password = null, $status = false)
- {
- /**
- | We will check is array passed as first argument
- | then we will simply return Auth instance
- */
- if (is_array($user)) {
- $this->setCredential($user);
- return $this;
- }
- $credential = array();
- if ($status) {
- $credential = array('username' => $user, 'password' => $password, 'status' => $status);
- } else {
- $credential = array('username' => $user, 'password' => $password);
- }
- $this->setCredential($credential);
- return $this;
- }
- private function setFailure($key)
- {
- $this->valid = false;
- $this->attempt++;
- $this->setError($key, 0);
- return false;
- }
- /**
- * @return array|null
- */
- private function setWhere()
- {
- $credentials = $this->getCredential();
- $i = 0;
- foreach ($credentials as $key => $value) {
- if ($i == 0) {
- $this->username = $value;
- $where = static::user()->where($key, '=', $value);
- }
- if ($i == 2 || $key == 'status') {
- $where = static::user()->where($key, '=', $value);
- }
- $i++;
- }
- return $where;
- }
- /**
- * @return bool
- */
- private function createSession()
- {
- $hasSession = $this->setSession();
- $this->setUserInfo(self::$user);
- return ($hasSession) ? true : false;
- }
- /**
- * We will set session
- *
- * @return mixed
- */
- private function setSession()
- {
- $primaryKey = null;
- $data = array();
- $primaryKey = self::$user[0]->getPrimaryKey();
- $data[$primaryKey] = self::$user[0]->{$primaryKey};
- foreach (self::$user[0]->getAttributes() as $key => $val) {
- $data[$key] = $val;
- }
- $data['isLoggedIn'] = true;
- $data['flashMsg'] = static::$msg . ucfirst($this->username);
- Session::set('auth:' . trim($this->table), $data);
- return true;
- }
- /**
- * We will set authentication error as property
- *
- * @param $key
- * @param $value
- */
- private function setError($key, $value)
- {
- $this->errors[$key] = $value;
- }
- /**
- * We will set user information into Auth property
- * So that you can easily access those information directly
- * from the auth instance
- *
- * @param $userInfo
- */
- private function setUserInfo($userInfo)
- {
- foreach ($userInfo as $key => $value) {
- $this->{$key} = $value;
- }
- }
- }
|