Auth.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. /*
  3. * This file is part of the Cygnite package.
  4. *
  5. * (c) Sanjoy Dey <[email protected]>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Application\Components\Authentication;
  11. use Cygnite\Auth\AuthManager;
  12. use Cygnite\Auth\AuthInterface;
  13. use Cygnite\Common\UrlManager\Url;
  14. use Cygnite\Foundation\Application;
  15. use Cygnite\Common\SessionManager\Session;
  16. use Cygnite\Auth\Exception\InvalidCredentialException;
  17. class Auth extends AuthManager implements AuthInterface
  18. {
  19. public static $user = array();
  20. public static $msg = 'Welcome! ';
  21. public $username;
  22. public $valid = false;
  23. public $attempt = 0;
  24. protected $item = array();
  25. protected $errors = array();
  26. private $credential = array();
  27. private $table;
  28. /**
  29. * We will make Auth instance and return singleton
  30. * instance to the user
  31. *
  32. * @return object
  33. */
  34. public static function make()
  35. {
  36. $app = self::getContainer();
  37. $auth = __CLASS__;
  38. return $app->singleton(
  39. 'auth',
  40. function ($c) use ($auth) {
  41. return new $auth;
  42. }
  43. );
  44. }
  45. /**
  46. * Get user credentials
  47. *
  48. * @return array|null
  49. */
  50. public function getCredential()
  51. {
  52. return !empty($this->credential) ? $this->credential : null;
  53. }
  54. /**
  55. * Set User Credentials to authentication
  56. *
  57. * @param $credential
  58. */
  59. public function setCredential($credential)
  60. {
  61. $this->credential = $credential;
  62. }
  63. /**
  64. * We will validate user and return boolean value
  65. *
  66. * $input = array('email' => '[email protected]', 'password' => 'xyz@324', 'status' => 1);
  67. * $auth->verify($input);
  68. *
  69. * @param $user
  70. * @param null $password
  71. * @param bool $status
  72. * @throws \Exception
  73. * @return bool
  74. */
  75. public function verify($user, $password = null, $status = false)
  76. {
  77. $this->table = $this->table();
  78. $credential = array();
  79. if (is_array($user)) {
  80. $credential = $this->credential($user)->getCredential();
  81. } else {
  82. $credential = $this->credential($user, $password, $status)->getCredential();
  83. }
  84. /**
  85. | Get user information from model
  86. | to verify against user input
  87. */
  88. $userInfo = $this->setWhere()->findAll();
  89. if ($userInfo->count() > 0) {
  90. /*
  91. | Validate user against password
  92. | if user validated return true
  93. */
  94. if (trim($userInfo[0]->password) == trim($credential['password'])) {
  95. $this->valid = true;
  96. self::$user = $userInfo;
  97. $this->attempt = 0;
  98. return true;
  99. } else {
  100. return $this->setFailure('password');
  101. } // password validation end
  102. } else {
  103. return $this->setFailure('user');
  104. } // no user found
  105. }
  106. /**
  107. * Login user with user credentials
  108. *
  109. * @throws \Cygnite\Auth\Exception\InvalidCredentialException
  110. * @return boolean
  111. */
  112. public function login()
  113. {
  114. if ($this->valid) {
  115. return $this->createSession();
  116. } else {
  117. $credential = $this->getCredential();
  118. if (empty($credential)) {
  119. throw new InvalidCredentialException('Please set credential using Auth::setCredential($credential) to login.');
  120. }
  121. if ($valid = $this->verify($credential)) {
  122. return ($valid) ? $this->createSession() : $valid;
  123. }
  124. }
  125. }
  126. /**
  127. * Check user logged in or not
  128. *
  129. * @return boolean
  130. */
  131. public function isLoggedIn()
  132. {
  133. //If user has valid session, and such is logged in
  134. if (Session::has('auth:' . trim($this->table))) {
  135. $session = Session::get('auth:' . trim($this->table));
  136. return (isset($session['isLoggedIn']) && $session['isLoggedIn'] == true) ? true : false;
  137. }
  138. return false;
  139. }
  140. public function rememberMe()
  141. {
  142. }
  143. /**
  144. * Return number of un-successful attempt by user
  145. *
  146. * @return int
  147. */
  148. public function attempts()
  149. {
  150. return $this->attempt;
  151. }
  152. /**
  153. * Magic Method for handling dynamic data access.
  154. */
  155. public function __get($key)
  156. {
  157. return $this->item[$key];
  158. }
  159. /**
  160. * Magic Method for handling the dynamic setting of data.
  161. */
  162. public function __set($key, $value)
  163. {
  164. $this->item[$key] = $value;
  165. }
  166. /**
  167. * We will destroy current user session and return to
  168. * application base url
  169. */
  170. public function logout($redirect = true)
  171. {
  172. Session::delete();
  173. ($redirect) ? Url::redirectTo(Url::getBase()) : '';
  174. }
  175. public function userInfo()
  176. {
  177. if (Session::has('auth:' . trim($this->table))) {
  178. $user = Session::get('auth:' . trim($this->table));
  179. return $user;
  180. }
  181. }
  182. /**
  183. * Set user credentials into array
  184. *
  185. * @param $user
  186. * @param null $password
  187. * @param bool $status
  188. * @return $this
  189. */
  190. protected function credential($user, $password = null, $status = false)
  191. {
  192. /**
  193. | We will check is array passed as first argument
  194. | then we will simply return Auth instance
  195. */
  196. if (is_array($user)) {
  197. $this->setCredential($user);
  198. return $this;
  199. }
  200. $credential = array();
  201. if ($status) {
  202. $credential = array('username' => $user, 'password' => $password, 'status' => $status);
  203. } else {
  204. $credential = array('username' => $user, 'password' => $password);
  205. }
  206. $this->setCredential($credential);
  207. return $this;
  208. }
  209. private function setFailure($key)
  210. {
  211. $this->valid = false;
  212. $this->attempt++;
  213. $this->setError($key, 0);
  214. return false;
  215. }
  216. /**
  217. * @return array|null
  218. */
  219. private function setWhere()
  220. {
  221. $credentials = $this->getCredential();
  222. $i = 0;
  223. foreach ($credentials as $key => $value) {
  224. if ($i == 0) {
  225. $this->username = $value;
  226. $where = static::user()->where($key, '=', $value);
  227. }
  228. if ($i == 2 || $key == 'status') {
  229. $where = static::user()->where($key, '=', $value);
  230. }
  231. $i++;
  232. }
  233. return $where;
  234. }
  235. /**
  236. * @return bool
  237. */
  238. private function createSession()
  239. {
  240. $hasSession = $this->setSession();
  241. $this->setUserInfo(self::$user);
  242. return ($hasSession) ? true : false;
  243. }
  244. /**
  245. * We will set session
  246. *
  247. * @return mixed
  248. */
  249. private function setSession()
  250. {
  251. $primaryKey = null;
  252. $data = array();
  253. $primaryKey = self::$user[0]->getPrimaryKey();
  254. $data[$primaryKey] = self::$user[0]->{$primaryKey};
  255. foreach (self::$user[0]->getAttributes() as $key => $val) {
  256. $data[$key] = $val;
  257. }
  258. $data['isLoggedIn'] = true;
  259. $data['flashMsg'] = static::$msg . ucfirst($this->username);
  260. Session::set('auth:' . trim($this->table), $data);
  261. return true;
  262. }
  263. /**
  264. * We will set authentication error as property
  265. *
  266. * @param $key
  267. * @param $value
  268. */
  269. private function setError($key, $value)
  270. {
  271. $this->errors[$key] = $value;
  272. }
  273. /**
  274. * We will set user information into Auth property
  275. * So that you can easily access those information directly
  276. * from the auth instance
  277. *
  278. * @param $userInfo
  279. */
  280. private function setUserInfo($userInfo)
  281. {
  282. foreach ($userInfo as $key => $value) {
  283. $this->{$key} = $value;
  284. }
  285. }
  286. }