Exemple #1
0
 /**
  * Question management page.
  */
 public function action_index()
 {
     // Set page title
     $this->title(l('security_questions'));
     // Extract questions
     $questions = json_decode(settings('security_questions'), true);
     // Add an empty question
     if (!count($questions)) {
         $questions[] = array('question' => '', 'answers' => '');
     }
     // Check if the form has been submitted
     $errors = array();
     if (Request::method() == 'post') {
         // Process questions
         $updated_questions = array();
         foreach (Request::$post['questions'] as $id => $question) {
             // Check fields
             foreach ($question as $field => $value) {
                 if (empty($value)) {
                     $errors[$id][$field] = true;
                 }
             }
             // Add if no errors
             if (!isset($errors[$id])) {
                 $updated_questions[] = $question;
             }
         }
         // Save and redirect
         if (!count($errors)) {
             $this->db->update('settings')->set(array('value' => json_encode($updated_questions)))->where('setting', 'security_questions')->exec();
             Request::redirect(Request::requestUri());
         }
     }
     View::set(compact('questions', 'errors'));
 }
Exemple #2
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);
 }