PageRenderTime 39ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/modules/user/controllers/AdminController.php

https://github.com/joshuaswarren/weatherhub
PHP | 165 lines | 117 code | 16 blank | 32 comment | 13 complexity | d478461e7c82ab787651172501dcc13b MD5 | raw file
  1. <?php
  2. class AdminController extends Controller
  3. {
  4. public $defaultAction = 'admin';
  5. private $_model;
  6. /**
  7. * @return array action filters
  8. */
  9. public function filters()
  10. {
  11. return CMap::mergeArray(parent::filters(),array(
  12. 'accessControl', // perform access control for CRUD operations
  13. ));
  14. }
  15. /**
  16. * Specifies the access control rules.
  17. * This method is used by the 'accessControl' filter.
  18. * @return array access control rules
  19. */
  20. public function accessRules()
  21. {
  22. return array(
  23. array('allow', // allow admin user to perform 'admin' and 'delete' actions
  24. 'actions'=>array('admin','delete','create','update','view'),
  25. 'users'=>UserModule::getAdmins(),
  26. ),
  27. array('deny', // deny all users
  28. 'users'=>array('*'),
  29. ),
  30. );
  31. }
  32. /**
  33. * Manages all models.
  34. */
  35. public function actionAdmin()
  36. {
  37. $dataProvider=new CActiveDataProvider('User', array(
  38. 'pagination'=>array(
  39. 'pageSize'=>Yii::app()->controller->module->user_page_size,
  40. ),
  41. ));
  42. $this->render('index',array(
  43. 'dataProvider'=>$dataProvider,
  44. ));
  45. }
  46. /**
  47. * Displays a particular model.
  48. */
  49. public function actionView()
  50. {
  51. $model = $this->loadModel();
  52. $this->render('view',array(
  53. 'model'=>$model,
  54. ));
  55. }
  56. /**
  57. * Creates a new model.
  58. * If creation is successful, the browser will be redirected to the 'view' page.
  59. */
  60. public function actionCreate()
  61. {
  62. $model=new User;
  63. $profile=new Profile;
  64. if(isset($_POST['User']))
  65. {
  66. $model->attributes=$_POST['User'];
  67. $model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
  68. $model->createtime=time();
  69. $model->lastvisit=time();
  70. $profile->attributes=$_POST['Profile'];
  71. $profile->user_id=0;
  72. if($model->validate()&&$profile->validate()) {
  73. $model->password=Yii::app()->controller->module->encrypting($model->password);
  74. if($model->save()) {
  75. $profile->user_id=$model->id;
  76. $profile->save();
  77. }
  78. $this->redirect(array('view','id'=>$model->id));
  79. } else $profile->validate();
  80. }
  81. $this->render('create',array(
  82. 'model'=>$model,
  83. 'profile'=>$profile,
  84. ));
  85. }
  86. /**
  87. * Updates a particular model.
  88. * If update is successful, the browser will be redirected to the 'view' page.
  89. */
  90. public function actionUpdate()
  91. {
  92. $model=$this->loadModel();
  93. $profile=$model->profile;
  94. if(isset($_POST['User']))
  95. {
  96. $model->attributes=$_POST['User'];
  97. $profile->attributes=$_POST['Profile'];
  98. if($model->validate()&&$profile->validate()) {
  99. $old_password = User::model()->notsafe()->findByPk($model->id);
  100. if ($old_password->password!=$model->password) {
  101. $model->password=Yii::app()->controller->module->encrypting($model->password);
  102. $model->activkey=Yii::app()->controller->module->encrypting(microtime().$model->password);
  103. }
  104. $model->save();
  105. $profile->save();
  106. $this->redirect(array('view','id'=>$model->id));
  107. } else $profile->validate();
  108. }
  109. $this->render('update',array(
  110. 'model'=>$model,
  111. 'profile'=>$profile,
  112. ));
  113. }
  114. /**
  115. * Deletes a particular model.
  116. * If deletion is successful, the browser will be redirected to the 'index' page.
  117. */
  118. public function actionDelete()
  119. {
  120. if(Yii::app()->request->isPostRequest)
  121. {
  122. // we only allow deletion via POST request
  123. $model = $this->loadModel();
  124. $profile = Profile::model()->findByPk($model->id);
  125. $profile->delete();
  126. $model->delete();
  127. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  128. if(!isset($_POST['ajax']))
  129. $this->redirect(array('/user/admin'));
  130. }
  131. else
  132. throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
  133. }
  134. /**
  135. * Returns the data model based on the primary key given in the GET variable.
  136. * If the data model is not found, an HTTP exception will be raised.
  137. */
  138. public function loadModel()
  139. {
  140. if($this->_model===null)
  141. {
  142. if(isset($_GET['id']))
  143. $this->_model=User::model()->notsafe()->findbyPk($_GET['id']);
  144. if($this->_model===null)
  145. throw new CHttpException(404,'The requested page does not exist.');
  146. }
  147. return $this->_model;
  148. }
  149. }