'User has been registered', 'insert' => 'User has been created', 'update' => 'User data has been updated', 'delete' => 'User has been deleted' ); protected $_errorMessages = array( 'access' => 'User have not access for this operation', 'register' => 'Wrong data for register', 'insert' => 'User has not been created', 'update' => 'User data has not been updated', 'none' => 'Wrong parameter: $id', 'not_found' => 'User not found', ); public function behaviors() { return [ 'access' => [ 'class' => AccessControlBehavior::className(), 'rules' => [ [ 'allow' => true, 'actions' => ['register'] ], [ 'allow' => Yii::$app->user->checkAccess('editUser'), 'actions' => ['list', 'view', 'save', 'delete'], 'roles' => ['@'] ], // deny all [ 'allow' => false ] ] ] ]; } /*protected function _getSearchModel() { return new UsersSearch(); }*/ public function actionRegister() { $this->baseVerifyAjaxRequest(); $model = new Users(); /** @type Users $model */ $model->scenario = 'signup'; $data = $this->getInputJson(); $data['phone'] = $data['login']; if ($model->load(array($model->formName() => $data))) { if (!Users::$activeAfterRegistration) { $model->activateUser(); } if ($model->save()) { $modelLogin = new LoginForm(); $modelLogin->load(array($modelLogin->formName() => $data)); $modelLogin->login(); $this->_status->setSuccess(Yii::t('api', $this->_successMessages['register'])); $this->_status->data = Users::getUserData(); } else { $this->_status->setErrorReport(Yii::t('api', $this->_errorMessages['register']),$model->errors); } } else { $this->_status->setErrorReport(Yii::t('api', $this->_errorMessages['register']), $model->errors); } return $this->answer(); } public function actionSave() { $this->baseVerifyAjaxRequest(); $data = $this->getInputJson(); if (isset($data['birthDate'])){ $data['birthDate'] = date('Y-m-d', strtotime($data['birthDate'])); } if ($data['phone']) { $data['login'] = $data['phone']; } /** @type \app\models\Users $model */ $model = null; if (isset($data['id'])) { $model = Users::find($data['id']); $model->setScenario('update'); // restrict edit admin's if (!Yii::$app->user->checkAccess('editAdmin') && $model->roleId === 'admin') { throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } } else { $model = new Users(); $model->setScenario('insert'); // restrict set roleId = "admin" if (!Yii::$app->user->checkAccess('editAdmin') && isset($data['roleId']) && 'admin' === $data['roleId']) { throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } } $model->load(array($model->formName() => $data)); $this->_status = $model->ajaxValidate(); if ($this->_status->isSuccess()) { if($model->save()) { $data = $model->getAttributes(); $data['password'] = null; $data['repassword'] = null; $this->_status->data = $data; $this->_status->message = Yii::t('api', $this->_successMessages['update']); } else { $this->_status->setErrorReport(Yii::t('api', $this->_errorMessages[$model->scenario]), $model->errors); } } else { $this->_status->setErrorReport(Yii::t('api', $this->_errorMessages[$model->scenario]), $model->errors); } return $this->answer(); } public function actionList() { $this->baseVerifyAjaxRequest(); $data = $this->getRequestGetParams(); $model = new Users(); $model->setScenario('list'); $query = $model->find()->select($model->safeAttributes()); if (isset($data['filter'])) { if (isset($data['filter']['query'])) { $query->andWhere(['LIKE', 'name', $data['filter']['query']]); $query->orWhere(['LIKE', 'phone', $data['filter']['query']]); } if (isset($data['filter']['roleId'])) $query->andWhere(['roleId' => $data['filter']['roleId']]); if (isset($data['filter']['enabled'])) $query->andWhere(['enabled' => $data['filter']['enabled']]); } $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => isset($data['pagination']) ? $data['pagination'] : []]); $this->_status->data = [ 'list' => $query->asArray()->all(), 'filter' => (isset($data['filter'])) ? $data['filter'] : null, 'pagination' => [ 'pageSize' => (int) $dataProvider->pagination->pageSize, 'totalCount' => (int) $dataProvider->pagination->totalCount, 'pageCount' => (int) $dataProvider->pagination->getPageCount(), 'currentPageIndex' => (int) $dataProvider->pagination->getPage(), ] ]; return $this->answer(); } public function actionView() { $this->baseVerifyAjaxRequest(); $data = $this->getRequestGetParams(); if (isset($data['id'])) { $model = Users::find($data['id']); if ($model) { $model->setScenario('view'); $this->_status->data = $model->getAttributes($model->safeAttributes()); } else { $this->_status->setError(\Yii::t('api', $this->_errorMessages['not_found'])); } } else { $this->_status->setError(\Yii::t('api', $this->_errorMessages['none'])); } return $this->answer(); } public function actionDelete() { $this->baseVerifyAjaxRequest(); $data = $this->getInputJson(); if (isset($data['id'])) { $model = Users::find($data['id']); if ($model) { // restrict delete admin's if (!Yii::$app->user->checkAccess('editAdmin') && $model->roleId === 'admin') { throw new ForbiddenHttpException(Yii::t('yii', 'You are not allowed to perform this action.')); } $model->delete(); $this->_status->message = \Yii::t('api', $this->_successMessages['delete']); } else { $this->_status->setError(\Yii::t('api', $this->_errorMessages['not_found'])); } } else { $this->_status->setError(\Yii::t('api', $this->_errorMessages['none'])); } return $this->answer(); } }