Advertisement
Guest User

Untitled

a guest
Oct 5th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.35 KB | None | 0 0
  1. // UsersController
  2.  
  3. class UsersController extends AppController {
  4. var $name = "Users";
  5. public function login() {
  6. if (empty($this->data)) {
  7. return;
  8. }
  9.  
  10. $user = $this->User->authsomeLogin('credentials', $this->data['User']);
  11.  
  12. if (!$user) {
  13. $this->Session->setFlash('Unknown user or wrong password');
  14. return;
  15. }
  16.  
  17. }
  18.  
  19. public function register() {
  20. if (empty($this->data)) {
  21. return;
  22. }
  23.  
  24. if ($this->User->save($this->data)) {
  25. $this->Session->setFlash('Your entry has been saved.');
  26. if($this->login()) {
  27. $this->redirect(array('controller'=>'users', 'action'=>'setup'));
  28. }
  29. }
  30.  
  31. }
  32.  
  33. function setup() {
  34. if(empty($this->data)) {
  35. return;
  36. }
  37.  
  38. $this->validate = false;
  39. if ($this->User->save($this->data, array('validate' => false))) {
  40. $this->Session->setFlash('Your entry has been saved.');
  41. $this->redirect(array('controller'=>'users', 'action'=>'view'));
  42. } else {
  43. $this->Session->setFlash('Data not saved');
  44.  
  45. debug($this->data);
  46. }
  47. }
  48.  
  49. function edit() {
  50. if(empty($this->data)) {
  51. return;
  52. }
  53.  
  54. if ($this->User->save($this->data)) {
  55. $this->Session->setFlash('Your entry has been saved.');
  56. $this->redirect(array('controller'=>'users', 'action'=>'view'));
  57. }
  58. }
  59.  
  60. }
  61.  
  62.  
  63. // User Model
  64.  
  65. class User extends AppModel {
  66.  
  67. var $validate = array(
  68. 'username' => array(
  69. 'alphaNumeric' => array(
  70. 'rule'=>'alphaNumeric',
  71. 'message'=>'Letters and numbers only'
  72. ),
  73. 'between'=>array(
  74. 'rule'=>array('between', 6, 25),
  75. 'message'=>'Must be between 6 and 25 characters'
  76. ),
  77. 'isUnique'=>array(
  78. 'rule'=>'isUnique',
  79. 'message'=>'That username is already taken'
  80. )
  81.  
  82. ),
  83. 'password'=>array(
  84. 'minLength'=>array(
  85. 'rule'=>array('minLength', 6),
  86. 'message'=>'Password must be at least 6 characters'
  87. ),
  88. 'passwordMatch'=>array(
  89. 'rule'=> 'passwordMatch',
  90. 'message'=>'Your passwords must match'
  91. )
  92. ),
  93. 'email'=>array(
  94. 'email'=>array(
  95. 'rule'=>'email',
  96. 'message'=>'Not a valid email address'
  97. ),
  98. 'emailMatch'=>array(
  99. 'rule'=> 'emailMatch',
  100. 'message'=>'Your email addresses must match'
  101. )
  102. ),
  103. 'state'=>array(
  104. 'alphaNumeric'=>array(
  105. 'rule'=> 'alphaNumeric',
  106. 'message'=>'Not a valid state'
  107. )
  108. ),
  109. 'city'=>array(
  110. 'alphaNumeric'=>array(
  111. 'rule'=>'alphaNumeric',
  112. 'message'=>'Not a valid city'
  113. )
  114. ),
  115. 'zip'=>array(
  116. 'zip'=>array(
  117. 'rule'=>'postal',
  118. 'message'=>'Not a valid postal code'
  119. )
  120. )
  121. );
  122.  
  123. function passwordMatch() {
  124. if ($this->data['User']['password'] == $this->data['User']['confirm_password']) {
  125. return true;
  126. }
  127.  
  128. return false;
  129. }
  130.  
  131. function emailMatch() {
  132. if ($this->data['User']['email'] == $this->data['User']['confirm_email']) {
  133. return true;
  134. }
  135.  
  136. return false;
  137. }
  138.  
  139. function beforeSave() {
  140. if(!empty($this->data['User']['password'])) {
  141. $this->data['User']['password'] = Authsome::hash($this->data['User']['password']);
  142. return true;
  143. }
  144. }
  145.  
  146. public function authsomeLogin($type, $credentials = array()) {
  147. switch ($type) {
  148. case 'guest':
  149. return array('it'=>'works');
  150. case 'credentials':
  151. $password = Authsome::hash($credentials['password']);
  152.  
  153. $conditions = array(
  154. 'User.username' => $credentials['username'],
  155. 'User.password' => $password,
  156. );
  157. break;
  158. default:
  159. return null;
  160. }
  161.  
  162. return $this->find('first', compact('conditions'));
  163. }
  164. }
  165.  
  166. // Setup (update) view
  167.  
  168. echo $form->create('User', array('controller'=>'users', 'action'=>'setup'));
  169. echo $form->input('firstname', array('label'=>'First Name'));
  170. echo $form->input('lastname', array('label'=>'Last Name'));
  171. echo $form->input('email', array('label'=>'Email'));
  172. echo $form->input('confirm_email', array('label'=>'Confirm Email'));
  173. echo $form->input('phone', array('label'=>'Phone'));
  174. echo $form->input('street', array('label'=>'Street'));
  175. echo $form->input('city', array('label'=>'City'));
  176. echo $form->input('state', array('label'=>'State'));
  177. echo $form->input('zip', array('label'=>'Postal Code'));
  178. echo $form->input('id', array('type'=>'hidden', 'value'=>Authsome::get('User.id')));
  179. echo $form->end('Save');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement