跳转到内容

Phalcon acl

注意

所有类都以前缀命名Phalcon

Acl\Adapter\AbstractAdapterAbstract

GitHub上的源码

  • 命名空间

    • Phalcon\Acl\Adapter
  • 使用

    • Phalcon\Acl\Enum
    • Phalcon\Events\AbstractEventsAware
    • Phalcon\Events\EventsAwareInterface
  • 继承

    AbstractEventsAware

  • 实现

    • AdapterInterface
    • EventsAwareInterface

Phalcon\Acl 适配器的适配器

属性

/**
 * Access Granted
 *
 * @var bool
 */
protected $accessGranted = false;

/**
 * Active access which the list is checking if some role can access it
 *
 * @var string|null
 */
protected $activeAccess;

/**
 * Component which the list is checking if some role can access it
 *
 * @var string|null
 */
protected $activeComponent;

/**
 * Role which the list is checking if it's allowed to certain
 * component/access
 *
 * @var string|null
 */
protected $activeRole;

/**
 * Default access
 *
 * @var int
 */
protected $defaultAccess;

方法

public function getActiveAccess(): string | null;
列表正在检查某个角色是否可以访问的活动访问权限

public function getActiveComponent(): string | null;
列表正在检查某个角色是否可以访问的组件

public function getActiveRole(): string | null;
列表正在检查的角色,以确定其是否允许访问特定的组件/访问权限

public function getDefaultAction(): int;
返回默认的 ACL 访问级别

public function setDefaultAction( int $defaultAccess ): void;
设置默认访问级别 (Phalcon\Acl\Enum::ALLOW 或 Phalcon\Acl\Enum::DENY)

Acl\Adapter\AdapterInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Acl\Adapter
  • 使用

    • Phalcon\Acl\ComponentInterface
    • Phalcon\Acl\RoleInterface
  • 继承

  • 实现

Phalcon\Acl 适配器的接口

方法

public function addComponent( mixed $componentValue, mixed $accessList ): bool;
向 ACL 列表中添加一个组件

访问名称可以是特定的动作,例如搜索、更新、删除等,或者它们的列表

public function addComponentAccess( string $componentName, mixed $accessList ): bool;
向组件添加访问权限

public function addInherit( string $roleName, mixed $roleToInherits ): bool;
检查一个角色是否继承自另一个现有角色

public function addRole( mixed $role, mixed $accessInherits = null ): bool;
向 ACL 列表中添加一个角色。第二个参数允许从其他现有角色继承访问数据

public function allow( string $roleName, string $componentName, mixed $access, mixed $func = null ): void;
允许角色访问组件

public function deny( string $roleName, string $componentName, mixed $access, mixed $func = null ): void;
拒绝角色访问组件

public function dropComponentAccess( string $componentName, mixed $accessList ): void;
从组件中移除访问权限

public function getActiveAccess(): null | string;
返回列表正在检查某个角色是否可以访问的访问权限

public function getActiveComponent(): null | string;
返回列表正在检查某个角色是否可以访问的组件

public function getActiveRole(): null | string;
返回列表正在检查的角色,以确定其是否允许访问特定的组件/访问权限

public function getComponents(): ComponentInterface[];
返回一个包含列表中注册的所有组件的数组

public function getDefaultAction(): int;
返回默认的 ACL 访问级别

public function getInheritedRoles( string $roleName = string ): array;
返回传递的角色名称的继承角色。如果未指定角色名称,则返回整个数组。如果找不到角色,则返回空数组

public function getNoArgumentsDefaultAction(): int;
如果在 isAllowed 动作中没有提供参数且存在 accessKey 的函数,则返回默认的 ACL 访问级别

public function getRoles(): RoleInterface[];
返回一个包含列表中注册的所有角色的数组

public function isAllowed( mixed $roleName, mixed $componentName, string $access, array $parameters = null ): bool;
检查角色是否被允许访问组件中的某个动作

public function isComponent( string $componentName ): bool;
检查组件是否存在于组件列表中

public function isRole( string $roleName ): bool;
检查角色是否存在于角色列表中

public function setDefaultAction( int $defaultAccess ): void;
设置默认访问级别 (Phalcon\Ac\Enuml::ALLOW 或 Phalcon\Acl\Enum::DENY)

public function setNoArgumentsDefaultAction( int $defaultAccess ): void;
如果在 isAllowed 动作中未提供参数且存在 accessKey 的函数,则设置默认访问级别 (Phalcon\Acl\Enum::ALLOW 或 Phalcon\Acl\Enum::DENY)

Acl\Adapter\Memory

GitHub上的源码

  • 命名空间

    • Phalcon\Acl\Adapter
  • 使用

    • Phalcon\Acl\Component
    • Phalcon\Acl\ComponentAwareInterface
    • Phalcon\Acl\ComponentInterface
    • Phalcon\Acl\Enum
    • Phalcon\Acl\Exception
    • Phalcon\Acl\Role
    • Phalcon\Acl\RoleAwareInterface
    • Phalcon\Acl\RoleInterface
    • ReflectionClass
    • ReflectionFunction
  • 继承

    AbstractAdapter

  • 实现

在内存中管理 ACL 列表

$acl = new \Phalcon\Acl\Adapter\Memory();

$acl->setDefaultAction(
    \Phalcon\Acl\Enum::DENY
);

// Register roles
$roles = [
    "users"  => new \Phalcon\Acl\Role("Users"),
    "guests" => new \Phalcon\Acl\Role("Guests"),
];
foreach ($roles as $role) {
    $acl->addRole($role);
}

// Private area components
$privateComponents = [
    "companies" => ["index", "search", "new", "edit", "save", "create", "delete"],
    "products"  => ["index", "search", "new", "edit", "save", "create", "delete"],
    "invoices"  => ["index", "profile"],
];

foreach ($privateComponents as $componentName => $actions) {
    $acl->addComponent(
        new \Phalcon\Acl\Component($componentName),
        $actions
    );
}

// Public area components
$publicComponents = [
    "index"   => ["index"],
    "about"   => ["index"],
    "session" => ["index", "register", "start", "end"],
    "contact" => ["index", "send"],
];

foreach ($publicComponents as $componentName => $actions) {
    $acl->addComponent(
        new \Phalcon\Acl\Component($componentName),
        $actions
    );
}

// Grant access to public areas to both users and guests
foreach ($roles as $role) {
    foreach ($publicComponents as $component => $actions) {
        $acl->allow($role->getName(), $component, "*");
    }
}

// Grant access to private area to role Users
foreach ($privateComponents as $component => $actions) {
    foreach ($actions as $action) {
        $acl->allow("Users", $component, $action);
    }
}

属性

/**
 * Access
 *
 * @var mixed
 */
protected $access;

/**
 * Access List
 *
 * @var mixed
 */
protected $accessList;

/**
 * Returns the latest function used to acquire access
 *
 * @var mixed
 */
protected $activeFunction;

/**
 * Returns number of additional arguments(excluding role and resource) for active function
 *
 * @var int
 */
protected $activeFunctionCustomArgumentsCount = ;

/**
 * Returns the latest key used to acquire access
 *
 * @var string|null
 */
protected $activeKey;

/**
 * Components
 *
 * @var mixed
 */
protected $components;

/**
 * Component Names
 *
 * @var mixed
 */
protected $componentsNames;

/**
 * Function List
 *
 * @var mixed
 */
protected $func;

/**
 * Default action for no arguments is `allow`
 *
 * @var mixed
 */
protected $noArgumentsDefaultAction;

/**
 * Roles
 *
 * @var mixed
 */
protected $roles;

/**
 * Role Inherits
 *
 * @var mixed
 */
protected $roleInherits;

方法

public function __construct();
Phalcon\Acl\Adapter\Memory 构造函数

public function addComponent( mixed $componentValue, mixed $accessList ): bool;
向 ACL 列表中添加一个组件

访问名称可以是特定的动作,例如搜索、更新、删除等,或者它们的列表

示例:

// Add a component to the list allowing access to an action
$acl->addComponent(
    new Phalcon\Acl\Component("customers"),
    "search"
);

$acl->addComponent("customers", "search");

// Add a component  with an access list
$acl->addComponent(
    new Phalcon\Acl\Component("customers"),
    [
        "create",
        "search",
    ]
);

$acl->addComponent(
    "customers",
    [
        "create",
        "search",
    ]
);

public function addComponentAccess( string $componentName, mixed $accessList ): bool;
向组件添加访问权限

public function addInherit( string $roleName, mixed $roleToInherits ): bool;
检查一个角色是否继承自另一个现有角色

$acl->addRole("administrator", "consultant");
$acl->addRole("administrator", ["consultant", "consultant2"]);

public function addRole( mixed $role, mixed $accessInherits = null ): bool;
向 ACL 列表中添加一个角色。第二个参数允许从其他现有角色继承访问数据

$acl->addRole(
    new Phalcon\Acl\Role("administrator"),
    "consultant"
);

$acl->addRole("administrator", "consultant");
$acl->addRole("administrator", ["consultant", "consultant2"]);

public function allow( string $roleName, string $componentName, mixed $access, mixed $func = null ): void;
允许角色访问组件。你可以使用*作为通配符

// Allow access to guests to search on customers
$acl->allow("guests", "customers", "search");

// Allow access to guests to search or create on customers
$acl->allow("guests", "customers", ["search", "create"]);

// Allow access to any role to browse on products
$acl->allow("*", "products", "browse");

// Allow access to any role to browse on any component
$acl->allow("*", "*", "browse");

public function deny( string $roleName, string $componentName, mixed $access, mixed $func = null ): void;
拒绝角色访问组件。你可以使用*作为通配符

// Deny access to guests to search on customers
$acl->deny("guests", "customers", "search");

// Deny access to guests to search or create on customers
$acl->deny("guests", "customers", ["search", "create"]);

// Deny access to any role to browse on products
$acl->deny("*", "products", "browse");

// Deny access to any role to browse on any component
$acl->deny("*", "*", "browse");

public function dropComponentAccess( string $componentName, mixed $accessList ): void;
从组件中移除访问权限

public function getActiveFunction(): mixed;
返回用于获取访问权限的最新函数

public function getActiveFunctionCustomArgumentsCount(): int;
返回活动函数的额外参数数量(不包括角色和资源)

public function getActiveKey(): string | null;
返回用于获取访问权限的最新键

public function getComponents(): ComponentInterface[];
返回一个包含列表中注册的所有组件的数组

public function getInheritedRoles( string $roleName = string ): array;
返回传递的角色名称的继承角色。如果未指定角色名称,则返回整个数组。如果找不到角色,则返回空数组

public function getNoArgumentsDefaultAction(): int;
返回在isAllowed动作中没有提供参数时的默认 ACL 访问级别,如果func(可调用)存在则适用于accessKey

public function getRoles(): RoleInterface[];
返回一个包含列表中注册的所有角色的数组

public function isAllowed( mixed $roleName, mixed $componentName, string $access, array $parameters = null ): bool;
检查角色是否被允许访问组件中的某个动作

// Does andres have access to the customers component to create?
$acl->isAllowed("andres", "Products", "create");

// Do guests have access to any component to edit?
$acl->isAllowed("guests", "*", "edit");

public function isComponent( string $componentName ): bool;
检查组件是否存在于组件列表中

public function isRole( string $roleName ): bool;
检查角色是否存在于角色列表中

public function setNoArgumentsDefaultAction( int $defaultAccess ): void;
设置默认访问级别 (Phalcon\Enum::ALLOWPhalcon\Enum::DENY) 对于在 isAllowed 动作中没有提供参数的情况,如果存在针对 accessKey 的 func

Acl\Component

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

    • ComponentInterface

此类定义了组件实体及其描述

属性

/**
 * Component description
 *
 * @var string
 */
private $description;

/**
 * Component name
 *
 * @var string
 */
private $name;

方法

public function __construct( string $name, string $description = null );
Phalcon\Acl\Component 构造函数

public function __toString(): string;
public function getDescription(): string;
public function getName(): string;

Acl\ComponentAwareInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

可在 allow 方法中作为 RESOURCE 使用的类的接口

方法

public function getComponentName(): string;
返回组件名称

Acl\ComponentInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

Phalcon\Acl\Component 的接口

方法

public function __toString(): string;
魔术方法 __toString

public function getDescription(): string;
返回组件描述

public function getName(): string;
返回组件名称

Acl\Enum

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

Phalcon\Acl\Adapter 适配器的常量

常量

const ALLOW = 1;
const DENY = 0;

Acl\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

    \Exception

  • 实现

由 Phalcon\Acl 抛出的异常的类

Acl\Role

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

    • RoleInterface

此类定义了角色实体及其描述

属性

/**
 * Role description
 *
 * @var string
 */
private $description;

/**
 * Role name
 *
 * @var string
 */
private $name;

方法

public function __construct( string $name, string $description = null );
Phalcon\Acl\Role 构造函数

public function __toString(): string;
public function getDescription(): string;
public function getName(): string;

Acl\RoleAwareInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

可在 allow 方法中作为 ROLE 使用的类的接口

方法

public function getRoleName(): string;
返回角色名称

Acl\RoleInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Acl
  • 使用

  • 继承

  • 实现

Phalcon\Acl\Role 的接口

方法

public function __toString(): string;
魔术方法 __toString

public function getDescription(): string;
返回角色描述

public function getName(): string;
返回角色名称

无噪 Logo
无噪文档
25 年 6 月翻译
文档源↗