2009-01-12 21:13:18 -08:00
|
|
|
|
<?php
|
|
|
|
|
|
class UsersController extends AppController {
|
|
|
|
|
|
|
2010-01-10 15:34:31 -08:00
|
|
|
|
var $name = 'Users';
|
2011-03-15 20:50:54 -07:00
|
|
|
|
var $helpers = array('Html', 'Form', 'Javascript', 'Text');
|
2010-01-10 15:34:31 -08:00
|
|
|
|
var $components = array('Acl','Auth', 'Cookie');
|
|
|
|
|
|
var $paginate = array(
|
2011-03-05 21:27:20 -08:00
|
|
|
|
'Users' => array('order' => array('User.id' => 'desc'),
|
|
|
|
|
|
'limit' => 200
|
|
|
|
|
|
),
|
|
|
|
|
|
'Enquiry' => array('order' => array('Enquiry.id' => 'desc'), 'limit' => 250)
|
2010-01-10 15:34:31 -08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
function beforeFilter() {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->Auth->allow('add');
|
|
|
|
|
|
$this->set('currentuser', $this->Auth->user());
|
|
|
|
|
|
$this->Auth->autoRedirect = false;
|
|
|
|
|
|
//$this->login();
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function login() { //Provided by the authComponent
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->pageTitle = ': Login';
|
|
|
|
|
|
//$this->Session->setFlash(__('Please enter your Username and Password to continue', true));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Auth Cookie code from http://www.webdevelopment2.com/cakephp-auth-component-tutorial-3/ */
|
|
|
|
|
|
//– code inside this function will execute only when autoRedirect was set to false (i.e. in a beforeFilter).
|
|
|
|
|
|
if ($this->Auth->user()) {
|
|
|
|
|
|
if (!empty($this->data) && $this->data['User']['remember_me']) {
|
|
|
|
|
|
$cookie = array();
|
|
|
|
|
|
$cookie['username'] = $this->data['User']['username'];
|
|
|
|
|
|
$cookie['password'] = $this->data['User']['password'];
|
|
|
|
|
|
$this->Cookie->write('Auth.User', $cookie, true, '+2 weeks');
|
|
|
|
|
|
unset($this->data['User']['remember_me']);
|
|
|
|
|
|
}
|
|
|
|
|
|
$this->redirect($this->Auth->redirect());
|
|
|
|
|
|
}
|
|
|
|
|
|
if (empty($this->data)) {
|
|
|
|
|
|
$cookie = $this->Cookie->read('Auth.User');
|
|
|
|
|
|
if (!is_null($cookie)) {
|
|
|
|
|
|
if ($this->Auth->login($cookie)) {
|
|
|
|
|
|
// Clear auth message, just in case we use it.
|
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
|
// $this->Session->setFlash(__('Welcome back '.$cookie['username']), true);
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->Session->del('Message.auth');
|
|
|
|
|
|
$this->redirect($this->Auth->redirect());
|
|
|
|
|
|
} else { // Delete invalid Cookie
|
|
|
|
|
|
$this->Cookie->del('Auth.User');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function logout() {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->redirect($this->Auth->logout());
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function index() {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->User->recursive = 0;
|
|
|
|
|
|
$this->set('users', $this->paginate());
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
2011-03-05 21:27:20 -08:00
|
|
|
|
/**
|
|
|
|
|
|
* View a User.
|
|
|
|
|
|
*
|
|
|
|
|
|
* Depending on the type of user. Fetch the appropriate data and render the appropriate template.
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param int $id
|
|
|
|
|
|
*/
|
2010-01-10 15:34:31 -08:00
|
|
|
|
function view($id = null) {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
if (!$id) {
|
|
|
|
|
|
$this->Session->setFlash(__('Invalid User.', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$user = $this->User->read(null, $id);
|
|
|
|
|
|
$this->set('user', $user);
|
|
|
|
|
|
|
2011-03-15 21:37:49 -07:00
|
|
|
|
/*$emailIDs = array();
|
2011-03-15 20:50:54 -07:00
|
|
|
|
foreach($user['Email'] as $email) {
|
|
|
|
|
|
$emailIDs[] = $email['id'];
|
|
|
|
|
|
}
|
|
|
|
|
|
$emails = $this->User->Email->find('all', array('conditions'=>array('Email.id'=>$emailIDs)));
|
|
|
|
|
|
$this->set('emails', $emails);
|
2011-03-15 21:37:49 -07:00
|
|
|
|
/*/
|
2011-03-15 20:50:54 -07:00
|
|
|
|
|
|
|
|
|
|
|
2011-03-05 21:27:20 -08:00
|
|
|
|
switch($user['User']['type']) {
|
2011-03-09 23:18:26 -08:00
|
|
|
|
case 'contact':
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->render('viewContact');
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'principle':
|
|
|
|
|
|
|
|
|
|
|
|
$this->render('viewPrinciple');
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
|
case 'user':
|
2011-03-15 23:51:43 -07:00
|
|
|
|
//$this->set('enquiries', $this->paginate('Enquiry', array('Enquiry.user_id' => $id)));
|
2011-03-05 21:27:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$this->render('viewUser');
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-10 17:22:57 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function add_edit() {
|
|
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($this->params['named']['type'])) {
|
|
|
|
|
|
$this->set('type', $this->params['named']['type']);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($this->params['named']['principle_id'])) {
|
|
|
|
|
|
$this->set('principle_id', $this->params['named']['principle_id']);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($this->params['named']['customer_id'])) {
|
|
|
|
|
|
$this->set('customer_id', $this->params['named']['customer_id']);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($this->params['named']['user_id'])) {
|
|
|
|
|
|
$userID = $this->params['named']['user_id'];
|
|
|
|
|
|
$this->set('user_id', $userID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($this->params['named']['action'])) {
|
|
|
|
|
|
$action = $this->params['named']['action'];
|
|
|
|
|
|
$this->set('action', $action);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(isset($action) && isset($userID)) {
|
|
|
|
|
|
$this->data = $this->User->read(null, $userID);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-05 21:27:20 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-03-09 23:18:26 -08:00
|
|
|
|
function add_user() {
|
|
|
|
|
|
|
|
|
|
|
|
Configure::write('debug', 0);
|
|
|
|
|
|
$this->layout = 'ajax';
|
2011-03-05 21:27:20 -08:00
|
|
|
|
if (!empty($this->data)) {
|
2011-03-09 23:18:26 -08:00
|
|
|
|
$this->User->create();
|
|
|
|
|
|
$this->User->set($this->data['User']);
|
|
|
|
|
|
if($this->User->validates()) {
|
|
|
|
|
|
if ($this->User->save($this->data)) {
|
|
|
|
|
|
$message = __('The User has been saved.', true);
|
|
|
|
|
|
$data = $this->data;
|
|
|
|
|
|
$this->set('success', compact('message', 'data'));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
$message = __('The User could not be saved. Please, try again.', true);
|
|
|
|
|
|
$User = $this->User->invalidFields();
|
|
|
|
|
|
$data = compact('User');
|
|
|
|
|
|
$this->set('errors', compact('message', 'data'));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
|
|
|
$this->redirect('/');
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*if (!empty($this->data)) {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
$this->User->create();
|
|
|
|
|
|
if ($this->User->save($this->data)) {
|
|
|
|
|
|
$this->Session->setFlash(__('The User has been saved', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2011-03-09 23:18:26 -08:00
|
|
|
|
}*/
|
2010-01-10 15:34:31 -08:00
|
|
|
|
|
2011-03-05 21:27:20 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* the default generated add() method. Used for system users, rather than contacts & principle contacts.
|
|
|
|
|
|
*/
|
2010-01-10 15:34:31 -08:00
|
|
|
|
function add() {
|
|
|
|
|
|
|
2011-03-05 21:27:20 -08:00
|
|
|
|
if (!empty($this->data)) {
|
|
|
|
|
|
$this->User->create();
|
|
|
|
|
|
if ($this->User->save($this->data)) {
|
|
|
|
|
|
$this->Session->setFlash(__('The User has been saved', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function edit($id = null) {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
if (!$id && empty($this->data)) {
|
|
|
|
|
|
$this->Session->setFlash(__('Invalid User', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
}
|
|
|
|
|
|
if (!empty($this->data)) {
|
|
|
|
|
|
if ($this->User->save($this->data)) {
|
|
|
|
|
|
$this->Session->setFlash(__('The User has been saved', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
} else {
|
|
|
|
|
|
$this->Session->setFlash(__('The User could not be saved. Please, try again.', true));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (empty($this->data)) {
|
|
|
|
|
|
$this->data = $this->User->read(null, $id);
|
|
|
|
|
|
}
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function delete($id = null) {
|
2011-03-05 21:27:20 -08:00
|
|
|
|
if (!$id) {
|
|
|
|
|
|
$this->Session->setFlash(__('Invalid id for User', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
}
|
|
|
|
|
|
if ($this->User->del($id)) {
|
|
|
|
|
|
$this->Session->setFlash(__('User deleted', true));
|
|
|
|
|
|
$this->redirect(array('action'=>'index'));
|
|
|
|
|
|
}
|
2010-01-10 15:34:31 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-03-15 23:51:43 -07:00
|
|
|
|
|
2009-01-12 21:13:18 -08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|