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

/wolf/app/controllers/UserController.php

http://wolfcms.googlecode.com/
PHP | 332 lines | 221 code | 57 blank | 54 comment | 66 complexity | 79e620e3344938f70da4fde7dd7cb7f9 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. * Wolf CMS - Content Management Simplified. <http://www.wolfcms.org>
  4. * Copyright (C) 2008-2010 Martijn van der Kleijn <martijn.niji@gmail.com>
  5. * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
  6. *
  7. * This file is part of Wolf CMS. Wolf CMS is licensed under the GNU GPLv3 license.
  8. * Please see license.txt for the full license text.
  9. */
  10. /**
  11. * @package wolf
  12. * @subpackage controllers
  13. *
  14. * @author Martijn van der Kleijn <martijn.niji@gmail.com>
  15. * @author Philippe Archambault <philippe.archambault@gmail.com>
  16. * @copyright Martijn van der Kleijn, 2008,2009,2010
  17. * @copyright Philippe Archambault, 2008
  18. * @license http://www.gnu.org/licenses/gpl.html GPL License
  19. */
  20. /**
  21. * Class UserController
  22. *
  23. * @package wolf
  24. * @subpackage controllers
  25. *
  26. * @since 0.1
  27. */
  28. class UserController extends Controller {
  29. public function __construct() {
  30. AuthUser::load();
  31. if (!AuthUser::isLoggedIn()) {
  32. redirect(get_url('login'));
  33. }
  34. $this->setLayout('backend');
  35. $this->assignToLayout('sidebar', new View('user/sidebar'));
  36. }
  37. public function index() {
  38. if (!AuthUser::hasPermission('user_view')) {
  39. Flash::set('error', __('You do not have permission to access the requested page!'));
  40. if (Setting::get('default_tab') === 'user') {
  41. redirect(get_url('page'));
  42. }
  43. else {
  44. redirect(get_url());
  45. }
  46. }
  47. $this->display('user/index', array(
  48. 'users' => User::findAll()
  49. ));
  50. }
  51. public function add() {
  52. if (!AuthUser::hasPermission('user_add')) {
  53. Flash::set('error', __('You do not have permission to access the requested page!'));
  54. redirect(get_url());
  55. }
  56. // check if trying to save
  57. if (get_request_method() == 'POST') {
  58. return $this->_add();
  59. }
  60. // check if user have already enter something
  61. $user = Flash::get('post_data');
  62. if (empty($user)) {
  63. $user = new User;
  64. $user->language = Setting::get('language');
  65. }
  66. $this->display('user/edit', array(
  67. 'action' => 'add',
  68. 'csrf_token' => SecureToken::generateToken(BASE_URL.'user/add'),
  69. 'user' => $user,
  70. 'permissions' => Record::findAllFrom('Role')
  71. ));
  72. }
  73. private function _add() {
  74. use_helper('Validate');
  75. $data = $_POST['user'];
  76. Flash::set('post_data', (object) $data);
  77. // Add pre-save checks here
  78. $errors = false;
  79. // CSRF checks
  80. if (isset($_POST['csrf_token'])) {
  81. $csrf_token = $_POST['csrf_token'];
  82. if (!SecureToken::validateToken($csrf_token, BASE_URL.'user/add')) {
  83. Flash::set('error', __('Invalid CSRF token found!'));
  84. redirect(get_url('user/add'));
  85. }
  86. }
  87. else {
  88. Flash::set('error', __('No CSRF token found!'));
  89. redirect(get_url('user/add'));
  90. }
  91. // check if pass and confirm are equal and >= 5 chars
  92. if (strlen($data['password']) >= 5 && $data['password'] == $data['confirm']) {
  93. //$data['password'] = sha1($data['password']);
  94. unset($data['confirm']);
  95. }
  96. else {
  97. Flash::set('error', __('Password and Confirm are not the same or too small!'));
  98. redirect(get_url('user/add'));
  99. }
  100. // check if username >= 3 chars
  101. if (strlen($data['username']) < 3) {
  102. Flash::set('error', __('Username must contain a minimum of 3 characters!'));
  103. redirect(get_url('user/add'));
  104. }
  105. // check if username != password
  106. if ($data['username'] == $data['password']) {
  107. Flash::set('error', __('The password must not be the same as the username!'));
  108. redirect(get_url('user/add'));
  109. }
  110. // Check alphanumerical fields
  111. $fields = array('username', 'name');
  112. foreach ($fields as $field) {
  113. if (!empty($data[$field]) && !Validate::alphanum_space($data[$field])) {
  114. $errors[] = __('Illegal value for :fieldname field!', array(':fieldname' => $field));
  115. }
  116. }
  117. if (!empty($data['email']) && !Validate::email($data['email'])) {
  118. $errors[] = __('Illegal value for :fieldname field!', array(':fieldname' => 'email'));
  119. }
  120. if (!empty($data['language']) && !Validate::alpha($data['language'])) {
  121. $errors[] = __('Illegal value for :fieldname field!', array(':fieldname' => 'language'));
  122. }
  123. if ($errors !== false) {
  124. // Set the errors to be displayed.
  125. Flash::set('error', implode('<br/>', $errors));
  126. redirect(get_url('user/add'));
  127. }
  128. $user = new User($data);
  129. // Generate a salt and create encrypted password
  130. $user->salt = AuthUser::generateSalt();
  131. $user->password = AuthUser::generateHashedPassword($user->password, $user->salt);
  132. if ($user->save()) {
  133. // now we need to add permissions if needed
  134. if (!empty($_POST['user_permission']))
  135. UserRole::setPermissionsFor($user->id, $_POST['user_permission']);
  136. Flash::set('success', __('User has been added!'));
  137. Observer::notify('user_after_add', $user->name);
  138. }
  139. else {
  140. Flash::set('error', __('User has not been added!'));
  141. }
  142. redirect(get_url('user'));
  143. }
  144. public function edit($id) {
  145. if (AuthUser::getId() != $id && !AuthUser::hasPermission('user_edit')) {
  146. Flash::set('error', __('You do not have permission to access the requested page!'));
  147. redirect(get_url());
  148. }
  149. // check if trying to save
  150. if (get_request_method() == 'POST') {
  151. return $this->_edit($id);
  152. }
  153. if ($user = User::findById($id)) {
  154. $this->display('user/edit', array(
  155. 'action' => 'edit',
  156. 'csrf_token' => SecureToken::generateToken(BASE_URL.'user/edit'),
  157. 'user' => $user,
  158. 'permissions' => Record::findAllFrom('Role')
  159. ));
  160. }
  161. else {
  162. Flash::set('error', __('User not found!'));
  163. }
  164. redirect(get_url('user'));
  165. }
  166. // edit
  167. /**
  168. * @todo merge _add() and _edit() into one _store()
  169. *
  170. * @param <type> $id
  171. */
  172. private function _edit($id) {
  173. use_helper('Validate');
  174. $data = $_POST['user'];
  175. Flash::set('post_data', (object) $data);
  176. // Add pre-save checks here
  177. $errors = false;
  178. // CSRF checks
  179. if (isset($_POST['csrf_token'])) {
  180. $csrf_token = $_POST['csrf_token'];
  181. if (!SecureToken::validateToken($csrf_token, BASE_URL.'user/edit')) {
  182. Flash::set('error', __('Invalid CSRF token found!'));
  183. redirect(get_url('user/edit/'.$id));
  184. }
  185. }
  186. else {
  187. Flash::set('error', __('No CSRF token found!'));
  188. redirect(get_url('user/edit/'.$id));
  189. }
  190. // check if user want to change the password
  191. if (strlen($data['password']) > 0) {
  192. // check if pass and confirm are egal and >= 5 chars
  193. if (strlen($data['password']) >= 5 && $data['password'] == $data['confirm']) {
  194. unset($data['confirm']);
  195. }
  196. else {
  197. Flash::set('error', __('Password and Confirm are not the same or too small!'));
  198. redirect(get_url('user/edit/'.$id));
  199. }
  200. }
  201. else {
  202. unset($data['password'], $data['confirm']);
  203. }
  204. // Check alphanumerical fields
  205. $fields = array('username', 'name');
  206. foreach ($fields as $field) {
  207. if (!empty($data[$field]) && !Validate::alphanum_space($data[$field])) {
  208. $errors[] = __('Illegal value for :fieldname field!', array(':fieldname' => $field));
  209. }
  210. }
  211. if (!empty($data['email']) && !Validate::email($data['email'])) {
  212. $errors[] = __('Illegal value for :fieldname field!', array(':fieldname' => 'email'));
  213. }
  214. if (!empty($data['language']) && !Validate::alpha($data['language'])) {
  215. $errors[] = __('Illegal value for :fieldname field!', array(':fieldname' => 'language'));
  216. }
  217. if ($errors !== false) {
  218. // Set the errors to be displayed.
  219. Flash::set('error', implode('<br/>', $errors));
  220. redirect(get_url('user/edit/'.$id));
  221. }
  222. $user = Record::findByIdFrom('User', $id);
  223. if (isset($data['password'])) {
  224. if (empty($user->salt)) {
  225. $user->salt = AuthUser::generateSalt();
  226. }
  227. $data['password'] = AuthUser::generateHashedPassword($data['password'], $user->salt);
  228. }
  229. $user->setFromData($data);
  230. if ($user->save()) {
  231. if (AuthUser::hasPermission('user_edit')) {
  232. // now we need to add permissions
  233. $data = isset($_POST['user_permission']) ? $_POST['user_permission'] : array();
  234. UserRole::setPermissionsFor($user->id, $data);
  235. }
  236. Flash::set('success', __('User has been saved!'));
  237. Observer::notify('user_after_edit', $user->name);
  238. }
  239. else {
  240. Flash::set('error', __('User has not been saved!'));
  241. }
  242. if (AuthUser::getId() == $id) {
  243. redirect(get_url('user/edit/'.$id));
  244. }
  245. else {
  246. redirect(get_url('user'));
  247. }
  248. }
  249. public function delete($id) {
  250. if (!AuthUser::hasPermission('user_delete')) {
  251. Flash::set('error', __('You do not have permission to access the requested page!'));
  252. redirect(get_url());
  253. }
  254. // security (dont delete the first admin)
  255. if ($id > 1) {
  256. // find the user to delete
  257. if ($user = Record::findByIdFrom('User', $id)) {
  258. if ($user->delete()) {
  259. Flash::set('success', __('User <strong>:name</strong> has been deleted!', array(':name' => $user->name)));
  260. Observer::notify('user_after_delete', $user->name);
  261. }
  262. else {
  263. Flash::set('error', __('User <strong>:name</strong> has not been deleted!', array(':name' => $user->name)));
  264. }
  265. }
  266. else {
  267. Flash::set('error', __('User not found!'));
  268. }
  269. }
  270. else {
  271. Flash::set('error', __('Action disabled!'));
  272. }
  273. redirect(get_url('user'));
  274. }
  275. }