/trunk/chimbien/app/plugins/users/controllers/users_controller.php
PHP | 346 lines | 288 code | 28 blank | 30 comment | 35 complexity | c689ce7d64f362efdd9b3ef749ca626a MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, BSD-3-Clause
- <?php
- class UsersController extends UsersAppController {
- public $type = 'USER';
- /**
- * Controller Name
- * @access public
- * @var string
- */
- public $name = 'Users';
- /**
- * Models
- * @access public
- * @var array
- */
- public $uses = array('User', 'AccessLevel', 'Province');
- /**
- * Components
- * @access public
- * @var array
- */
- public $components = array('Email');
- /**
- * Pagination
- * @access public
- * @var array
- */
- public $paginate = array(
- 'User' => array(
- 'order' => 'User.username ASC',
- 'limit' => 25,
- 'contain' => false
- )
- );
- var $helpers = array('Javascript', 'Ckeditor', 'Status');
- function beforeFilter() {
- parent::beforeFilter();
- }
- function admin_profile(){
- if (!empty($this->data)) {
- $this->data['User']['id'] = $this->Auth->user('id');
- if ($this->User->save($this->data)) {
- $this->Session->setFlash(__('The profile has been saved', true),'success');
- } else {
- $this->Session->setFlash(__('The Profile could not be saved. Please, try again.', true),'success');
- }
- }
- if (empty($this->data)) {
- $this->data = $this->User->read(null, $id);
- }
- }
- function admin_index() {
- //filter
- $keyword = trim($this->data['Search']['keyword']);
- //$checkout = $this->data['Search']['checkout'];
- $conds = array();
- //if(!empty($checkout)){
- // $conds['Room.checkout']=$checkout-1;
- //}
- if (!empty($keyword)) {
- $conds['OR'] = array(
- 'lower(CONVERT(User.username USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
- 'lower(CONVERT(User.name USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%'
- );
- }
- //End filter
- //$this->User->Behaviors->attach('Containable');
- $users = $this->User->find('all', array(
- 'fields'=>array('User.id','User.name','User.username','User.status','User.isAdmin'),
- 'conditions' => $conds,
- 'order' => 'User.created'
- ));
- $this->set(compact('users'));
- }
-
- function admin_permission($id = null){
- $this->layout = 'ajax';
- if(!empty($this->params['form'])){
- Configure::write('debug', 0);
- $this->autoRender = false;
- $message = array();
- $this->loadModel('Permission');
- $this->Permission->deleteAll(array('Permission.user_id'=>$id));
- foreach($this->params['form'] as $controller){
- $per['Permission']['user_id'] = $id;
- $per['Permission']['controller'] = $controller;
- $this->Permission->create();
- $this->Permission->save($per);
- }
- $message['error'] = true;
- return json_encode($message);
-
- }
- $listcons = App::objects('controller');
- $listcons[count($listcons)] = 'users';
- $this->User->Behaviors->attach('Containable');
- $user = $this->User->find('first', array(
- 'fields'=>array('User.id','User.name'),
- 'conditions' => array('User.id'=>$id)
- ));
- $this->set(compact('listcons','user'));
- }
-
- function getPermission($id = null){
- $this->loadModel('Permission');
- $pers = $this->Permission->find('list',array('fields'=>array('Permission.controller'),'conditions'=>array('Permission.user_id'=>$id)));
- return $pers;
- }
- function admin_add() {
- if (!empty($this->data)) {
- $this->User->create();
- $this->data['User']['password'] = $this->Auth->password('123456');
- $this->data['User']['activation_code'] = md5(uniqid());
- if ($this->User->save($this->data)) {
- $this->Session->setFlash(__('The users has been saved', true));
- $this->redirect(array('action' => 'index'));
- } else {
- $this->Session->setFlash(__('The users could not be saved. Please, try again.', true));
- }
- }
- $provinces = $this->Province->find('list');
- $accesslevel = $this->AccessLevel->find('list');
- $this->set('provinces', $provinces);
- $this->set('accesslevel', $accesslevel);
- }
- function admin_edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid user', true));
- $this->redirect(array('action' => 'index'));
- }
- //print_r($this->data);
- if (!empty($this->data)) {
- $this->User->id = $id;
- 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);
- }
- $provinces = $this->Province->find('list');
- $this->set('provinces', $provinces);
- }
-
- function admin_changeStatusAll($ids = null, $status=null) {
- if (!empty($ids)) {
- $id = explode('-', $ids);
- $this->User->updateAll(array('User.status' => $status,'User.modified'=>'"'.date('Y-m-d H:i:s').'"'), array('User.id' => $id));
- }
- $this->Session->setFlash(sprintf(__('Chuy?n ??i tr?ng thái thŕnh công.', true)), 'admin/flashsuccess', '', 'success');
- $this->redirect(array('action' => 'index'));
- }
-
- function admin_changestatus() {
- Configure::write('debug', 0);
- $this->autoRender = false;
- $id = $this->params['form']['id'];
- $status = $this->params['form']['status'];
- $this->User->updateAll(array('User.status' => $status), array('User.id' => $id));
- $return = array();
- $return['id'] = $id;
- $return['newstatus'] = ($status == 0) ? 1 : 0;
- echo json_encode($return);
- }
- function getProvinceById($id=null) {
- return $this->Province->findById($id);
- }
- function admin_dashboard() {
- $this->set('title_for_layout','B?ng ?i?u khi?n');
- }
- function userLog(){
- $this->loadModel('Log');
- $log['Log']['user_id'] = $this->Auth->user('id');
- $log['Log']['login'] = date('Y-m-d H:i:s');
- $log['Log']['ip'] = $_SERVER['REMOTE_ADDR'];
- $log['Log']['browser'] = $_SERVER['HTTP_USER_AGENT'];
- $this->Log->create();
- $this->Log->save($log);
- $this->Session->write('log',$this->Log->id);
- $this->Session->write('Auth.User.ip',$log['Log']['ip']);
- }
- function admin_login() {
- $this->set('title_for_layout','Admin Login');
- $this->layout = "admin_login";
- if ($this->Auth->user('id') && !empty($this->data)) {
- $this->userLog();
- $this->redirect('/admin');
- } else {
- empty($this->data) ? $this->set('deny', '') : $this->set('deny', 'Access Deny');
- }
- }
- function admin_logout() {
- //$this->Session->setFlash(__('Log out successful.', true));
- $this->loadModel('Log');
- $this->Log->updateAll(array('Log.logout'=>'"'.date('Y-m-d H:i:s').'"'),array('Log.id'=>$this->Session->read('log')));
- $this->Session->delete('User');
- $this->redirect($this->Auth->logout());
- exit();
- }
- function admin_delete($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid id for user', true));
- $this->redirect(array('plugin' => 'users', 'controller' => 'users', 'action' => 'index'));
- }
- if ($this->User->delete($id)) {
- $this->Session->setFlash(__('User deleted', true));
- $this->redirect(array('plugin' => 'users', 'controller' => 'users', 'action' => 'index'));
- }
- $this->Session->setFlash(__('User was not deleted', true));
- $this->redirect(array('plugin' => 'users', 'controller' => 'users', 'action' => 'index'));
- }
- function admin_deleteAll() {
- Configure::write('debug', 0);
- $this->autoRender = false;
- $id = $this->params['form']['id'];
- $ids = explode('-', $id);
- foreach ($ids as $id) {
- $this->User->delete($id);
- }
- if (count($ids) > 1) {
- $this->Session->setFlash(__('Users deleted', true));
- } else {
- $this->Session->setFlash(__('User deleted', true));
- }
- echo 'true';
- }
- function admin_changepassword() {
- if ($this->Auth->user('id') && !empty($this->data)) {
- $curpass = $this->Auth->password($this->data['User']['current_password']);
- $confirmpass = $this->Auth->password($this->data['User']['password_confirm']);
- $user = $this->User->find('first', array('conditions' => array('User.id'=>$this->Auth->user('id'),'User.username' => $this->data['User']['username'],'User.password' => $curpass)));
- if (count($user) > 0) {
- $users['User']['id']=$this->Auth->user('id');
- $users['User']['username'] = $this->data['User']['username'];
- $users['User']['password'] = $this->data['User']['password'];
- if ($this->User->save($users)) {
- $this->Session->setFlash(__('Password has been change', true),'admin/flashsuccess','','success');
- }
- }else{
- $this->Session->setFlash(__('Error Current Password', true),'admin/flashsuccess','','error');
- }
- }
- }
- function checkExistMail() {
- Configure::write('debug', 0);
- $this->autoRender = false;
- $return = array();
- $email = $this->params['form']['email'];
- $user_email = $this->User->findByEmail($email);
- if ($user_email != false) {
- $return['msg'] = 'Email nŕy ?ă t?n t?i trong h? th?ng, vui lňng ch??n email khác.';
- $return['error'] = 'true';
- } else {
- $return['msg'] = '';
- if (!empty($email))
- $return['error'] = 'false';
- else
- $return['error'] = 'true';
- }
- echo json_encode($return);
- }
- function admin_saveSmtp() {
- Configure::write('debug', 0);
- $this->autoRender = false;
- $this->settings['smtp']['Username'] = $this->params['form']['smtpUsername'];
- $this->settings['smtp']['Password'] = $this->params['form']['smtpPassword'];
- $this->settings['smtp']['Host'] = $this->params['form']['smtpHost'];
- $this->settings['smtp']['Type'] = $this->params['form']['smtpType'];
- $this->settings['smtp']['Port'] = $this->params['form']['smtpPort'];
- $this->saveSettings();
- $message = array();
- $message['error'] = 'true';
- echo json_encode($message);
- }
- function writeLogHere($userid) {
- //log action here
- $value = 'desc@register';
- $value = 'userid@' . $userid . '::' . $value . '::date@' . date('Y-m-d H:j:s');
- $this->loadModel('Watchlog');
- $this->data['Watchlog']['key'] = "REGISTER";
- $this->data['Watchlog']['value'] = "$value";
- $this->data['Watchlog']['browser'] = $_SERVER['HTTP_USER_AGENT'];
- $this->data['Watchlog']['ip'] = $_SERVER['REMOTE_ADDR'];
- $this->data['Watchlog']['type'] = "REGISTER";
- $this->data['Watchlog']['user_id'] = $userid;
- $this->Watchlog->create();
- $this->Watchlog->save($this->data);
- }
- function admin_forgot() {
- if ($this->RequestHandler->isAjax()) {
- Configure::write('debug', 0);
- $this->autoRender = false;
- $message = array();
- if (!empty($this->params['form']['email'])) {
- $user = $this->User->find('first', array('conditions' => array('User.email' => $this->params['form']['email'], 'User.status' => 1)));
- if (count($user) > 0) {
- $this->set(compact('user'));
- $par = array();
- $par['from'] = 'tksoft@yahoo.com';
- $par['replyTo'] = $user['User']['email'];
- $par['fromName'] = 'Web Admin';
- $par['to'] = $user['User']['email'];
- $par['subject'] = 'TKSOFT - Quęn m?t kh?u';
- $par['template'] = 'admin_forgotpassword';
- $par['data'] = '';
- $message['error'] = $this->sendMail($par);
- $user['User']['password'] = $this->Auth->password('123456');
- $this->User->saveAll($user);
- echo json_encode($message);
- return;
- }
- $message['error'] = "Email c?a b?n ch?a d??c active ho?c không có trong h? th?ng!";
- echo json_encode($message);
- return;
- }
- $message['error'] = "B?n ch?a nh?p ??a ch? Email!";
- echo json_encode($message);
- return;
- }
- }
- }