/ Published in: PHP
this is all the actions needed for auth and the users login/register system
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php class UsersController extends AppController { var $name = 'Users'; function beforeFilter() { parent::beforeFilter(); } function index() { $users = $this->User->find('all'); } else { $this->set('users', $users); } } function view($id) { $user = $this->User->findById($id); $this->set('user', $user); } else { $this->redirect('index'); } } function add() { $this->User->set($this->data); if ($this->User->validates()) { $this->data['User']['password'] = $this->data['User']['clear_password']; $this->User->save($this->Auth->hashPasswords($this->data), false); $this->redirect('index'); } else { } } } function edit($id = null) { foreach ($this->data['User'] as $field => $data) { $fields[] = $field; } } $fields[] = 'password'; $fields[] = 'clear_password'; $fields[] = 'confirm_password'; } $this->User->set($this->data); if ($this->User->validates()) { $this->data['User']['password'] = $this->data['User']['confirm_password']; } $this->User->save($this->Auth->hashPasswords($this->data), false, $fields); $this->Session->setFlash($this->data['User']['name'].' Updated', 'default', array('class'=>'good')); $this->redirect('index'); } } else { $user = $this->User->findById($id); $this->redirect('index'); } else { $this->data = $user; } } } function delete($id) { $user = $this->User->findById($id); } else { if ($user['User']['id'] == $this->Session->read('Auth.User.id')) { } else { if ($this->User->del($id)) { } else { $this->Session->setFlash('Failed to delete '.$user['User']['name'], 'default', array('class'=>'bad')); } } } $this->redirect('index'); } function login() { // Auth Magic } function logout() { $this->Session->del('Auth.User'); $this->redirect('login'); } } ?>
URL: http://www.jbcrawford.net/archives/45