PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/application/controllers/admin/user.php

https://gitlab.com/fredec/ionizecms-1.0.8.x
PHP | 543 lines | 300 code | 123 blank | 120 comment | 31 complexity | 38b41dd61cf9aca6ae2bba858f92f2ee MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Ionize
  4. * User Controller
  5. *
  6. * @package Ionize
  7. * @author Ionize Dev Team
  8. * @license http://doc.ionizecms.com/en/basic-infos/license-agreement
  9. * @link http://ionizecms.com
  10. * @since Version 0.9.0
  11. */
  12. class User extends My_Admin
  13. {
  14. /** @var Role_model */
  15. public $current_role = NULL;
  16. /** @var User_model */
  17. public $user_model;
  18. /** @var Role_model */
  19. public $role_model;
  20. /**
  21. * Constructor
  22. *
  23. */
  24. function __construct()
  25. {
  26. parent::__construct();
  27. // Models
  28. $this->load->model(
  29. array(
  30. 'user_model',
  31. 'role_model'
  32. ), '', TRUE);
  33. // Current connected user level
  34. $this->current_role = User()->get_role();
  35. }
  36. // ------------------------------------------------------------------------
  37. /**
  38. * Default
  39. *
  40. */
  41. function index($page=1, $nb=30)
  42. {
  43. $this->template['users_count_all'] = $this->user_model->count_all();
  44. $roles = $this->role_model->get_list();
  45. $this->template['roles'] = array_filter($roles, array($this, '_filter_roles'));
  46. $this->output('user/index');
  47. }
  48. // ------------------------------------------------------------------------
  49. function get_list($page=1)
  50. {
  51. // Nb and Minimum
  52. $nb = ($this->input->post('nb')) ? $this->input->post('nb') : '50';
  53. if ($nb < 25) $nb = 25;
  54. $page = $page - 1;
  55. $offset = $page * $nb;
  56. // Send the filter elements to the view
  57. $this->template['filter'] = array();
  58. // Like conditions
  59. $like = array();
  60. foreach(array('username', 'screen_name', 'email') as $key)
  61. {
  62. if( $this->input->post($key))
  63. {
  64. $like[$key] = $this->input->post($key);
  65. $this->template['filter'][$key] = $like[$key];
  66. }
  67. }
  68. // Where
  69. $where = array();
  70. if( $this->input->post('id_role'))
  71. {
  72. $this->template['filter']['id_role'] = $this->input->post('id_role');
  73. $where['user.id_role'] = $this->input->post('id_role');
  74. }
  75. // Order by last registered
  76. if( $this->input->post('registered'))
  77. {
  78. $where['order_by'] = 'join_date DESC';
  79. }
  80. $where = array_merge(
  81. $where,
  82. array(
  83. 'limit' => $nb,
  84. 'offset' => $offset,
  85. 'like' => $like,
  86. 'role_level <= ' => $this->current_role['role_level']
  87. )
  88. );
  89. // Get user list filtered on levels <= current_user level
  90. $this->template['users'] = $this->user_model->get_list_with_role($where);
  91. // Pagination
  92. $this->template['current_page'] = $page + 1;
  93. $this->template['nb'] = $nb;
  94. $this->template['users_count'] = $this->user_model->count($where);
  95. $this->template['users_pages'] = ceil($this->template['users_count'] / $nb);
  96. // XHR answer
  97. $this->output('user/list');
  98. }
  99. // ------------------------------------------------------------------------
  100. public function get_pagination_list()
  101. {
  102. $page = $this->input->post('page');
  103. $filter = $this->input->post('filter');
  104. $post_where = $this->input->post('where');
  105. $nb_by_page = 50;
  106. if ( ! $page) $page = 1;
  107. // Filter by role : Do not show upper user to lower role !
  108. $where = array(
  109. 'role_level <= ' => $this->current_role['role_level']
  110. );
  111. if ($post_where)
  112. $where = array_merge($where, $post_where);
  113. $data = $this->user_model->get_pagination_list($page, $filter, $nb_by_page, $where);
  114. $result = array(
  115. 'items' => $data['items'],
  116. 'nb' => $data['nb'],
  117. 'nb_by_page' => $nb_by_page,
  118. 'page' => $page,
  119. 'filter' => $filter,
  120. );
  121. $this->xhr_output($result);
  122. }
  123. // ------------------------------------------------------------------------
  124. /**
  125. * Creation Form
  126. *
  127. */
  128. public function create()
  129. {
  130. $this->template['user'] = $this->user_model->feed_blank_template();
  131. // Get roles list filtered on level <= current_user level
  132. $roles = $this->role_model->get_list();
  133. $this->template['roles'] = array_filter($roles, array($this, '_filter_roles'));
  134. $this->output('user/user');
  135. }
  136. // ------------------------------------------------------------------------
  137. /**
  138. * Edit one user
  139. *
  140. */
  141. public function edit()
  142. {
  143. $id_user = $this->input->post('id_user');
  144. $db_user = $this->user_model->get_user(array('id_user' => $id_user));
  145. $this->template['user'] = $db_user;
  146. // Panel from which the user is edited
  147. $this->template['from'] = $this->input->post('from');
  148. if ($this->current_role['role_level'] >= $db_user['role_level'])
  149. {
  150. // Get roles, filtered on level <= $current_role level
  151. $roles = $this->role_model->get_list();
  152. $this->template['roles'] = array_filter($roles, array($this, '_filter_roles'));
  153. $this->output('user/user');
  154. }
  155. else
  156. {
  157. $this->output('user/user_no_edit');
  158. }
  159. }
  160. // ------------------------------------------------------------------------
  161. /**
  162. * Save
  163. *
  164. */
  165. public function save()
  166. {
  167. if ($this->input->post('email'))
  168. {
  169. $id_user = $this->input->post('id_user');
  170. $post = $this->input->post();
  171. $post['salt'] = User()->get_salt();
  172. if ( empty($id_user)) $post['join_date'] = date('Y-m-d H:i:s');
  173. // Passwords must match
  174. if (($this->input->post('password') != '') &&
  175. ($this->input->post('password') === $this->input->post('password2')))
  176. {
  177. $post['password'] = User()->encrypt($this->input->post('password'), $post);
  178. }
  179. else
  180. {
  181. unset($post['password'], $post['password2']);
  182. }
  183. // New user?
  184. if ($id_user == false)
  185. {
  186. $post['id_user'] = null;
  187. }
  188. // Save
  189. $new_id_user = $this->user_model->save($post);
  190. // Send message to user if needed
  191. $message = $this->input->post('message');
  192. if ( ! is_null($new_id_user) && $message != '')
  193. {
  194. // Update
  195. if ($id_user)
  196. {
  197. $subject = Settings::get('site_title') . ' : ' .lang('ionize_subject_your_account_has_been_updated');
  198. $message_intro = lang('ionize_message_your_account_has_been_created');
  199. }
  200. else
  201. {
  202. $subject = Settings::get('site_title') . ' : ' .lang('ionize_subject_your_account_has_been_created');
  203. $message_intro = lang('ionize_message_your_account_has_been_updated');
  204. }
  205. // Group
  206. $user = $this->user_model->get_user(array('id_user' => $new_id_user));
  207. $email_data = array(
  208. 'message_intro' => $message_intro,
  209. 'message' => $message,
  210. 'role' => $user['role_name'],
  211. 'firstname' => $user['firstname'],
  212. 'lastname' => $user['lastname'],
  213. 'email' => $user['email'],
  214. 'username' => $post['firstname'] . ' ' . $post['lastname'],
  215. );
  216. $this->send_email(
  217. Settings::get('site_email'),
  218. $post['email'],
  219. $subject,
  220. $email_data,
  221. 'mail/system/to_user'
  222. );
  223. }
  224. // Reload user list
  225. /* if ( ! empty($post['from']) && $post['from'] === 'dashboard')
  226. {
  227. $this->_reload_dashboard();
  228. }
  229. else
  230. {
  231. $this->_reload_user_list();
  232. }*/
  233. // Success message
  234. $this->success(lang('ionize_message_user_saved'), array('id_user' => $new_id_user));
  235. }
  236. }
  237. // ------------------------------------------------------------------------
  238. /**
  239. * Delete
  240. *
  241. */
  242. public function delete()
  243. {
  244. $id_user = $this->input->post('id_user');
  245. $current_user_id = User()->getId();
  246. if($id_user != $current_user_id)
  247. {
  248. $affected_rows = $this->user_model->delete($id_user);
  249. if ($affected_rows > 0)
  250. {
  251. // Update role list panel
  252. $this->_reload_user_list();
  253. $this->success(lang('ionize_message_user_deleted'));
  254. }
  255. else
  256. {
  257. $this->error(lang('ionize_message_user_not_deleted'));
  258. }
  259. }
  260. else
  261. {
  262. $this->error(lang('ionize_message_user_cannot_delete_yourself'));
  263. }
  264. }
  265. // ------------------------------------------------------------------------
  266. /**
  267. * Return the current user or NULL if not logged in.
  268. * Used by Ionize.User() JS object to get the current user
  269. *
  270. */
  271. function get_current_user()
  272. {
  273. $user = User()->get_user();
  274. if ( $user !== FALSE)
  275. {
  276. $user['role'] = User()->get_role();
  277. // Removes the password, even it is encoded
  278. if (isset($user['password'])) unset($user['password']);
  279. if (isset($user['salt'])) unset($user['salt']);
  280. // Returns the current user as JSON object
  281. if ($this->is_xhr())
  282. {
  283. echo json_encode($user);
  284. exit();
  285. }
  286. else
  287. {
  288. return $user;
  289. }
  290. }
  291. return NULL;
  292. }
  293. // ------------------------------------------------------------------------
  294. public function get_rules()
  295. {
  296. $rules = Authority::get_rules_array();
  297. if ($this->is_xhr())
  298. {
  299. $data = array(
  300. 'rules' => $rules
  301. );
  302. $this->xhr_output($data);
  303. }
  304. }
  305. // ------------------------------------------------------------------------
  306. public function send_email($from, $to, $subject, $data, $view)
  307. {
  308. if ( !empty($from) && !empty($to))
  309. {
  310. $this->load->library('email');
  311. $this->email->subject($subject);
  312. $this->email->from($from, Settings::get("site_title"));
  313. $this->email->to($to);
  314. // Loads the view
  315. $view_content = $this->load->view($view, $data, true);
  316. $this->email->message($view_content);
  317. // Send silently
  318. $result = @$this->email->send();
  319. return $result;
  320. }
  321. else
  322. {
  323. log_message('error', 'Error : Backend send_mail : Ether the website email or the receiver email isn\'t set');
  324. return FALSE;
  325. }
  326. }
  327. // ------------------------------------------------------------------------
  328. /**
  329. * Must be called by XHR
  330. * Called by User Edition form Validation
  331. *
  332. * Returns 1 if true, 0 if false
  333. *
  334. */
  335. function check_email_exists()
  336. {
  337. $id_user = $this->input->post('id_user');
  338. $email = $this->input->post('email');
  339. $exists = $this->user_model->user_with_same_email_exists($email, $id_user);
  340. $this->xhr_output($exists);
  341. }
  342. // ------------------------------------------------------------------------
  343. /**
  344. * Must be called by XHR
  345. * Called by User Edition form Validation
  346. *
  347. * Returns 1 if true, 0 if false
  348. *
  349. */
  350. function check_username_exists()
  351. {
  352. $id_user = $this->input->post('id_user');
  353. $username = $this->input->post('username');
  354. $exists = $this->user_model->check_username_exists($username, $id_user);
  355. $this->xhr_output($exists);
  356. }
  357. // ------------------------------------------------------------------------
  358. /**
  359. * Search users by email
  360. * XHR
  361. *
  362. * Used by AutoCompleter
  363. *
  364. */
  365. function search_email()
  366. {
  367. $min = 2;
  368. $max = 50;
  369. $limit = 7;
  370. $search = $this->input->post('search');
  371. $results = array();
  372. // quick validation
  373. if(strlen($search) >= $min && strlen($search) <= $max)
  374. {
  375. $results = $this->user_model->simple_search($search, 'email', $limit);
  376. $list = '';
  377. foreach ($results as $result)
  378. {
  379. $list .= "<li data-id=\"$result[id_user]\" data-firstname=\"$result[firstname]\" data-lastname=\"$result[lastname]\"><span>$result[email]</span><a class=\"link \" ></a></li>";
  380. }
  381. if ( ! empty($list))
  382. echo $list;
  383. die();
  384. }
  385. }
  386. // ------------------------------------------------------------------------
  387. /**
  388. * Roles filter callback function
  389. *
  390. */
  391. public function _filter_roles($row)
  392. {
  393. return ($row['role_level'] <= $this->current_role['role_level']);
  394. }
  395. // ------------------------------------------------------------------------
  396. private function _reload_user_list()
  397. {
  398. // Save options : as callback
  399. $this->callback[] = array(
  400. 'fn' => 'ION.HTML',
  401. 'args' => array(
  402. 'user/get_list',
  403. '',
  404. array(
  405. 'update'=> 'userList'
  406. )
  407. )
  408. );
  409. }
  410. // ------------------------------------------------------------------------
  411. private function _reload_dashboard()
  412. {
  413. $this->update = array(
  414. array(
  415. 'element' => 'mainPanel',
  416. 'url' => admin_url() . 'dashboard'
  417. )
  418. );
  419. }
  420. }