定义 ACO
现在 Tor 已经定义了自己的 ARO,应当识别并定义 ACO 了。在本例中,将定义 ACO 来表示产品,并将 ACO 组织为组,就像为 ARO 所做的那样。
在 products 控制器中添加 ACO 定义
将在 products 控制器的 add 函数中添加最初的 ACO 定义,类似于在用户注册中处理 ARO 定义所做的操作。add 函数很快就会完全具有 Bake 所提供的功能。它应当类似于清单 13。
清单 13. add 函数
function add() {
if (!empty($this->data)) {
$this->cleanUpFields();
$this->Product->create();
if ($this->Product->save($this->data)) {
$this->Session->setFlash('The Product
has been saved');
$this->redirect(array('action'=>'index'),
null, true);
} else {
$this->Session->setFlash('The Product
could not be
saved. Please, try again.');
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
}
}
$dealers = $this->Product->Dealer->generateList();
$this->set(compact('dealers'));
}
|
CakePHP 同样使得为 ACO 添加定义十分简单。首先,在控制器中添加 $components 类变量,正如您对 users 控制器所做的操作。
清单 14. 在控制器中添加 $components 类变量
<?php
class ProductsController extends AppController
{
var $components = array('Acl');
...
|
创建 ACO 的过程几乎与创建 ARO 的过程完全一样。调用 ACL 的 ACO 对象上的 create 方法。这一次,别名仅仅使用产品的标题是不够的,因为产品标题可能不是惟一的。可以使用产品 ID 和产品标题的组合作为别名。模型是 Product,foreign_key 是新产品的 ID,parent_id 是插入此产品的经销商的 ID(目前还没有设置这个 ID,稍后完成)。将这些整合到 add 函数中,它应当类似于清单 15。
清单 15. 新的 add 函数
function add() {
if (!empty($this->data)) {
$this->cleanUpFields();
$this->Product->create();
if ($this->Product->save($this->data)) {
$dealer = $this->Product->Dealer->read(null,
$this->data['Product']['dealer_id']);
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
$parent = $this->Acl->Aco->findByAlias($dealer
['Dealer']['title']);
$this->Acl->Aco->create(array(
'alias' =>
$this->Product->id.'-'.$this->data['Product']['title'],
'model' => 'Product',
'foreign_key' => $this->Product->id,
'parent_id' => $parent['Aco']['id'])
);
$this->Acl->Aco->save();
$this->Session->setFlash('The Product
has been saved');
$this->redirect(array('action'=>'index'),
null, true);
} else {
$this->Session->setFlash('The Product could not
be saved. Please, try again.');
|-------10--------20--------30--------40--------50--------60--------70--------80--------9|
|-------- XML error: The previous line is longer than the max of 90 characters ---------|
}
}
$dealers = $this->Product->Dealer->generateList();
$this->set(compact('dealers'));
}
|
以上就是在 Tor 中为创建的产品自动创建 ACO 所需的全部操作。在继续执行操作之前,应当为现有产品和组创建 ACO。
为经销商添加 ACO 定义
可以使用 Cake Console 定义 ACO,方法与为现有用户定义 ARO 的方法几乎一样。访问 CakePHP 在 http://localhost/products 上生成的产品列表会有帮助。
再次通过命令行在 /webroot/app 目录中运行几个 create 命令。首先,创建组来表示在创建 dealers 表时创建的经销商。但是这一次,指定要创建 ACO。
../cake/console/cake acl create aco root "Tor Johnson School Of Drama"
../cake/console/cake acl create aco root "Chriswell's Psychic Friends"
|
可以运行 ../cake/console/cake acl view aco 来检验组是否符合预期。
图 11. 对于没有产品的经销商的 ACO 输出
接下来,从 products 表中删除现有产品。访问产品索引(http://localhost/products/index)并单击每个产品旁边的 Delete,就能够完成此操作。
由于到目前为止只创建了几个产品,因此删除并重新创建它们是添加 ACO 最简单的方法。请先不要测试新产品的 add 函数。现在已经为现有经销商创建了 ACO 并已删除了现有产品,下面要设置一些权限。
|