Guest User

Buggy Cake login code

a guest
Jul 28th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. <?php // Only for hightlighting
  2.  
  3. // Actions in controllers
  4. ////////////////////////////////////////////////////////////////////////////////
  5. // In app controller
  6.     function beforeFilter () {
  7.         $this->Auth->loginAction = array (
  8.             'controller' => 'users',
  9.             'action' => 'login'
  10.         );
  11.        
  12.         $this->Auth->autoRedirect = array (
  13.             'controller' => 'users',
  14.             'action' => 'dashboard'
  15.         );
  16.        
  17.         $this->Auth->loginRedirect = array (
  18.             'controller' => 'users',
  19.             'action' => 'dashboard'
  20.         );
  21.        
  22.         $this->Auth->logoutRedirect = array (
  23.             'controller' => 'users',
  24.             'action' => 'logout'
  25.         );
  26.        
  27.         $this->Auth->loginError = "No t'has identificat correctament";
  28.         $this->Auth->authError = "Has d'identificar-te com a usuari per accedir a aquesta secció";
  29.        
  30.         $this->Auth->actionPath = 'controllers/';
  31.        
  32.         $this->Auth->authorize = 'actions';
  33.     }
  34.  
  35. // In users controller
  36.     public function login () {
  37.         $this->set ('title_for_layout', 'ARE · Gestió de Pràctiques externes');
  38.         $this->set ('header_text', 'ARE · Gestió de Pràctiques externes');
  39.        
  40.         $userinfo = $this->Auth->user ();
  41.         if ($userinfo) {           
  42.             if (substr($userinfo['User']['validate'], 0, 2) == 'v:') {
  43.                 $this->Auth->logout ();
  44.                 $this->redirect(array(
  45.                     'controller' => 'users',
  46.                     'action' => 'login'
  47.                 ));
  48.             } else {
  49.                 $this->redirect(array(
  50.                     'controller' => 'users',
  51.                     'action' => 'dashboard'
  52.                 ));
  53.             }
  54.         } elseif (isset($this->data)) {
  55.             if ($this->Auth->login($this->data) == 0) {
  56.                 $this->set ('cleanLogin', false);
  57.             }
  58.         } else {
  59.             $this->set ('cleanLogin', true);
  60.         }
  61.     }
  62.    
  63.     public function dashboard () {
  64.         $this->set ('title_for_layout', 'ARE · Panell de gestió');
  65.         $this->set ('header_text', 'ARE · Panell de gestió');
  66.        
  67.         $userinfo = $this->Auth->user ();
  68.        
  69.         // The user have to be validated
  70.         if (substr($userinfo['User']['validate'], 0, 2) == 'v:') {
  71.             $this->Auth->logout ();
  72.             $this->redirect(array(
  73.                 'controller' => 'users',
  74.                 'action' => 'login'
  75.             ));
  76.         }
  77.        
  78.         $groupid = $userinfo['User']['group_id'];
  79.         $dashboard = $this->requestAction (
  80.             array (
  81.                 'controller' => $this->group_numbers[$groupid],
  82.                 'action' => 'dashboard'
  83.             ),
  84.             array (
  85.                 'named' => array (),
  86.                 'pass' => array (),
  87.                 'return'
  88.             )
  89.         );
  90.        
  91.         $this->set('dashboard', $dashboard);
  92.     }
  93.  
  94. // In companies controller (the specific user I'm trying represents a company
  95.     public function dashboard () {
  96.         $session = $this->Session;
  97.         $user = $this->Auth->user ();
  98.        
  99.         if (!$session->check ('Role')) {
  100.             $company = $this->Company->find (
  101.                 'first',
  102.                 array (
  103.                     'conditions' => array (
  104.                         'user_id' => $user['User']['id']
  105.                     ),
  106.                     'recursive' => -1
  107.                 )
  108.             );
  109.            
  110.             $session->write ('Role', $company);
  111.         }
  112.        
  113.         $this->set ('named', array ());
  114.     }
  115.  
  116. ?> // End of PHP block (for hightlighting)
  117.  
  118. // The users login view
  119. ////////////////////////////////////////////////////////////////////////////////
  120. <form id="UserLoginForm" method="post" action="<?php echo $html->url(array ('controller' => 'users', 'action' => 'login')); ?>" accept-charset="utf-8">
  121.     <div style="display:none;"><input type="hidden" name="_method" value="POST" /></div>
  122.     <div id="login-box">
  123.         <div class="input text login-field">
  124.             <label for="UserUsername">Usuari: </label>
  125.             <input name="data[User][username]" type="text" maxlength="32" id="UserUsername" />
  126.         </div>
  127.         <div class="input password login-field">
  128.             <label for="UserPassword">Clau: </label>
  129.             <input type="password" name="data[User][password]" id="UserPassword" />
  130.         </div>
  131.         <div class="submit login-button">
  132.             <input type="submit" value="Entra" />
  133.         </div>
  134.         <div class="login-register">
  135.         <?php
  136.             echo $html->link (
  137.                 'Registri un usuari per a la seva empresa',
  138.                 array (
  139.                     'controller' => 'companies',
  140.                     'action' => 'create'
  141.                 )
  142.             ).'<br/>';
  143.             echo $html->link (
  144.                 'Registra\'t com a estudiant',
  145.                 array (
  146.                     'controller' => 'students',
  147.                     'action' => 'create'
  148.                 )
  149.             ).'<br/>';
  150.         ?>
  151.         </div>
  152.     </div>
  153.     <?php if (!$cleanLogin): ?>
  154.     <div class="login-error">No ha introduit les dades correctes</div>
  155.     <?php endif;?>
  156. </form>
Advertisement
Add Comment
Please, Sign In to add comment