/** * Returns the logged user. * * @param boolean $reset * If TREU fetches the userdata from the database. (default FALSE) * To increase performance once the user is fetched it is stored in a * static variable. * * @return mixed * User entity if there's a logged user, FALSE otherwise */ function current_user($reset = FALSE) { static $current_user; if (!isset($current_user) || $reset) { $CI = get_instance(); $uid = $CI->session->userdata('user_uid'); if ($uid !== FALSE) { // There is a logged user. $current_user = $CI->user_model->get($uid); if ($current_user && $current_user->is_active()) { // Logged user found. Set logged and return. $current_user->set_logged(); return $current_user; } elseif ($current_user && !$current_user->is_active()) { // The user is no longer active. // Kill session and redirect to login. $CI->session->sess_destroy(); redirect('login'); } } $current_user = User_entity::build(array()); $current_user->set_logged(FALSE); } return $current_user; }
/** * Setup the base status of the application. * These are not fixtures. This is needed data for the app * to work. */ public function live_setup() { if (ENVIRONMENT != 'development') { show_error('Not allowed. Only available during development'); } $this->_tear_down(); // Create needed folders. if (!is_dir('files/surveys')) { mkdir('files/surveys', 0777, TRUE); } if (!is_dir('files/survey_results')) { mkdir('files/survey_results', 0777, TRUE); } // Admin user $admin = new User_entity(array('email' => '*****@*****.**', 'name' => 'Admin', 'username' => 'admin', 'author' => 0)); $admin->set_password(hash_password('admin'))->set_status(User_entity::STATUS_ACTIVE)->set_roles(array(ROLE_ADMINISTRATOR)); $this->user_model->save($admin); // Database indexes. $this->mongo_db->addIndex('call_tasks', array('ctid' => 'asc')); redirect('/login'); }
public function test_set_methods() { // Some values can be set in the constructor. $userdata = array('name' => 'A new test user', 'username' => 'new_test_user', 'email' => '*****@*****.**'); $user = new User_entity($userdata); // Must be converted to int before sending to DB. $user->set_status("2"); $user->set_password('password'); $this->assertInternalType('int', $user->status); $this->assertEquals('password', $user->password); $user->set_roles(array('role1', 'role2')); $this->assertEquals(array('role1', 'role2'), $user->roles); $user->set_roles(array('role1', 'role2', 'role2')); $this->assertEquals(array('role1', 'role2'), $user->roles); $user->set_roles('not_a_role'); $this->assertEmpty($user->roles); $user->set_roles(NULL); $this->assertEmpty($user->roles); }
/** * Saves a user to the database. * If the user is not saved yet, its uid will be added to the * user_entity. * @param User_entity (by reference) * * @return boolean * Whether or not the save was successful. */ public function save(User_entity &$entity) { // To ensure date consistency. $date = Mongo_db::date(); // Set update date: $entity->updated = $date; if ($entity->author === NULL) { $entity->author = current_user()->uid; } $prepared_data = array(); foreach ($entity as $field_name => $field_value) { $prepared_data[$field_name] = $field_value; } if ($entity->is_new()) { // Add new properties. $entity->uid = increment_counter(self::COUNTER_COLLECTION); $entity->created = clone $date; // Add properties to prepared_data. $prepared_data['uid'] = $entity->uid; $prepared_data['created'] = $entity->created; $result = $this->mongo_db->insert(self::COLLECTION, $prepared_data); return $result !== FALSE ? TRUE : FALSE; } else { $result = $this->mongo_db->set($prepared_data)->where('uid', $entity->uid)->update(self::COLLECTION, array('upsert' => TRUE)); return $result !== FALSE ? TRUE : FALSE; } }
public function test_api_survey_with_status_restrictions() { // Here we are testing all the API but only for status restrictions. // Every other test case should be tested elsewhere. // Cleanup self::$CI->mongo_db->dropCollection('aw_datacollection_test', 'surveys'); self::$CI->mongo_db->dropCollection('aw_datacollection_test', 'call_tasks'); $this->_reset_status_restrictions(); // Shorter statuses. $draft = Survey_entity::STATUS_DRAFT; $open = Survey_entity::STATUS_OPEN; $closed = Survey_entity::STATUS_CLOSED; $canceled = Survey_entity::STATUS_CANCELED; // Login user $this->_change_user(9903); ///////////////////////////////////////////////////////////////// // Set actions to be allowed only in Draft status. $mock_config = self::$status_resctriction_config; $mock_config['enketo collect data'] = array(Survey_entity::STATUS_DRAFT); $mock_config['enketo testrun'] = array(Survey_entity::STATUS_DRAFT); $this->_set_status_restrictions($mock_config); // Logged user is 9903 // User is agent. // Create survey. // Status open. // Valid xml file. // User is assigned to survey. $survey = Survey_entity::build(array('sid' => 1, 'status' => Survey_entity::STATUS_OPEN, 'files' => array('xml' => 'valid_survey.xml'), 'agents' => array(9903))); self::$CI->survey_model->save($survey); // Create call task self::$CI->mongo_db->insert('call_tasks', array('ctid' => 1001, 'number' => "1100500000000", 'created' => Mongo_db::date(), 'updated' => Mongo_db::date(), 'assigned' => Mongo_db::date(), 'author' => 1, 'assignee_uid' => 9903, 'survey_sid' => 1, 'activity' => array())); self::$CI->api_survey_xslt_transform(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']); $this->assertArrayHasKey('xml_form', $result); self::$CI->api_survey_request_respondents(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']); // User assigned to call task. // Call task is assigned to survey. // User is assigned to survey. // Survey is the one data is being submitted for. $_POST = array('csrf_aw_datacollection' => self::$CI->security->get_csrf_hash(), 'respondent' => array('ctid' => 1001, 'form_data' => '<valid><tag/></valid>')); self::$CI->api_survey_enketo_form_submit(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']); ///////////////////////////////////////////////////////////////// // Test again with correct status restrictions. $mock_config = self::$status_resctriction_config; $mock_config['enketo collect data'] = array(Survey_entity::STATUS_OPEN); $mock_config['enketo testrun'] = array(Survey_entity::STATUS_OPEN); $this->_set_status_restrictions($mock_config); self::$CI->api_survey_xslt_transform(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']); $this->assertArrayHasKey('xml_form', $result); self::$CI->api_survey_request_respondents(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']); // User assigned to call task. // Call task is assigned to survey. // User is assigned to survey. // Survey is the one data is being submitted for. $_POST = array('csrf_aw_datacollection' => self::$CI->security->get_csrf_hash(), 'respondent' => array('ctid' => 1001, 'form_data' => '<valid><tag/></valid>')); self::$CI->api_survey_enketo_form_submit(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']); ///////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////// // To test the manage agents api we need an admin. $this->_change_user(9901); // Logged user 9901. // User is administrator. // Create survey. // Status open. // Valid xml file. $survey = Survey_entity::build(array('sid' => 2, 'status' => Survey_entity::STATUS_OPEN, 'files' => array('xml' => 'valid_survey.xml'), 'agents' => array())); self::$CI->survey_model->save($survey); // Create new agent. // Absolute minimum properties for the test. $user_agent = User_entity::build(array('uid' => 8801, 'status' => User_entity::STATUS_ACTIVE, 'roles' => array(ROLE_CC_AGENT))); self::$CI->user_model->save($user_agent); // Set conditions. $mock_config = self::$status_resctriction_config; $mock_config['manage agents'] = array(Survey_entity::STATUS_DRAFT); $this->_set_status_restrictions($mock_config); // User is an agent. // Action assign $_POST = array('uid' => 8801, 'action' => 'assign', 'csrf_aw_datacollection' => self::$CI->security->get_csrf_hash()); self::$CI->api_survey_manage_agents(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 403, 'message' => 'Not allowed.'), $result['status']); ///////////////////////////////////////////////////////////////// // Set conditions. $mock_config = self::$status_resctriction_config; $mock_config['manage agents'] = array(Survey_entity::STATUS_OPEN); $this->_set_status_restrictions($mock_config); // User is an agent. // Action assign $_POST = array('uid' => 8801, 'action' => 'assign', 'csrf_aw_datacollection' => self::$CI->security->get_csrf_hash()); self::$CI->api_survey_manage_agents(1); $result = json_decode(self::$CI->output->get_output(), TRUE); $this->assertEquals(array('code' => 200, 'message' => 'Ok!'), $result['status']); }
/** * Creates User_entity injecting dependencies. * Input params must be the same as in the __construct * * @access public * @static * * @param array * User data to construct the user. * * @return User_entity */ public static function build($user_data) { $user = new User_entity($user_data); $CI = get_instance(); // Inject dependencies. $user->set_permissions_array($CI->config->item('permissions'))->set_roles_labels($CI->config->item('roles')); return $user; }
/** * Used by user_add * When adding an account. */ protected function _add_account() { $this->form_validation->set_rules('user_name', 'Name', 'trim|required|xss_clean'); $this->form_validation->set_rules('user_username', 'Username', 'trim|required|xss_clean|alpha_dash|callback__cb_check_unique[username]'); $this->form_validation->set_rules('user_email', 'Email', 'trim|required|xss_clean|valid_email|callback__cb_check_unique[email]'); $this->form_validation->set_rules('user_new_password', 'Password', 'trim|required|min_length[8]'); $this->form_validation->set_rules('user_roles', 'Roles', 'callback__cb_check_roles'); $this->form_validation->set_rules('user_status', 'Status', 'callback__cb_check_status'); // To be picked up by the validation object needs a rule, even if empty. $this->form_validation->set_rules('user_notify', 'Notify'); $this->form_validation->set_error_delimiters('<small class="error">', '</small>'); if ($this->form_validation->run() == FALSE) { $this->load->view('base/html_start'); $this->load->view('components/navigation', array('active_menu' => 'users')); $this->load->view('users/user_form', array('user' => NULL, 'action' => 'add')); $this->load->view('base/html_end'); } else { // Some values can be set in the constructor. $userdata = array('name' => $this->input->post('user_name'), 'username' => $this->input->post('user_username'), 'email' => $this->input->post('user_email'), 'author' => current_user()->uid); $user = User_entity::build($userdata); $user->set_password(hash_password($this->input->post('user_new_password')))->set_status($this->input->post('user_status'))->set_roles($this->input->post('user_roles')); // Save $this->user_model->save($user); // Notify user? if ($this->input->post('user_notify') == 'notify') { $this->load->library('email'); $this->email->from($this->config->item('aw_admin_email'), $this->config->item('aw_admin_name')); $this->email->to($user->email); // Load message data from config. $this->config->load('email_messages'); $message_account_created = $this->config->item('message_account_created'); // Replace placeholders. $placeholders = array('{{username}}' => $user->username, '{{name}}' => $user->name, '{{password}}' => $this->input->post('user_new_password')); $message_account_created['subject'] = strtr($message_account_created['subject'], $placeholders); $message_account_created['message'] = strtr($message_account_created['message'], $placeholders); $this->email->subject($message_account_created['subject']); $this->email->message($message_account_created['message']); $this->email->send(); } if ($this->user_model->save($user)) { Status_msg::success('User successfully created.'); } else { Status_msg::error('Error creating user. Try again.'); } redirect('users'); } }
$this->user_model->save($user); //////////////////////////////////////////////// // User 2 $user = new User_entity(array('email' => '*****@*****.**', 'name' => 'Regular user', 'username' => 'regular', 'author' => 1)); $user->set_password(hash_password('regular'))->set_status(User_entity::STATUS_ACTIVE); $this->user_model->save($user); //////////////////////////////////////////////// // User 3 $user = new User_entity(array('email' => '*****@*****.**', 'name' => 'The Agent', 'username' => 'agent', 'author' => 1)); $user->set_password(hash_password('agent'))->set_status(User_entity::STATUS_ACTIVE)->set_roles(array(ROLE_CC_AGENT)); $this->user_model->save($user); //////////////////////////////////////////////// // User 4 $user = new User_entity(array('email' => '*****@*****.**', 'name' => 'The Moderator', 'username' => 'moderator', 'author' => 1)); $user->set_password(hash_password('moderator'))->set_status(User_entity::STATUS_ACTIVE)->set_roles(array(ROLE_MODERATOR)); $this->user_model->save($user); //////////////////////////////////////////////// // User 5 $user = new User_entity(array('email' => '*****@*****.**', 'name' => 'The Blocked Agent', 'username' => 'blocked', 'author' => 1)); $user->set_password(hash_password('blocked'))->set_status(User_entity::STATUS_BLOCKED)->set_roles(array(ROLE_CC_AGENT)); $this->user_model->save($user); //////////////////////////////////////////////// // User 6 $user = new User_entity(array('email' => '*****@*****.**', 'name' => 'The Deleted', 'username' => 'deleted', 'author' => 1)); $user->set_password(hash_password('deleted'))->set_status(User_entity::STATUS_DELETED); $this->user_model->save($user); //////////////////////////////////////////////// // User 7 $user = new User_entity(array('email' => '*****@*****.**', 'name' => 'The All Roles', 'username' => 'all_roles', 'author' => 1)); $user->set_password(hash_password('all_roles'))->set_status(User_entity::STATUS_ACTIVE)->set_roles(array(ROLE_ADMINISTRATOR, ROLE_MODERATOR, ROLE_CC_AGENT)); $this->user_model->save($user);
/** * @depends test_get_user_by_uid */ public function test_add_user() { // Some values can be set in the constructor. $userdata = array('name' => 'A new test user', 'username' => 'new_test_user', 'email' => '*****@*****.**'); $user = new User_entity($userdata); $user->set_password(hash_password('test_password'))->set_status(User_entity::STATUS_ACTIVE)->set_roles(NULL); // Save. // We have two test users. This one will be added with uid $user->uid. self::$CI->user_model->save($user); $saved_user = self::$CI->user_model->get($user->uid); $this->assertEquals('A new test user', $saved_user->name); $this->assertEquals('new_test_user', $saved_user->username); $this->assertEquals('*****@*****.**', $saved_user->email); $this->assertEquals(User_entity::STATUS_ACTIVE, $saved_user->status); $this->assertInternalType('int', $saved_user->status); $this->assertEmpty($saved_user->roles); }