Exemplo n.º 1
0
 /**
  * Handles the permissions listing and saving...
  *
  * Nice sexy DRY code right here, eh?
  */
 public function action_index($type)
 {
     // If the type of permissions is 'groups', set it to 'usergroups'.
     $type = $type == 'groups' ? 'usergroup' : 'role';
     // Has the form been submitted?
     if (Request::method() == 'post') {
         $global_defaults = Permission::defaults(0, 0, $type);
         // Loop over group/role and get id and permissions
         foreach (Request::$post['perm'] as $type_id => $permissions) {
             // Loop over permissions for id and value
             foreach ($permissions as $permission_id => $value) {
                 // Fetch permission
                 $perm = Permission::find($permission_id);
                 // Are we dealing with a default?
                 if ($type_id == 0) {
                     // Does it exist?
                     if ($perm->project_id > 0) {
                         // We we need to delete it?
                         if ($global_defaults[$perm->action]->value == $value) {
                             $perm->delete();
                         } elseif ($perm->value != $value) {
                             $perm->set('value', $value);
                             $perm->save();
                         }
                     } else {
                         // Should we create it?
                         if ($perm->value != $value) {
                             // Create the permission
                             $perm = new Permission(array('project_id' => $this->project->id, 'type' => $type, 'type_id' => $type_id, 'action' => $perm->action, 'value' => $value));
                             $perm->save();
                         }
                     }
                 } elseif ($perm and $perm->type_id == $type_id and $value == -1 and $type_id > 0) {
                     $perm->delete();
                 } elseif ($value == 0 or $value == 1) {
                     // Update
                     if ($perm and $perm->type_id == $type_id) {
                         $perm->value = $value;
                         $perm->save();
                     } else {
                         $perm = new Permission(array('project_id' => $this->project->id, 'type' => $type, 'type_id' => $type_id, 'action' => $perm->action, 'value' => $value));
                         $perm->save();
                     }
                 }
             }
         }
         Request::redirect(Request::requestUri());
     }
     // Setup the page
     $this->permissions_for($type);
 }