Example #1
0
 /**
  * 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']);
 }
Example #2
0
 /**
  * Promoting the user of given ID.
  * @param integer $id
  * @return \yii\web\Response
  */
 public function actionPromote($id = null)
 {
     $model = (new PodiumUser())->findOne((int) $id);
     if (empty($model->user)) {
         $this->error('Sorry! We can not find User with this ID.');
     } else {
         if ($model->getRole() != User::ROLE_MEMBER) {
             $this->error('You can only promote Members to Moderators.');
         } else {
             $transaction = User::getDb()->beginTransaction();
             try {
                 if ($model->promoteTo(User::ROLE_MODERATOR)) {
                     if (!empty(Yii::$app->authManager->getRolesByUser($model->getId()))) {
                         Yii::$app->authManager->revoke(Yii::$app->authManager->getRole('podiumUser'), $model->getId());
                     }
                     if (Yii::$app->authManager->assign(Yii::$app->authManager->getRole('podiumModerator'), $model->getId())) {
                         $transaction->commit();
                         Log::info('User promoted', !empty($model->getId()) ? $model->getId() : '', __METHOD__);
                         $this->success('User has been promoted.');
                         return $this->redirect(['admin/mods', 'id' => $model->getId()]);
                     }
                 }
                 $this->error('Sorry! There was an error while promoting the user.');
             } catch (Exception $e) {
                 $transaction->rollBack();
                 Log::error($e->getMessage(), null, __METHOD__);
                 $this->error('Sorry! There was an error while promoting the user.');
             }
         }
     }
     return $this->redirect(['members']);
 }