public static function _init() { // get the list of valid roles try { static::$_valid_roles = \Cache::get(\Config::get('ormauth.cache_prefix', 'auth') . '.roles'); } catch (\CacheNotFoundException $e) { static::$_valid_roles = \Model\Auth_Role::find('all'); \Cache::set(\Config::get('ormauth.cache_prefix', 'auth') . '.roles', static::$_valid_roles); } }
/** * Run the actual migration */ protected static function run_migration() { // make sure we've got a usertable we can work with static::usertable(); // get the simpleauth config \Config::load('simpleauth', true); $simpleauth = \Config::get('simpleauth', array()); // process the defined roles foreach (\Config::get('simpleauth.roles', array()) as $role => $config) { // skip all non-standard roles if ($role == '#' or !is_array($config)) { continue; } // do we already have this role? $result = \DB::select('id')->from(static::$data['ormauth_table'] . '_roles')->where('name', '=', $role)->execute(); if (count($result)) { $role_id = $result[0]['id']; } else { \Cli::write('- creating role: ' . $role, 'light_green'); list($role_id, $rows_affected) = \DB::insert(static::$data['ormauth_table'] . '_roles')->set(array('name' => $role))->execute(); } // fetch the role as an ORM object, and assign the defined permissions to it $role = \Model\Auth_Role::find($role_id); if ($role) { foreach ($config as $area => $permissions) { foreach ($permissions as $permission) { $perm = \Model\Auth_Permission::query()->where('area', '=', $area)->where('permission', '=', $permission)->get_one(); if (!$perm) { \Cli::write('- creating permission: ' . $area . '.' . $permission, 'light_green'); $perm = \Model\Auth_Permission::forge(array('area' => $area, 'permission' => $permission, 'description' => $area . '.' . $permission, 'actions' => serialize(array()))); } $role->permissions[] = $perm; } } // update the role and save the permissions $role->save(); } } // process the defined groups foreach (\Config::get('simpleauth.groups', array()) as $group => $config) { // ignore invalid entries if (!isset($config['name']) or !isset($config['roles'])) { continue; } // do we already have this group? $result = \DB::select('id')->from(static::$data['ormauth_table'] . '_groups')->where('name', '=', $config['name'])->execute(); if (count($result)) { $group_id = $result[0]['id']; } else { \Cli::write('- creating group: ' . $config['name'], 'light_green'); list($group_id, $rows_affected) = \DB::insert(static::$data['ormauth_table'] . '_groups')->set(array('name' => $config['name']))->execute(); } // update the user group entries \DB::update(static::$data['ormauth_table'])->set(array('group_id' => $group_id))->where('group_id', '=', $group)->execute(); // fetch the group as an ORM object, and assign the defined roles to it $group = \Model\Auth_Group::find($group_id); if ($group) { foreach ($config['roles'] as $role) { $role = \Model\Auth_Role::query()->where('name', '=', $role)->get_one(); if (!$role) { $role = \Model\Auth_Role::forge(array('name' => $role)); \Cli::write('- creating role: ' . $role, 'light_green'); } $group->roles[] = $role; } // update the group and save the roles $group->save(); } } return true; }