PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/system/cms/modules/users/controllers/admin.php

https://github.com/shamoons/pyrocms
PHP | 464 lines | 300 code | 50 blank | 114 comment | 29 complexity | 269be333648ea8646f554c0cbd401267 MD5 | raw file
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * Admin controller for the users module
  4. *
  5. * @author Phil Sturgeon - PyroCMS Dev Team
  6. * @package PyroCMS
  7. * @subpackage Users module
  8. * @category Modules
  9. */
  10. class Admin extends Admin_Controller {
  11. /**
  12. * Validation array
  13. * @access private
  14. * @var array
  15. */
  16. private $validation_rules = array(
  17. array(
  18. 'field' => 'first_name',
  19. 'label' => 'lang:user_first_name_label',
  20. 'rules' => 'required|utf8'
  21. ),
  22. array(
  23. 'field' => 'last_name',
  24. 'label' => 'lang:user_last_name_label',
  25. 'rules' => 'utf8'
  26. ),
  27. array(
  28. 'field' => 'email',
  29. 'label' => 'lang:user_email_label',
  30. 'rules' => 'required|valid_email'
  31. ),
  32. array(
  33. 'field' => 'password',
  34. 'label' => 'lang:user_password_label',
  35. 'rules' => 'min_length[6]|max_length[20]'
  36. ),
  37. array(
  38. 'field' => 'confirm_password',
  39. 'label' => 'lang:user_password_confirm_label',
  40. 'rules' => 'matches[password]'
  41. ),
  42. array(
  43. 'field' => 'username',
  44. 'label' => 'lang:user_username',
  45. 'rules' => 'required|alpha_numeric|min_length[3]|max_length[20]'
  46. ),
  47. array(
  48. 'field' => 'display_name',
  49. 'label' => 'lang:user_display_name',
  50. 'rules' => 'min_length[3]|max_length[50]'
  51. ),
  52. array(
  53. 'field' => 'group_id',
  54. 'label' => 'lang:user_group_label',
  55. 'rules' => 'required|callback__group_check'
  56. ),
  57. array(
  58. 'field' => 'active',
  59. 'label' => 'lang:user_active_label',
  60. 'rules' => ''
  61. )
  62. );
  63. /**
  64. * Constructor method
  65. * @access public
  66. * @return void
  67. */
  68. public function __construct()
  69. {
  70. parent::__construct();
  71. // Load the required classes
  72. $this->load->model('users_m');
  73. $this->load->model('groups/group_m');
  74. $this->load->helper('user');
  75. $this->load->library('form_validation');
  76. $this->lang->load('user');
  77. $this->data->groups = $this->group_m->get_all();
  78. $this->data->groups_select = array_for_select($this->data->groups, 'id', 'description');
  79. $this->template->set_partial('shortcuts', 'admin/partials/shortcuts');
  80. }
  81. /**
  82. * List all users
  83. * @access public
  84. * @return void
  85. */
  86. public function index()
  87. {
  88. //base where clause
  89. $base_where = array('active' => 0);
  90. //determine active param
  91. $base_where['active'] = $this->input->post('f_module') ? (int) $this->input->post('f_active') : $base_where['active'];
  92. //determine group param
  93. $base_where = $this->input->post('f_group') ? $base_where + array('group_id' => (int) $this->input->post('f_group')) : $base_where;
  94. //keyphrase param
  95. $base_where = $this->input->post('f_keywords') ? $base_where + array('name' => $this->input->post('f_keywords')) : $base_where;
  96. // Create pagination links
  97. $pagination = create_pagination('admin/users/index', $this->users_m->count_by($base_where));
  98. // Using this data, get the relevant results
  99. $users = $this->users_m
  100. ->order_by('active', 'desc')
  101. ->limit($pagination['limit'])
  102. ->get_many_by($base_where);
  103. //unset the layout if we have an ajax request
  104. $this->input->is_ajax_request() ? $this->template->set_layout(FALSE) : '';
  105. // Render the view
  106. $this->template
  107. ->set('pagination', $pagination)
  108. ->set('users', $users)
  109. ->set_partial('filters', 'admin/partials/filters')
  110. ->append_metadata(js('admin/filter.js'))
  111. ->title($this->module_details['name'])
  112. ->build('admin/index', $this->data);
  113. }
  114. /**
  115. * Method for handling different form actions
  116. * @access public
  117. * @return void
  118. */
  119. public function action()
  120. {
  121. // Determine the type of action
  122. switch ($this->input->post('btnAction'))
  123. {
  124. case 'activate':
  125. $this->activate();
  126. break;
  127. case 'delete':
  128. $this->delete();
  129. break;
  130. default:
  131. redirect('admin/users');
  132. break;
  133. }
  134. }
  135. /**
  136. * Create a new user
  137. *
  138. * @access public
  139. * @return void
  140. */
  141. public function create()
  142. {
  143. // We need a password don't you think?
  144. $this->validation_rules[2]['rules'] .= '|callback__email_check';
  145. $this->validation_rules[3]['rules'] .= '|required';
  146. $this->validation_rules[5]['rules'] .= '|callback__username_check';
  147. // Set the validation rules
  148. $this->form_validation->set_rules($this->validation_rules);
  149. $email = $this->input->post('email');
  150. $password = $this->input->post('password');
  151. $username = $this->input->post('username');
  152. $user_data = array(
  153. 'first_name' => $this->input->post('first_name'),
  154. 'last_name' => $this->input->post('last_name'),
  155. 'display_name' => $this->input->post('display_name'),
  156. 'group_id' => $this->input->post('group_id')
  157. );
  158. if ($this->form_validation->run() !== FALSE)
  159. {
  160. // Hack to activate immediately
  161. if ($this->input->post('active'))
  162. {
  163. $this->config->config['ion_auth']['email_activation'] = FALSE;
  164. }
  165. $group = $this->group_m->get($this->input->post('group_id'));
  166. // Try to register the user
  167. if ($user_id = $this->ion_auth->register($username, $password, $email, $user_data, $group->name))
  168. {
  169. // Set the flashdata message and redirect
  170. $this->session->set_flashdata('success', $this->ion_auth->messages());
  171. redirect('admin/users');
  172. }
  173. // Error
  174. else
  175. {
  176. $this->data->error_string = $this->ion_auth->errors();
  177. }
  178. }
  179. else
  180. {
  181. // Dirty hack that fixes the issue of having to re-add all data upon an error
  182. if ($_POST)
  183. {
  184. $member = (object) $_POST;
  185. }
  186. }
  187. // Loop through each validation rule
  188. foreach ($this->validation_rules as $rule)
  189. {
  190. $member->{$rule['field']} = set_value($rule['field']);
  191. }
  192. // Render the view
  193. $this->data->member = & $member;
  194. $this->template
  195. ->title($this->module_details['name'], lang('user_add_title'))
  196. ->build('admin/form', $this->data);
  197. }
  198. /**
  199. * Edit an existing user
  200. *
  201. * @access public
  202. * @param int $id The ID of the user to edit
  203. * @return void
  204. */
  205. public function edit($id = 0)
  206. {
  207. // confirm_password is required in case the user enters a new password
  208. if ($this->input->post('password') && $this->input->post('password') != '')
  209. {
  210. $this->validation_rules[3]['rules'] .= '|required';
  211. $this->validation_rules[3]['rules'] .= '|matches[password]';
  212. }
  213. // Get the user's data
  214. $member = $this->ion_auth->get_user($id);
  215. // Got user?
  216. if (!$member)
  217. {
  218. $this->session->set_flashdata('error', $this->lang->line('user_edit_user_not_found_error'));
  219. redirect('admin/users');
  220. }
  221. // Check to see if we are changing usernames
  222. if ($member->username != $this->input->post('username'))
  223. {
  224. $this->validation_rules[6]['rules'] .= '|callback__username_check';
  225. }
  226. // Check to see if we are changing emails
  227. if ($member->email != $this->input->post('email'))
  228. {
  229. $this->validation_rules[5]['rules'] .= '|callback__email_check';
  230. }
  231. // Run the validation
  232. $this->form_validation->set_rules($this->validation_rules);
  233. if ($this->form_validation->run() === TRUE)
  234. {
  235. // Get the POST data
  236. $update_data['first_name'] = $this->input->post('first_name');
  237. $update_data['last_name'] = $this->input->post('last_name');
  238. $update_data['email'] = $this->input->post('email');
  239. $update_data['active'] = $this->input->post('active');
  240. $update_data['username'] = $this->input->post('username');
  241. $update_data['display_name'] = $this->input->post('display_name');
  242. $update_data['group_id'] = $this->input->post('group_id');
  243. // Password provided, hash it for storage
  244. if ($this->input->post('password') && $this->input->post('confirm_password'))
  245. {
  246. $update_data['password'] = $this->input->post('password');
  247. }
  248. if ($this->ion_auth->update_user($id, $update_data))
  249. {
  250. $this->session->set_flashdata('success', $this->ion_auth->messages());
  251. }
  252. else
  253. {
  254. $this->session->set_flashdata('error', $this->ion_auth->errors());
  255. }
  256. // Redirect the user
  257. redirect('admin/users');
  258. }
  259. else
  260. {
  261. // Dirty hack that fixes the issue of having to re-add all data upon an error
  262. if ($_POST)
  263. {
  264. $member = (object) $_POST;
  265. $member->full_name = $member->first_name . ' ' . $member->last_name;
  266. }
  267. }
  268. // Loop through each validation rule
  269. foreach ($this->validation_rules as $rule)
  270. {
  271. if ($this->input->post($rule['field']) !== FALSE)
  272. {
  273. $member->{$rule['field']} = set_value($ractivaule['field']);
  274. }
  275. }
  276. // Render the view
  277. $this->data->member = & $member;
  278. $this->template
  279. ->title($this->module_details['name'], sprintf(lang('user_edit_title'), $member->full_name))
  280. ->build('admin/form', $this->data);
  281. }
  282. /**
  283. * Show a user preview
  284. * @access public
  285. * @param int $id The ID of the user
  286. * @return void
  287. */
  288. public function preview($id = 0)
  289. {
  290. $data->user = $this->ion_auth->get_user($id);
  291. $this->template
  292. ->set_layout('modal', 'admin')
  293. ->build('admin/preview', $data);
  294. }
  295. /**
  296. * Activate a user
  297. * @access public
  298. * @param int $id The ID of the user to activate
  299. * @return void
  300. */
  301. public function activate()
  302. {
  303. $ids = $this->input->post('action_to');
  304. // Activate multiple
  305. if (empty($ids))
  306. {
  307. $this->session->set_flashdata('error', $this->lang->line('user_activate_error'));
  308. redirect('admin/users');
  309. }
  310. $activated = 0;
  311. $to_activate = 0;
  312. foreach ($ids as $id)
  313. {
  314. if ($this->ion_auth->activate($id))
  315. {
  316. $activated++;
  317. }
  318. $to_activate++;
  319. }
  320. $this->session->set_flashdata('success', sprintf($this->lang->line('user_activate_success'), $activated, $to_activate));
  321. redirect('admin/users');
  322. }
  323. /**
  324. * Delete an existing user
  325. *
  326. * @access public
  327. * @param int $id The ID of the user to delete
  328. * @return void
  329. */
  330. public function delete($id = 0)
  331. {
  332. $ids = ($id > 0) ? array($id) : $this->input->post('action_to');
  333. if (!empty($ids))
  334. {
  335. $deleted = 0;
  336. $to_delete = 0;
  337. foreach ($ids as $id)
  338. {
  339. // Make sure the admin is not trying to delete themself
  340. if ($this->ion_auth->get_user()->id == $id)
  341. {
  342. $this->session->set_flashdata('notice', $this->lang->line('user_delete_self_error'));
  343. continue;
  344. }
  345. if ($this->ion_auth->delete_user($id))
  346. {
  347. $deleted++;
  348. }
  349. $to_delete++;
  350. }
  351. if ($to_delete > 0)
  352. {
  353. $this->session->set_flashdata('success', sprintf($this->lang->line('user_mass_delete_success'), $deleted, $to_delete));
  354. }
  355. }
  356. // The array of id's to delete is empty
  357. else
  358. $this->session->set_flashdata('error', $this->lang->line('user_mass_delete_error'));
  359. // Redirect
  360. redirect('admin/users');
  361. }
  362. /**
  363. * Username check
  364. *
  365. * @return bool
  366. * @author Ben Edmunds
  367. * */
  368. public function _username_check($username)
  369. {
  370. if ($this->ion_auth->username_check($username))
  371. {
  372. $this->form_validation->set_message('_username_check', $this->lang->line('user_error_username'));
  373. return FALSE;
  374. }
  375. else
  376. {
  377. return TRUE;
  378. }
  379. }
  380. /**
  381. * Email check
  382. *
  383. * @return bool
  384. * @author Ben Edmunds
  385. * */
  386. public function _email_check($email)
  387. {
  388. if ($this->ion_auth->email_check($email))
  389. {
  390. $this->form_validation->set_message('_email_check', $this->lang->line('user_error_email'));
  391. return FALSE;
  392. }
  393. else
  394. {
  395. return TRUE;
  396. }
  397. }
  398. /**
  399. * Check that a proper group has been selected
  400. *
  401. * @return bool
  402. * @author Stephen Cozart
  403. */
  404. public function _group_check($group)
  405. {
  406. if ( ! $this->group_m->get($group))
  407. {
  408. $this->form_validation->set_message('_group_check', $this->lang->line('regex_match'));
  409. return FALSE;
  410. }
  411. return TRUE;
  412. }
  413. }
  414. /* End of file admin.php */