views:

120

answers:

1

I'm building a controller to manage group based ACL in CakePHP and when I create or edit a group I want to be able to select what permissions it has. The group data table only stores a group ID and a group Name as the permissions are stored in the ACO/ARO table.

I have an array from the ACO that I want to turn into a set of checkboxes so you can check them to allow access from that group to that ACO. So first off, how do I turn this array into a set of checkboxes. The array looks like this:

array(
    [0] => array(
        [Aco] => array(
            [alias] => 'alias'
            [id] => 1)
        [children] => array (
            [0] => array(
                [Aco]=>
            ...etc  
    ))
    [1] => array(
        ...etc
)

My next question is how can I check these once the form has been submitted to the controller to allow the selected actions?

[Update] Ok changing the angle of my question, how can I use the Form helper to create forms that are not based on any Model?

A: 

You may want to check PoundCake Control Panel (for CakePHP 1.2) - take a look at the screenshots. The functionality you want is present there but radio inputs are used instead of checkboxes. Users and Groups are linked to ACOs. So, changing Users or Groups tree structure you change the tree structure of ACOs. Menu items are linked to AROs. So, changing the tree structure of menus you change the tree structure of AROs.

For example, in groups_controller.php:

public function permissions($id = null){
    $this->_checkIdPresence($id);
    if(!empty($this->data)){
        $aroNode = array('model' => 'Group', 'foreign_key' => $this->data['Group']['id']);
        foreach($this->data['Acos'] as $aco){
            if(!empty($aco['permission'])){
                $do = '';
                if($aco['permission'] == '1'){
                    $do = 'allow';
                }elseif($aco['permission'] == '-1'){
                    $do = 'deny';
                }
                $acoNode = array('model' => $aco['model'], 'foreign_key' => $aco['foreign_key']);
                if($this->Acl->{$do}($aroNode, $acoNode, '*')){
                    $message = sprintf(__('<i>%s</i> permission to <i>%s</i> has been saved.', true),ucfirst($do), $aco['name']);
                    $this->_flash($message, 'success');
                }else{
                    $message = sprintf(
                        __('<i>%s</i> permission to <i>%s</i> could not be saved. Please, try again.', true),
                        ucfirst($do), $aco['name']
                    );
                    $this->_flash($message, 'error');
                }
            }
        }
    }
    // Get ARO for the current Group and all related ACOs from the db
    $this->data = $this->Group->read(null, $id);
    $acosTree = $this->ControlPanel->checkPermissions($id);
    $existingPermissions = $this->ControlPanel->findRecordedPermissions($id);
    $this->set(compact('acosTree', 'existingPermissions'));
}
bancer