Posted By

sevennbsp on 05/15/10


Tagged

login edit auth view controller delete index cakephp add users logout


Versions (?)

users_controller auth


 / Published in: PHP
 

URL: http://www.jbcrawford.net/archives/45

this is all the actions needed for auth and the users login/register system

  1. <?php
  2. class UsersController extends AppController {
  3. var $name = 'Users';
  4.  
  5. function beforeFilter() {
  6. parent::beforeFilter();
  7. }
  8.  
  9. function index() {
  10. $users = $this->User->find('all');
  11. if (empty($users)) {
  12. $this->Session->setFlash('There are no users defined', 'default', array('class'=>'bad'));
  13. } else {
  14. $this->set('users', $users);
  15. }
  16. }
  17.  
  18. function view($id) {
  19. $user = $this->User->findById($id);
  20. if (!empty($user)) {
  21. $this->set('user', $user);
  22. } else {
  23. $this->Session->setFlash('Invalid User ID', 'default', array('class'=>'bad'));
  24. $this->redirect('index');
  25. }
  26. }
  27.  
  28. function add() {
  29. if (!empty($this->data)) {
  30. $this->User->set($this->data);
  31. if ($this->User->validates()) {
  32. $this->data['User']['password'] = $this->data['User']['clear_password'];
  33. $this->User->save($this->Auth->hashPasswords($this->data), false);
  34. $this->Session->setFlash($this->data['User']['name'].' Added', 'default', array('class'=>'good'));
  35. $this->redirect('index');
  36. } else {
  37. $this->Session->setFlash('Please correct the errors below', 'default', array('class'=>'bad'));
  38. }
  39. }
  40. }
  41.  
  42. function edit($id = null) {
  43. if (!empty($this->data)) {
  44. foreach ($this->data['User'] as $field => $data) {
  45. if (!in_array($field, array('clear_password', 'confirm_password'))) {
  46. $fields[] = $field;
  47. }
  48. }
  49. if (!empty($this->data['User']['clear_password']) || !empty($this->data['User']['confirm_password'])) {
  50. $fields[] = 'password';
  51. $fields[] = 'clear_password';
  52. $fields[] = 'confirm_password';
  53. }
  54. $this->User->set($this->data);
  55. if ($this->User->validates()) {
  56. if (!empty($this->data['User']['clear_password'])) {
  57. $this->data['User']['password'] = $this->data['User']['confirm_password'];
  58. }
  59. $this->User->save($this->Auth->hashPasswords($this->data), false, $fields);
  60. $this->Session->setFlash($this->data['User']['name'].' Updated', 'default', array('class'=>'good'));
  61. $this->redirect('index');
  62. }
  63. } else {
  64. $user = $this->User->findById($id);
  65. if (empty($user)) {
  66. $this->Session->setFlash('Invalid User ID', 'default', array('class'=>'bad'));
  67. $this->redirect('index');
  68. } else {
  69. unset($user['User']['password']);
  70. $this->data = $user;
  71. }
  72. }
  73. }
  74.  
  75. function delete($id) {
  76. $user = $this->User->findById($id);
  77. if (empty($user)) {
  78. $this->Session->setFlash('Invalid User ID', 'default', array('class'=>'bad'));
  79. } else {
  80. if ($user['User']['id'] == $this->Session->read('Auth.User.id')) {
  81. $this->Session->setFlash('Sorry, you can not delete yourself', 'default', array('class'=>'bad'));
  82. } else {
  83. if ($this->User->del($id)) {
  84. $this->Session->setFlash($user['User']['name'].' Deleted', 'default', array('class'=>'good'));
  85. } else {
  86. $this->Session->setFlash('Failed to delete '.$user['User']['name'], 'default', array('class'=>'bad'));
  87. }
  88. }
  89. }
  90. $this->redirect('index');
  91. }
  92.  
  93. function login() {
  94. // Auth Magic
  95. }
  96.  
  97. function logout() {
  98. $this->Session->del('Auth.User');
  99. $this->Session->setFlash('You are now logged out', 'default', array('class'=>'good'));
  100. $this->redirect('login');
  101. }
  102. }
  103. ?>

Report this snippet  

You need to login to post a comment.