yii\web\User::can PHP 메소드

can() 공개 메소드

Note that you must configure "authManager" application component in order to use this method. Otherwise it will always return false.
public can ( string $permissionName, array $params = [], boolean $allowCaching = true ) : boolean
$permissionName string the name of the permission (e.g. "edit post") that needs access check.
$params array name-value pairs that would be passed to the rules associated with the roles and permissions assigned to the user.
$allowCaching boolean whether to allow caching the result of access check. When this parameter is true (default), if the access check of an operation was performed before, its result will be directly returned when calling this method to check the same operation. If this parameter is false, this method will always call [[\yii\rbac\CheckAccessInterface::checkAccess()]] to obtain the up-to-date access result. Note that this caching is effective only within the same request and only works when `$params = []`.
리턴 boolean whether the user can perform the operation as specified by the given permission.
    public function can($permissionName, $params = [], $allowCaching = true)
    {
        if ($allowCaching && empty($params) && isset($this->_access[$permissionName])) {
            return $this->_access[$permissionName];
        }
        if (($accessChecker = $this->getAccessChecker()) === null) {
            return false;
        }
        $access = $accessChecker->checkAccess($this->getId(), $permissionName, $params);
        if ($allowCaching && empty($params)) {
            $this->_access[$permissionName] = $access;
        }
        return $access;
    }

Usage Example

예제 #1
0
 /**
  * Проверка на возможность отображать элемент меню.
  *
  * @param array $item
  * @return bool
  */
 private function canShowMenuItem($item)
 {
     if (!isset($item['roles'])) {
         return true;
     }
     foreach ($item['roles'] as $role) {
         if ($this->user->can($role)) {
             return true;
         }
     }
     return false;
 }
All Usage Examples Of yii\web\User::can