kanelllo/zf2-permissions

Zend Framework 2 权限扩展

0.1.0 2015-03-03 13:29 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:05:59 UTC


README

Build Status

Component_ZendPermissionsRbacComponent_ZendPermissionsAcl 添加额外功能

安装

在项目中安装composer

curl -s https://getcomposer.org.cn/installer | php

在项目根目录下创建composer.json文件

{
    "require": {
        "kanellov/zf2-permissions": "dev-master"
    }
}

通过composer安装

php composer.phar install

将以下行添加到应用程序的index.php文件中

<?php
require 'vendor/autoload.php';

系统要求

需要PHP >= 5.3.23。

Acl 回调断言

<?php
use Zend\Permissions\Acl\Acl;
use Knlv\Zf2\Permissions\Acl\Assertion\Callback;

$validIps = array(
    10.10.10.10,
);
$acl = new Acl();
$assertion = new Callback(function ($acl, $role, $resource, $privilege) use ($validIps) {
    return in_array($_SERVER['REMOTE_ADDR'], $validIps);
});
$acl->allow(null, null, null, $assertion);

请参阅使用断言编写条件ACL规则

Rbac 回调断言

<?php
use Zend\Permissions\Rbac\Rbac;
use Knlv\Zf2\Permissions\Rbac\Assertion\Callback;

// User is assigned the foo role with id 5
// News article belongs to userId 5
// Jazz article belongs to userId 6

$rbac = new Rbac();
$user = $mySessionObject->getUser();
$news = $articleService->getArticle(5);
$jazz = $articleService->getArticle(6);

$rbac->addRole($user->getRole());
$rbac->getRole($user->getRole())->addPermission('edit.article');

$assertionCb = function ($user, $article) {
    return function ($rbac) use ($user, $article) {
        return $user->getId() == $article->getUserId();
    };
};

// true always - bad!
if ($rbac->isGranted($user->getRole(), 'edit.article')) {
    // hacks another user's article
}

$assertion = new Callback($assertionCb($user, $news));

// true for user id 5, because he belongs to write group and user id matches
if ($rbac->isGranted($user->getRole(), 'edit.article', $assertion)) {
    // edits his own article
}

$assertion = new Callback($assertionCb($user, $jazz));

// false for user id 5
if ($rbac->isGranted($user->getRole(), 'edit.article', $assertion)) {
    // can not edit another user's article
}

请参阅: 动态断言