public function demoteTo($role)
    {
        $transaction = static::getDb()->beginTransaction();
        try {
            $this->scenario = 'role';
            $this->role = $role;
            if (!$this->save()) {
                throw new Exception('User saving error!');
            }
            if (Podium::getInstance()->rbac->getRolesByUser($this->id)
                && !Podium::getInstance()->rbac->revoke(Podium::getInstance()->rbac->getRole(Rbac::ROLE_MODERATOR), $this->id)) {
                throw new Exception('User role revoking error!');
            }
            if (!Podium::getInstance()->rbac->assign(Podium::getInstance()->rbac->getRole(Rbac::ROLE_USER), $this->id)) {
                throw new Exception('User role assigning error!');
            }
            if ((new Query())->from(Mod::tableName())->where(['user_id' => $this->id])->exists()
                && !Podium::getInstance()->db->createCommand()->delete(Mod::tableName(), ['user_id' => $this->id])->execute()) {
                throw new Exception('Moderator deleting error!');
            }
            Activity::updateRole($this->id, User::ROLE_MEMBER);

            $transaction->commit();
            Log::info('User demoted', $this->id, __METHOD__);
            return true;
        } catch (Exception $e) {
            $transaction->rollBack();
            Log::error($e->getMessage(), null, __METHOD__);
        }
        return false;
    }
public function demoteTo($role)
    {
        $transaction = static::getDb()->beginTransaction();
        try {
            $this->scenario = 'role';
            $this->role = $role;
            if (!$this->save()) {
                throw new Exception('User saving error!');
            }
            if (Podium::getInstance()->rbac->getRolesByUser($this->id)
                && !Podium::getInstance()->rbac->revoke(Podium::getInstance()->rbac->getRole(Rbac::ROLE_MODERATOR), $this->id)) {
                throw new Exception('User role revoking error!');
            }
            if (!Podium::getInstance()->rbac->assign(Podium::getInstance()->rbac->getRole(Rbac::ROLE_USER), $this->id)) {
                throw new Exception('User role assigning error!');
            }
            if ((new Query())->from(Mod::tableName())->where(['user_id' => $this->id])->exists()
                && !Podium::getInstance()->db->createCommand()->delete(Mod::tableName(), ['user_id' => $this->id])->execute()) {
                throw new Exception('Moderator deleting error!');
            }
            Activity::updateRole($this->id, User::ROLE_MEMBER);

            $transaction->commit();
            Log::info('User demoted', $this->id, __METHOD__);
            return true;
        } catch (Exception $e) {
            $transaction->rollBack();
            Log::error($e->getMessage(), null, __METHOD__);
        }
        return false;
    }
public function demoteTo ( $ role ) { $ transaction = static :: getDb ( ) -> beginTransaction ( ) ; try { $ this -> scenario = 'role' ; $ this -> role = $ role ; if ( ! $ this -> save ( ) ) { throw new Exception ( 'User saving error!' ) ; } if ( Podium :: getInstance ( ) -> rbac -> getRolesByUser ( $ this -> id ) && ! Podium :: getInstance ( ) -> rbac -> revoke ( Podium :: getInstance ( ) -> rbac -> getRole ( Rbac :: ROLE_MODERATOR ) , $ this -> id ) ) { throw new Exception ( 'User role revoking error!' ) ; } if ( ! Podium :: getInstance ( ) -> rbac -> assign ( Podium :: getInstance ( ) -> rbac -> getRole ( Rbac :: ROLE_USER ) , $ this -> id ) ) { throw new Exception ( 'User role assigning error!' ) ; } if ( ( new Query ( ) ) -> from ( Mod :: tableName ( ) ) -> where ( [ 'user_id' => $ this -> id ] ) -> exists ( ) && ! Podium :: getInstance ( ) -> db -> createCommand ( ) -> delete ( Mod :: tableName ( ) , [ 'user_id' => $ this -> id ] ) -> execute ( ) ) { throw new Exception ( 'Moderator deleting error!' ) ; } Activity :: updateRole ( $ this -> id , User :: ROLE_MEMBER ) ; $ transaction -> commit ( ) ; Log :: info ( 'User demoted' , $ this -> id , __METHOD__ ) ; return true ; } catch ( Exception $ e ) { $ transaction -> rollBack ( ) ; Log :: error ( $ e -> getMessage ( ) , null , __METHOD__ ) ; } return false ; }
Demotes user to given role.
@param int $role
@return bool
Demotes user to given role .