承接 ac/kalinka 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

ac/kalinka

Composer 安装命令:

composer require ac/kalinka

包简介

Flexible authorization library for PHP

README 文档

README

Build Status

Kalinka helps you determine who's allowed to do what in your app.

API Documentation

This library is for authorization, not authentication. You'll need a different library for actually logging your users in and out, managing passwords, OpenID, and so on. Kalinka is for determining what actions are available to your users after they've logged in.

Installation

You can get Kalinka via composer:

"require": {
    ...
    "ac/kalinka": "dev-master"
}

Getting Started

Create a base abstract Guard class for your app. Guards are where you define your security policies. Policies are just boolean-retuning methods whose names start with "policy". Here is an example:

use AC\Kalinka\Guard\BaseGuard;

abstract class MyAppBaseGuard extends BaseGuard
{
    protected function policyAdmin($subject)
    {
        return $subject->isAdmin();
    }
}

The subject above is the user whose privileges are being checked. This can be an instance of your app's User class (as the code above assumes), or it can be something as simple as the name of the user as a string. The important thing is that it lets you find out everything you need about the user to determine what they're allowed to do.

MyAppBaseGuard is abstract because it doesn't define any actions. Actions are strings which represent various things the subject might want to do, and which you may or may not permit. For your more specific Guard classes, you'll need to provide a getActions method which returns an array:

class DocumentGuard extends MyAppBaseGuard
{
    public function getActions()
    {
        return ["read", "write"];
    }
}

Policies may also accept an object as their second argument, which is a specific resource that the subject is trying to get at. For example, suppose you have Documents that allow access based on whether or not the user owns that particular document, and/or whether the document is "unlocked":

class DocumentGuard extends MyAppBaseGuard
{
    public function getActions()
    {
        return ["read", "write"];
    }

    protected function policyDocumentOwner($subject, $object)
    {
        return ($object->getOwnerId() == $subject->getId());
    }

    protected function policyDocumentUnlocked($subject, $object)
    {
        return !($object->isLocked());
    }
}

When your app wants to do some actual access checks, these are done through an Authorizer. The Authorizer determines what Guard policies are applied in any given situation. You can reference the policies implemented in your Guards, as well as the default "allow" policy that simply always permits access.

If you don't define any policy for an action, it is always denied by default.

For most basic use cases, you can derive from SimpleAuthorizer:

use AC\Kalinka\Authorizer\SimpleAuthorizer;
use MyApp\Guards\CommentGuard;
use MyApp\Guards\DocumentGuard;

class MyAppAuthorizer extends SimpleAuthorizer
{
    public function __construct($subject)
    {
        parent::__construct($subject);

        $this->registerGuards([
            "document" => new DocumentGuard,
            "comment" => new CommentGuard,
            // ... and so on for all your protected resources
        ]);

        $this->registerPolicies([
            "document" => [
                "read" => "allow",
                "write" => "documentOwner"
            ],
            "comment" => [
                "read" => "allow",
                "create" => "allow",
                "delete" => "admin"
            ],
            // ...
        ]);
    }
}

Now after all that, we're ready to do authorization! Whenever you want to check if access to some resource is allowed, just create an instance of your Authorizer class and call the can method:

use MyApp\MyAppAuthorizer;

$auth = new MyAppAuthorizer($currentUser);
if ($auth->can("write", "document", $someDocument)) {
    $someDocument->setContent($newValue);
} else {
    print "Access denied!\n";
}

Combining Policies

Suppose that you only want to allow documents to be edited only if they are unlocked and the user owns them. This can be done by supplying a list of policies instead of a single string:

// ...
$this->registerPolicies([
    "document" => [
        "read" => "allow",
        "write" => [
            "documentUnlocked",
            "documentOwner"
        ]
    ],
    // ...
]);

Sometimes an action is permitted if any one of several different policies allows it, even if the others do not. Suppose that for the purposes of writing documents, being an admin is as good as being the document's owner, but the rule about the document being unlocked still applies to everyone. In that case, you can use an inner list:

// ...
$this->registerPolicies([
    "document" => [
        "read" => "allow",
        "write" => [
            "documentUnlocked",
            ["documentOwner", "admin"]
        ]
    ],
    // ...
]);

The principle here is that the outer list is AND-connected, while inner lists are OR-connected.

If you need something even more complicated than that, you could always implement it as its own policy. Policies can call each other with the checkPolicy() method on BaseGuard.

Roles

SimpleAuthorizer will only work for very straightforward setups, where the same policies apply to everyone. In real systems, it's more common that you have a bunch of different roles that people can belong to, each of which has access to resources under a variety of different circumstances. The easiest way to accomplish this is to extend from RoleAuthorizer, which provides a registerRolePolicies() method that replaces the functionality of SimpleAuthorizer's registerPolicies() method:

use AC\Kalinka\Authorizer\RoleAuthorizer;
use MyApp\Guards\CommentGuard;
use MyApp\Guards\DocumentGuard;

class MyAppAuthorizer extends RoleAuthorizer
{
    public function __construct(MyUserClass $user)
    {
        $roleNames = [];
        foreach ($user->getRoles() as $role) {
            $roleNames[] = $role->getName();
        }
        parent::__construct($user, $roleNames);

        $this->registerGuards([
            "document" => new DocumentGuard,
            "comment" => new CommentGuard,
            // ... and so on for all your protected resources
        ]);

        $this->registerRolePolicies([
            "guest" => [
                "document" => [
                    "read" => "allow",
                ],
                "comment" => [
                    "read" => "allow",
                ]
                // ...
            ],
            "customer" => [
                "document" => [
                    "read" => "allow",
                    "write" => [
                        "documentUnlocked",
                        "documentOwner"
                    ]
                ],
                "comment" => [
                    "read" => "allow",
                    "create" => "allow",
                ],
                // ...
            ],
            // ...
        ]);
    }
}

The roles are supplied as a list of strings. When a permissions check is made, each role is tried individually; if any role assigned to the user allows the action, then it is allowed overall.

This is a much more flexible solution than adding role-like policies to your Guards, as we did above with the policyAdmin() method of MyAppBaseGuard.

The RoleAuthorizer also provides a special role named 'superuser', which automatically allows all actions.

Partially Included Roles

You may sometimes have special situations where the desired permissions don't match up perfectly with your roles. For example, you might have a user who has all the rights of the "contributor" role, but also can act as an "administrator" when it comes to manipulating comments. You can handle this situation with your RoleAuthorizer derivative by using the registerRoleInclusions() method:

use AC\Kalinka\Authorizer\RoleAuthorizer;

class MyAppAuthorizer extends RoleAuthorizer
{
    public function __construct(MyUserClass $user)
    {
        // ...

        if ($user->isCommentAdmin()) {
            $this->registerRoleInclusions([
                "comment" => "administrator"
            ]);
        }
    }
}

It is also possible to include only particular actions from a role:

$this->registerRoleInclusions([
    "comment" => ["update" => "administrator", "delete" => "administrator"]
]);

These included sections are treated as though they were another role; access is permitted if any included policy lists approve it, or if any of the policy lists from the user's regular roles approve it.

ac/kalinka 适用场景与选型建议

ac/kalinka 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.31k 次下载、GitHub Stars 达 6, 最近一次更新时间为 2013 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「authorization」 「privilege」 「cancan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ac/kalinka 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ac/kalinka 我们能提供哪些服务?
定制开发 / 二次开发

基于 ac/kalinka 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.31k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 13
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 8
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-02-15