PageRenderTime 31ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Controller/UsersController.php

http://github.com/rsmartin/NiceAuth
PHP | 204 lines | 176 code | 13 blank | 15 comment | 38 complexity | 7206e1933593411fce6e5a7a24edb1ad MD5 | raw file
  1. <?php
  2. /**
  3. * User Controller for NiceAuth Plugin
  4. *
  5. * NiceAuth : User Authentication and Authorization Plugin for CakePHP
  6. * Copyright 2011, R.S.Martin (http://rsmartin.me)
  7. *
  8. * Licensed under The MIT License
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @author RSMartin
  12. * @copyright Copyright (c) 2011, RSMartin (http://rsmartin.me)
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT
  14. */
  15. App::uses('NiceAuthAppController', 'NiceAuth.Controller');
  16. App::uses('Controller', 'Controller');
  17. App::uses('ComponentCollection', 'Controller');
  18. App::uses('AclComponent', 'Controller/Component');
  19. App::uses('DbAcl', 'Model');
  20. App::uses('CakeEmail', 'Network/Email');
  21. App::import('NiceAuth.Vendor', 'Lightopenid');
  22. App::import('NiceAuth.Vendor', 'recaptchalib');
  23. class UsersController extends NiceAuthAppController {
  24. public $name = "Users";
  25. public $uses = array("Aro", "NiceAuth.Group", "NiceAuth.User");
  26. public $components = array(
  27. 'Auth' => array(
  28. 'authenticate' => array(
  29. 'Form',
  30. 'NiceAuth.Openid'
  31. )
  32. ),
  33. 'Acl'
  34. );
  35. public function index() {
  36. if ($this->Auth->user('id')) {
  37. $user = $this->User->findById($this->Auth->user());
  38. $this->set('user', $user);
  39. if ($this->request->is('post')) {
  40. $req = $this->request->data;
  41. if ($user['User']['password'] == AuthComponent::password($req['User']['old_password'])) {
  42. if ($req['User']['password'] == $req['User']['password_verify']) {
  43. $user['User']['password'] = $req['User']['password'];
  44. if($this->User->save($user)) {
  45. $this->Session->setFlash('New Password Saved!');
  46. }
  47. else {
  48. $this->Session->setFlash('The new passwords didn\'t match.');
  49. }
  50. }
  51. else {
  52. $this->Session->setFlash('The new passwords didn\'t match.');
  53. }
  54. }
  55. else {
  56. $this->Session->setFlash('The old password you entered was incorrect');
  57. }
  58. }
  59. }
  60. else {
  61. $this->redirect('/login');
  62. }
  63. }
  64. public function beforeFilter() {
  65. parent::beforeFilter();
  66. $this->Auth->userModel = 'User';
  67. $this->Auth->allow('*');
  68. //Custom Layout for User Controller
  69. $this->layout = 'user';
  70. }
  71. private function fixAlias() {
  72. $user = $this->User->read();
  73. $this->Aro->findByForeignKey($user['User']['id']);
  74. $this->Aro->save(array('alias' => $user['User']['username']));
  75. }
  76. private function sendEmail($type, $to, $vars = null) {
  77. if ($type == "registration" && Configure::read('NiceAuth.regEmail') == true) {
  78. $email = new CakeEmail('default');
  79. $email->to($to)
  80. ->emailFormat('html')
  81. ->subject(Configure::read('NiceAuth.regSubject'))
  82. ->template('NiceAuth.register')
  83. ->viewVars($vars)
  84. ->send();
  85. }
  86. elseif ($type == "reset") {
  87. $email = new CakeEmail('default');
  88. $email->to($to)
  89. ->emailFormat('html')
  90. ->subject(Configure::read('NiceAuth.resetSubject'))
  91. ->template('NiceAuth.reset')
  92. ->viewVars($vars)
  93. ->send();
  94. }
  95. }
  96. public function passwordReset() {
  97. if ($this->request->is('post')) {
  98. if ($user = $this->User->findByEmail($this->request->data['User']['email'])) {
  99. $newPass = uniqid(rand());
  100. $user['User']['password'] = $newPass;
  101. if($this->User->save($user)) {
  102. $pass = array('password' => $newPass);
  103. $this->sendEmail('reset', $this->request->data['User']['email'], $pass);
  104. }
  105. $this->Session->setFlash('You will receive an email shortly!');
  106. //$this->redirect('/');
  107. }
  108. else {
  109. $this->Session->setFlash('The email address you entered could not be found.');
  110. }
  111. }
  112. }
  113. public function register(){
  114. $this->set('groups', $this->Group->find('list'));
  115. if ($this->request->is('post')) {
  116. $resp = recaptcha_check_answer(Configure::read('NiceAuth.recaptchaPrivate'), $_SERVER["REMOTE_ADDR"], $this->request->data["recaptcha_challenge_field"], $this->request->data["recaptcha_response_field"]);
  117. if ($resp->is_valid) {
  118. $this->User->create();
  119. $this->User->set(array(
  120. 'group_id' => Configure::read('NiceAuth.defaultGroup')
  121. ));
  122. if ($this->User->save($this->request->data)) {
  123. $this->fixAlias();
  124. $this->Session->setFlash(__('You\'r account has been setup.'));
  125. $newUser = $this->User->read();
  126. $emailVars = array('username' => $newUser['User']['username']);
  127. $this->sendEmail('register', $newUser['User']['email'], $emailVars);
  128. $this->redirect('/me');
  129. }
  130. else {
  131. $this->Session->setFlash('Unable to create your\'re account. Please try again.');
  132. }
  133. }
  134. else {
  135. $this->Session->setFlash('The Verification Captcha you entered did not match, please try again.');
  136. }
  137. }
  138. elseif ($this->request->is('get')) {
  139. if (isset($this->request->query['openid_mode'])) {
  140. $openid = new Lightopenid($_SERVER['SERVER_NAME']);
  141. $ret = $openid->getAttributes();
  142. $data = $openid->data;
  143. if ($this->User->findByEmail($ret['contact/email']) == false) {
  144. $this->User->create();
  145. $newUser = array('username' => $ret['contact/email'], 'email' => $ret['contact/email'], 'password' => $data['openid_identity'], 'group_id' => Configure::read('NiceAuth.defaultGroup'));
  146. $this->User->save($newUser);
  147. $user = $this->User->read();
  148. $this->fixAlias();
  149. $emailVars = array('username' => $user['User']['username']);
  150. $this->sendEmail('register', $user['User']['email'], $emailVars);
  151. $this->Auth->login($user['User']);
  152. $this->Session->setFlash('Your account has been created.');
  153. $this->redirect('/me');
  154. }
  155. else {
  156. $this->Session->setFlash('This email address already exists, please try logging in instead.');
  157. }
  158. }
  159. }
  160. }
  161. public function login(){
  162. if ($this->request->is('post') || ($this->request->is('get') && isset($this->request->query['openid_mode']))) {
  163. if ($this->Auth->login()) {
  164. $this->redirect($this->Auth->redirect());
  165. }
  166. else {
  167. $this->Session->setFlash(__('Invalid username or password, try again'));
  168. }
  169. }
  170. }
  171. public function openid() {
  172. if ($this->request->is('post')) {
  173. $openid = new Lightopenid($_SERVER['SERVER_NAME']);
  174. $openid->identity = $this->request->data['openid'];
  175. $openid->required = array('contact/email');
  176. if ($this->request->data['type'] == 'register') {
  177. $openid->returnUrl = 'http://'.$_SERVER['SERVER_NAME'].Router::url(array('controller' => 'users', 'action' => 'register'));
  178. }
  179. else {
  180. $openid->returnUrl = 'http://'.$_SERVER['SERVER_NAME'].Router::url(array('controller' => 'users', 'action' => 'login'));
  181. }
  182. $this->redirect($openid->authUrl());
  183. }
  184. }
  185. public function logout(){
  186. $this->Auth->logout();
  187. $this->Session->setFlash('You have been successfully logged out.');
  188. $this->redirect('/');
  189. }
  190. }
  191. ?>