2015-06-30 23:27:17 +08:00
|
|
|
<?php
|
|
|
|
App::uses('AppController', 'Controller');
|
|
|
|
/**
|
|
|
|
* Users Controller
|
|
|
|
*
|
|
|
|
* @property User $User
|
|
|
|
*/
|
|
|
|
class UsersController extends AppController {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Components
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $components = array('RequestHandler', 'Paginator');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* index method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* This also creates a thumbnail for each user.
|
|
|
|
*/
|
|
|
|
public function index() {
|
|
|
|
$this->User->recursive = 0;
|
|
|
|
|
|
|
|
$users = $this->Paginator->paginate('User');
|
|
|
|
|
|
|
|
$this->set(compact('users'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* view method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function view($id = null) {
|
|
|
|
$this->User->recursive = 1;
|
|
|
|
if (!$this->User->exists($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid user'));
|
|
|
|
}
|
|
|
|
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
|
|
|
|
$user = $this->User->find('first', $options);
|
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'user' => $user,
|
|
|
|
'_serialize' => array('user')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add method
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function add() {
|
|
|
|
if ($this->request->is('post')) {
|
|
|
|
$this->User->create();
|
|
|
|
if ($this->User->save($this->request->data)) {
|
|
|
|
return $this->flash(__('The user has been saved.'), array('action' => 'index'));
|
|
|
|
}
|
|
|
|
$this->Session->setFlash(
|
|
|
|
__('The user could not be saved. Please, try again.')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* edit method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function edit($id = null) {
|
|
|
|
$this->User->id = $id;
|
|
|
|
|
|
|
|
if (!$this->User->exists($id)) {
|
|
|
|
throw new NotFoundException(__('Invalid user'));
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:51:28 +08:00
|
|
|
if ( $this->request->is('post') || $this->request->is('put') ) {
|
|
|
|
if ( $this->User->save($this->request->data) ) {
|
2015-06-30 23:27:17 +08:00
|
|
|
$message = 'Saved';
|
|
|
|
} else {
|
|
|
|
$message = 'Error';
|
|
|
|
}
|
|
|
|
} else {
|
2020-07-20 05:51:28 +08:00
|
|
|
$this->request->data = $this->User->read(null, $id);
|
|
|
|
unset($this->request->data['User']['password']);
|
|
|
|
}
|
2015-06-30 23:27:17 +08:00
|
|
|
|
|
|
|
$this->set(array(
|
|
|
|
'message' => $message,
|
|
|
|
'_serialize' => array('message')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* delete method
|
|
|
|
*
|
|
|
|
* @throws NotFoundException
|
|
|
|
* @param string $id
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete($id = null) {
|
|
|
|
$this->User->id = $id;
|
2020-07-20 05:51:28 +08:00
|
|
|
if ( !$this->User->exists() ) {
|
2015-06-30 23:27:17 +08:00
|
|
|
throw new NotFoundException(__('Invalid user'));
|
|
|
|
}
|
|
|
|
$this->request->allowMethod('post', 'delete');
|
2020-07-20 05:51:28 +08:00
|
|
|
if ( $this->User->delete() ) {
|
2015-06-30 23:27:17 +08:00
|
|
|
$message = 'The user has been deleted.';
|
|
|
|
} else {
|
|
|
|
$message = 'The user could not be deleted. Please, try again.';
|
|
|
|
}
|
|
|
|
$this->set(array(
|
|
|
|
'message' => $message,
|
|
|
|
'_serialize' => array('message')
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:51:28 +08:00
|
|
|
public function beforeFilter() {
|
|
|
|
parent::beforeFilter();
|
2015-06-30 23:27:17 +08:00
|
|
|
|
2020-07-20 05:51:28 +08:00
|
|
|
if ( ZM_OPT_USE_AUTH ) {
|
|
|
|
$this->Auth->allow('add', 'logout');
|
|
|
|
} else {
|
|
|
|
$this->Auth->allow();
|
2015-06-30 23:27:17 +08:00
|
|
|
}
|
2020-07-20 05:51:28 +08:00
|
|
|
}
|
2015-06-30 23:27:17 +08:00
|
|
|
|
|
|
|
public function login() {
|
2020-07-20 05:51:28 +08:00
|
|
|
if ( !ZM_OPT_USE_AUTH ) {
|
2015-06-30 23:27:17 +08:00
|
|
|
$this->set(array(
|
|
|
|
'message' => 'Login is not required.',
|
|
|
|
'_serialize' => array('message')
|
|
|
|
));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-07-20 05:51:28 +08:00
|
|
|
if ( $this->request->is('post') ) {
|
|
|
|
if ( $this->Auth->login() ) {
|
2015-06-30 23:27:17 +08:00
|
|
|
return $this->redirect($this->Auth->redirectUrl());
|
|
|
|
}
|
|
|
|
$this->Session->setFlash(__('Invalid username or password, try again'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function logout() {
|
|
|
|
return $this->redirect($this->Auth->logout());
|
|
|
|
}
|
|
|
|
}
|