So this is what I ultimately came up with:

function permissions(){
$id = $this->request->data['Permissions']['id'];
$fields = array();
foreach($this->request->data as $fieldName => $val){
if($fieldName != 'Permissions'){
array_push($fields, $fieldName);
}
}
foreach($fields as $field){
foreach($this->request->data[$field] as $key => $value){
if($value == 1){
$this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), 
array('model' => 'ControllerNode', 'foreign_key' => $key), 
strtolower($field));
}else{
$this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), 
array('model' => 'ControllerNode', 'foreign_key' => $key), 
strtolower($field));
}
}
}
$this->redirect(array('controller' => 'Subgroups', 'action' => 
'edit/'.$id));
}

Any suggestions on improvement would be cool, but this seems to work as 
expected. The whole Idea is to create a permissions control panel that lets 
me add actions to controllers without coding anything. Then I can assign 
permissions for each action per group/subgroup/user again without needing 
any code. Seems to work pretty well.

On Thursday, October 4, 2012 1:14:16 AM UTC-4, Chris Thoman wrote:
>
> Hi,
>
> Firstly, I'm new here so hopefully I'm posting this in the right area.
>
> I've created a form using cake's form helper. The form consists of 
> checkboxes for each child of a particular record found with a normal 
> find('all') call. Given that I know there are four children for each 
> parent, I've come up with the following code that works perfectly (at least 
> for my skill level it gets the job done lol).
>
> View code to generate form inputs (dynamically generates inputs per child, 
> not limited to 4 children):
>
>> <?php foreach($controllerNodes as $controllerNode): ?>
>>
>> <div class="wrapperFull">
>>
>> <div class="inputLabel fontSizeDefault fontWeightBold"><?php echo 
>>> $controllerNode['ControllerNode']['name']; ?></div>
>>
>> <div class="clearfix">
>>
>> <?php
>>
>> foreach($controllerNode['ActionNode'] as $key => $value){
>>
>> echo 
>>> $this->Form->input($value['name'].'.'.$controllerNode['ControllerNode']['id'],
>>>  
>>> array(
>>
>> 'type' => 'checkbox',
>>
>> 'after' => '<div class="inputCheckboxLabel fontSizeSmall 
>>> floatLeft">'.$value['name'].'</div>',
>>
>> ));
>>
>> }
>>
>> ?>
>>
>> </div>
>>
>> </div>
>>
>> <?php endforeach; ?>
>>
>>  
>  Controller code to save the 4 results I know always exist:
>
>> function permissions(){
>>
>> $id = $this->request->data['Permissions']['id'];
>>
>> if($this->request->is('post')){
>>
>> foreach($this->request->data['Create'] as $key => $value){
>>>
>> if($value == 1){
>>
>> $this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'create');
>>
>> }else{
>>
>> $this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'create');
>>
>> }
>>
>> }
>>
>> foreach($this->request->data['Read'] as $key => $value){
>>
>> if($value == 1){
>>
>> $this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'read');
>>
>> }else{
>>
>> $this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'read');
>>
>> }
>>
>> }
>>
>> foreach($this->request->data['Update'] as $key => $value){
>>
>> if($value == 1){
>>
>> $this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'update');
>>
>> }else{
>>
>> $this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'update');
>>
>> }
>>
>> }
>>
>> foreach($this->request->data['Delete'] as $key => $value){
>>
>> if($value == 1){
>>
>> $this->Acl->allow(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'delete');
>>
>> }else{
>>
>> $this->Acl->deny(array('model' => 'Subgroup', 'foreign_key' => $id), 
>>> array('model' => 'ControllerNode', 'foreign_key' => $key), 'delete');
>>
>> }
>>
>> }
>>
>> }
>>
>> }
>>
>>  
>  So my goal is to get the controller to generate the above foreach loops 
> dynamically based on the amount of $this->request->data coming in from the 
> form. I could always add another foreach loop for every new input I create 
> down the road, but I much prefer the idea of these loops being generated 
> based on the info from the database (which draws the form, which submits 
> the data).
>
> In all my searching, I've not figured out a way to know the ammount of 
> inputs submitted by the form although I'm sure there's probably an easy 
> answer staring right at me :o
>
> Thanks a lot!
>

-- 
Like Us on FacekBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

--- 
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
Visit this group at http://groups.google.com/group/cake-php?hl=en.


Reply via email to