No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

233 líneas
7.5KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use common\models\User;
  5. use backend\models\MailForm;
  6. use yii\data\ActiveDataProvider;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use yii\filters\AccessControl;
  11. use common\helpers\Upload ;
  12. use common\helpers\Password ;
  13. use common\models\UserEtablissement ;
  14. use common\models\Etablissement ;
  15. use yii\base\UserException ;
  16. /**
  17. * UserController implements the CRUD actions for User model.
  18. */
  19. class UserController extends BackendController
  20. {
  21. public function behaviors()
  22. {
  23. return [
  24. 'verbs' => [
  25. 'class' => VerbFilter::className(),
  26. 'actions' => [
  27. 'delete' => ['post'],
  28. ],
  29. ],
  30. 'access' => [
  31. 'class' => AccessControl::className(),
  32. 'rules' => [
  33. [
  34. 'allow' => true,
  35. 'roles' => ['@'],
  36. 'matchCallback' => function ($rule, $action) {
  37. if($action->actionMethod == 'actionIndex' ||
  38. $action->actionMethod == 'actionCreate' ||
  39. $action->actionMethod == 'actionUpdate')
  40. {
  41. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  42. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER ;
  43. }
  44. else {
  45. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  46. }
  47. }
  48. ]
  49. ],
  50. ],
  51. ];
  52. }
  53. /**
  54. * Lists all User models.
  55. * @return mixed
  56. */
  57. public function actionIndex()
  58. {
  59. $dataProvider = new ActiveDataProvider([
  60. 'query' =>
  61. (new \yii\db\Query())
  62. ->select('*')
  63. ->from('user, user_etablissement')
  64. ->where('user.id = user_etablissement.id_user')
  65. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  66. ]);
  67. return $this->render('index', [
  68. 'dataProvider' => $dataProvider,
  69. ]);
  70. }
  71. /**
  72. * Displays a single User model.
  73. * @param integer $id
  74. * @return mixed
  75. */
  76. public function actionView($id)
  77. {
  78. return $this->render('view', [
  79. 'model' => $this->findModel($id),
  80. ]);
  81. }
  82. /**
  83. * Creates a new User model.
  84. * If creation is successful, the browser will be redirected to the 'view' page.
  85. * @return mixed
  86. */
  87. public function actionCreate()
  88. {
  89. $model = new User();
  90. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  91. // save use
  92. $password = Password::generate() ;
  93. $model->setPassword($password);
  94. $model->generateAuthKey();
  95. $model->username = $model->email ;
  96. $model->save() ;
  97. // liaison etablissement / user
  98. $user_etablissement = new UserEtablissement() ;
  99. $user_etablissement->id_user = $model->id ;
  100. $user_etablissement->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  101. $user_etablissement->save() ;
  102. // send mail
  103. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  104. Yii::$app->mailer->compose() ;
  105. $mail = Yii::$app->mailer->compose(
  106. ['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'],
  107. ['user' => $model, 'etablissement' => $etablissement, 'password' => $password])
  108. ->setTo($model->email)
  109. ->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
  110. ->setSubject('[La boîte à pain] Inscription')
  111. ->send() ;
  112. return $this->redirect(['index']);
  113. } else {
  114. return $this->render('create', [
  115. 'model' => $model,
  116. ]);
  117. }
  118. }
  119. /**
  120. * Updates an existing User model.
  121. * If update is successful, the browser will be redirected to the 'view' page.
  122. * @param integer $id
  123. * @return mixed
  124. */
  125. public function actionUpdate($id)
  126. {
  127. $model = $this->findModel($id);
  128. $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one() ;
  129. $user_appartient_etablissement = UserEtablissement::findOne(['id_user' =>$id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]) ;
  130. if(($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN)
  131. {
  132. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  133. return $this->redirect(['index']);
  134. } else {
  135. return $this->render('update', [
  136. 'model' => $model,
  137. ]);
  138. }
  139. }
  140. else {
  141. throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs boulangeries, soit parce qu'il n'est pas lié à la votre.");
  142. }
  143. }
  144. /**
  145. * Deletes an existing User model.
  146. * If deletion is successful, the browser will be redirected to the 'index' page.
  147. * @param integer $id
  148. * @return mixed
  149. */
  150. /*public function actionDelete($id)
  151. {
  152. $this->findModel($id)->delete();
  153. return $this->redirect(['index']);
  154. }*/
  155. public function actionMail() {
  156. /*$model = new MailForm() ;
  157. $model->subject = '[Le Chat des Noisettes] ' ;
  158. $model->body =
  159. "Bonjour,
  160. PS : Si vous ne souhaitez plus recevoir ces emails, rendez-vous dans votre compte sur www.lechatdesnoisettes.com." ;
  161. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  162. $id_user = Yii::$app->request->post('id_user') ;
  163. $user = User::findIdentity($id_user) ;
  164. if($user)
  165. {
  166. $model->sendEmail($user->email) ;
  167. }
  168. }*/
  169. $users = (new \yii\db\Query())
  170. ->select('*')
  171. ->from('user, user_etablissement')
  172. ->where('user.id = user_etablissement.id_user')
  173. ->andWhere('user_etablissement.id_etablissement = '.Yii::$app->user->identity->id_etablissement)
  174. ->all() ;
  175. $arr_users = [] ;
  176. foreach($users as $u) {
  177. if(isset($u['email']))
  178. $arr_users[] = $u['email'] ;
  179. }
  180. return $this->render('liste_mails', [
  181. //'model' => $model,
  182. 'users' => $arr_users
  183. ]);
  184. }
  185. /**
  186. * Finds the User model based on its primary key value.
  187. * If the model is not found, a 404 HTTP exception will be thrown.
  188. * @param integer $id
  189. * @return User the loaded model
  190. * @throws NotFoundHttpException if the model cannot be found
  191. */
  192. protected function findModel($id)
  193. {
  194. if (($model = User::findOne($id)) !== null) {
  195. return $model;
  196. } else {
  197. throw new NotFoundHttpException('The requested page does not exist.');
  198. }
  199. }
  200. }