From 603c3049a1ce7249c55ff8338fc3ea69323f0cb3 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 22 Jan 2010 09:39:29 -0800 Subject: Treat identity providers just like other modules and use the admin_module to install and switch to a different identity provider. --- modules/user/helpers/user_installer.php | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'modules/user/helpers') diff --git a/modules/user/helpers/user_installer.php b/modules/user/helpers/user_installer.php index 0cba502f..dd21c93c 100644 --- a/modules/user/helpers/user_installer.php +++ b/modules/user/helpers/user_installer.php @@ -20,6 +20,13 @@ class user_installer { static function install() { $db = Database::instance(); + $current_provider = module::get_var("gallery", "identity_provider"); + if (!empty($current_provider)) { + module::uninstall($current_provider); + } + IdentityProvider::reset(); + module::set_var("gallery", "identity_provider", "user"); + $db->query("CREATE TABLE IF NOT EXISTS {users} ( `id` int(9) NOT NULL auto_increment, `name` varchar(32) NOT NULL, @@ -70,19 +77,6 @@ class user_installer { $admin->admin = true; $admin->save(); - $current_provider = module::get_var("gallery", "identity_provider"); - if (empty($current_provider)) { - // If there is no provider defined then we are doing an initial install - // so we need to set the provider and make the administrator own everything - // If the installer is called and there is an identity provider, then we - // are switching identity providers and and the event handlers will do the - // right things - module::set_var("gallery", "identity_provider", "user"); - - // Let the admin own everything - $db->query("update {items} set owner_id = {$admin->id}"); - } - $root = ORM::factory("item", 1); access::allow($everybody, "view", $root); access::allow($everybody, "view_full", $root); @@ -93,6 +87,10 @@ class user_installer { module::set_var("user", "mininum_password_length", 5); module::set_version("user", 2); + module::event("identity_provider_changed", $current_provider, "user"); + + auth::login(IdentityProvider::instance()->admin_user()); + Session::instance()->regenerate(); } static function upgrade($version) { -- cgit v1.2.3 From ae568b6182544b84067aa099eec494da477d083f Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 22 Jan 2010 12:09:11 -0800 Subject: Refactor the identity provider installation in to a common helper method (change_provider) with an initialization callback. --- modules/gallery/libraries/IdentityProvider.php | 29 +++++++++++ modules/user/helpers/user_installer.php | 68 ++++++++++++-------------- 2 files changed, 61 insertions(+), 36 deletions(-) (limited to 'modules/user/helpers') diff --git a/modules/gallery/libraries/IdentityProvider.php b/modules/gallery/libraries/IdentityProvider.php index bcb3056a..f7be33e3 100644 --- a/modules/gallery/libraries/IdentityProvider.php +++ b/modules/gallery/libraries/IdentityProvider.php @@ -57,6 +57,35 @@ class IdentityProvider_Core { Kohana_Config::instance()->clear("identity"); } + /** + * Return a commen confirmation message + */ + static function confirmation_message() { + return t("Are you sure you want to change your Identity Provider? " . + "Continuing will delete all existing users."); + } + + static function change_provider($new_provider) { + $current_provider = module::get_var("gallery", "identity_provider"); + if (!empty($current_provider)) { + module::uninstall($current_provider); + } + + IdentityProvider::reset(); + $provider = new IdentityProvider($new_provider); + + module::set_var("gallery", "identity_provider", $new_provider); + + if (method_exists("{$new_provider}_installer", "initialize")) { + call_user_func("{$new_provider}_installer::initialize"); + } + + module::event("identity_provider_changed", $current_provider, $new_provider); + + auth::login($provider->admin_user()); + Session::instance()->regenerate(); + } + /** * Loads the configured driver and validates it. * diff --git a/modules/user/helpers/user_installer.php b/modules/user/helpers/user_installer.php index dd21c93c..3882f5f2 100644 --- a/modules/user/helpers/user_installer.php +++ b/modules/user/helpers/user_installer.php @@ -18,15 +18,40 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class user_installer { + static function check_environment() { + return array("warn" => array(IdentityProvider::confirmation_message())); + } + static function install() { - $db = Database::instance(); - $current_provider = module::get_var("gallery", "identity_provider"); - if (!empty($current_provider)) { - module::uninstall($current_provider); + IdentityProvider::change_provider("user"); + } + + static function upgrade($version) { + if ($version == 1) { + module::set_var("user", "mininum_password_length", 5); + + module::set_version("user", $version = 2); + } + } + + static function uninstall() { + // Delete all users and groups so that we give other modules an opportunity to clean up + foreach (ORM::factory("user")->find_all() as $user) { + $user->delete(); + } + + foreach (ORM::factory("group")->find_all() as $group) { + $group->delete(); } - IdentityProvider::reset(); - module::set_var("gallery", "identity_provider", "user"); + $db = Database::instance(); + $db->query("DROP TABLE IF EXISTS {users};"); + $db->query("DROP TABLE IF EXISTS {groups};"); + $db->query("DROP TABLE IF EXISTS {groups_users};"); + } + + static function initialize() { + $db = Database::instance(); $db->query("CREATE TABLE IF NOT EXISTS {users} ( `id` int(9) NOT NULL auto_increment, `name` varchar(32) NOT NULL, @@ -84,36 +109,7 @@ class user_installer { access::allow($registered, "view", $root); access::allow($registered, "view_full", $root); - module::set_var("user", "mininum_password_length", 5); - module::set_version("user", 2); - module::event("identity_provider_changed", $current_provider, "user"); - - auth::login(IdentityProvider::instance()->admin_user()); - Session::instance()->regenerate(); - } - - static function upgrade($version) { - if ($version == 1) { - module::set_var("user", "mininum_password_length", 5); - - module::set_version("user", $version = 2); - } - } - - static function uninstall() { - // Delete all users and groups so that we give other modules an opportunity to clean up - foreach (ORM::factory("user")->find_all() as $user) { - $user->delete(); - } - - foreach (ORM::factory("group")->find_all() as $group) { - $group->delete(); - } - - $db = Database::instance(); - $db->query("DROP TABLE IF EXISTS {users};"); - $db->query("DROP TABLE IF EXISTS {groups};"); - $db->query("DROP TABLE IF EXISTS {groups_users};"); + module::set_var("user", "mininum_password_length", 5); } } \ No newline at end of file -- cgit v1.2.3 From df313cac567bee77f5a73308381fe67dcac9b92c Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Fri, 22 Jan 2010 12:30:17 -0800 Subject: Change the check_environment method in the module helper and the module installers to can_activate to reflect that it is doing more than just checking the environment. --- modules/gallery/controllers/admin_modules.php | 2 +- modules/gallery/helpers/module.php | 8 ++++---- modules/slideshow/helpers/slideshow_installer.php | 2 +- modules/user/helpers/user_installer.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'modules/user/helpers') diff --git a/modules/gallery/controllers/admin_modules.php b/modules/gallery/controllers/admin_modules.php index 46defbef..a2168280 100644 --- a/modules/gallery/controllers/admin_modules.php +++ b/modules/gallery/controllers/admin_modules.php @@ -42,7 +42,7 @@ class Admin_Modules_Controller extends Admin_Controller { if ($info->active && !$desired && module::is_active($module_name)) { $messages = array_merge($messages, module::can_deactivate($module_name)); } else if (!$info->active && $desired && !module::is_active($module_name)) { - $messages = array_merge($messages, module::check_environment($module_name)); + $messages = array_merge($messages, module::can_activate($module_name)); } } diff --git a/modules/gallery/helpers/module.php b/modules/gallery/helpers/module.php index 595f600b..f680ff6a 100644 --- a/modules/gallery/helpers/module.php +++ b/modules/gallery/helpers/module.php @@ -120,17 +120,17 @@ class module_Core { } /** - * Check that the module can be installed. (i.e. all the prerequistes exist) + * Check that the module can be activated. (i.e. all the prerequistes exist) * @param string $module_name * @return array an array of warning or error messages to be displayed */ - static function check_environment($module_name) { + static function can_activate($module_name) { module::_add_to_path($module_name); $messages = array(); $installer_class = "{$module_name}_installer"; - if (method_exists($installer_class, "check_environment")) { - $messages = call_user_func(array($installer_class, "check_environment")); + if (method_exists($installer_class, "can_activate")) { + $messages = call_user_func(array($installer_class, "can_activate")); } // Remove it from the active path diff --git a/modules/slideshow/helpers/slideshow_installer.php b/modules/slideshow/helpers/slideshow_installer.php index 319e2e79..8d612f3e 100644 --- a/modules/slideshow/helpers/slideshow_installer.php +++ b/modules/slideshow/helpers/slideshow_installer.php @@ -34,7 +34,7 @@ class slideshow_installer { site_status::clear("slideshow_needs_rss"); } - static function check_environment() { + static function can_activate() { $messages = array(); if (!module::is_active("rss")) { $messages["warn"][] = t("The Slideshow module requires the RSS module."); diff --git a/modules/user/helpers/user_installer.php b/modules/user/helpers/user_installer.php index 3882f5f2..38f8020b 100644 --- a/modules/user/helpers/user_installer.php +++ b/modules/user/helpers/user_installer.php @@ -18,7 +18,7 @@ * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ class user_installer { - static function check_environment() { + static function can_activate() { return array("warn" => array(IdentityProvider::confirmation_message())); } -- cgit v1.2.3