<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public $components = array('Paginator', 'Session', 'Flash');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('login','logout');
}
public function login(){
$this->layout = 'admin_login';
$groups = array('1' => 'admin', '2' => 'agent');
// if already logged-in, redirect
if ($this->Session->check ( 'Auth.User' )) {
$get_prefix = $groups[$this->Auth->user ( 'group_id' )];
$this->redirect('/'.$get_prefix.'-dashboard');
}
if ($this->request->is ( 'post' )) {
if ($this->Auth->login ()) {
//@see prefixes
$get_prefix = $groups[$this->Auth->user ( 'group_id' )];
/// $this->components ['Auth'] ['loginRedirect'] [$get_prefix] = true;
$this->Flash->success ( __ ( 'Welcome, ' . $this->Auth->user ( 'name' ) ) );
$this->redirect('/'.$get_prefix.'-dashboard');
/// $this->redirect ( $this->Auth->redirectUrl () );
} else {
// echo AuthComponent::password($this ->data['User']['password']);
$this->Flash->error ( __ ( 'Invalid username or password' ), 'alert alert-danger' );
}
}
}
public function logout(){
$this->Flash->success ( __ ( 'Logout Successfully. Thanks !!!' ) );
$this->Auth->logout();
$this->redirect('/login');
}
public function admin_add_agent() {
if ($this->request->is('post')) {
$this->User->create();
$this->request->data['User']['group_id'] = AGENT;
$this->request->data['User']['token'] = md5(date('Y-m-d H:i:s'));
unset($this->request->data['User']['email']);
if ($this->User->saveAll($this->request->data)) {
$this->Flash->success(__('The Agent has been saved.'));
return $this->redirect('/'.$this->params['prefix'].'/agents');
} else {
$this->Flash->error(__('The agent could not be saved. Please, try again.'));
}
}
}
public function admin_edit_agent($token = null) {
//@valid token
$this->is_empty($token);
$options = array('conditions' => array('User.token' => $token));
$data = $this->User->find('first', $options);
if(empty($data)) {
$this->is_empty($token);
}
if ($this->request->is(array('post', 'put'))) {
if(!empty($this->request->data['User']['password']) ||
!empty($this->request->data['User']['repeat_password'])) {
}
if(empty($this->request->data['User']['password']) &&
empty($this->request->data['User']['repeat_password'])) {
unset($this->request->data['User']['password']);
unset($this->request->data['User']['repeat_password']);
}
$this->request->data['User']['token'] = md5(date('Y-m-d H:i:s'));
$this->request->data['User']['id'] = $data['User']['id'];
$this->request->data['UserAgent']['id'] = $data['UserAgent']['id'];
if ($this->User->saveAll($this->request->data)) {
$this->Flash->success(__('The user has been updated.'));
return $this->redirect('/'.$this->params['prefix'].'/agents');
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
} else {
$data['User']['password'] = $data['User']['repeat_password'] = '';
$this->request->data = $data;
}
}
public function admin_agent_listing() {
$this->paginate = array(
'conditions' => array(
'group_id' => AGENT
),
'order' => array(
'User.created' => 'DESC'
)
);
$this->set('agents', $this->Paginator->paginate());
}
public function admin_delete_agent($token = null){
$this->is_empty($token);
$id = $this->User->field('id', array('User.token' => $token));
$this->is_empty($id);
$this->request->allowMethod('post', 'delete');
$in_use = $this->User->Applicant->field('id', array('agent_user_id' => $id));
if (!$in_use && $this->User->delete($id, true)) {
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. It is in use.'));
}
return $this->redirect('/'.$this->params['prefix'].'/agents');
}
private function is_empty($token = null){
if(empty($token)) {
$this->Flash->error(__('Oops !!!, you are trying with wrong data'));
$this->redirect('/'.$this->params['prefix'].'/agent_lsting');
}
}
public function admin_profile($token = null) {
if(empty($token)) {
$this->Flash->error(__('Oops !!!, you are trying with old data'));
$this->redirect('/admin-dashboard');
}
$user_token = $this->Auth->user('token');
if($token != $user_token) {
$this->redirect('/'.$this->params['prefix'].'/profile/'.$user_token);
}
if ($this->request->is(array('post', 'put'))) {
if($this -> request -> data['User']['is_pwd_changed'] == 0) {
unset($this ->request->data['User']['password']);
}
unset($this -> request -> data['User']['is_pwd_changed']);
if ($this->User->save($this->request->data)) {
$this->Flash->success(__('The user has been updated.'));
return $this->redirect('/'.$this->params['prefix'].'/profile/'.$user_token);
} else {
$this->Flash->error(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array(
'recursive' => -1,
'conditions' => array('User.' . $this->User->primaryKey => $this->Auth->user('id')));
$this->request->data = $this->User->find('first', $options);
}
}
}