05.19 php saved
Crazy
Tags add more
Authentication Autha  
Note
Problem with loging in when using $this->Auth->allow('*');
  1. class AppController extends Controller {
  2.     var $components = array('Auth');
  3. }
  4.  
  5.  
  6. class UsersController extends AppController {
  7.     var $name = "Users";
  8.     var $helpers = array('Html', 'Form');
  9.     var $components = array('Auth');
  10.  
  11.     function beforeFilter() {
  12.         $this->layout  = "login";
  13.         $this->Auth->allow('*');
  14.         Security::setHash("md5");
  15.     }
  16.  
  17.     function login() { }
  18.  
  19.     function logout() {
  20.         $this->redirect($this->Auth->logout());
  21.         $this->redirect("/");
  22.     }
  23.  
  24.     function register() {
  25.         if (!empty($this->data)) {
  26.             $this->cleanUpFields();
  27.             $this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
  28.             $this->User->create();
  29.             if ($this->User->save($this->data)) {
  30.                 $this->Session->write('User', $this->User->findByUsername($this->data['User']['username']));
  31.                 $this->Session->setFlash('Thank you for registering.');
  32.                 $this->redirect('/');
  33.             } else {
  34.                 $this->Session->setFlash('The User could not be saved. Please, try again.');
  35.             }
  36.         }
  37.     }
  38.  
  39. }
  40.  
  41.  
  42. //View for the login form
  43. <div class="login">
  44. <h2>Login</h2>
  45.     <?php echo $form->create(null, array('action' => 'login'));?>
  46.         <?php echo $form->input('User.username', array("label" => "Username : "));?>
  47.         <?php echo $form->input('User.password', array("label" => "Password : "));?>
  48.         <?php echo $form->submit('Login');?>
  49.     <?php echo $form->end(); ?>
  50.     <div>
  51.         <?php
  52.             if ($session->check('Message.flash')) { $session->flash(); }
  53.             if ($session->check('Message.auth')) { $session->flash('auth'); }
  54.         ?>
  55.     </div>
  56. </div>
  57.  
  58. //View for the registration
  59. <div class="login">
  60.     <h2>Register an Account</h2>
  61.     <div>
  62.             <?php echo $form->create(null, array('action' => 'register'));?>
  63.             <?php echo $form->input('User.username', array("label" => "Username : "));?>
  64.             <?php echo $form->input('User.password', array("label" => "Password : "));?>
  65.             <div style="text-align:center;"><?php echo $form->submit('Register');?></div>
  66.         <?php echo $form->end(); ?>
  67.     </div>
  68.     <div>
  69.         An account request for the admin needs to be approved. <br />
  70.         Contact <u><b>Crazy</b></u> to activate your account
  71.     </div>
  72.     <div>
  73.         If you already have an account and want to log in, go <a href='/users/login'>here</a>
  74.     </div>
  75. </div>
  76.  
  77.  
Parsed in 0.185 seconds, using GeSHi 1.0.7.14

Modify this Paste