用户控制器身份验证

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用户控制器身份验证相关的知识,希望对你有一定的参考价值。

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. ?>

以上是关于用户控制器身份验证的主要内容,如果未能解决你的问题,请参考以下文章

从Salesforce到Taleo的基本HTTP身份验证

如何使用 SAML 2.0 响应进行特定 DNN 用户角色组身份验证和授权?

没有登录按钮的 Facebook 身份验证

Django REST框架--认证和权限

如何在 Laravel 5.2 中创建多重身份验证

(实用篇)php通过会话控制实现身份验证实例