PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/chimbien/app/plugins/users/controllers/users_controller.php

http://goldcat.googlecode.com/
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
  1. <?php
  2. class UsersController extends UsersAppController {
  3. public $type = 'USER';
  4. /**
  5. * Controller Name
  6. * @access public
  7. * @var string
  8. */
  9. public $name = 'Users';
  10. /**
  11. * Models
  12. * @access public
  13. * @var array
  14. */
  15. public $uses = array('User', 'AccessLevel', 'Province');
  16. /**
  17. * Components
  18. * @access public
  19. * @var array
  20. */
  21. public $components = array('Email');
  22. /**
  23. * Pagination
  24. * @access public
  25. * @var array
  26. */
  27. public $paginate = array(
  28. 'User' => array(
  29. 'order' => 'User.username ASC',
  30. 'limit' => 25,
  31. 'contain' => false
  32. )
  33. );
  34. var $helpers = array('Javascript', 'Ckeditor', 'Status');
  35. function beforeFilter() {
  36. parent::beforeFilter();
  37. }
  38. function admin_profile(){
  39. if (!empty($this->data)) {
  40. $this->data['User']['id'] = $this->Auth->user('id');
  41. if ($this->User->save($this->data)) {
  42. $this->Session->setFlash(__('The profile has been saved', true),'success');
  43. } else {
  44. $this->Session->setFlash(__('The Profile could not be saved. Please, try again.', true),'success');
  45. }
  46. }
  47. if (empty($this->data)) {
  48. $this->data = $this->User->read(null, $id);
  49. }
  50. }
  51. function admin_index() {
  52. //filter
  53. $keyword = trim($this->data['Search']['keyword']);
  54. //$checkout = $this->data['Search']['checkout'];
  55. $conds = array();
  56. //if(!empty($checkout)){
  57. // $conds['Room.checkout']=$checkout-1;
  58. //}
  59. if (!empty($keyword)) {
  60. $conds['OR'] = array(
  61. 'lower(CONVERT(User.username USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%',
  62. 'lower(CONVERT(User.name USING utf8)) LIKE' => '%' . mb_strtolower($keyword, 'utf-8') . '%'
  63. );
  64. }
  65. //End filter
  66. //$this->User->Behaviors->attach('Containable');
  67. $users = $this->User->find('all', array(
  68. 'fields'=>array('User.id','User.name','User.username','User.status','User.isAdmin'),
  69. 'conditions' => $conds,
  70. 'order' => 'User.created'
  71. ));
  72. $this->set(compact('users'));
  73. }
  74. function admin_permission($id = null){
  75. $this->layout = 'ajax';
  76. if(!empty($this->params['form'])){
  77. Configure::write('debug', 0);
  78. $this->autoRender = false;
  79. $message = array();
  80. $this->loadModel('Permission');
  81. $this->Permission->deleteAll(array('Permission.user_id'=>$id));
  82. foreach($this->params['form'] as $controller){
  83. $per['Permission']['user_id'] = $id;
  84. $per['Permission']['controller'] = $controller;
  85. $this->Permission->create();
  86. $this->Permission->save($per);
  87. }
  88. $message['error'] = true;
  89. return json_encode($message);
  90. }
  91. $listcons = App::objects('controller');
  92. $listcons[count($listcons)] = 'users';
  93. $this->User->Behaviors->attach('Containable');
  94. $user = $this->User->find('first', array(
  95. 'fields'=>array('User.id','User.name'),
  96. 'conditions' => array('User.id'=>$id)
  97. ));
  98. $this->set(compact('listcons','user'));
  99. }
  100. function getPermission($id = null){
  101. $this->loadModel('Permission');
  102. $pers = $this->Permission->find('list',array('fields'=>array('Permission.controller'),'conditions'=>array('Permission.user_id'=>$id)));
  103. return $pers;
  104. }
  105. function admin_add() {
  106. if (!empty($this->data)) {
  107. $this->User->create();
  108. $this->data['User']['password'] = $this->Auth->password('123456');
  109. $this->data['User']['activation_code'] = md5(uniqid());
  110. if ($this->User->save($this->data)) {
  111. $this->Session->setFlash(__('The users has been saved', true));
  112. $this->redirect(array('action' => 'index'));
  113. } else {
  114. $this->Session->setFlash(__('The users could not be saved. Please, try again.', true));
  115. }
  116. }
  117. $provinces = $this->Province->find('list');
  118. $accesslevel = $this->AccessLevel->find('list');
  119. $this->set('provinces', $provinces);
  120. $this->set('accesslevel', $accesslevel);
  121. }
  122. function admin_edit($id = null) {
  123. if (!$id && empty($this->data)) {
  124. $this->Session->setFlash(__('Invalid user', true));
  125. $this->redirect(array('action' => 'index'));
  126. }
  127. //print_r($this->data);
  128. if (!empty($this->data)) {
  129. $this->User->id = $id;
  130. if ($this->User->save($this->data)) {
  131. $this->Session->setFlash(__('The user has been saved', true));
  132. $this->redirect(array('action' => 'index'));
  133. } else {
  134. $this->Session->setFlash(__('The user could not be saved. Please, try again.', true));
  135. }
  136. }
  137. if (empty($this->data)) {
  138. $this->data = $this->User->read(null, $id);
  139. }
  140. $provinces = $this->Province->find('list');
  141. $this->set('provinces', $provinces);
  142. }
  143. function admin_changeStatusAll($ids = null, $status=null) {
  144. if (!empty($ids)) {
  145. $id = explode('-', $ids);
  146. $this->User->updateAll(array('User.status' => $status,'User.modified'=>'"'.date('Y-m-d H:i:s').'"'), array('User.id' => $id));
  147. }
  148. $this->Session->setFlash(sprintf(__('Chuy?n ??i tr?ng thái thŕnh công.', true)), 'admin/flashsuccess', '', 'success');
  149. $this->redirect(array('action' => 'index'));
  150. }
  151. function admin_changestatus() {
  152. Configure::write('debug', 0);
  153. $this->autoRender = false;
  154. $id = $this->params['form']['id'];
  155. $status = $this->params['form']['status'];
  156. $this->User->updateAll(array('User.status' => $status), array('User.id' => $id));
  157. $return = array();
  158. $return['id'] = $id;
  159. $return['newstatus'] = ($status == 0) ? 1 : 0;
  160. echo json_encode($return);
  161. }
  162. function getProvinceById($id=null) {
  163. return $this->Province->findById($id);
  164. }
  165. function admin_dashboard() {
  166. $this->set('title_for_layout','B?ng ?i?u khi?n');
  167. }
  168. function userLog(){
  169. $this->loadModel('Log');
  170. $log['Log']['user_id'] = $this->Auth->user('id');
  171. $log['Log']['login'] = date('Y-m-d H:i:s');
  172. $log['Log']['ip'] = $_SERVER['REMOTE_ADDR'];
  173. $log['Log']['browser'] = $_SERVER['HTTP_USER_AGENT'];
  174. $this->Log->create();
  175. $this->Log->save($log);
  176. $this->Session->write('log',$this->Log->id);
  177. $this->Session->write('Auth.User.ip',$log['Log']['ip']);
  178. }
  179. function admin_login() {
  180. $this->set('title_for_layout','Admin Login');
  181. $this->layout = "admin_login";
  182. if ($this->Auth->user('id') && !empty($this->data)) {
  183. $this->userLog();
  184. $this->redirect('/admin');
  185. } else {
  186. empty($this->data) ? $this->set('deny', '') : $this->set('deny', 'Access Deny');
  187. }
  188. }
  189. function admin_logout() {
  190. //$this->Session->setFlash(__('Log out successful.', true));
  191. $this->loadModel('Log');
  192. $this->Log->updateAll(array('Log.logout'=>'"'.date('Y-m-d H:i:s').'"'),array('Log.id'=>$this->Session->read('log')));
  193. $this->Session->delete('User');
  194. $this->redirect($this->Auth->logout());
  195. exit();
  196. }
  197. function admin_delete($id = null) {
  198. if (!$id) {
  199. $this->Session->setFlash(__('Invalid id for user', true));
  200. $this->redirect(array('plugin' => 'users', 'controller' => 'users', 'action' => 'index'));
  201. }
  202. if ($this->User->delete($id)) {
  203. $this->Session->setFlash(__('User deleted', true));
  204. $this->redirect(array('plugin' => 'users', 'controller' => 'users', 'action' => 'index'));
  205. }
  206. $this->Session->setFlash(__('User was not deleted', true));
  207. $this->redirect(array('plugin' => 'users', 'controller' => 'users', 'action' => 'index'));
  208. }
  209. function admin_deleteAll() {
  210. Configure::write('debug', 0);
  211. $this->autoRender = false;
  212. $id = $this->params['form']['id'];
  213. $ids = explode('-', $id);
  214. foreach ($ids as $id) {
  215. $this->User->delete($id);
  216. }
  217. if (count($ids) > 1) {
  218. $this->Session->setFlash(__('Users deleted', true));
  219. } else {
  220. $this->Session->setFlash(__('User deleted', true));
  221. }
  222. echo 'true';
  223. }
  224. function admin_changepassword() {
  225. if ($this->Auth->user('id') && !empty($this->data)) {
  226. $curpass = $this->Auth->password($this->data['User']['current_password']);
  227. $confirmpass = $this->Auth->password($this->data['User']['password_confirm']);
  228. $user = $this->User->find('first', array('conditions' => array('User.id'=>$this->Auth->user('id'),'User.username' => $this->data['User']['username'],'User.password' => $curpass)));
  229. if (count($user) > 0) {
  230. $users['User']['id']=$this->Auth->user('id');
  231. $users['User']['username'] = $this->data['User']['username'];
  232. $users['User']['password'] = $this->data['User']['password'];
  233. if ($this->User->save($users)) {
  234. $this->Session->setFlash(__('Password has been change', true),'admin/flashsuccess','','success');
  235. }
  236. }else{
  237. $this->Session->setFlash(__('Error Current Password', true),'admin/flashsuccess','','error');
  238. }
  239. }
  240. }
  241. function checkExistMail() {
  242. Configure::write('debug', 0);
  243. $this->autoRender = false;
  244. $return = array();
  245. $email = $this->params['form']['email'];
  246. $user_email = $this->User->findByEmail($email);
  247. if ($user_email != false) {
  248. $return['msg'] = 'Email nŕy ?ă t?n t?i trong h? th?ng, vui lňng ch??n email khác.';
  249. $return['error'] = 'true';
  250. } else {
  251. $return['msg'] = '';
  252. if (!empty($email))
  253. $return['error'] = 'false';
  254. else
  255. $return['error'] = 'true';
  256. }
  257. echo json_encode($return);
  258. }
  259. function admin_saveSmtp() {
  260. Configure::write('debug', 0);
  261. $this->autoRender = false;
  262. $this->settings['smtp']['Username'] = $this->params['form']['smtpUsername'];
  263. $this->settings['smtp']['Password'] = $this->params['form']['smtpPassword'];
  264. $this->settings['smtp']['Host'] = $this->params['form']['smtpHost'];
  265. $this->settings['smtp']['Type'] = $this->params['form']['smtpType'];
  266. $this->settings['smtp']['Port'] = $this->params['form']['smtpPort'];
  267. $this->saveSettings();
  268. $message = array();
  269. $message['error'] = 'true';
  270. echo json_encode($message);
  271. }
  272. function writeLogHere($userid) {
  273. //log action here
  274. $value = 'desc@register';
  275. $value = 'userid@' . $userid . '::' . $value . '::date@' . date('Y-m-d H:j:s');
  276. $this->loadModel('Watchlog');
  277. $this->data['Watchlog']['key'] = "REGISTER";
  278. $this->data['Watchlog']['value'] = "$value";
  279. $this->data['Watchlog']['browser'] = $_SERVER['HTTP_USER_AGENT'];
  280. $this->data['Watchlog']['ip'] = $_SERVER['REMOTE_ADDR'];
  281. $this->data['Watchlog']['type'] = "REGISTER";
  282. $this->data['Watchlog']['user_id'] = $userid;
  283. $this->Watchlog->create();
  284. $this->Watchlog->save($this->data);
  285. }
  286. function admin_forgot() {
  287. if ($this->RequestHandler->isAjax()) {
  288. Configure::write('debug', 0);
  289. $this->autoRender = false;
  290. $message = array();
  291. if (!empty($this->params['form']['email'])) {
  292. $user = $this->User->find('first', array('conditions' => array('User.email' => $this->params['form']['email'], 'User.status' => 1)));
  293. if (count($user) > 0) {
  294. $this->set(compact('user'));
  295. $par = array();
  296. $par['from'] = 'tksoft@yahoo.com';
  297. $par['replyTo'] = $user['User']['email'];
  298. $par['fromName'] = 'Web Admin';
  299. $par['to'] = $user['User']['email'];
  300. $par['subject'] = 'TKSOFT - Quęn m?t kh?u';
  301. $par['template'] = 'admin_forgotpassword';
  302. $par['data'] = '';
  303. $message['error'] = $this->sendMail($par);
  304. $user['User']['password'] = $this->Auth->password('123456');
  305. $this->User->saveAll($user);
  306. echo json_encode($message);
  307. return;
  308. }
  309. $message['error'] = "Email c?a b?n ch?a d??c active ho?c không có trong h? th?ng!";
  310. echo json_encode($message);
  311. return;
  312. }
  313. $message['error'] = "B?n ch?a nh?p ??a ch? Email!";
  314. echo json_encode($message);
  315. return;
  316. }
  317. }
  318. }