Exemple #1
0
 /**
  * Promotes user to given role.
  * @param integer $role
  * @return boolean
  */
 public function promoteTo($role)
 {
     $transaction = static::getDb()->beginTransaction();
     try {
         $this->scenario = 'role';
         $this->role = $role;
         if ($this->save()) {
             if (Yii::$app->authManager->getRolesByUser($this->id)) {
                 Yii::$app->authManager->revoke(Yii::$app->authManager->getRole(Rbac::ROLE_USER), $this->id);
             }
             if (Yii::$app->authManager->assign(Yii::$app->authManager->getRole(Rbac::ROLE_MODERATOR), $this->id)) {
                 Activity::updateRole($this->id, User::ROLE_MODERATOR);
                 $transaction->commit();
                 Log::info('User promoted', $this->id, __METHOD__);
                 return true;
             }
         }
     } catch (Exception $e) {
         $transaction->rollBack();
         Log::error($e->getMessage(), null, __METHOD__);
     }
     return false;
 }
 /**
  * Promoting the user of given ID.
  * @param integer $id
  * @return \yii\web\Response
  */
 public function actionPromote($id = null)
 {
     if (User::can(Rbac::PERM_PROMOTE_USER)) {
         $model = User::findOne((int) $id);
         if (empty($model)) {
             $this->error(Yii::t('podium/flash', 'Sorry! We can not find User with this ID.'));
         } else {
             if ($model->role != User::ROLE_MEMBER) {
                 $this->error(Yii::t('podium/flash', 'You can only promote Members to Moderators.'));
             } else {
                 $transaction = User::getDb()->beginTransaction();
                 try {
                     if ($model->promoteTo(User::ROLE_MODERATOR)) {
                         if (Yii::$app->authManager->getRolesByUser($model->id)) {
                             Yii::$app->authManager->revoke(Yii::$app->authManager->getRole(Rbac::ROLE_USER), $model->id);
                         }
                         if (Yii::$app->authManager->assign(Yii::$app->authManager->getRole(Rbac::ROLE_MODERATOR), $model->id)) {
                             Activity::updateRole($model->id, User::ROLE_MODERATOR);
                             $transaction->commit();
                             Log::info('User promoted', $model->id, __METHOD__);
                             $this->success(Yii::t('podium/flash', 'User has been promoted.'));
                             return $this->redirect(['admin/mods', 'id' => $model->id]);
                         }
                     }
                     $this->error(Yii::t('podium/flash', 'Sorry! There was an error while promoting the user.'));
                 } catch (Exception $e) {
                     $transaction->rollBack();
                     Log::error($e->getMessage(), null, __METHOD__);
                     $this->error(Yii::t('podium/flash', 'Sorry! There was an error while promoting the user.'));
                 }
             }
         }
     } else {
         $this->error(Yii::t('podium/flash', 'You are not allowed to perform this action.'));
     }
     return $this->redirect(['members']);
 }