Revision: 26901
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at May 15, 2010 02:04 by sevennbsp
Initial Code
<?php
class UsersController extends AppController {
var $name = 'Users';
function beforeFilter() {
parent::beforeFilter();
}
function index() {
$users = $this->User->find('all');
if (empty($users)) {
$this->Session->setFlash('There are no users defined', 'default', array('class'=>'bad'));
} else {
$this->set('users', $users);
}
}
function view($id) {
$user = $this->User->findById($id);
if (!empty($user)) {
$this->set('user', $user);
} else {
$this->Session->setFlash('Invalid User ID', 'default', array('class'=>'bad'));
$this->redirect('index');
}
}
function add() {
if (!empty($this->data)) {
$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->Session->setFlash($this->data['User']['name'].' Added', 'default', array('class'=>'good'));
$this->redirect('index');
} else {
$this->Session->setFlash('Please correct the errors below', 'default', array('class'=>'bad'));
}
}
}
function edit($id = null) {
if (!empty($this->data)) {
foreach ($this->data['User'] as $field => $data) {
if (!in_array($field, array('clear_password', 'confirm_password'))) {
$fields[] = $field;
}
}
if (!empty($this->data['User']['clear_password']) || !empty($this->data['User']['confirm_password'])) {
$fields[] = 'password';
$fields[] = 'clear_password';
$fields[] = 'confirm_password';
}
$this->User->set($this->data);
if ($this->User->validates()) {
if (!empty($this->data['User']['clear_password'])) {
$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);
if (empty($user)) {
$this->Session->setFlash('Invalid User ID', 'default', array('class'=>'bad'));
$this->redirect('index');
} else {
unset($user['User']['password']);
$this->data = $user;
}
}
}
function delete($id) {
$user = $this->User->findById($id);
if (empty($user)) {
$this->Session->setFlash('Invalid User ID', 'default', array('class'=>'bad'));
} else {
if ($user['User']['id'] == $this->Session->read('Auth.User.id')) {
$this->Session->setFlash('Sorry, you can not delete yourself', 'default', array('class'=>'bad'));
} else {
if ($this->User->del($id)) {
$this->Session->setFlash($user['User']['name'].' Deleted', 'default', array('class'=>'good'));
} 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->Session->setFlash('You are now logged out', 'default', array('class'=>'good'));
$this->redirect('login');
}
}
?>
Initial URL
http://www.jbcrawford.net/archives/45
Initial Description
this is all the actions needed for auth and the users login/register system
Initial Title
users_controller auth
Initial Tags
login, cakephp
Initial Language
PHP