/**
  * Tests User_Model::custom_validate
  *
  * @test
  * @dataProvider provider_custom_validate
  */
 public function test_custom_validate($valid, $invalid)
 {
     // set up mock, for prevent_superadmin_modification
     $auth = $this->getMock('Auth', array('logged_in'));
     $auth->expects($this->exactly(2))->method('logged_in')->with($this->equalTo('superadmin'))->will($this->returnValue(True));
     // Save initial data
     $initial_valid = $valid;
     $initial_invalid = $invalid;
     // Test with valid data
     $response = User_Model::custom_validate($valid, $auth);
     $this->assertEquals(TRUE, $valid instanceof Validation);
     $this->assertTrue($response, Kohana::debug($valid->errors()));
     // Test with invalid data
     $response = User_Model::custom_validate($invalid, $auth);
     $this->assertEquals(TRUE, $invalid instanceof Validation);
     $this->assertFalse($response);
     // restore valid, invalid
     $valid = $initial_valid;
     $invalid = $initial_invalid;
     // Test modification to superadmin as admin
     $auth = $this->getMock('Auth', array('logged_in'));
     $auth->expects($this->once())->method('logged_in')->with($this->equalTo('superadmin'))->will($this->returnValue(False));
     $response = User_Model::custom_validate($valid, $auth);
     $this->assertTrue($valid instanceof Validation);
     $this->assertFalse($response, Kohana::debug($valid->errors()));
 }
Esempio n. 2
0
 /**
  * Tests User_Model::custom_validate
  *
  * @test
  * @dataProvider provider_custom_validate
  */
 public function test_custom_validate($valid, $invalid)
 {
     // Test with valid data
     $response = User_Model::custom_validate($valid);
     $this->assertEquals(TRUE, $valid instanceof Validation);
     $this->assertTrue($response, Kohana::debug($valid->errors()));
     // Test with invalid data
     $response = User_Model::custom_validate($invalid);
     $this->assertEquals(TRUE, $invalid instanceof Validation);
     $this->assertFalse($response);
 }
Esempio n. 3
0
 /**
  * Edit a user
  * @param bool|int $user_id The id no. of the user
  * @param bool|string $saved
  */
 public function edit($user_id = FALSE, $saved = FALSE)
 {
     $this->template->content = new View('admin/users/edit');
     if ($user_id) {
         $user_exists = ORM::factory('user')->find($user_id);
         if (!$user_exists->loaded) {
             // Redirect
             url::redirect(url::site() . 'admin/users/');
         }
     }
     // Setup and initialize form field names
     $form = array('username' => '', 'name' => '', 'email' => '', 'password' => '', 'notify' => '', 'role' => '');
     $this->template->content->user_id = $user_id;
     if ($user_id == FALSE) {
         // Tack this on when adding a new user
         $form['password'] = '';
         $form['password_again'] = '';
     }
     // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $user = "";
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Get the submitted data
         $post = $_POST;
         // Add the user_id to the $_POST data
         $user_id = $user_id ? $user_id : NULL;
         $post = array_merge($post, array('user_id' => $user_id));
         if (User_Model::custom_validate($post)) {
             $user = ORM::factory('user', $user_id);
             $user->name = $post->name;
             $user->email = $post->email;
             $user->notify = $post->notify;
             if ($user_id == NULL) {
                 $user->password = $post->password;
             }
             // We can only set a new password if we are using the standard ORM method,
             //    otherwise it won't actually change the password used for authentication
             if (isset($post->new_password) and Kohana::config('riverid.enable') == FALSE and strlen($post->new_password) > 0) {
                 $user->password = $post->new_password;
             }
             // Existing User??
             if ($user->loaded) {
                 // Prevent modification of the main admin account username or role
                 if ($user->id != 1) {
                     $user->username = $post->username;
                     // Remove Old Roles
                     foreach ($user->roles as $role) {
                         $user->remove($role);
                     }
                     // Add New Roles
                     if ($post->role != 'none') {
                         $user->add(ORM::factory('role', 'login'));
                         $user->add(ORM::factory('role', $post->role));
                     }
                 }
             } else {
                 $user->username = $post->username;
                 // Add New Roles
                 if ($post->role != 'none') {
                     $user->add(ORM::factory('role', 'login'));
                     $user->add(ORM::factory('role', $post->role));
                 }
             }
             $user->save();
             //Event for adding user admin details
             Event::run('ushahidi_action.users_add_admin', $post);
             Event::run('ushahidi_action.user_edit', $user);
             // Redirect
             url::redirect(url::site() . 'admin/users/');
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('auth'));
             $form_error = TRUE;
         }
     } else {
         if ($user_id) {
             // Retrieve Current Incident
             $user = ORM::factory('user', $user_id);
             if ($user->loaded) {
                 // Some users don't have roles so we have this "none" role
                 $role = 'none';
                 foreach ($user->roles as $user_role) {
                     $role = $user_role->name;
                 }
                 $form = array('user_id' => $user->id, 'username' => $user->username, 'name' => $user->name, 'email' => $user->email, 'notify' => $user->notify, 'role' => $role);
             }
         }
     }
     $roles = ORM::factory('role')->where('id != 1')->orderby('name', 'asc')->find_all();
     foreach ($roles as $role) {
         $role_array[$role->name] = utf8::strtoupper($role->name);
     }
     // Add one additional role for users with no role
     $role_array['none'] = utf8::strtoupper(Kohana::lang('ui_main.none'));
     $this->template->content->id = $user_id;
     $this->template->content->display_roles = $this->display_roles;
     $this->template->content->user = $user;
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->yesno_array = array('1' => utf8::strtoupper(Kohana::lang('ui_main.yes')), '0' => utf8::strtoupper(Kohana::lang('ui_main.no')));
     $this->template->content->role_array = $role_array;
 }
Esempio n. 4
0
 /**
  * Edit a user
  * @param bool|int $user_id The id no. of the user
  * @param bool|string $saved
  */
 public function edit($user_id = FALSE, $saved = FALSE)
 {
     $this->template->content = new View('admin/users_edit');
     if ($user_id) {
         $user_exists = ORM::factory('user')->find($user_id);
         if (!$user_exists->loaded) {
             // Redirect
             url::redirect(url::site() . 'admin/users/');
         }
     }
     // Setup and initialize form field names
     $form = array('username' => '', 'password' => '', 'password_again' => '', 'name' => '', 'email' => '', 'notify' => '', 'role' => '');
     // Copy the form as errors, so the errors will be stored with keys corresponding to the form field names
     $errors = $form;
     $form_error = FALSE;
     $form_saved = FALSE;
     $form_action = "";
     $user = "";
     // check, has the form been submitted, if so, setup validation
     if ($_POST) {
         // Get the submitted data
         $post = $_POST;
         // Add the user_id to the $_POST data
         $user_id = $user_id ? $user_id : NULL;
         $post = array_merge($post, array('user_id' => $user_id));
         if (User_Model::custom_validate($post)) {
             $user = ORM::factory('user', $user_id);
             $user->name = $post->name;
             $user->email = $post->email;
             $user->notify = $post->notify;
             // Existing User??
             if ($user->loaded == true) {
                 // Prevent modification of the main admin account username or role
                 if ($user->id != 1) {
                     $user->username = $post->username;
                     // Remove Old Roles
                     foreach ($user->roles as $role) {
                         $user->remove($role);
                     }
                     // Add New Roles
                     $user->add(ORM::factory('role', 'login'));
                     $user->add(ORM::factory('role', $post->role));
                 }
                 $post->password != '' ? $user->password = $post->password : '';
             } else {
                 $user->username = $post->username;
                 $user->password = $post->password;
                 // Add New Roles
                 $user->add(ORM::factory('role', 'login'));
                 $user->add(ORM::factory('role', $post->role));
             }
             $user->save();
             //Event for adding user admin details
             Event::run('ushahidi_action.users_add_admin', $post);
             Event::run('ushahidi_action.user_edit', $user);
             // Redirect
             url::redirect(url::site() . 'admin/users/');
         } else {
             // repopulate the form fields
             $form = arr::overwrite($form, $post->as_array());
             // populate the error fields, if any
             $errors = arr::overwrite($errors, $post->errors('auth'));
             $form_error = TRUE;
         }
     } else {
         if ($user_id) {
             // Retrieve Current Incident
             $user = ORM::factory('user', $user_id);
             if ($user->loaded == true) {
                 foreach ($user->roles as $user_role) {
                     $role = $user_role->name;
                 }
                 $form = array('user_id' => $user->id, 'username' => $user->username, 'password' => '', 'password_again' => '', 'name' => $user->name, 'email' => $user->email, 'notify' => $user->notify, 'role' => $role);
             }
         }
     }
     $roles = ORM::factory('role')->where('id != 1')->orderby('name', 'asc')->find_all();
     $role_array = array("login" => "NONE");
     foreach ($roles as $role) {
         $role_array[$role->name] = strtoupper($role->name);
     }
     $this->template->content->id = $user_id;
     $this->template->content->display_roles = $this->display_roles;
     $this->template->content->user = $user;
     $this->template->content->form = $form;
     $this->template->content->errors = $errors;
     $this->template->content->form_error = $form_error;
     $this->template->content->form_saved = $form_saved;
     $this->template->content->yesno_array = array('1' => strtoupper(Kohana::lang('ui_main.yes')), '0' => strtoupper(Kohana::lang('ui_main.no')));
     $this->template->content->role_array = $role_array;
 }