console/controllers/UserController.php
public function actionPermission($user, $operation, $permission)
    {
        $user = $this->getUser($user);
        $permission = Yii::$app->authManager->getPermission($permission);
        if ($operation == 'assign') {
            try {
                $assignment = Yii::$app->authManager->assign($permission, $user);
            } catch (\yii\db\IntegrityException $ex) {
                echo "Failed to assign `" . $permission->name . "`.\n";
                echo "Maybe the permission has been assigned.\n";
                return static::EXIT_CODE_ERROR;
            }
            if ($assignment) {
                echo "`$permission->name`" . " assigned to User (" . $user->getID() . ") successfully.\n";
            } else {
                echo "Failed to assign `" . $permission->name . "`.\n";
            }
            return static::EXIT_CODE_NORMAL;
        }
        if ($operation == 'revoke') {
            $assignment = Yii::$app->authManager->revoke($permission, $user);
            if ($assignment) {
                echo "`$permission->name`" . " revoked from User (" . $user->getID() . ").\n";
            } else {
                echo "Failed to revoke `" . $permission->name . "`.\n";
                echo "Maybe the permission has not been assigned yet.\n";
            }
            return static::EXIT_CODE_NORMAL;
        }
        echo "Unrecognized operation: $operation.\n";
        echo "The accepted operations are `assign` and `revoke`.\n";
        return static::EXIT_CODE_ERROR;
    }
public function actionPermission($user, $operation, $permission)
    {
        $user = $this->getUser($user);
        $permission = Yii::$app->authManager->getPermission($permission);
        if ($operation == 'assign') {
            try {
                $assignment = Yii::$app->authManager->assign($permission, $user);
            } catch (\yii\db\IntegrityException $ex) {
                echo "Failed to assign `" . $permission->name . "`.\n";
                echo "Maybe the permission has been assigned.\n";
                return static::EXIT_CODE_ERROR;
            }
            if ($assignment) {
                echo "`$permission->name`" . " assigned to User (" . $user->getID() . ") successfully.\n";
            } else {
                echo "Failed to assign `" . $permission->name . "`.\n";
            }
            return static::EXIT_CODE_NORMAL;
        }
        if ($operation == 'revoke') {
            $assignment = Yii::$app->authManager->revoke($permission, $user);
            if ($assignment) {
                echo "`$permission->name`" . " revoked from User (" . $user->getID() . ").\n";
            } else {
                echo "Failed to revoke `" . $permission->name . "`.\n";
                echo "Maybe the permission has not been assigned yet.\n";
            }
            return static::EXIT_CODE_NORMAL;
        }
        echo "Unrecognized operation: $operation.\n";
        echo "The accepted operations are `assign` and `revoke`.\n";
        return static::EXIT_CODE_ERROR;
    }
public function actionPermission ( $ user , $ operation , $ permission ) { $ user = $ this -> getUser ( $ user ) ; $ permission = Yii :: $ app -> authManager -> getPermission ( $ permission ) ; if ( $ operation == 'assign' ) { try { $ assignment = Yii :: $ app -> authManager -> assign ( $ permission , $ user ) ; } catch ( \ yii \ db \ IntegrityException $ ex ) { echo "Failed to assign `" . $ permission -> name . "`.\n" ; echo "Maybe the permission has been assigned.\n" ; return static :: EXIT_CODE_ERROR ; } if ( $ assignment ) { echo "`$permission->name`" . " assigned to User (" . $ user -> getID ( ) . ") successfully.\n" ; } else { echo "Failed to assign `" . $ permission -> name . "`.\n" ; } return static :: EXIT_CODE_NORMAL ; } if ( $ operation == 'revoke' ) { $ assignment = Yii :: $ app -> authManager -> revoke ( $ permission , $ user ) ; if ( $ assignment ) { echo "`$permission->name`" . " revoked from User (" . $ user -> getID ( ) . ").\n" ; } else { echo "Failed to revoke `" . $ permission -> name . "`.\n" ; echo "Maybe the permission has not been assigned yet.\n" ; } return static :: EXIT_CODE_NORMAL ; } echo "Unrecognized operation: $operation.\n" ; echo "The accepted operations are `assign` and `revoke`.\n" ; return static :: EXIT_CODE_ERROR ; }
Assign a permission to user or revoke a permission.
@param User|string|integer $user User ID.
@param string $operation Only `assign` and `revoke` are acceptable.
@param string $permission Permission name.
@return int
Assign a permission to user or revoke a permission .