Changes to Identity interface to allow for multiple Identity providers. What I've tested to this point, is you can install a new provider, switch to it, login as administrator, uninstall the default user module, reinstall the user module, switch back to the user module and login.

This commit is contained in:
Tim Almdal
2009-10-20 16:32:22 -07:00
parent 098b57bf18
commit 7f9441c33d
7 changed files with 164 additions and 82 deletions

View File

@@ -21,7 +21,7 @@ class Admin_Identity_Controller extends Admin_Controller {
public function index() {
$view = new Admin_View("admin.html");
$view->content = new View("admin_identity.html");
$view->content->available = Identity::active();
$view->content->available = Identity::providers();
$view->content->active = module::get_var("gallery", "identity_provider", "user");
print $view;
}
@@ -39,18 +39,36 @@ class Admin_Identity_Controller extends Admin_Controller {
access::verify_csrf();
$active_provider = module::get_var("gallery", "identity_provider", "user");
$providers = Identity::active();
$providers = Identity::providers();
$new_provider = $this->input->post("provider");
if ($new_provider != $active_provider) {
module::event("identity_change", $new_provider);
module::event("pre_identity_change", $active_provider, $new_provider);
Identity::deactivate();
// Switch authentication
module::set_var("gallery", "identity_provider", $new_provider);
Identity::reset();
Identity::activate();
// @todo this type of collation is questionable from an i18n perspective
message::success(t("Changed to %description",
array("description" => $providers->$new_provider)));
try {
Session::instance()->destroy();
} catch (Exception $e) {
// We don't care if there was a problem destroying the session.
}
url::redirect(item::root()->abs_url());
}
message::info(t("The selected provider \"%description\" is already active.",
array("description" => $providers->$new_provider)));
url::redirect("admin/identity");
}
}

View File

@@ -191,7 +191,7 @@ class gallery_event_Core {
->id("sidebar")
->label(t("Manage Sidebar"))
->url(url::site("admin/sidebar"))));
if (count(Identity::active()) > 1) {
if (count(Identity::providers()) > 1) {
$menu
->append(Menu::factory("submenu")
->id("identity_menu")

View File

@@ -39,13 +39,24 @@ class Identity_Core {
* @param string configuration
* @return Identity_Core
*/
static function & instance($config="default") {
if (!isset(Identity::$instance)) {
static function & instance() {
if (!isset(self::$instance)) {
// Create a new instance
Identity::$instance = new Identity($config);
self::$instance = new Identity();
}
return Identity::$instance;
return self::$instance;
}
/**
* Returns a singleton instance of Identity.
* There can only be one Identity driver configured at a given point
*
* @param string configuration
* @return Identity_Core
*/
static function reset() {
self::$instance = new Identity();
}
/**
@@ -83,11 +94,11 @@ class Identity_Core {
}
/**
* Return a list of installed and activated Identity Drivers.
* Return a list of installed Identity Drivers.
*
* @return boolean true if the driver supports updates; false if read only
*/
static function active() {
static function providers() {
if (empty(self::$active)) {
$drivers = new ArrayObject(array(), ArrayObject::ARRAY_AS_PROPS);
foreach (module::active() as $module) {
@@ -102,6 +113,20 @@ class Identity_Core {
return self::$active;
}
/**
* @see Identity_Driver::activate.
*/
static function activate() {
self::instance()->driver->activate();
}
/**
* @see Identity_Driver::deactivate.
*/
static function deactivate() {
self::instance()->driver->deactivate();
}
/**
* Determine if if the current driver supports updates.
*

View File

@@ -18,6 +18,16 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/
interface Identity_Driver {
/**
* Initialize the provider so it is ready to use
*/
public function activate();
/**
* Cleanup up this provider so it is unavailable for use and won't conflict with the current driver
*/
public function deactivate();
/**
* Return the guest user.
*

View File

@@ -24,6 +24,91 @@
* Note: by design, this class does not do any permission checking.
*/
class user_Core {
/**
* Initialize the provider so it is ready to use
*/
public function activate() {
$db = Database::instance();
$db->query("CREATE TABLE IF NOT EXISTS {users} (
`id` int(9) NOT NULL auto_increment,
`name` varchar(32) NOT NULL,
`full_name` varchar(255) NOT NULL,
`password` varchar(64) NOT NULL,
`login_count` int(10) unsigned NOT NULL DEFAULT 0,
`last_login` int(10) unsigned NOT NULL DEFAULT 0,
`email` varchar(64) default NULL,
`admin` BOOLEAN default 0,
`guest` BOOLEAN default 0,
`hash` char(32) default NULL,
`url` varchar(255) default NULL,
`locale` char(10) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`hash`),
UNIQUE KEY(`name`))
DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE IF NOT EXISTS {groups} (
`id` int(9) NOT NULL auto_increment,
`name` char(64) default NULL,
`special` BOOLEAN default 0,
PRIMARY KEY (`id`),
UNIQUE KEY(`name`))
DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE IF NOT EXISTS {groups_users} (
`group_id` int(9) NOT NULL,
`user_id` int(9) NOT NULL,
PRIMARY KEY (`group_id`, `user_id`),
UNIQUE KEY(`user_id`, `group_id`))
DEFAULT CHARSET=utf8;");
$everybody = group::create("Everybody");
$everybody->special = true;
$everybody->save();
$registered = group::create("Registered Users");
$registered->special = true;
$registered->save();
$guest = user::create("guest", "Guest User", "");
$guest->guest = true;
$guest->remove($registered);
$guest->save();
$admin = user::create("admin", "Gallery Administrator", "admin");
$admin->admin = true;
$admin->save();
// Let the admin own everything
$db->update("items", array("owner_id" => $admin->id), array("owner_id" => "IS NULL"));
$root = ORM::factory("item", 1);
access::allow($everybody, "view", $root);
access::allow($everybody, "view_full", $root);
access::allow($registered, "view", $root);
access::allow($registered, "view_full", $root);
}
/**
* Cleanup up this provider so it is unavailable for use and won't conflict with the current driver
*/
public function deactivate() {
// 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};");
}
/**
* Return the guest user.
*

View File

@@ -19,87 +19,17 @@
*/
class user_installer {
static function install() {
$db = Database::instance();
$db->query("CREATE TABLE IF NOT EXISTS {users} (
`id` int(9) NOT NULL auto_increment,
`name` varchar(32) NOT NULL,
`full_name` varchar(255) NOT NULL,
`password` varchar(64) NOT NULL,
`login_count` int(10) unsigned NOT NULL DEFAULT 0,
`last_login` int(10) unsigned NOT NULL DEFAULT 0,
`email` varchar(64) default NULL,
`admin` BOOLEAN default 0,
`guest` BOOLEAN default 0,
`hash` char(32) default NULL,
`url` varchar(255) default NULL,
`locale` char(10) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY(`hash`),
UNIQUE KEY(`name`))
DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE IF NOT EXISTS {groups} (
`id` int(9) NOT NULL auto_increment,
`name` char(64) default NULL,
`special` BOOLEAN default 0,
PRIMARY KEY (`id`),
UNIQUE KEY(`name`))
DEFAULT CHARSET=utf8;");
$db->query("CREATE TABLE IF NOT EXISTS {groups_users} (
`group_id` int(9) NOT NULL,
`user_id` int(9) NOT NULL,
PRIMARY KEY (`group_id`, `user_id`),
UNIQUE KEY(`user_id`, `group_id`))
DEFAULT CHARSET=utf8;");
$everybody = group::create("Everybody");
$everybody->special = true;
$everybody->save();
$registered = group::create("Registered Users");
$registered->special = true;
$registered->save();
$guest = user::create("guest", "Guest User", "");
$guest->guest = true;
$guest->remove($registered);
$guest->save();
$admin = user::create("admin", "Gallery Administrator", "admin");
$admin->admin = true;
$admin->save();
// Let the admin own everything
$db->update("items", array("owner_id" => $admin->id), array("owner_id" => "IS NULL"));
user::activate();
module::set_version("user", 1);
$root = ORM::factory("item", 1);
access::allow($everybody, "view", $root);
access::allow($everybody, "view_full", $root);
access::allow($registered, "view", $root);
access::allow($registered, "view_full", $root);
}
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();
}
user::deactivate();
try {
Session::instance()->destroy();
} catch (Exception $e) {
// We don't care if there was a problem destroying the session.
}
$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};");
}
}

View File

@@ -21,6 +21,20 @@
* Based on the Cache_Sqlite_Driver developed by the Kohana Team
*/
class Identity_Gallery_Driver implements Identity_Driver {
/**
* @see Identity_Driver::activate.
*/
public function activate() {
user::activate();
}
/**
* @see Identity_Driver::deactivate.
*/
public function deactivate() {
user::deactivate();
}
/**
* @see Identity_Driver::guest.
*/