04.04 php saved
dw
Tags add more
auth cakephp  
Note
Auth component is hashing passwords in admin_add, but not admin_pw.
  1. class AppController extends Controller{
  2.  
  3.     var $helpers = array('Javascript','Html', 'Form','pdf' );
  4.     var $components = array('Auth','Session','RequestHandler');
  5.  
  6.     function beforeFilter(){
  7.        Security::setHash("md5");
  8.         $this->Auth->model = 'User';
  9.         $this->Auth->fields = array('username' => 'username', 'password' => 'password');
  10.         $this->Auth->sessionKey = 'User';
  11.         $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
  12.         $this->Auth->loginRedirect = array('controller' => 'profiles', 'action'=>'report_index');
  13.         $this->Auth->logoutRedirect = null;
  14.         $this->Auth->loginError = 'Invalid username / password combination. Please try again';
  15.         $this->Auth->authorize = 'controller';
  16.     }
  17.    
  18.      function checkAdmin(){
  19.     if($this->Session->read('User.admin')=="1"){
  20.         return true;
  21.     }
  22.      }
  23.  
  24.      function flash($msg,$to){
  25.     $this->Session->setFlash($msg);
  26.     $this->redirect($to);
  27.     exit;
  28.      }
  29.  
  30. }
  31.  
  32.  
  33. class UsersController extends AppController {
  34.  
  35.    var $name = 'Users';
  36.  
  37.    function beforeFilter(){
  38.         parent::beforeFilter();
  39.         $this->Auth->allow('pw', 'changepw');
  40.    }
  41.  
  42.    function admin_pw($id=null){
  43.  
  44.     if(!$this->checkAdmin()){
  45.         $this->flash('You are not authorized for this.',array('contoller'=>'profiles','action'=>'report_index'));
  46.     }else{
  47.         if(empty($this->data)){
  48.             if(!$id){
  49.                 $this->flash('Invalid id for User', array('action'=>'admin_index'));
  50.             }
  51.             $user = $this->User->read(null, $id);
  52.             $this->set('user', $user);
  53.         }else{
  54.             //the following is needed to hash pw
  55.             //$this->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
  56.             if($this->User->save($this->data)){
  57.                 $this->flash('Password Changed',array('action'=>'admin_index'));
  58.             }else{
  59.                 $this->flash('Error',array('action'=>'admin_index'));
  60.             }
  61.         }
  62.     }
  63.   }
  64.  
  65.   function admin_add(){
  66.     if(!$this->checkAdmin()){
  67.         $this->flash('You are not authorized for this.',array('contoller'=>'profiles','action'=>'report_index'));
  68.     }else{
  69.         if(empty($this->data)){
  70.             $this->render();
  71.         }else{
  72.             //save
  73.             if($this->User->save($this->data)){
  74.                 $this->flash('User added.',array('action'=>'admin_index'));
  75.             }else{
  76.                 $this->flash('Error',array('action'=>'admin_index'));
  77.             }
  78.         }
  79.     }
  80.   }
  81. }
  82.  
  83.  
  84. class User extends AppModel {
  85.  
  86.     var $name = 'User';
  87.     var $useTable = 'newhire_users';
  88. }
  89.  
  90.  
  91. // views/admin_add.ctp
  92. <div id="users">
  93.     <h2>Add User</h2>
  94.  
  95.     <?php
  96.         echo $form->create('User',array('action'=>'add'));
  97.     ?>
  98.  
  99.     <table >
  100.        <?php
  101.             echo $html->tableCells(array(
  102.                         "User Name",
  103.                         $form->input('User.username', array('type'=>'text','size' => '30','label'=>false))
  104.                         ));
  105.                        
  106.             echo $html->tableCells(array(
  107.                         "Password",
  108.                         $form->password('User.password', array('size' => '30','label'=>false))
  109.                         ));
  110.                        
  111.             echo $html->tableCells(array(
  112.                         "Admin",
  113.                         $form->checkbox('User.admin')
  114.                         ));
  115.  
  116.         ?>
  117.     </table>
  118.  
  119.     <div class="submit toppad">
  120.         <?php
  121.             echo $form->submit('Save');
  122.             echo $form->end();
  123.         ?>
  124.     </div>
  125.  
  126. </div>
  127.  
  128.  
  129. // views/admin_pw.ctp
  130. <div id="users">
  131.     <h2>Edit User</h2>
  132.  
  133.     <?php echo $form->create('User',array('action'=>'admin_pw')); ?>
  134.    
  135.     <table>
  136.         <?php
  137.             echo $form->hidden(
  138.                         'User.id',
  139.                         array('value'=>$user['User']['id'])
  140.                         );
  141.                        
  142.             echo $html->tableCells(array(
  143.                         "New Password for ".$user['User']['username'],
  144.                         $form->password('User.password', array('size' => '30','value'=>'','label'=>false))
  145.                         ));
  146.         ?>
  147.     </table>
  148.  
  149.         <?php
  150.             echo $form->submit('Save');
  151.             echo $form->end();
  152.         ?>
  153.    
  154. </div>
Parsed in 0.341 seconds, using GeSHi 1.0.7.14

Modify this Paste