Guest User

Untitled

a guest
Aug 16th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. <?php
  2.  
  3. class AuthComponent extends Component {
  4. public $authorized = true;
  5. public $autoCheck = true;
  6. public $controller;
  7. public $fields = array(
  8. "id" => "id",
  9. "username" => "username",
  10. "password" => "password"
  11. );
  12. public $hash = "sha1";
  13. public $loggedIn;
  14. public $loginAction = "/users/login";
  15. public $loginRedirect = "/";
  16. public $logoutAction = "/users/logout";
  17. public $logoutRedirect = "/";
  18. public $permissions = array();
  19. public $user = array();
  20. public $userModel = "Users";
  21. public $userScope = array();
  22. public $useSalt = true;
  23. public $expires;
  24. public $path = "/";
  25. public $domain = "";
  26. public $secure = false;
  27. public $recursion;
  28. public $loginError = "loginFailed";
  29. public $authError = "notAuthorized";
  30. public $authenticate = false;
  31.  
  32. public function initialize($controller) {
  33. $this->controller = $controller;
  34. }
  35. public function startup($controller) {
  36. $this->allow($this->loginAction);
  37. if($this->autoCheck):
  38. $this->check();
  39. endif;
  40. if(Mapper::match($this->loginAction)):
  41. $this->login();
  42. endif;
  43. }
  44. public function shutdown($controller) {
  45. if(Mapper::match($this->loginAction)):
  46. $this->loginRedirect();
  47. endif;
  48. }
  49. public function check() {
  50. if(!$this->authorized()):
  51. $this->setAction(Mapper::here());
  52. $this->error($this->authError);
  53. $this->controller->redirect($this->loginAction);
  54. return false;
  55. endif;
  56. return true;
  57. }
  58. public function authorized() {
  59. return $this->loggedIn() || $this->isPublic();
  60. }
  61. public function isPublic() {
  62. $here = Mapper::here();
  63. $authorized = $this->authorized;
  64. foreach($this->permissions as $url => $permission):
  65. if(Mapper::match($url, $here)):
  66. $authorized = $permission;
  67. endif;
  68. endforeach;
  69. return $authorized;
  70. }
  71. public function allow($url = null) {
  72. if(is_null($url)):
  73. $this->authorized = true;
  74. else:
  75. $this->permissions[$url] = true;
  76. endif;
  77. }
  78. public function deny($url = null) {
  79. if(is_null($url)):
  80. $this->authorized = false;
  81. else:
  82. $this->permissions[$url] = false;
  83. endif;
  84. }
  85. public function loggedIn() {
  86. if(is_null($this->loggedIn)):
  87. $user = Cookie::read("user_id");
  88. $password = Cookie::read("password");
  89. if(!is_null($user) && !is_null($password)):
  90. $user = $this->identify(array(
  91. $this->fields["id"] => $user,
  92. $this->fields["password"] => $password
  93. ));
  94. $this->loggedIn = !empty($user);
  95. else:
  96. $this->loggedIn = false;
  97. endif;
  98. endif;
  99. return $this->loggedIn;
  100. }
  101. public function identify($conditions) {
  102. $userModel = Model::load($this->userModel);
  103. if(!$userModel):
  104. $this->error("missingModel", array("model" => $this->userModel));
  105. return false;
  106. endif;
  107. $params = array(
  108. "conditions" => array_merge(
  109. $this->userScope,
  110. $conditions
  111. ),
  112. "recursion" => is_null($this->recursion) ? $userModel->recursion : $this->recursion
  113. );
  114. return $this->user = $userModel->first($params);
  115. }
  116. public function hash($password) {
  117. return Security::hash($password, $this->hash, $this->useSalt);
  118. }
  119. public function login() {
  120. if(!empty($this->controller->data)):
  121. $password = $this->hash($this->controller->data[$this->fields["password"]]);
  122. $user = $this->identify(array(
  123. $this->fields["username"] => $this->controller->data[$this->fields["username"]],
  124. $this->fields["password"] => $password
  125. ));
  126. if(!empty($user)):
  127. $this->authenticate = true;
  128. else:
  129. $this->error($this->loginError);
  130. endif;
  131. endif;
  132. }
  133. public function loginRedirect() {
  134. if($this->authenticate):
  135. $this->authenticate($this->user["id"], $this->user["password"]);
  136. if($redirect = $this->getAction()):
  137. $this->loginRedirect = $redirect;
  138. endif;
  139. $this->controller->redirect($this->loginRedirect);
  140. endif;
  141. }
  142. public function authenticate($id, $password) {
  143. Cookie::set("domain", $this->domain);
  144. Cookie::set("path", $this->path);
  145. Cookie::set("secure", $this->secure);
  146. Cookie::write("user_id", $id, $this->expires);
  147. Cookie::write("password", $password, $this->expires);
  148. }
  149. public function logout() {
  150. Cookie::set("domain", $this->domain);
  151. Cookie::set("path", $this->path);
  152. Cookie::set("secure", $this->secure);
  153. Cookie::delete("user_id");
  154. Cookie::delete("password");
  155. $this->controller->redirect($this->logoutRedirect);
  156. }
  157. public function user($field = null) {
  158. if($this->loggedIn()):
  159. if(is_null($field)):
  160. return $this->user;
  161. else:
  162. return $this->user[$field];
  163. endif;
  164. else:
  165. return null;
  166. endif;
  167. }
  168. public function setAction($action) {
  169. Session::write("Auth.action", $action);
  170. }
  171. public function getAction() {
  172. $action = Session::read("Auth.action");
  173. Session::delete("Auth.action");
  174. return $action;
  175. }
  176. public function error($type, $details = array()) {
  177. Session::writeFlash("Auth.error", $type);
  178. }
  179. }
Add Comment
Please, Sign In to add comment