11.07 php saved
derelm
Tags add more
auth  
Note
simple Auth component example updated
  1. /*
  2.     This is a very simplistic example of the Auth component.
  3.  
  4.     It assumes you have a model `User` with at least these fields:
  5.     * `username`
  6.     * `password` (should be a CHAR of length 40)
  7.     * `is_admin` (BOOL/INT)
  8. */
  9.  
  10. //---
  11. // app_controller.php
  12.  
  13. class AppController extends Controller {
  14.     var $components = array('Auth');
  15.  
  16.     function beforeFilter() {
  17.         $this->Auth->authorize = "controller";
  18.         //$this->Auth->allow();    // if uncommented, allows anonymous access to any action
  19.     }
  20.  
  21.     function isAuthorized() {
  22.         /* defines if the loggedin user may access the current action
  23.            in this case only users with an `is_admin` field and a value of `true`
  24.            are allowed to use admin routes of this controller */
  25.  
  26.         if (isset($this->params['admin'])) {
  27.             return $this->Auth->user('is_admin');
  28.         }
  29.         return true;
  30.     }
  31. }
  32.  
  33. //---
  34. // controllers/users.php
  35.  
  36. class UsersController extends AppController {
  37.  
  38.     var $name = 'Users';
  39.     var $helpers = array('Html', 'Form');
  40.  
  41.     function login() {
  42.     }
  43.  
  44.     function logout() {
  45.         return $this->Auth->logout();
  46.     }
  47. }
  48.  
  49. //---
  50. // views/users/login.ctp
  51.  
  52. <form method="post" action=".">
  53.     <p><?php echo $form->input('User/username'); ?></p>
  54.     <p><?php echo $form->input('User/password'); ?></p>
  55.     <p><?php echo $html->submit(); ?></p>
  56. </form>
  57.  
  58. //---
  59. // views/users/logout.ctp
  60.  
  61. <p>You've been logged out successfully.</p>
  62.  
  63.  
Parsed in 0.060 seconds, using GeSHi 1.0.7.14

Modify this Paste