【问题标题】:Zend ACL and access to specific objectsZend ACL 和对特定对象的访问
【发布时间】:2014-09-07 13:19:54
【问题描述】:

我的应用层需要一个 ACL,我一直在研究 Zend ACL,它似乎可以满足我的需求,但我对以下 [1] 感到困惑:

例如,如果要将默认规则应用于 城市,只需将规则分配给城市,而不是 为每个建筑物分配相同的规则。一些建筑物可能需要 然而,这种规则的例外,这可以在 Zend\Permissions\Acl\Acl 通过将此类例外规则分配给每个 需要此类异常的构建。

这很棒。正是我需要的。但是,我该如何实现呢?

通过阅读 Zend ACL 的文档,我实际上找不到这样的示例。所以,假设我有一个CityBuilding 资源,每个资源都实现了ResourceInterface。像这样的:

class City implements ResourceInterface {

   public function getResourceId()
   {
      return "city"; // Is this supposed to be the "type" or the "unique Id"?
   }

   public $buildings = array();

}

class Building implements ResourceInterface {

   public function getResourceId()
   {
      return "building"; // Again, the "type" or "unique Id"?
   }

}

正如上面代码中的 cmets 可能已经说明的那样,资源 ID 是什么?它是否代表资源的“类型”,即这是一个城市或建筑物,还是需要一个唯一的 Id,即“city-1”等?

如果答案是它需要是“类型”,那么问题就变成了;如何指定独特的建筑物?但是,如果答案是 ID 必须是唯一的,那么问题就变成了;我如何识别资源的“类型”和每个建筑物的“一揽子”许可,如从文档中引用的内容中所述。

任何见解将不胜感激。

[1]http://zf2.readthedocs.org/en/latest/modules/zend.permissions.acl.intro.html

【问题讨论】:

标签: php zend-framework2 zend-acl


【解决方案1】:

resource Id 必须是唯一值。对于分配全局规则,您需要对资源使用继承。很简单,当您将资源添加到 acl 时,您需要将 city 资源作为 building 资源的父级传递。

这是一个示例:

$acl = new Acl();

//the original Acl resource class takes a `resourceId` as constructor parameter
$mycity1 = new Resource('mycity1');
$acl->addResource($mycity1);

$mybuiding1 = new Resource('mybulding1');
//city is the buildings parent
$acl->addResource($mybuiding1,$mycity1);

//you dont even have to create a class just define the resource as string
$acl->addResource('secure_buildings',$mycity1);

$acl->addRole('myrole1');
//roles have inheritance too
$acl->addRole('myrole2','myrole1');

//myrole1 and myrole2 has access to city and all its building
$acl->allow('myrole1','mycity1');
//myrole2 has access to city and all its building except 'secure_buildings'
$acl->deny('myrole2','secure_buildings');

子资源bulding 继承父资源city 的规则(如果没有为其定义)。

更新评论:

ACL 不知道也不关心您拥有什么资源类型,只要它们具有唯一的资源 id,acl 威胁所有资源平等并且只寻找resourceId 和继承。

当您定义规则时,您只需为 allowdeny 提供 resourceId ,只要它们被定义为资源并添加到 ACL 中,它们是什么类型并不重要堆栈。

当您执行$acl->inAllowed 时,您只需要roleIdresourceId 并且acl 并不关心它们的类型,只是它们已被定义为它们是否具有父级的资源abd。 .

示例:我希望这是足够的示例

$acl = new Acl();
$acl->addResource('City'); //all the cities
$acl->addResource('myCity1', 'City'); //city1 inherits City
$acl->addResource('Building', 'City'); //all the buildings in all the cities
$acl->addResource('normal_buildings', 'Building');
$acl->addResource('secure_buildings', 'Building');
$acl->addResource('top_secure_buildings', 'secure_buildings');

$acl->addRole('Civilian');
$acl->addRole('High_Level_Security', 'Civilian');

$acl->allow('Civilian', 'City');
$acl->deny('Civilian', 'secure_buildings');

$acl->allow('High_Level_Security', 'secure_buildings');
$acl->deny('High_Level_Security', 'top_secure_buildings');

var_dump($acl->isAllowed('Civilian', 'City'));//true  -> direct allow rule
var_dump($acl->isAllowed('Civilian', 'myCity1'));//true  -> inherited from City allow rule
var_dump($acl->isAllowed('Civilian', 'Building'));//true  -> inherited from City allow rule
var_dump($acl->isAllowed('Civilian', 'normal_buildings'));//true  -> inherited from City allow rule
var_dump($acl->isAllowed('Civilian', 'secure_buildings'));//false  -> direct deny rule
var_dump($acl->isAllowed('Civilian', 'top_secure_buildings'));//false   -> inherited from secure_building deny rule

var_dump($acl->isAllowed('High_Level_Security', 'City'));//true  -> inherited from Civilian->City allow rule
var_dump($acl->isAllowed('High_Level_Security', 'myCity1'));//true  -> inherited from Civilian->City allow rule
var_dump($acl->isAllowed('High_Level_Security', 'Building'));//true  -> inherited from Civilian->City allow rule
var_dump($acl->isAllowed('High_Level_Security', 'normal_buildings'));//true  -> inherited from Civilian->City allow rule
var_dump($acl->isAllowed('High_Level_Security', 'secure_buildings'));//true  -> direct allow rule
var_dump($acl->isAllowed('High_Level_Security', 'top_secure_buildings'));//false  -> direct deny rule

【讨论】:

  • 用示例更新了答案,您意识到 Zend 是开源的,您实际上可以检查源代码以了解发生了什么?正确的 ?如果您检查代码,您会看到 acl 没有给出 **** 资源是什么类型,如果资源没有直接规则,它会检查父级等等...
  • 您的最后一次更新现在完全有意义了。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2016-06-13
  • 2017-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-02
  • 2016-08-09
  • 2012-08-23
相关资源
最近更新 更多