[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], 'access' => [ 'class' => \yii\filters\AccessControl::className(), 'only' => [ 'index', 'create','update','view'], 'rules' => [ // allow authenticated users [ 'allow' => true, 'roles' => ['@'], ], // everything else is denied ], ], ]; } /** * Lists all User models. * @return mixed */ public function actionIndex() { $searchModel = new UserSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * Displays a single User model. * @param integer $id * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new UserCreate(); $accounts = Account::readAccounts(); if ($model->load(Yii::$app->request->post()) && $model->save()) { $this->updateAccountAssignments($model); // return $this->redirect(['view', 'id' => $model->id]); } return $this->render('create', [ 'model' => $model, 'accounts' => $accounts, ]); } public function updateAccountAssignments($model){ echo "saving accounts"; UserAccountAssignment::deleteAll(['id_user' => $model->id]); foreach ( $model->selected_accounts as $id_account ){ echo "saving account"; $uaa = new UserAccountAssignment(); $uaa->id_user = $model->id; $uaa->id_account = $id_account; $uaa->save(); } } /** * Updates an existing User model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = UserUpdate::findOne(['id' => $id]); if ( $model == null ){ throw new NotFoundHttpException('The requested page does not exist.'); } $accounts = Account::readAccounts(); $this->applyAccounts($model); if ($model->load(Yii::$app->request->post()) && $model->save()) { $this->updateAccountAssignments($model); return $this->redirect(['view', 'id' => $model->id]); } else { } return $this->render('update', [ 'model' => $model, 'accounts' => $accounts, ]); } private function applyAccounts($model ){ $assignedAccounts = $model->userAccountAssignments; foreach ($assignedAccounts as $acc ){ $model->selected_accounts[] = $acc->id_account; } } /** * Deletes an existing User model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $user = $this->findModel($id); $user->updateAttributes(['status' => User::STATUS_DELETED]); return $this->redirect(['index']); } /** * Finds the User model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return User the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = User::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }