예제 #1
0
 /**
  * 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)
 {
     $modelUser = $this->findModel($id);
     if (Yii::$app->request->post('User')) {
         //
         Role::deleteAll(['user_id' => $id]);
         if (Yii::$app->request->post('User')['item_name'] == '') {
             $sendRoles[] = 'Пользователь';
         } else {
             $sendRoles = Yii::$app->request->post('User')['item_name'];
         }
         //
         foreach ($sendRoles as $one) {
             $model = new Role();
             $model->item_name = $one;
             $model->user_id = $id;
             $model->created_at = time();
             $model->save();
         }
     }
     if ($modelUser->load(Yii::$app->request->post()) && $modelUser->save()) {
         return $this->redirect(['view', 'id' => $modelUser->id]);
     } else {
         $value = ArrayHelper::map($modelUser->role, "item_name", "item_name");
         $allRoles = [];
         foreach (AuthItem::getRoles() as $item_name) {
             if ($item_name->name != 'Создатель') {
                 $allRoles[$item_name->name] = $item_name->name;
             }
         }
         return $this->render('update', ['modelUser' => $modelUser, 'allRoles' => $allRoles, 'value' => $value]);
     }
 }
예제 #2
0
 /**
  * This method is called before each test method.
  *
  * @param \Codeception\Event\TestEvent $event
  */
 public function _before($event)
 {
     // delete this signed up user
     User::deleteAll(['email' => '*****@*****.**', 'username' => 'demo']);
     // delete roles
     Role::deleteAll();
 }
예제 #3
0
 /**
  * Assigns the appropriate role to the registered user.
  * If this is the first registered user in our system, he will get the
  * root role (this should be you), if not, he will get the user role.
  *
  * @param  User   $user User object.
  * @return string       Role name.
  */
 public static function assignRole(User $user)
 {
     $id = $user->getId();
     // make sure there are no leftovers
     Role::deleteAll(['user_id' => $id]);
     $auth = Yii::$app->authManager;
     $role = $auth->getRole($user->user_role);
     $auth->assign($role, $id);
     // return assigned role name in case you want to use this method in tests
     return $role->name;
 }
예제 #4
0
 /**
  * Set user role assigment if it not set in auth_assignment
  */
 public static function checkRoleAssignment()
 {
     $user = Yii::$app->getUser()->getIdentity();
     $id = $user->getId();
     if (empty($id)) {
         return;
     }
     if (null === Role::findOne(['user_id' => $id])) {
         $role = new Role();
         $role->item_name = $user->user_role;
         $role->user_id = $id;
         $role->save(false);
     }
 }
예제 #5
0
 /**
  * Assigns the appropriate role to the registered user.
  * If this is the first registered user in our system, he will get the
  * theCreator role (this should be you), if not, he will get the member role.
  *
  * @param  integer $id The id of the registered user.
  * @return string      Role name.
  */
 public static function assignRole($id)
 {
     // make sure there are no leftovers
     Role::deleteAll(['user_id' => $id]);
     $usersCount = User::find()->count();
     $auth = Yii::$app->authManager;
     // this is the first user in our system, give him theCreator role
     if ($usersCount == 1) {
         $role = $auth->getRole('theCreator');
         $auth->assign($role, $id);
     } else {
         $role = $auth->getRole('member');
         $auth->assign($role, $id);
     }
     // return assigned role name in case you want to use this method in tests
     return $role->name;
 }
예제 #6
0
 /**
  * Deletes an existing User model.
  * If deletion is successful, the browser will be redirected to the 'index' page.
  *
  * @param  integer $id The user id.
  * @return \yii\web\Response
  *
  * @throws NotFoundHttpException
  */
 public function actionDelete($id)
 {
     $this->findModel($id)->delete();
     // delete this user's role from auth_assignment table
     if ($role = Role::find()->where(['user_id' => $id])->one()) {
         $role->delete();
     }
     return $this->redirect(['index']);
 }
예제 #7
0
파일: User.php 프로젝트: amitab61ds/test61
 /**
  * Relation with Role model.
  *
  * @return \yii\db\ActiveQuery
  */
 public function getRole()
 {
     // User has_one Role via Role.user_id -> id
     return $this->hasOne(Role::className(), ['user_id' => 'id']);
 }
 /**
  * Clean up the objects against which you tested.
  */
 protected function tearDown()
 {
     // delete roles
     Role::deleteAll();
 }