from($migrations['table'])->where('type', '=', 'package')->where('name', '=', 'auth')->execute(); if (count($result) < 7) { $errors[] = 'Auth database migrations haven\'t run (succesfully).'; $errors[] = 'There is a discrepancy between your migration configuration file and the migration table.'; } } // check the fields of the users table $usertable = array( 'id' => array('type' => 'int', 'constraint' => 11, 'auto_increment' => true), 'username' => array('type' => 'varchar', 'constraint' => 50, 'after' => 'id'), 'password' => array('type' => 'varchar', 'constraint' => 255, 'after' => 'username'), 'group_id' => array('type' => 'int', 'constraint' => 11, 'default' => 1, 'after' => 'password'), 'email' => array('type' => 'varchar', 'constraint' => 255, 'after' => 'group_id'), 'last_login' => array('type' => 'varchar', 'constraint' => 25, 'after' => 'email'), 'previous_login' => array('type' => 'varchar', 'constraint' => 25, 'default' => 0, 'after' => 'last_login'), 'login_hash' => array('type' => 'varchar', 'constraint' => 255, 'after' => 'previous_login'), 'user_id' => array('type' => 'int', 'constraint' => 11, 'default' => 0, 'after' => 'login_hash'), 'created_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0, 'after' => 'user_id'), 'updated_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0, 'after' => 'created_at'), ); foreach ($usertable as $field => $value) { if (\DBUtil::field_exists(static::$data['ormauth_table'], $field)) { unset($usertable[$field]); } } if ( ! empty($usertable)) { $errors[] = 'User table "'.static::$data['ormauth_table'].'" is missing the field(s): '.implode(', ', array_keys($usertable)); } // process the results of the validation if ($errors) { // display all errors \Cli::write('You environment did not validate:', 'light_red'); foreach ($errors as $error) { \Cli::write('* '.$error); } return false; } // inform the user we're good to go \Cli::write('Environment validated', 'light_green'); return true; } /** * 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']; } // no, add it 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']; } // no, add it 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; } /* * Deal with potential changes in users table layout between simpleauth and ormauth */ protected static function usertable() { if ( ! \DBUtil::table_exists(static::$data['ormauth_table'])) { if ( ! \DBUtil::table_exists(static::$data['simpleauth_table'])) { // table users \DBUtil::create_table(static::$data['ormauth_table'], array( 'id' => array('type' => 'int', 'constraint' => 11, 'auto_increment' => true), 'username' => array('type' => 'varchar', 'constraint' => 50), 'password' => array('type' => 'varchar', 'constraint' => 255), 'group_id' => array('type' => 'int', 'constraint' => 11, 'default' => 1), 'email' => array('type' => 'varchar', 'constraint' => 255), 'last_login' => array('type' => 'varchar', 'constraint' => 25), 'previous_login' => array('type' => 'varchar', 'constraint' => 25, 'default' => 0), 'login_hash' => array('type' => 'varchar', 'constraint' => 255), 'user_id' => array('type' => 'int', 'constraint' => 11, 'default' => 0), 'created_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0), 'updated_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0), ), array('id')); // add a unique index on username and email \DBUtil::create_index(static::$data['ormauth_table'], array('username', 'email'), 'username', 'UNIQUE'); } else { \DBUtil::rename_table(static::$data['simpleauth_table'], static::$data['ormauth_table']); } } // run a check on required fields, and deal with missing ones. we might be migrating from simpleauth if (\DBUtil::field_exists(static::$data['ormauth_table'], 'group')) { \DBUtil::modify_fields(static::$data['ormauth_table'], array( 'group' => array('name' => 'group_id', 'type' => 'int', 'constraint' => 11), )); } if ( ! \DBUtil::field_exists(static::$data['ormauth_table'], 'group_id')) { \DBUtil::add_fields(static::$data['ormauth_table'], array( 'group_id' => array('type' => 'int', 'constraint' => 11, 'default' => 1, 'after' => 'password'), )); } if ( ! \DBUtil::field_exists(static::$data['ormauth_table'], 'previous_login')) { \DBUtil::add_fields(static::$data['ormauth_table'], array( 'previous_login' => array('type' => 'varchar', 'constraint' => 25, 'default' => 0, 'after' => 'last_login'), )); } if ( ! \DBUtil::field_exists(static::$data['ormauth_table'], 'user_id')) { \DBUtil::add_fields(static::$data['ormauth_table'], array( 'user_id' => array('type' => 'int', 'constraint' => 11, 'default' => 0, 'after' => 'login_hash'), )); } if (\DBUtil::field_exists(static::$data['ormauth_table'], 'created')) { \DBUtil::modify_fields(static::$data['ormauth_table'], array( 'created' => array('name' => 'created_at', 'type' => 'int', 'constraint' => 11), )); } if ( ! \DBUtil::field_exists(static::$data['ormauth_table'], 'created_at')) { \DBUtil::add_fields(static::$data['ormauth_table'], array( 'created_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0, 'after' => 'user_id'), )); } if (\DBUtil::field_exists(static::$data['ormauth_table'], 'updated')) { \DBUtil::modify_fields(static::$data['ormauth_table'], array( 'updated' => array('name' => 'updated_at', 'type' => 'int', 'constraint' => 11), )); } if ( ! \DBUtil::field_exists(static::$data['ormauth_table'], 'updated_at')) { \DBUtil::add_fields(static::$data['ormauth_table'], array( 'updated_at' => array('type' => 'int', 'constraint' => 11, 'default' => 0, 'after' => 'created_at'), )); } } }