/** * Create everything needed for a new context to run. * * This includes creating permissions, assigning them to certain roles, and * creating an application migration for the permissions. * * @todo Create the migration file if $migrate is true... * * @param string $name The name of the context to create. * @param array $roles The roles (names or IDs) which should have permission * to view this module. * @param boolean $migrate If true, will create a migration file. * * @return boolean False on error, else true. */ public static function create_context($name = '', $roles = array(), $migrate = false) { if (empty($name)) { self::$errors = lang('ui_no_context_name'); return false; } // Write the context name to the config file. self::$ci->load->helper('config_file'); $contexts = self::getContexts(); $lowerName = strtolower($name); // Add the context if it is not already in the list of contexts. if (!in_array($lowerName, $contexts)) { array_unshift($contexts, $lowerName); if (!write_config('application', array('contexts' => $contexts), null)) { self::$errors[] = lang('ui_cant_write_config'); return false; } } // Create an entry in the application_lang file for the context. if (!function_exists('addLanguageLine')) { self::$ci->load->helper('translate/languages'); } $temp = addLanguageLine('application_lang.php', array("bf_context_{$lowerName}" => $name), 'english'); if (!$temp) { // @todo set error/return if the language line was not added successfully? } // Create the relevant permissions. $cname = 'Site.' . ucfirst($name) . '.View'; // Get the permission ID, either from an existing permission or by inserting // a new permission. self::$ci->load->model('permissions/permission_model'); if (self::$ci->permission_model->permission_exists($cname)) { $pid = self::$ci->permission_model->find_by('name', $cname)->permission_id; } else { $pid = self::$ci->permission_model->insert(array('name' => $cname, 'description' => 'Allow user to view the ' . ucwords($name) . ' Context.')); } // Assign the permission to the supplied roles. // If no roles were supplied, exit, indicating success. if (empty($roles)) { return true; } // Assign the permission to each role. self::$ci->load->model('roles/role_permission_model'); foreach ($roles as $role) { if (is_numeric($role)) { // Assign By Id. self::$ci->role_permission_model->delete($role, $pid); self::$ci->role_permission_model->create($role, $pid); } else { // Assign By Name. self::$ci->role_permission_model->assign_to_role($role, $cname); } } // if ($migrate) { // @todo create a migration file. // } return true; }
/** * Creates everything needed for a new context to run. Includes * creating permissions, assigning them to certain roles, and * even creating an application migration for the permissions. * * @param string $name The name of the context to create. * @param array $roles The names or id's of the roles to give permissions to view. * @param bool $migrate If TRUE, will create an app migration file. * * @return bool */ public static function create_context($name = '', $roles = array(), $migrate = false) { if (empty($name)) { self::$errors = lang('ui_no_context_name'); return false; } // 1. Try to write to the config file so it will show in the menu no // matter what. self::$ci->load->helper('config_file'); $contexts = self::$contexts; $lowerName = strtolower($name); // If it isn't in the list of contexts, add it if (!in_array($lowerName, $contexts)) { array_unshift($contexts, $lowerName); if (!write_config('application', array('contexts' => $contexts), null)) { self::$errors[] = lang('ui_cant_write_config'); return false; } } // 2. Language File if (!function_exists('addLanguageLine')) { self::$ci->load->helper('translate/languages'); $temp = addLanguageLine('application_lang.php', array("bf_context_{$lowerName}" => $name), 'english'); } // 3. Create the relevant permissions $cname = 'Site.' . ucfirst($name) . '.View'; // 3.1. create the actual permission self::$ci->load->model('permissions/permission_model'); if (self::$ci->permission_model->permission_exists($cname)) { $pid = self::$ci->permission_model->find_by('name', $cname)->permission_id; } else { $pid = self::$ci->permission_model->insert(array('name' => $cname, 'description' => 'Allow user to view the ' . ucwords($name) . ' Context.')); } // Are there any roles to apply this to? If not, quit, since there will // be nothing to migrate if (count($roles) == 0) { return true; } self::$ci->load->model('roles/role_permission_model'); foreach ($roles as $role) { // Assign By Id if (is_numeric($role)) { self::$ci->role_permission_model->delete_role_permissions($role, $pid); self::$ci->role_permission_model->create_role_permissions($role, $pid); } else { self::$ci->role_permission_model->assign_to_role($role, $cname); } } return true; }
/** * Displays the create a context form. * * @access public * * @return void */ public function create_context() { // Load our roles for display in the form. $this->load->model('roles/role_model'); $roles = $this->role_model->select('role_id, role_name')->find_all(); Template::set('roles', $roles); // Form submittal? if ($this->input->post('submit')) { $this->form_validation->set_rules('context_name', 'Context Name', 'required|trim|alpha_numeric|xss_clean'); if ($this->form_validation->run() !== false) { /* Validated! */ $name = $this->input->post('context_name'); $for_roles = $this->input->post('roles'); $migrate = $this->input->post('migrate') == 'on' ? true : false; // Try to save the context, using the UI/Context helper $this->load->library('ui/contexts'); if (Contexts::create_context($name, $for_roles, $migrate)) { Template::set_message('Context succesfully created.', 'success'); redirect(SITE_AREA . '/developer/builder'); } else { Template::set_message('Error creating Context: ' . Contexts::errors(), 'error'); } } } Template::set('toolbar_title', lang('mb_create_a_context')); Template::render(); }
/** * Display the form which allows the user to create a context. * * @return void */ public function create_context() { // Form submittal? if (isset($_POST['build'])) { $this->form_validation->set_rules('context_name', 'lang:mb_context_name', 'required|trim|alpha_numeric'); if ($this->form_validation->run() !== false) { // Validated! $name = $this->input->post('context_name'); $for_roles = $this->input->post('roles'); $migrate = $this->input->post('migrate') == 'on'; // Try to save the context, using the UI/Context helper $this->load->library('ui/contexts'); if (Contexts::create_context($name, $for_roles, $migrate)) { Template::set_message(lang('mb_context_create_success'), 'success'); redirect(SITE_AREA . '/developer/builder'); } // Creating the context failed Template::set_message(lang('mb_context_create_error') . Contexts::errors(), 'error'); } } // Load roles for display in the form. $this->load->model('roles/role_model'); $this->role_model->select(array('role_id', 'role_name'))->where('deleted', 0); Template::set('roles', $this->role_model->find_all()); Template::set('toolbar_title', lang('mb_create_a_context')); Template::render(); }
/** * Creates everything needed for a new context to run. Includes * creating permissions, assigning them to certain roles, and * even creating an application migration for the permissions. * * @access public * @static * * @param string $name The name of the context to create. * @param array $roles The names or id's of the roles to give permissions to view. * @param bool $migrate If TRUE, will create an app migration file. * * @return bool */ public static function create_context($name = '', $roles = array(), $migrate = false) { if (empty($name)) { self::$errors = lang('ui_no_context_name'); return false; } /* 1. Try to write it to the config file so that it will show in the menu no matter what. */ self::$ci->load->helper('config_file'); $contexts = self::$contexts; // If it alread exists, we don't need to do anything! if (!in_array(strtolower($name), $contexts)) { array_unshift($contexts, strtolower($name)); if (!write_config('application', array('contexts' => $contexts), null)) { self::$errors[] = lang('ui_cant_write_config'); return false; } } /* 2. Create our permissions */ $cname = 'Site.' . ucfirst($name) . '.View'; // First - create the actual permission self::$ci->load->model('permissions/permission_model'); if (!self::$ci->permission_model->permission_exists($cname)) { $pid = self::$ci->permission_model->insert(array('name' => $cname, 'description' => 'Allow user to view the ' . ucwords($name) . ' Context.')); } else { $pid = self::$ci->permission_model->find_by('name', $cname)->permission_id; $exists = true; } // Do we have any roles to apply this to? // If we don't we can quite since there won't be anything // to migrate. if (count($roles) == 0) { return true; } self::$ci->load->model('roles/role_permission_model'); foreach ($roles as $role) { // Assign By Id if (is_numeric($role)) { self::$ci->role_permission_model->delete_role_permissions($role, $pid); self::$ci->role_permission_model->create_role_permissions($role, $pid); } else { self::$ci->role_permission_model->assign_to_role($role, $cname); } } // If we made it here, we were successful! return true; }