/** * 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; }