From d59a91f5296e402604ddd42a76e6c2bd2eb716d0 Mon Sep 17 00:00:00 2001 From: Daniel Lamb Date: Thu, 11 Jul 2013 12:01:10 -0300 Subject: [PATCH 1/4] Logging in as user 1 in islandora_install_solution_pack() for drush installation of islandora modules. --- includes/solution_packs.inc | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/includes/solution_packs.inc b/includes/solution_packs.inc index 26bd77b5..4e43a4c3 100644 --- a/includes/solution_packs.inc +++ b/includes/solution_packs.inc @@ -256,6 +256,14 @@ function islandora_install_solution_pack($module, $op = 'install') { return; } + // Login as user 1 if the installation is coming from the command line (e.g. + // drush). + if (drupal_is_cli()) { + global $user; + $user = user_load(1); + user_login_finalize(); + } + $t = get_t(); // Some general replacements. From 4d3642b1bc5971c2877bc809e1396eb736aef636 Mon Sep 17 00:00:00 2001 From: Daniel Lamb Date: Thu, 11 Jul 2013 12:46:39 -0300 Subject: [PATCH 2/4] Logging in as user 1 on solution pack uninstalls as well. --- includes/solution_packs.inc | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/includes/solution_packs.inc b/includes/solution_packs.inc index 4e43a4c3..8e726f34 100644 --- a/includes/solution_packs.inc +++ b/includes/solution_packs.inc @@ -336,6 +336,15 @@ function islandora_install_solution_pack($module, $op = 'install') { * directly for each solution pack. */ function islandora_uninstall_solution_pack($module) { + + // Login as user 1 if the uninstall is coming from the command line (e.g. + // drush). + if (drupal_is_cli()) { + global $user; + $user = user_load(1); + user_login_finalize(); + } + $t = get_t(); module_load_include('module', 'islandora', 'islandora'); module_load_include('inc', 'islandora', 'includes/utilities'); From 0a213cb6d05befe17d5de2d9857b8fd148770d15 Mon Sep 17 00:00:00 2001 From: Daniel Lamb Date: Thu, 11 Jul 2013 16:49:26 -0300 Subject: [PATCH 3/4] Tuque cache will clear if new user is provided to islandora_get_tuque_connection. Tests. --- islandora.info | 1 + islandora.module | 14 ++++++--- tests/islandora_tuque.test | 59 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/islandora_tuque.test diff --git a/islandora.info b/islandora.info index ce6ba947..31d79491 100644 --- a/islandora.info +++ b/islandora.info @@ -17,4 +17,5 @@ files[] = tests/hooks.test files[] = tests/ingest.test files[] = tests/hooked_access.test files[] = tests/islandora_manage_permissions.test +files[] = tests/islandora_tuque.test php = 5.3 diff --git a/islandora.module b/islandora.module index 5b45fb1a..d13d0b4e 100644 --- a/islandora.module +++ b/islandora.module @@ -812,7 +812,7 @@ function islandora_default_islandora_view_object($object) { * * Includes some very basic error logging. * - * @param object $user + * @param object $usr * The user to connect as. * @param string $url * The URL to connect to. @@ -820,12 +820,18 @@ function islandora_default_islandora_view_object($object) { * @return IslandoraTuque * A IslandoraTuque instance */ -function islandora_get_tuque_connection($user = NULL, $url = NULL) { +function islandora_get_tuque_connection($usr = NULL, $url = NULL) { $tuque = &drupal_static(__FUNCTION__); - if (!$tuque) { + + global $user; + + $no_tuque_yet = !$tuque; + $user_is_different = ($usr && $usr->uid != $user->uid); + + if ($no_tuque_yet || $user_is_different) { if (IslandoraTuque::exists()) { try { - $tuque = new IslandoraTuque($user, $url); + $tuque = new IslandoraTuque($usr, $url); } catch (Exception $e) { drupal_set_message(t('Unable to connect to the repository %e', array('%e' => $e)), 'error'); diff --git a/tests/islandora_tuque.test b/tests/islandora_tuque.test new file mode 100644 index 00000000..4d8f0041 --- /dev/null +++ b/tests/islandora_tuque.test @@ -0,0 +1,59 @@ + 'Islandora Tuque', + 'description' => 'Tests basic tuque functionality.', + 'group' => 'Islandora', + ); + } + + /** + * Prepares enviroment for testing. + * + * @see IslandoraWebTestCase::setUp() + */ + public function setUp() { + parent::setUp(array('islandora')); + } + + /** + * Tests that a new tuque instance is created when a new user is provided. + */ + public function testTuqueUserChange() { + global $user; + + $tuque = islandora_get_tuque_connection($user); + + $old_user_name = $tuque->connection->username; + + $new_user = user_load(0); + + $tuque = islandora_get_tuque_connection($new_user); + + $new_user_name = $tuque->connection->username; + + $this->assertNotEqual($old_user_name, $new_user_name, "User in tuque's connection changes when a new user is provided to islandora_get_tuque_connection()."); + + $old_user_name = $new_user_name; + + $tuque = islandora_get_tuque_connection(); + + $new_user_name = $tuque->connection->username; + + $this->assertEqual($old_user_name, $new_user_name, "User in tuque's connection does not change when no user is provided to islandora_get_tuque_connection()."); + } + +} From 0fc33b5e2fc1ece9f5b11029d3c4c3e4cdd2b395 Mon Sep 17 00:00:00 2001 From: Daniel Lamb Date: Fri, 12 Jul 2013 22:50:29 -0300 Subject: [PATCH 4/4] Committing so christian can write the test. --- islandora.module | 87 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 75 insertions(+), 12 deletions(-) diff --git a/islandora.module b/islandora.module index d13d0b4e..e7e501fb 100644 --- a/islandora.module +++ b/islandora.module @@ -812,7 +812,7 @@ function islandora_default_islandora_view_object($object) { * * Includes some very basic error logging. * - * @param object $usr + * @param object $new_user * The user to connect as. * @param string $url * The URL to connect to. @@ -820,30 +820,93 @@ function islandora_default_islandora_view_object($object) { * @return IslandoraTuque * A IslandoraTuque instance */ -function islandora_get_tuque_connection($usr = NULL, $url = NULL) { +function islandora_get_tuque_connection($new_user = NULL, $url = NULL) { $tuque = &drupal_static(__FUNCTION__); global $user; - $no_tuque_yet = !$tuque; - $user_is_different = ($usr && $usr->uid != $user->uid); - - if ($no_tuque_yet || $user_is_different) { - if (IslandoraTuque::exists()) { - try { - $tuque = new IslandoraTuque($usr, $url); + // $user_for_tuque = NULL; + // + // if TUQUE DOES NOT EXIST { + // if PROVIDED USER IS NOT NULL { + // MAKE TUQUE WITH PROVIDED USER + // } + // else + // { + // MAKE TUQUE WITH GLOBAL USER + // } + // } + // else { + // if PROVIDED USER IS NOT NULL { + // if PROVIDED USER IS NOT TUQUE'S USER { + // MAKE TUQUE WITH PROVIDED USER + // } + // else { + // LEAVE TUQUE ALONE + // } + // } + // else { + // if GLOBAL USER IS NOT TUQUE'S USER { + // MAKE TUQUE WITH GLOBAL USER + // } + // else { + // LEAVE TUQUE ALONE + // } + // } + // } + + // If tuque is NULL. + if (!$tuque) { + // If the provided user is not null, pass it along to Tuque. + if ($new_user) { + $tuque = islandora_make_tuque($new_user, $url); + } + // Otherwise give Tuque the global user. + else { + $tuque = islandora_make_tuque($user, $url); + } + } + // Else, tuque already exists. So only make it if the users are different. + else { + if ($new_user) { + // Get the name of the new user. If it's '', then it's actually + // 'anonymous' as far as Tuque is concerned. + $new_user_name = $new_user->name; + if (empty($new_user_name)) { + $new_user_name = 'anonymous'; } - catch (Exception $e) { - drupal_set_message(t('Unable to connect to the repository %e', array('%e' => $e)), 'error'); + if ($new_user_name != $tuque->connection->username) { + $tuque = islandora_make_tuque($new_user); } } else { - return NULL; + // Get the name of the global user. If it's '', then it's actually + // 'anonymous' as far as Tuque is concerned. + $user_name = $user->name; + if (empty($user_name)) { + $user_name = 'anonymous'; + } + if ($user->name != $tuque->connection->username) { + $tuque = islandora_make_tuque($user); + } } } return $tuque; } +function islandora_make_tuque($user, $url) { + if (IslandoraTuque::exists()) { + try { + return new IslandoraTuque($user, $url); + } + catch (Exception $e) { + drupal_set_message(t('Unable to connect to the repository %e', array('%e' => $e)), 'error'); + } + } + + return NULL; +} + /** * Loads the object from the given ID if possible. *