PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/controllers/users_controller.php

https://github.com/makies/croogo
PHP | 301 lines | 238 code | 33 blank | 30 comment | 54 complexity | f766723d7da14d530b7bba4f96d982a3 MD5 | raw file
  1. <?php
  2. /**
  3. * Users Controller
  4. *
  5. * PHP version 5
  6. *
  7. * @category Controller
  8. * @package Croogo
  9. * @version 1.0
  10. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  11. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  12. * @link http://www.croogo.org
  13. */
  14. class UsersController extends AppController {
  15. /**
  16. * Controller name
  17. *
  18. * @var string
  19. * @access public
  20. */
  21. public $name = 'Users';
  22. /**
  23. * Components
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. public $components = array(
  29. 'Email',
  30. );
  31. /**
  32. * Models used by the Controller
  33. *
  34. * @var array
  35. * @access public
  36. */
  37. public $uses = array('User');
  38. public function beforeFilter() {
  39. parent::beforeFilter();
  40. if (in_array($this->params['action'], array('admin_login', 'login'))) {
  41. $field = $this->Auth->fields['username'];
  42. if (!empty($this->data) && empty($this->data['User'][$field])) {
  43. $this->redirect(array('action' => $this->params['action']));
  44. }
  45. $cacheName = 'auth_failed_' . $this->data['User'][$field];
  46. if (Cache::read($cacheName, 'users_login') >= Configure::read('User.failed_login_limit')) {
  47. $this->Session->setFlash(__('You have reached maximum limit for failed login attempts. Please try again after a few minutes.', true), 'default', array('class' => 'error'));
  48. $this->redirect(array('action' => $this->params['action']));
  49. }
  50. }
  51. }
  52. public function beforeRender() {
  53. parent::beforeRender();
  54. if (in_array($this->params['action'], array('admin_login', 'login'))) {
  55. if (!empty($this->data)) {
  56. $field = $this->Auth->fields['username'];
  57. $cacheName = 'auth_failed_' . $this->data['User'][$field];
  58. $cacheValue = Cache::read($cacheName, 'users_login');
  59. Cache::write($cacheName, (int)$cacheValue + 1, 'users_login');
  60. }
  61. }
  62. }
  63. public function admin_index() {
  64. $this->set('title_for_layout', __('Users', true));
  65. $this->User->recursive = 0;
  66. $this->set('users', $this->paginate());
  67. }
  68. public function admin_add() {
  69. if (!empty($this->data)) {
  70. $this->User->create();
  71. $this->data['User']['activation_key'] = md5(uniqid());
  72. if ($this->User->save($this->data)) {
  73. $this->Session->setFlash(__('The User has been saved', true), 'default', array('class' => 'success'));
  74. $this->redirect(array('action' => 'index'));
  75. } else {
  76. $this->Session->setFlash(__('The User could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
  77. unset($this->data['User']['password']);
  78. }
  79. } else {
  80. $this->data['User']['role_id'] = 2; // default Role: Registered
  81. }
  82. $roles = $this->User->Role->find('list');
  83. $this->set(compact('roles'));
  84. }
  85. public function admin_edit($id = null) {
  86. if (!$id && empty($this->data)) {
  87. $this->Session->setFlash(__('Invalid User', true), 'default', array('class' => 'error'));
  88. $this->redirect(array('action' => 'index'));
  89. }
  90. if (!empty($this->data)) {
  91. if ($this->User->save($this->data)) {
  92. $this->Session->setFlash(__('The User has been saved', true), 'default', array('class' => 'success'));
  93. $this->redirect(array('action' => 'index'));
  94. } else {
  95. $this->Session->setFlash(__('The User could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
  96. }
  97. }
  98. if (empty($this->data)) {
  99. $this->data = $this->User->read(null, $id);
  100. }
  101. $roles = $this->User->Role->find('list');
  102. $this->set(compact('roles'));
  103. }
  104. public function admin_reset_password($id = null) {
  105. if (!$id && empty($this->data)) {
  106. $this->Session->setFlash(__('Invalid User', true), 'default', array('class' => 'error'));
  107. $this->redirect(array('action' => 'index'));
  108. }
  109. if (!empty($this->data)) {
  110. $user = $this->User->findById($id);
  111. if ($user['User']['password'] == Security::hash($this->data['User']['current_password'], null, true)) {
  112. if ($this->User->save($this->data)) {
  113. $this->Session->setFlash(__('Password has been reset.', true), 'default', array('class' => 'success'));
  114. $this->redirect(array('action' => 'index'));
  115. } else {
  116. $this->Session->setFlash(__('Password could not be reset. Please, try again.', true), 'default', array('class' => 'error'));
  117. }
  118. } else {
  119. $this->Session->setFlash(__('Current password did not match. Please, try again.', true), 'default', array('class' => 'error'));
  120. }
  121. }
  122. if (empty($this->data)) {
  123. $this->data = $this->User->read(null, $id);
  124. }
  125. }
  126. public function admin_delete($id = null) {
  127. if (!$id) {
  128. $this->Session->setFlash(__('Invalid id for User', true), 'default', array('class' => 'error'));
  129. $this->redirect(array('action' => 'index'));
  130. }
  131. if (!isset($this->params['named']['token']) || ($this->params['named']['token'] != $this->params['_Token']['key'])) {
  132. $blackHoleCallback = $this->Security->blackHoleCallback;
  133. $this->$blackHoleCallback();
  134. }
  135. if ($this->User->delete($id)) {
  136. $this->Session->setFlash(__('User deleted', true), 'default', array('class' => 'success'));
  137. $this->redirect(array('action' => 'index'));
  138. }
  139. }
  140. public function admin_login() {
  141. $this->set('title_for_layout', __('Admin Login', true));
  142. $this->layout = "admin_login";
  143. }
  144. public function admin_logout() {
  145. $this->Session->setFlash(__('Log out successful.', true), 'default', array('class' => 'success'));
  146. $this->redirect($this->Auth->logout());
  147. }
  148. public function index() {
  149. $this->set('title_for_layout', __('Users', true));
  150. }
  151. public function add() {
  152. $this->set('title_for_layout', __('Register', true));
  153. if (!empty($this->data)) {
  154. $this->User->create();
  155. $this->data['User']['role_id'] = 2; // Registered
  156. $this->data['User']['activation_key'] = md5(uniqid());
  157. $this->data['User']['status'] = 0;
  158. $this->data['User']['username'] = htmlspecialchars($this->data['User']['username']);
  159. $this->data['User']['website'] = htmlspecialchars($this->data['User']['website']);
  160. $this->data['User']['name'] = htmlspecialchars($this->data['User']['name']);
  161. if ($this->User->save($this->data)) {
  162. $this->data['User']['password'] = null;
  163. $this->Email->from = Configure::read('Site.title') . ' '
  164. . '<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])).'>';
  165. $this->Email->to = $this->data['User']['email'];
  166. $this->Email->subject = __('[' . Configure::read('Site.title') . '] Please activate your account', true);
  167. $this->Email->template = 'register';
  168. $this->set('user', $this->data);
  169. $this->Email->send();
  170. $this->Session->setFlash(__('You have successfully registered an account. An email has been sent with further instructions.', true), 'default', array('class' => 'success'));
  171. $this->redirect(array('action' => 'login'));
  172. } else {
  173. $this->Session->setFlash(__('The User could not be saved. Please, try again.', true), 'default', array('class' => 'error'));
  174. }
  175. }
  176. }
  177. public function activate($username = null, $key = null) {
  178. if ($username == null || $key == null) {
  179. $this->redirect(array('action' => 'login'));
  180. }
  181. if ($this->User->hasAny(array(
  182. 'User.username' => $username,
  183. 'User.activation_key' => $key,
  184. 'User.status' => 0,
  185. ))) {
  186. $user = $this->User->findByUsername($username);
  187. $this->User->id = $user['User']['id'];
  188. $this->User->saveField('status', 1);
  189. $this->User->saveField('activation_key', md5(uniqid()));
  190. $this->Session->setFlash(__('Account activated successfully.', true), 'default', array('class' => 'success'));
  191. } else {
  192. $this->Session->setFlash(__('An error occurred.', true), 'default', array('class' => 'error'));
  193. }
  194. $this->redirect(array('action' => 'login'));
  195. }
  196. public function edit() {}
  197. public function forgot() {
  198. $this->set('title_for_layout', __('Forgot Password', true));
  199. if (!empty($this->data) && isset($this->data['User']['username'])) {
  200. $user = $this->User->findByUsername($this->data['User']['username']);
  201. if (!isset($user['User']['id'])) {
  202. $this->Session->setFlash(__('Invalid username.', true), 'default', array('class' => 'error'));
  203. $this->redirect(array('action' => 'login'));
  204. }
  205. $this->User->id = $user['User']['id'];
  206. $activationKey = md5(uniqid());
  207. $this->User->saveField('activation_key', $activationKey);
  208. $this->set(compact('user', 'activationKey'));
  209. $this->Email->from = Configure::read('Site.title') . ' '
  210. . '<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])).'>';
  211. $this->Email->to = $user['User']['email'];
  212. $this->Email->subject = '[' . Configure::read('Site.title') . '] ' . __('Reset Password', true);
  213. $this->Email->template = 'forgot_password';
  214. if ($this->Email->send()) {
  215. $this->Session->setFlash(__('An email has been sent with instructions for resetting your password.', true), 'default', array('class' => 'success'));
  216. $this->redirect(array('action' => 'login'));
  217. } else {
  218. $this->Session->setFlash(__('An error occurred. Please try again.', true), 'default', array('class' => 'error'));
  219. }
  220. }
  221. }
  222. public function reset($username = null, $key = null) {
  223. $this->set('title_for_layout', __('Reset Password', true));
  224. if ($username == null || $key == null) {
  225. $this->Session->setFlash(__('An error occurred.', true), 'default', array('class' => 'error'));
  226. $this->redirect(array('action' => 'login'));
  227. }
  228. $user = $this->User->find('first', array(
  229. 'conditions' => array(
  230. 'User.username' => $username,
  231. 'User.activation_key' => $key,
  232. ),
  233. ));
  234. if (!isset($user['User']['id'])) {
  235. $this->Session->setFlash(__('An error occurred.', true), 'default', array('class' => 'error'));
  236. $this->redirect(array('action' => 'login'));
  237. }
  238. if (!empty($this->data) && isset($this->data['User']['password'])) {
  239. $this->User->id = $user['User']['id'];
  240. $user['User']['password'] = Security::hash($this->data['User']['password'], null, true);
  241. $user['User']['activation_key'] = md5(uniqid());
  242. if ($this->User->save($user['User'])) {
  243. $this->Session->setFlash(__('Your password has been reset successfully.', true), 'default', array('class' => 'success'));
  244. $this->redirect(array('action' => 'login'));
  245. } else {
  246. $this->Session->setFlash(__('An error occurred. Please try again.', true), 'default', array('class' => 'error'));
  247. }
  248. }
  249. $this->set(compact('user', 'username', 'key'));
  250. }
  251. public function login() {
  252. $this->set('title_for_layout', __('Log in', true));
  253. }
  254. public function logout() {
  255. $this->Session->setFlash(__('Log out successful.', true), 'default', array('class' => 'success'));
  256. $this->redirect($this->Auth->logout());
  257. }
  258. public function view($username) {
  259. $user = $this->User->findByUsername($username);
  260. if (!isset($user['User']['id'])) {
  261. $this->Session->setFlash(__('Invalid User.', true), 'default', array('class' => 'error'));
  262. $this->redirect('/');
  263. }
  264. $this->set('title_for_layout', $user['User']['name']);
  265. $this->set(compact('user'));
  266. }
  267. }
  268. ?>