PageRenderTime 29ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/users_controller.php

http://croogo.googlecode.com/
PHP | 264 lines | 204 code | 30 blank | 30 comment | 42 complexity | ca8b64d4018b7545f9e1e71a39bf1f37 MD5 | raw file
Possible License(s): LGPL-2.1
  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. var $name = 'Users';
  22. /**
  23. * Components
  24. *
  25. * @var array
  26. * @access public
  27. */
  28. var $components = array(
  29. 'Email',
  30. );
  31. /**
  32. * Models used by the Controller
  33. *
  34. * @var array
  35. * @access public
  36. */
  37. var $uses = array('User');
  38. function beforeFilter() {
  39. parent::beforeFilter();
  40. }
  41. function admin_index() {
  42. $this->pageTitle = __('Users', true);
  43. $this->User->recursive = 0;
  44. $this->set('users', $this->paginate());
  45. }
  46. function admin_add() {
  47. if (!empty($this->data)) {
  48. $this->User->create();
  49. $this->data['User']['activation_key'] = md5(uniqid());
  50. if ($this->User->save($this->data)) {
  51. $this->Session->setFlash(__('The User has been saved', true));
  52. $this->redirect(array('action' => 'index'));
  53. } else {
  54. $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
  55. unset($this->data['User']['password']);
  56. }
  57. } else {
  58. $this->data['User']['role_id'] = 2; // default Role: Registered
  59. }
  60. $roles = $this->User->Role->find('list');
  61. $this->set(compact('roles'));
  62. }
  63. function admin_edit($id = null) {
  64. if (!$id && empty($this->data)) {
  65. $this->Session->setFlash(__('Invalid User', true));
  66. $this->redirect(array('action' => 'index'));
  67. }
  68. if (!empty($this->data)) {
  69. if ($this->User->save($this->data)) {
  70. $this->Session->setFlash(__('The User has been saved', true));
  71. $this->redirect(array('action' => 'index'));
  72. } else {
  73. $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
  74. }
  75. }
  76. if (empty($this->data)) {
  77. $this->data = $this->User->read(null, $id);
  78. }
  79. $roles = $this->User->Role->find('list');
  80. $this->set(compact('roles'));
  81. }
  82. function admin_reset_password($id = null) {
  83. if (!$id && empty($this->data)) {
  84. $this->Session->setFlash(__('Invalid User', true));
  85. $this->redirect(array('action' => 'index'));
  86. }
  87. if (!empty($this->data)) {
  88. if ($this->User->save($this->data)) {
  89. $this->Session->setFlash(__('Password has been reset.', true));
  90. $this->redirect(array('action' => 'index'));
  91. } else {
  92. $this->Session->setFlash(__('Password could not be reset. Please, try again.', true));
  93. }
  94. }
  95. if (empty($this->data)) {
  96. $this->data = $this->User->read(null, $id);
  97. }
  98. }
  99. function admin_delete($id = null) {
  100. if (!$id) {
  101. $this->Session->setFlash(__('Invalid id for User', true));
  102. $this->redirect(array('action' => 'index'));
  103. }
  104. if ($this->User->delete($id)) {
  105. $this->Session->setFlash(__('User deleted', true));
  106. $this->redirect(array('action' => 'index'));
  107. }
  108. }
  109. function admin_login() {
  110. $this->pageTitle = __('Admin Login', true);
  111. $this->layout = "admin_login";
  112. }
  113. function admin_logout() {
  114. $this->Session->setFlash(__('Log out successful.', true));
  115. $this->redirect($this->Auth->logout());
  116. }
  117. function index() {
  118. $this->pageTitle = __('Users', true);
  119. }
  120. function add() {
  121. $this->pageTitle = __('Register', true);
  122. if (!empty($this->data)) {
  123. $this->User->create();
  124. $this->data['User']['role_id'] = 2; // Registered
  125. $this->data['User']['activation_key'] = md5(uniqid());
  126. $this->data['User']['status'] = 0;
  127. if ($this->User->save($this->data)) {
  128. $this->data['User']['password'] = null;
  129. $this->Email->from = Configure::read('Site.title') . ' '
  130. . '<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])).'>';
  131. $this->Email->to = $this->data['User']['email'];
  132. $this->Email->subject = __('[' . Configure::read('Site.title') . '] Please activate your account', true);
  133. $this->Email->template = 'register';
  134. $this->set('user', $this->data);
  135. $this->Email->send();
  136. $this->Session->setFlash(__('You have successfully registered an account. An email has been sent with further instructions.', true));
  137. $this->redirect(array('action' => 'login'));
  138. } else {
  139. $this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
  140. }
  141. }
  142. }
  143. function activate($username = null, $key = null) {
  144. if ($username == null || $key == null) {
  145. $this->redirect(array('action' => 'login'));
  146. }
  147. if ($this->User->hasAny(array(
  148. 'User.username' => $username,
  149. 'User.activation_key' => $key,
  150. 'User.status' => 0,
  151. ))) {
  152. $user = $this->User->findByUsername($username);
  153. $this->User->id = $user['User']['id'];
  154. $this->User->saveField('status', 1);
  155. $this->User->saveField('activation_key', md5(uniqid()));
  156. $this->Session->setFlash(__('Account activated successfully.', true));
  157. } else {
  158. $this->Session->setFlash(__('An error occurred.', true));
  159. }
  160. $this->redirect(array('action' => 'login'));
  161. }
  162. function edit() {}
  163. function forgot() {
  164. $this->pageTitle = __('Forgot Password', true);
  165. if (!empty($this->data) && isset($this->data['User']['username'])) {
  166. $user = $this->User->findByUsername($this->data['User']['username']);
  167. if (!isset($user['User']['id'])) {
  168. $this->Session->setFlash(__('Invalid username.', true));
  169. $this->redirect(array('action' => 'login'));
  170. }
  171. $this->User->id = $user['User']['id'];
  172. $activationKey = md5(uniqid());
  173. $this->User->saveField('activation_key', $activationKey);
  174. $this->set(compact('user', 'activationKey'));
  175. $this->Email->from = Configure::read('Site.title') . ' '
  176. . '<croogo@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME'])).'>';
  177. $this->Email->to = $user['User']['email'];
  178. $this->Email->subject = '[' . Configure::read('Site.title') . '] ' . __('Reset Password', true);
  179. $this->Email->template = 'forgot_password';
  180. if ($this->Email->send()) {
  181. $this->Session->setFlash(__('An email has been sent with instructions for resetting your password.', true));
  182. $this->redirect(array('action' => 'login'));
  183. } else {
  184. $this->Session->setFlash(__('An error occurred. Please try again.', true));
  185. }
  186. }
  187. }
  188. function reset($username = null, $key = null) {
  189. $this->pageTitle = __('Reset Password', true);
  190. if ($username == null || $key == null) {
  191. $this->Session->setFlash(__('An error occurred.', true));
  192. $this->redirect(array('action' => 'login'));
  193. }
  194. $user = $this->User->find('first', array(
  195. 'conditions' => array(
  196. 'User.username' => $username,
  197. 'User.activation_key' => $key,
  198. ),
  199. ));
  200. if (!isset($user['User']['id'])) {
  201. $this->Session->setFlash(__('An error occurred.', true));
  202. $this->redirect(array('action' => 'login'));
  203. }
  204. if (!empty($this->data) && isset($this->data['User']['password'])) {
  205. $this->User->id = $user['User']['id'];
  206. $user['User']['password'] = Security::hash($this->data['User']['password'], null, true);
  207. $user['User']['activation_key'] = md5(uniqid());
  208. if ($this->User->save($user['User'])) {
  209. $this->Session->setFlash(__('Your password has been reset successfully.', true));
  210. $this->redirect(array('action' => 'login'));
  211. } else {
  212. $this->Session->setFlash(__('An error occurred. Please try again.', true));
  213. }
  214. }
  215. $this->set(compact('user', 'username', 'key'));
  216. }
  217. function login() {
  218. $this->pageTitle = __('Log in', true);
  219. }
  220. function logout() {
  221. $this->Session->setFlash(__('Log out successful.', true));
  222. $this->redirect($this->Auth->logout());
  223. }
  224. function view($username) {
  225. $user = $this->User->findByUsername($username);
  226. if (!isset($user['User']['id'])) {
  227. $this->Session->setFlash(__('Invalid User.', true));
  228. $this->redirect('/');
  229. }
  230. $this->pageTitle = $user['User']['name'];
  231. $this->set(compact('user'));
  232. }
  233. }
  234. ?>