Defining ACOs
Now that Tor has its AROs defined, it's time to identify and define
your ACO. In this case, you're going to define ACOs to represent
products, organizing the ACOs into groups as you did for your
AROs.
Adding ACO definition to the
products controller
You are going to add the initial ACO definition to the products
controller in the add function, similar to
what you did with ARO definition in user registration. Right now, the
add function is exactly what Bake gave you.
It should look something like Listing 13.
Listing 13. The add function
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', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash('The Product could not be
saved. Please, try again.', true));
}
}
$dealers = $this->Product->Dealer->find('list');
$this->set(compact('dealers'));
}
|
Once again, CakePHP makes adding the definition for your ACOs very
simple. You start by adding the $components
class variable to the controller, like you did for the users
controller.
Listing 14. Adding $components class variable to the controller
<?php
class ProductsController extends AppController
{
var $components = array('Acl');
...
|
Creating an ACO looks almost exactly like creating an ARO. You call the
create method on the ACL's
ACO object. This time, the alias needs to
be more than just the product's title, since that may not be unique.
Instead, you can use a combination of the product's ID and the
product's title for the alias. The model will be Product, the
foreign_key will be the new product's ID,
and the parent_id will be the ID of the
dealer who inserted the product (you haven't set this up yet, but you
will shortly). Putting these pieces into your
add function, it should look something like
Listing 15.
Listing 15. New add function
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']);
$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.');
}
}
$dealers = $this->Product->Dealer->generateList();
$this->set(compact('dealers'));
}
|
That should be all you need to auto-create the ACOs for the products
created in Tor. Before you continue, you should create ACOs for the
existing products and groups.
Adding ACO definitions for the
dealers
The Cake Console can be used to define ACOs in much the same way it was
used to define the AROs for existing users. It will be helpful to pull
up that products list that CakePHP baked for you at
http://localhost/products.
Once again, from the command line, in the /webroot/app directory, you
will run some create commands. Start by
creating groups to represent the dealers you created way back when you
created the dealer table. But this time, specify that you are creating
an ACO.
../cake/console/cake acl create aco root "Tor Johnson School Of Drama"
../cake/console/cake acl create aco root "Chriswell's Psychic Friends"
|
You can run
../cake/console/cake acl view aco to verify
that the groups look as expected.
Figure 11. ACO dump with dealers no
products
Next, delete the existing products from the products table. You should
be able to do this by going to the products index
(http://localhost/products/index) and clicking Delete next to
each product.
Because you have only created a couple of products thus far, recreating
them is the shortest path to adding the ACOs you want. Don't test out
that new product add function just yet. Now
that you have ACOs created for your existing dealers and you've
deleted the existing products, you're ready to proceed with setting up
some permissions.
|