Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

113 lines
3.2KB

  1. <?php
  2. namespace frontend\controllers;
  3. use Yii;
  4. use common\models\User;
  5. use yii\data\ActiveDataProvider;
  6. use yii\web\Controller;
  7. use yii\web\NotFoundHttpException;
  8. use yii\filters\VerbFilter;
  9. use yii\filters\AccessControl;
  10. /**
  11. * UserController implements the CRUD actions for User model.
  12. */
  13. class UserController extends Controller
  14. {
  15. public function behaviors()
  16. {
  17. return [
  18. 'verbs' => [
  19. 'class' => VerbFilter::className(),
  20. 'actions' => [
  21. ],
  22. ],
  23. 'access' => [
  24. 'class' => AccessControl::className(),
  25. 'rules' => [
  26. [
  27. 'allow' => true,
  28. 'roles' => ['@'],
  29. /*'matchCallback' => function ($rule, $action) {
  30. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  31. }*/
  32. ]
  33. ],
  34. ],
  35. ];
  36. }
  37. /**
  38. * Updates an existing User model.
  39. * If update is successful, the browser will be redirected to the 'view' page.
  40. * @param integer $id
  41. * @return mixed
  42. */
  43. public function actionUpdate()
  44. {
  45. $model = $this->findModel(Yii::$app->user->identity->id);
  46. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  47. // l'utilisateur ne peut pas changer d'adresse email
  48. $old_model = $this->findModel(Yii::$app->user->identity->id) ;
  49. $model->email = $old_model->email ;
  50. // modification du mot de passe
  51. if(strlen($model->password_new))
  52. {
  53. //$model->setPassword($model->password_new) ;
  54. $model->password_hash = Yii::$app->security->generatePasswordHash($model->password_new);
  55. $model->password_old = '' ;
  56. $model->password_new = '' ;
  57. $model->password_new_confirm = '' ;
  58. }
  59. $model->save() ;
  60. Yii::$app->session->setFlash('success','Votre profil a bien été modifié.') ;
  61. return $this->render('update', [
  62. 'model' => $model,
  63. ]);
  64. } else {
  65. if(!$model->validate())
  66. {
  67. Yii::$app->session->setFlash('error','Le formulaire comporte des erreurs.') ;
  68. }
  69. return $this->render('update', [
  70. 'model' => $model,
  71. ]);
  72. }
  73. }
  74. public function actionCredit()
  75. {
  76. return $this->render('credit', [
  77. ]);
  78. }
  79. /**
  80. * Finds the User model based on its primary key value.
  81. * If the model is not found, a 404 HTTP exception will be thrown.
  82. * @param integer $id
  83. * @return User the loaded model
  84. * @throws NotFoundHttpException if the model cannot be found
  85. */
  86. protected function findModel($id)
  87. {
  88. if (($model = User::findOne($id)) !== null) {
  89. return $model;
  90. } else {
  91. throw new NotFoundHttpException('The requested page does not exist.');
  92. }
  93. }
  94. }