PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/controller/users.php

https://code.google.com/p/php-blackops-rcon/
PHP | 399 lines | 213 code | 65 blank | 121 comment | 41 complexity | 8772182ae1e97f7dc6eacedb4097e6c3 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. /**
  3. * User controller
  4. *
  5. * Copyright (c) 2010, EpicLegion
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  15. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
  17. * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  18. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  19. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  20. * POSSIBILITY OF SUCH DAMAGE.
  21. *
  22. * @author EpicLegion
  23. * @package rcon
  24. * @subpackage controller
  25. * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  26. */
  27. class Controller_Users extends Controller_Main {
  28. /**
  29. * Delete user
  30. *
  31. * @param string $id
  32. * @throws Kohana_Exception
  33. */
  34. public function action_delete($id)
  35. {
  36. // Validate ID
  37. if(!ctype_digit($id))
  38. {
  39. throw new Kohana_Exception('Invalid parameter');
  40. }
  41. // Get user
  42. $id = ORM::factory('user', (int) $id);
  43. // Validate
  44. if(!$id->loaded())
  45. {
  46. throw new Kohana_Exception('User not found');
  47. }
  48. // Selfkill?
  49. if($id->id == $this->user->id)
  50. {
  51. $this->notice(__('You cannot delete yourself'));
  52. $this->request->redirect('users');
  53. }
  54. // Log this action
  55. $this->log_action(__('Deleted user: :user', array(':user' => $id->username)));
  56. // Delete
  57. $id->delete();
  58. // Removed
  59. $this->notice(__('User has been successfully removed'));
  60. // Redirect
  61. $this->request->redirect('users');
  62. }
  63. /**
  64. * Edit user
  65. *
  66. * @param string $id
  67. * @throws Kohana_Exception
  68. */
  69. public function action_edit($id)
  70. {
  71. // Validate ID
  72. if(!ctype_digit($id))
  73. {
  74. throw new Kohana_Exception('Invalid parameter');
  75. }
  76. // Get user
  77. $id = ORM::factory('user', (int) $id);
  78. // Validate
  79. if(!$id->loaded())
  80. {
  81. throw new Kohana_Exception('User not found');
  82. }
  83. // Get roles
  84. $log_role = ORM::factory('role', array('name' => 'logs'));
  85. $users_role = ORM::factory('role', array('name' => 'users'));
  86. $servers_role = ORM::factory('role', array('name' => 'servers'));
  87. // Form
  88. if(!empty($_POST))
  89. {
  90. // Change password?
  91. if(isset($_POST['password']) AND !empty($_POST['password']))
  92. {
  93. $id->password = $_POST['password'];
  94. }
  95. // Add/remove log management permission
  96. if(!$id->has('roles', $log_role) AND isset($_POST['can_log']) AND $_POST['can_log'] == '1')
  97. {
  98. $id->add('roles', $log_role);
  99. }
  100. elseif($id->has('roles', $log_role) AND (!isset($_POST['can_log']) OR $_POST['can_log'] != '1'))
  101. {
  102. $id->remove('roles', $log_role);
  103. }
  104. // User management
  105. if(!$id->has('roles', $users_role) AND isset($_POST['can_users']) AND $_POST['can_users'] == '1')
  106. {
  107. $id->add('roles', $users_role);
  108. }
  109. elseif($id->has('roles', $users_role) AND (!isset($_POST['can_users']) OR $_POST['can_users'] != '1'))
  110. {
  111. $id->remove('roles', $users_role);
  112. }
  113. // Server management
  114. if(!$id->has('roles', $servers_role) AND isset($_POST['can_servers']) AND $_POST['can_servers'] == '1')
  115. {
  116. $id->add('roles', $servers_role);
  117. }
  118. elseif($id->has('roles', $servers_role) AND (!isset($_POST['can_servers']) OR $_POST['can_servers'] != '1'))
  119. {
  120. $id->remove('roles', $servers_role);
  121. }
  122. // Log this action
  123. $this->log_action(__('Updated user account: :user', array(':user' => $id->username)));
  124. // Save
  125. $id->save();
  126. // Done
  127. $this->notice(__('User has been successfully updated'));
  128. // Redirect
  129. $this->request->redirect('users');
  130. }
  131. // Title
  132. $this->title = __('Edit user account');
  133. // View
  134. $this->view = new View('users/edit');
  135. $this->view->user = $id;
  136. // Retrieve current user permissions
  137. $this->view->can_log = $id->has('roles', $log_role);
  138. $this->view->can_users = $id->has('roles', $users_role);
  139. $this->view->can_servers = $id->has('roles', $servers_role);
  140. }
  141. /**
  142. * View users
  143. */
  144. public function action_index()
  145. {
  146. // Submitted form?
  147. if(isset($_POST['username']) AND !empty($_POST['username']))
  148. {
  149. // Check other required fields
  150. if(!isset($_POST['password']) OR empty($_POST['password']) OR !isset($_POST['password_confirm']) OR empty($_POST['password_confirm'])
  151. OR !isset($_POST['email']) OR empty($_POST['email']))
  152. {
  153. // Notice
  154. $this->notice(__('Password, password confirmation and email is required'));
  155. // Redirect
  156. $this->request->redirect('users/index');
  157. }
  158. // Validate username
  159. if(!preg_match('/^[-\pL\pN_.]++$/uD', $_POST['username']))
  160. {
  161. $error = 'Invalid username format';
  162. }
  163. // Username length
  164. if(UTF8::strlen($_POST['username']) > 32 OR UTF8::strlen($_POST['username']) < 4)
  165. {
  166. $error = 'Invalid username length (min. 4, max. 32)';
  167. }
  168. // Password
  169. if(UTF8::strlen($_POST['password']) > 42 OR UTF8::strlen($_POST['password']) < 5)
  170. {
  171. $error = 'Invalid password length (min. 5, max. 42)';
  172. }
  173. // Password confirmation
  174. if($_POST['password_confirm'] != $_POST['password'])
  175. {
  176. $error = 'Entered passwords does not match';
  177. }
  178. // Email
  179. if(UTF8::strlen($_POST['email']) > 127 OR UTF8::strlen($_POST['email']) < 4 OR !Validate::email($_POST['email']))
  180. {
  181. $error = 'Invalid email (format or length)';
  182. }
  183. // Already exists?
  184. if(ORM::factory('user', array('username' => $_POST['username']))->loaded())
  185. {
  186. $error = 'The username is already in use';
  187. }
  188. // Email?
  189. if(ORM::factory('user', array('email' => $_POST['email']))->loaded())
  190. {
  191. $error = 'Email is already in use';
  192. }
  193. // Any errors?
  194. if(isset($error))
  195. {
  196. // Notice
  197. $this->notice(__($error));
  198. // Redirect
  199. $this->request->redirect('users/index');
  200. }
  201. // New user object
  202. $user = new Model_User;
  203. // Validate once more
  204. if($user->values($_POST)->check())
  205. {
  206. // Save
  207. $user->save();
  208. // Add login role
  209. $user->add('roles', new Model_Role(array('name' => 'login')));
  210. // Logs management permission
  211. if(isset($_POST['can_log']) AND $_POST['can_log'] == '1')
  212. {
  213. $user->add('roles', new Model_Role(array('name' => 'logs')));
  214. }
  215. // User management
  216. if(isset($_POST['can_users']) AND $_POST['can_users'] == '1')
  217. {
  218. $user->add('roles', new Model_Role(array('name' => 'users')));
  219. }
  220. // Server management
  221. if(isset($_POST['can_servers']) AND $_POST['can_servers'] == '1')
  222. {
  223. $user->add('roles', new Model_Role(array('name' => 'servers')));
  224. }
  225. // Log action
  226. $this->log_action(__('Added user: :user', array(':user' => $user->username)));
  227. // Notify user
  228. $this->notice(__('User has been added'));
  229. // Redirect
  230. $this->request->redirect('users');
  231. }
  232. else
  233. {
  234. // Unknown error
  235. $this->notice(__('Cannot create user account'));
  236. // Redirect
  237. $this->request->redirect('users');
  238. }
  239. }
  240. // Current title
  241. $this->title = __('User management');
  242. // View
  243. $this->view = new View('users/index');
  244. // Retrieve users
  245. $this->view->users = ORM::factory('user')->find_all();
  246. }
  247. /**
  248. * Actions log
  249. */
  250. public function action_logs()
  251. {
  252. // Set title
  253. $this->title = __('Log management');
  254. // View
  255. $this->view = new View('users/logs');
  256. // Load conditions
  257. if($this->session->get('conditions_log', FALSE))
  258. {
  259. // Load
  260. $conditions = $this->session->get('conditions_log');
  261. }
  262. else
  263. {
  264. // Default
  265. $conditions = array('user' => '', 'content' => '', 'date_from' => '', 'date_to' => time(), 'ip' => '');
  266. }
  267. // Apply new conditions
  268. if(isset($_POST['user']) AND isset($_POST['ip']) AND isset($_POST['content']) AND isset($_POST['date_from']) AND isset($_POST['date_to']))
  269. {
  270. // User
  271. $conditions['user'] = Security::xss_clean($_POST['user']);
  272. // Content
  273. $conditions['content'] = Security::xss_clean($_POST['content']);
  274. // Content
  275. $conditions['ip'] = Security::xss_clean($_POST['ip']);
  276. // Date (from)
  277. if($_POST['date_from'] AND strtotime($_POST['date_from']))
  278. {
  279. $conditions['date_from'] = strtotime($_POST['date_from']);
  280. }
  281. else
  282. {
  283. $conditions['date_from'] = '';
  284. }
  285. // Date (to)
  286. if($_POST['date_to'] AND strtotime($_POST['date_to']))
  287. {
  288. $conditions['date_to'] = strtotime($_POST['date_to']);
  289. }
  290. else
  291. {
  292. $conditions['date_to'] = time();
  293. }
  294. // Save conditions
  295. $this->session->set('conditions_log', $conditions);
  296. // Redirect
  297. $this->request->redirect('users/logs');
  298. }
  299. // Pagination
  300. $pagination = new Pagination(array(
  301. 'current_page' => array('source' => 'route', 'key' => 'id'),
  302. 'items_per_page' => 50,
  303. 'auto_hide' => TRUE,
  304. 'total_items' => Model_Log::count($conditions)
  305. ));
  306. // Get logs
  307. $this->view->logs = Model_Log::get('logs.id', 'DESC', $conditions, $pagination->offset, 50);
  308. // Set pagination
  309. $this->view->pagination = $pagination->render();
  310. // Conditions
  311. $this->view->conditions = $conditions;
  312. }
  313. /**
  314. * Set current tab and check permissions
  315. *
  316. * @see application/classes/controller/Controller_Main::before()
  317. */
  318. public function before()
  319. {
  320. // Run parent constructor
  321. parent::before();
  322. // Set tab
  323. $this->tab = 'users';
  324. // Check permissions
  325. if($this->request->action == 'logs')
  326. {
  327. $this->do_force_login('logs');
  328. }
  329. else
  330. {
  331. $this->do_force_login('users');
  332. }
  333. }
  334. }