PageRenderTime 51ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/user/controllers/YumUserController.php

http://yii-user-management.googlecode.com/
PHP | 387 lines | 291 code | 65 blank | 31 comment | 67 complexity | 766934ea443b8ceb5c35ec2c702fe7de MD5 | raw file
  1. <?php
  2. Yii::import('application.modules.user.controllers.YumController');
  3. class YumUserController extends YumController {
  4. public $defaultAction = 'login';
  5. public function accessRules() {
  6. return array(
  7. array('allow',
  8. 'actions'=>array('index', 'view', 'login'),
  9. 'users'=>array('*'),
  10. ),
  11. array('allow',
  12. 'actions'=>array('profile', 'logout', 'changepassword', 'passwordexpired', 'delete', 'browse'),
  13. 'users'=>array('@'),
  14. ),
  15. array('allow',
  16. 'actions'=>array('admin','delete','create','update', 'list', 'assign', 'generateData', 'csv'),
  17. 'expression' => 'Yii::app()->user->isAdmin()'
  18. ),
  19. array('allow',
  20. 'actions'=>array('create'),
  21. 'expression' => 'Yii::app()->user->can("user_create")'
  22. ),
  23. array('allow',
  24. 'actions'=>array('admin'),
  25. 'expression' => 'Yii::app()->user->can("user_admin")'
  26. ),
  27. array('deny', // deny all other users
  28. 'users'=>array('*'),
  29. ),
  30. );
  31. }
  32. public function actionGenerateData() {
  33. if(Yum::hasModule('role'))
  34. Yii::import('application.modules.role.models.*');
  35. if(isset($_POST['user_amount'])) {
  36. for($i = 0; $i < $_POST['user_amount']; $i++) {
  37. $user = new YumUser();
  38. $user->username = sprintf('Demo_%d_%d', rand(1, 50000), $i);
  39. $user->roles = array($_POST['role']);
  40. $user->password = YumUser::encrypt($_POST['password']);
  41. $user->createtime = time();
  42. $user->status = $_POST['status'];
  43. if($user->save()) {
  44. if(Yum::hasModule('profile')) {
  45. $profile = new YumProfile();
  46. $profile->user_id = $user->id;
  47. $profile->timestamp = time();
  48. $profile->privacy = 'protected';
  49. $profile->email = 'e@mail.de';
  50. $profile->save();
  51. }
  52. }
  53. }
  54. }
  55. $this->render('generate_data');
  56. }
  57. public function actionIndex() {
  58. // If the user is not logged in, so we redirect to the actionLogin,
  59. // which will render the login Form
  60. if(Yii::app()->user->isGuest)
  61. $this->actionLogin();
  62. else
  63. $this->actionList();
  64. }
  65. public function actionStats() {
  66. $this->redirect($this->createUrl('/user/statistics/index'));
  67. }
  68. public function actionPasswordExpired()
  69. {
  70. $this->actionChangePassword($expired = true);
  71. }
  72. public function actionLogin() {
  73. // Do not show the login form if a session expires but a ajax request
  74. // is still generated
  75. if(Yii::app()->user->isGuest && Yii::app()->request->isAjaxRequest)
  76. return false;
  77. $this->redirect(array('/user/auth'));
  78. }
  79. public function actionLogout() {
  80. $this->redirect(array('//user/auth/logout'));
  81. }
  82. public function beforeAction($event) {
  83. if(!Yii::app()->user instanceof YumWebUser)
  84. throw new CException(Yum::t('Please make sure that Yii uses the YumWebUser component instead of CWebUser in your config/main.php components section. Please see the installation instructions.'));
  85. if (Yii::app()->user->isAdmin())
  86. $this->layout = Yum::module()->adminLayout;
  87. else
  88. $this->layout = Yum::module()->layout;
  89. return parent::beforeAction($event);
  90. }
  91. /**
  92. * Change password
  93. */
  94. public function actionChangePassword($expired = false) {
  95. $uid = Yii::app()->user->id;
  96. if(isset($_GET['id']))
  97. $uid = $_GET['id'];
  98. $form = new YumUserChangePassword;
  99. $form->scenario = 'user_request';
  100. if(isset($_POST['YumUserChangePassword'])) {
  101. $form->attributes = $_POST['YumUserChangePassword'];
  102. $form->validate();
  103. if(YumUser::encrypt($form->currentPassword) != YumUser::model()->findByPk($uid)->password)
  104. $form->addError('currentPassword',
  105. Yum::t('Your current password is not correct'));
  106. if(!$form->hasErrors()) {
  107. if(YumUser::model()->findByPk($uid)->setPassword($form->password)) {
  108. Yum::setFlash('The new password has been saved');
  109. Yum::log(Yum::t('User {username} has changed his password', array(
  110. '{username}' => Yii::app()->user->name)));
  111. }
  112. else {
  113. Yum::setFlash('There was an error saving the password');
  114. Yum::log(
  115. Yum::t(
  116. 'User {username} tried to change his password, but an error occured', array(
  117. '{username}' => Yii::app()->user->name)), 'error');
  118. }
  119. $this->redirect(Yum::module()->returnUrl);
  120. }
  121. }
  122. if(Yii::app()->request->isAjaxRequest)
  123. $this->renderPartial('changepassword', array(
  124. 'form'=>$form,
  125. 'expired' => $expired));
  126. else
  127. $this->render('changepassword', array(
  128. 'form'=>$form,
  129. 'expired' => $expired));
  130. }
  131. // Redirects the user to the specified profile
  132. // if no profile is specified, redirect to the own profile
  133. public function actionProfile($id = null) {
  134. $this->redirect(array('//profile/profile/view',
  135. 'id' => $id ? $id : Yii::app()->user->id));
  136. }
  137. /**
  138. * Displays a User
  139. */
  140. public function actionView()
  141. {
  142. $model = $this->loadUser();
  143. $this->render('view',array(
  144. 'model'=>$model,
  145. ));
  146. }
  147. /**
  148. * Creates a new User.
  149. */
  150. public function actionCreate() {
  151. $model = new YumUser;
  152. if(Yum::hasModule('profile'))
  153. $profile = new YumProfile;
  154. $passwordform = new YumUserChangePassword;
  155. // When opening a empty user creation mask, we most probably want to
  156. // insert an _active_ user
  157. if(!isset($model->status))
  158. $model->status = 1;
  159. if(isset($_POST['YumUser'])) {
  160. $model->attributes=$_POST['YumUser'];
  161. if(Yum::hasModule('role'))
  162. $model->roles = Relation::retrieveValues($_POST);
  163. if(Yum::hasModule('profile') && isset($_POST['YumProfile']) )
  164. $profile->attributes = $_POST['YumProfile'];
  165. if(isset($_POST['YumUserChangePassword'])) {
  166. if($_POST['YumUserChangePassword']['password'] == '') {
  167. $password = YumUser::generatePassword();
  168. $model->setPassword($password);
  169. Yum::setFlash(Yum::t('The generated Password is {password}', array(
  170. '{password}' => $password)));
  171. } else {
  172. $passwordform->attributes = $_POST['YumUserChangePassword'];
  173. if($passwordform->validate())
  174. $model->setPassword($_POST['YumUserChangePassword']['password']);
  175. }
  176. }
  177. $model->activationKey = YumUser::encrypt(microtime() . $model->password);
  178. if($model->username == '' && isset($profile))
  179. $model->username = $profile->email;
  180. $model->validate();
  181. if(isset($profile))
  182. $profile->validate();
  183. if(!$model->hasErrors()
  184. && !$passwordform->hasErrors()) {
  185. $model->save();
  186. if(isset($profile)) {
  187. $profile->user_id = $model->id;
  188. $profile->save(array('user_id'), false);
  189. }
  190. $this->redirect(array('view', 'id'=>$model->id));
  191. }
  192. }
  193. $this->render('create',array(
  194. 'model' => $model,
  195. 'passwordform' => $passwordform,
  196. 'profile' => isset($profile) ? $profile : null,
  197. ));
  198. }
  199. public function actionUpdate() {
  200. $model = $this->loadUser();
  201. $passwordform = new YumUserChangePassword();
  202. if(isset($_POST['YumUser'])) {
  203. $model->attributes = $_POST['YumUser'];
  204. if(Yum::hasModule('role')) {
  205. Yii::import('application.modules.role.models.*');
  206. // Assign the roles and belonging Users to the model
  207. $model->roles = Relation::retrieveValues($_POST);
  208. }
  209. if(Yum::hasModule('profile')) {
  210. $profile = $model->profile;
  211. if(isset($_POST['YumProfile']) )
  212. $profile->attributes = $_POST['YumProfile'];
  213. }
  214. // Password change is requested ?
  215. if(isset($_POST['YumUserChangePassword'])
  216. && $_POST['YumUserChangePassword']['password'] != '') {
  217. $passwordform->attributes = $_POST['YumUserChangePassword'];
  218. if($passwordform->validate())
  219. $model->setPassword($_POST['YumUserChangePassword']['password']);
  220. }
  221. if(!$passwordform->hasErrors() && $model->save()) {
  222. if(isset($profile))
  223. $profile->save();
  224. $this->redirect(array('//user/user/view', 'id' => $model->id));
  225. }
  226. }
  227. $this->render('update', array(
  228. 'model'=>$model,
  229. 'passwordform' =>$passwordform,
  230. 'profile' => isset($profile) ? $profile : false,
  231. ));
  232. }
  233. /**
  234. * Deletes a user
  235. */
  236. public function actionDelete($id = null) {
  237. if(!$id)
  238. $id = Yii::app()->user->id;
  239. $user = YumUser::model()->findByPk($id);
  240. if(Yii::app()->user->isAdmin()) {
  241. //This is necesary for handling human stupidity.
  242. if($user && ($user->id == Yii::app()->user->id)) {
  243. Yum::setFlash('You can not delete your own admin account');
  244. $this->redirect(array('//user/user/admin'));
  245. }
  246. if($user->delete()) {
  247. Yum::setFlash('The User has been deleted');
  248. $this->redirect('user/user/admin');
  249. }
  250. } else if(isset($_POST['confirmPassword'])) {
  251. if($user->encrypt($_POST['confirmPassword']) == $user->password) {
  252. if($user->delete())
  253. $this->actionLogout();
  254. else
  255. Yum::setFlash('Error while deleting Account. Account was not deleted');
  256. } else {
  257. Yum::setFlash('Wrong password confirmation! Account was not deleted');
  258. }
  259. $this->redirect(array('//profile/profile/view'));
  260. }
  261. $this->render('confirmDeletion', array('model' => $user));
  262. }
  263. public function actionBrowse() {
  264. $search = '';
  265. if(isset($_POST['search_username']))
  266. $search = $_POST['search_username'];
  267. $criteria = new CDbCriteria;
  268. /* if(Yum::hasModule('profile')) {
  269. $criteria->join = 'LEFT JOIN '.Yum::module('profile')->privacysettingTable .' on t.id = privacysetting.user_id';
  270. $criteria->addCondition('appear_in_search = 1');
  271. } */
  272. $criteria->addCondition('status = 1 or status = 2 or status = 3');
  273. if($search)
  274. $criteria->addCondition("username = '{$search}'");
  275. $dataProvider=new CActiveDataProvider('YumUser', array(
  276. 'criteria' => $criteria,
  277. 'pagination'=>array(
  278. 'pageSize'=>50,
  279. )));
  280. $this->render('browse',array(
  281. 'dataProvider'=>$dataProvider,
  282. 'search_username' => $search ? $search : '',
  283. ));
  284. }
  285. public function actionList()
  286. {
  287. $dataProvider=new CActiveDataProvider('YumUser', array(
  288. 'pagination'=>array(
  289. 'pageSize'=>Yum::module()->pageSize,
  290. )));
  291. $this->render('index',array(
  292. 'dataProvider'=>$dataProvider,
  293. ));
  294. }
  295. public function actionAdmin()
  296. {
  297. if(Yum::hasModule('role'))
  298. Yii::import('application.modules.role.models.*');
  299. $this->layout = Yum::module()->adminLayout;
  300. $model = new YumUser('search');
  301. if(isset($_GET['YumUser']))
  302. $model->attributes = $_GET['YumUser'];
  303. $this->render('admin', array('model'=>$model));
  304. }
  305. /**
  306. * Loads the User Object instance
  307. * @return YumUser
  308. */
  309. public function loadUser($uid = 0)
  310. {
  311. if($this->_model === null)
  312. {
  313. if($uid != 0)
  314. $this->_model = YumUser::model()->findByPk($uid);
  315. elseif(isset($_GET['id']))
  316. $this->_model = YumUser::model()->findByPk($_GET['id']);
  317. if($this->_model === null)
  318. throw new CHttpException(404,'The requested User does not exist.');
  319. }
  320. return $this->_model;
  321. }
  322. }