You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.1KB

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