From 1f9eae70cacbf9d788f9f96142d01c8bb0f4dbd7 Mon Sep 17 00:00:00 2001 From: qadan Date: Wed, 20 Apr 2016 16:54:38 -0300 Subject: [PATCH 01/10] embargo roles whitelist --- .../includes/admin.form.inc | 4 +- .../includes/batch.inc | 59 +++++++++++++++ .../includes/embargo.inc | 50 +++++++++++-- .../includes/embargo_roles_manage.inc | 75 +++++++++++++++++++ .../islandora_scholar_embargo.drush.inc | 16 ++++ .../islandora_scholar_embargo.module | 26 +++++-- 6 files changed, 216 insertions(+), 14 deletions(-) create mode 100644 modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc diff --git a/modules/islandora_scholar_embargo/includes/admin.form.inc b/modules/islandora_scholar_embargo/includes/admin.form.inc index 1b9317ab..9a19c8c6 100644 --- a/modules/islandora_scholar_embargo/includes/admin.form.inc +++ b/modules/islandora_scholar_embargo/includes/admin.form.inc @@ -21,7 +21,9 @@ function islandora_embargo_admin(array $form, array &$form_state) { $options = islandora_get_content_models(); $selected = variable_get('islandora_embargo_content_models', array('ir:citationCModel', 'ir:thesisCModel')); foreach ($selected as $cmodel) { - $options = array($cmodel => $options[$cmodel]) + $options; + if (isset($options[$cmodel])) { + $options = array($cmodel => $options[$cmodel]) + $options; + } } foreach ($options as $key => $value) { $rows[$key] = array( diff --git a/modules/islandora_scholar_embargo/includes/batch.inc b/modules/islandora_scholar_embargo/includes/batch.inc index 2c582069..6f382c88 100644 --- a/modules/islandora_scholar_embargo/includes/batch.inc +++ b/modules/islandora_scholar_embargo/includes/batch.inc @@ -125,3 +125,62 @@ function islandora_scholar_embargo_lift_batch_finished($success, $results, $oper drupal_set_message(format_plural($results['skip_count'], '@count object skipped as the content model is not configured to allow embargoes.', '@count objects skipped as their content models are not configured to allow embargoes.')); } } + +/** + * Batch definition for re-applying embargos to embargoed objects. + * + * @return array + * The batch definition. + */ +function islandora_scholar_embargo_reapply_embargos_batch() { + return array( + 'title' => t('Re-Apply Embargos to Embargoed Objects'), + 'error_message' => t('An error has occurred and the embargos may not have all been re-applied.'), + 'init_message' => t('Preparing to re-apply embargos to embargoed objects.'), + 'progress_message' => t('Time elapsed: @elapsed
Estimated time remaining: @estimate.'), + 'file' => drupal_get_path('module', 'islandora_scholar_embargo') . '/includes/batch.inc', + 'operations' => array( + array('islandora_scholar_embargo_reapply_embargos_batch_operation', array()), + ), + ); +} + +/** + * Batch operation for re-applying embargos to embargoed objects. + * + * @param array $context + * The batch context. + */ +function islandora_scholar_embargo_reapply_embargos_batch_operation(&$context) { + module_load_include('inc', 'islandora_scholar_embargo', 'includes/embargo'); + $sandbox =& $context['sandbox']; + if (empty($sandbox)) { + $sandbox['total'] = islandora_scholar_embargo_get_all_embargoed(TRUE); + $sandbox['completed'] = 0; + } + + // Iterate through a set. + $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['completed']); + if (!empty($set)) { + foreach ($set as $embargoed_item) { + $id_parts = explode('/', $embargoed_item['obj']['value']); + $pid = array_shift($id_parts); + $dsid = array_shift($id_parts); + $dsids = is_null($dsid) ? $dsid : array($dsid); + islandora_scholar_embargo_lift_embargo($pid, $dsids); + islandora_scholar_embargo_set_embargo($pid, $dsids, $embargoed_item['date']['value']); + $sandbox['completed']++; + $context['message'] = t('Re-applied embargo to @item of @pid.', array( + '@item' => is_null($dsid) ? t('entire object') : $dsid, + '@pid' => $pid, + )); + } + } + + $context['finished'] = $sandbox['completed'] / $sandbox['total']; + if ($context['finished'] == 1) { + drupal_set_message(t('Re-applied embargos to @total objects.', array( + '@total' => $sandbox['completed'], + ))); + } +} diff --git a/modules/islandora_scholar_embargo/includes/embargo.inc b/modules/islandora_scholar_embargo/includes/embargo.inc index aeb0fff2..5af20021 100644 --- a/modules/islandora_scholar_embargo/includes/embargo.inc +++ b/modules/islandora_scholar_embargo/includes/embargo.inc @@ -445,24 +445,60 @@ EOQ; } /** - * Retrieve all embargoed items from an object. + * Gets the base SPARQL for a query requesting all embargoed items. * - * @return array - * An array containing the list of embargoed items on an object + * @return string + * A SPARQL query that can be used to get all embargoed items. */ -function islandora_scholar_embargo_get_all_embargoed() { - $current_embargo = << SELECT ?obj ?date FROM <#ri> WHERE { ?obj is:embargo-until ?date } +EOQ; +} + +/** + * Retrieve all embargoed items. + * + * @param bool $count + * Whether or not to just return the total count. + * + * @return array + * An array containing the list of embargoed items. + */ +function islandora_scholar_embargo_get_all_embargoed($count = FALSE) { + $current_embargo = islandora_scholar_embargo_get_embargoed_items_query(); + if (!$count) { + $current_embargo .= <<repository->ri->sparqlQuery($current_embargo, 'unlimited', '0'); - return $embargo_results; + $ri = $connection->repository->ri; + return $count ? $ri->countQuery($current_embargo, 'sparql') : $ri->sparqlQuery($current_embargo, 'unlimited', '0'); +} + +/** + * Gets a subset of all embargoed items. + * + * @param int $offset + * An offset to start from. + * @param int $limit + * The number of items to return in the subset. + * + * @return array + * An array containing a subset of embargoed items. + */ +function islandora_scholar_embargo_get_embargoed_items_subset($offset = 0, $limit = 10) { + $current_embargo = islandora_scholar_embargo_get_embargoed_items_query() . <<repository->ri->sparqlQuery($current_embargo, $limit); } /** diff --git a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc new file mode 100644 index 00000000..25a3735b --- /dev/null +++ b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc @@ -0,0 +1,75 @@ + array( + '#type' => 'fieldset', + '#title' => t('Roles'), + '#collapsible' => FALSE, + 'islandora_scholar_embargo_whitelisted_roles' => array( + '#type' => 'checkboxes', + '#description' => t('Check off all the roles that should have access to embargoed items. NOTE: since embargo XACML role entries are whitelists, embargoed objects in a Fedora repository shared between Drupal sites will be visible to the same role in both sites. Consider using site-unique roles when dealing with shared repositories.'), + '#options' => user_roles(TRUE, ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY), + '#default_value' => array_keys(variable_get('islandora_scholar_embargo_whitelisted_roles', array())), + ), + ), + 'note' => array( + '#markup' => t('Saving and applying will initiate a batch to modify the XACML policies of all embargoed objects. Bear in mind that this can take a long time to execute depending on the number of items to be embargoed, and that it may be more prudent to run the Drush command islandora_scholar_embargo_apply_roles_to_embargoed_objects instead. Number of objects to be embargoed: @number', array( + '@number' => number_format(islandora_scholar_embargo_get_all_embargoed(TRUE)), + )), + ), + 'submit_buttons' => array( + '#type' => 'item', + 'save' => array( + '#type' => 'submit', + '#value' => t('Save Without Applying'), + '#name' => 'save', + ), + 'save_and_apply' => array( + '#type' => 'submit', + '#value' => t('Save And Apply'), + '#name' => 'save_and_apply', + ), + ), + ); +} + +/** + * Submit handler for the roles management form. + * + * @param array $form + * The submitted roles admin form. + * @param array $form_state + * The state of the submitted form. + */ +function islandora_scholar_embargo_manage_roles_admin_form_submit($form, &$form_state) { + // Attach role names to role IDs to make it more useful as a variable. + $whitelist = array(); + foreach ($form_state['values']['islandora_scholar_embargo_whitelisted_roles'] as $rid) { + $whitelist[$rid] = $form['roles_wrapper']['islandora_scholar_embargo_whitelisted_roles']['#options'][$rid]; + } + variable_set('islandora_scholar_embargo_whitelisted_roles', $whitelist); + + // Kick off the batch if we were asked. + if ($form_state['triggering_element']['#name'] == 'save_and_apply') { + module_load_include('inc', 'islandora_scholar_embargo', 'includes/batch'); + batch_set(islandora_scholar_embargo_reapply_embargos_batch()); + } +} diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc b/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc index c1690b58..e9351f7c 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc @@ -24,6 +24,13 @@ function islandora_scholar_embargo_drush_command() { 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, 'callback' => 'islandora_scholar_embargo_lift_embargoes_update', ), + 'islandora_scholar_embargo_reapply_embargos' => array( + 'description' => dt('Applies the list of roles that can administer embargoes to all embargoed objects in the repository.'), + 'examples' => array('drush -v -u 1 islandora_scholar_embargo_apply_roles_to_embargoed_objects'), + 'aliases' => array('ise-roles'), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, + 'callback' => 'islandora_scholar_embargo_reapply_embargos', + ), ); } @@ -44,6 +51,15 @@ function islandora_scholar_embargo_lift_embargoes_update() { drush_backend_batch_process(); } +/** + * Runs the batch to apply whitelisted roles to embargoed objects. + */ +function islandora_scholar_embargo_reapply_embargos() { + module_load_include('inc', 'islandora_scholar_embargo', 'includes/batch'); + batch_set(islandora_scholar_embargo_reapply_embargos_batch()); + drush_backend_batch_process(); +} + /** * Batch operation for lifting embargoes for the update. * diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module index 9c092cf7..5802cf4e 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module @@ -10,8 +10,9 @@ define('ISLANDORA_SCHOLAR_EMBARGO_RELS_URI', 'info:islandora/islandora-system:def/scholar#'); define('ISLANDORA_SCHOLAR_EMBARGO_NOTIFICATION_PRED', 'embargo-expiry-notification-date'); define('ISLANDORA_SCHOLAR_EMBARGO_EXPIRY_PRED', 'embargo-until'); +define('ISLANDORA_SCHOLAR_EMBARGO_NAMESPACE_ROLE_PREFIX', 'islandora_scholar_embargo_namespace_roles'); define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_OWNED', 'can embargo owned objects'); -define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY', 'can embargo any object'); +define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY', 'can embargo or manage any object'); /** * Implements hook_menu(). @@ -51,6 +52,14 @@ function islandora_scholar_embargo_menu() { 'type' => MENU_LOCAL_TASK, 'access arguments' => array(ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY), ); + $items['admin/islandora/solution_pack_config/embargo/roles'] = array( + 'title' => 'Manage Embargo Roles', + 'file' => 'includes/embargo_roles_manage.inc', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('islandora_scholar_embargo_manage_roles_admin_form'), + 'type' => MENU_LOCAL_TASK, + 'access arguments' => array(ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY), + ); return $items; } @@ -91,7 +100,7 @@ function islandora_scholar_embargo_permission() { ), ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY => array( 'title' => t('Manage embargo on any objects'), - 'description' => t("User can add or remove embargo on any object in repository."), + 'description' => t("User can add or remove embargo on any object in repository. Note that XACML policies can override this setting; use the Islandora Scholar Embargo roles configuration page to establish trusted roles to write to embargoed objects."), ), ); } @@ -459,9 +468,10 @@ function islandora_scholar_embargo_set_embargo($param, $dsid = NULL, $end = 'ind } $xacml = new IslandoraXacml($item); - $xacml_changed = FALSE; $users = islandora_scholar_embargo_users_to_notify($item); + $roles = variable_get('islandora_scholar_embargo_whitelisted_roles', array()); + if ($dsid === NULL) { $item->relationships->remove(ISLANDORA_SCHOLAR_EMBARGO_RELS_URI, ISLANDORA_SCHOLAR_EMBARGO_EXPIRY_PRED); $item->relationships->remove(ISLANDORA_SCHOLAR_EMBARGO_RELS_URI, ISLANDORA_SCHOLAR_EMBARGO_NOTIFICATION_PRED); @@ -474,7 +484,10 @@ function islandora_scholar_embargo_set_embargo($param, $dsid = NULL, $end = 'ind } foreach ($users as $user) { $xacml->viewingRule->addUser($user->name); - $xacml_changed = TRUE; + } + foreach ($roles as $role) { + $xacml->viewingRule->addRole($role); + $xacml->managementRule->addRole($role); } } } @@ -502,14 +515,15 @@ function islandora_scholar_embargo_set_embargo($param, $dsid = NULL, $end = 'ind $embargo_ds->relationships->add(ISLANDORA_SCHOLAR_EMBARGO_RELS_URI, ISLANDORA_SCHOLAR_EMBARGO_NOTIFICATION_PRED, $notification, $type); } $xacml->datastreamRule->addDsid($datastream_id); - $xacml_changed = TRUE; foreach ($users as $user) { $xacml->datastreamRule->addUser($user->name); } } } } - if ($xacml_changed) { + // If we had to add users, roles or DSIDs, commit the changes and update the + // POLICY. + if (!empty($users) || !empty($roles) || !is_null($dsid)) { $xacml->writeBackToFedora(); } } From bfc85f269b9ce6ae3819042f6b9e3397912e46a8 Mon Sep 17 00:00:00 2001 From: qadan Date: Wed, 20 Apr 2016 17:03:33 -0300 Subject: [PATCH 02/10] that's totally a valid spelling BUT for consistency's sake, FINE, we'll jam an extra letter into the pluralization --- .../islandora_scholar_embargo/includes/batch.inc | 16 ++++++++-------- .../includes/embargo.inc | 14 +++++++------- .../includes/embargo_roles_manage.inc | 2 +- .../islandora_scholar_embargo.drush.inc | 8 ++++---- .../islandora_scholar_embargo.module | 4 ++-- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/modules/islandora_scholar_embargo/includes/batch.inc b/modules/islandora_scholar_embargo/includes/batch.inc index 6f382c88..c852b80a 100644 --- a/modules/islandora_scholar_embargo/includes/batch.inc +++ b/modules/islandora_scholar_embargo/includes/batch.inc @@ -127,31 +127,31 @@ function islandora_scholar_embargo_lift_batch_finished($success, $results, $oper } /** - * Batch definition for re-applying embargos to embargoed objects. + * Batch definition for re-applying embargoes to embargoed objects. * * @return array * The batch definition. */ -function islandora_scholar_embargo_reapply_embargos_batch() { +function islandora_scholar_embargo_reapply_embargoes_batch() { return array( 'title' => t('Re-Apply Embargos to Embargoed Objects'), - 'error_message' => t('An error has occurred and the embargos may not have all been re-applied.'), - 'init_message' => t('Preparing to re-apply embargos to embargoed objects.'), + 'error_message' => t('An error has occurred and the embargoes may not have all been re-applied.'), + 'init_message' => t('Preparing to re-apply embargoes to embargoed objects.'), 'progress_message' => t('Time elapsed: @elapsed
Estimated time remaining: @estimate.'), 'file' => drupal_get_path('module', 'islandora_scholar_embargo') . '/includes/batch.inc', 'operations' => array( - array('islandora_scholar_embargo_reapply_embargos_batch_operation', array()), + array('islandora_scholar_embargo_reapply_embargoes_batch_operation', array()), ), ); } /** - * Batch operation for re-applying embargos to embargoed objects. + * Batch operation for re-applying embargoes to embargoed objects. * * @param array $context * The batch context. */ -function islandora_scholar_embargo_reapply_embargos_batch_operation(&$context) { +function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) { module_load_include('inc', 'islandora_scholar_embargo', 'includes/embargo'); $sandbox =& $context['sandbox']; if (empty($sandbox)) { @@ -179,7 +179,7 @@ function islandora_scholar_embargo_reapply_embargos_batch_operation(&$context) { $context['finished'] = $sandbox['completed'] / $sandbox['total']; if ($context['finished'] == 1) { - drupal_set_message(t('Re-applied embargos to @total objects.', array( + drupal_set_message(t('Re-applied embargoes to @total objects.', array( '@total' => $sandbox['completed'], ))); } diff --git a/modules/islandora_scholar_embargo/includes/embargo.inc b/modules/islandora_scholar_embargo/includes/embargo.inc index 5af20021..a2efec2c 100644 --- a/modules/islandora_scholar_embargo/includes/embargo.inc +++ b/modules/islandora_scholar_embargo/includes/embargo.inc @@ -30,7 +30,7 @@ function islandora_scholar_embargo_form(array $form, array &$form_state, Abstrac // Table seems somewhat of overkill for now, but as the embargo offering // grows more robust this sort of formatting for the UI will make sense. $rows = array(); - $current_embargos = array(); + $current_embargoes = array(); $date = ''; foreach ($embargo_results as $result) { if ($result['obj']['value'] == $item->id) { @@ -52,12 +52,12 @@ function islandora_scholar_embargo_form(array $form, array &$form_state, Abstrac $type, $date, ); - $current_embargos[] = array( + $current_embargoes[] = array( 'type' => $type, 'date' => $date, ); } - $form_state['islandora_embargo'] = $current_embargos; + $form_state['islandora_embargo'] = $current_embargoes; $embargoed = TRUE; if (empty($embargo_results)) { $embargoed = FALSE; @@ -150,7 +150,7 @@ function islandora_scholar_embargo_form(array $form, array &$form_state, Abstrac '#title' => check_plain($embargoed ? t('Update Options') : t('Embargo Options')), '#options' => array( 'change_embargo_date' => $embargoed ? t('Change the embargo date') : t('Set the embargo date'), - 'indefinite_embargo' => t('Embargo indefinitely (Indefinite embargos must be lifted manually).'), + 'indefinite_embargo' => t('Embargo indefinitely (Indefinite embargoes must be lifted manually).'), ), '#default_value' => ($date === 'Indefinite') ? 'indefinite_embargo' : 'change_embargo_date', '#states' => array( @@ -265,7 +265,7 @@ function islandora_scholar_embargo_form_validate(array $form, array &$form_state $parsed = strtotime($parsed_date); if ($embargoed) { // This will need to be updated in the future to handle multiple - // embargos on a single object. + // embargoes on a single object. foreach ($form_state['islandora_embargo'] as $embargo_vals) { $embargo_date = $embargo_vals['date']; break; @@ -279,7 +279,7 @@ function islandora_scholar_embargo_form_validate(array $form, array &$form_state } } elseif ($form_state['values']['update_date_options'] === 'indefinite_embargo') { - // This will need to be updated in the future to handle multiple embargos + // This will need to be updated in the future to handle multiple embargoes // on a single object. $embargo_date = ''; foreach ($form_state['islandora_embargo'] as $embargo_vals) { @@ -390,7 +390,7 @@ function islandora_scholar_embargo_form_submit(array $form, array &$form_state) } } // Currently we just overwrite what's already in place. In the future, for - // multiple embargos on a single object, this will have to be adjusted. + // multiple embargoes on a single object, this will have to be adjusted. // If we're recursing do those objects in a batch. if (isset($form_state['values']['recurse']) && $form_state['values']['recurse'] && $form_state['values']['recurse_options'] != 'newchildren') { diff --git a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc index 25a3735b..8d2e518d 100644 --- a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc +++ b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc @@ -70,6 +70,6 @@ function islandora_scholar_embargo_manage_roles_admin_form_submit($form, &$form_ // Kick off the batch if we were asked. if ($form_state['triggering_element']['#name'] == 'save_and_apply') { module_load_include('inc', 'islandora_scholar_embargo', 'includes/batch'); - batch_set(islandora_scholar_embargo_reapply_embargos_batch()); + batch_set(islandora_scholar_embargo_reapply_embargoes_batch()); } } diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc b/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc index e9351f7c..52850d1d 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc @@ -24,12 +24,12 @@ function islandora_scholar_embargo_drush_command() { 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, 'callback' => 'islandora_scholar_embargo_lift_embargoes_update', ), - 'islandora_scholar_embargo_reapply_embargos' => array( + 'islandora_scholar_embargo_reapply_embargoes' => array( 'description' => dt('Applies the list of roles that can administer embargoes to all embargoed objects in the repository.'), 'examples' => array('drush -v -u 1 islandora_scholar_embargo_apply_roles_to_embargoed_objects'), 'aliases' => array('ise-roles'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, - 'callback' => 'islandora_scholar_embargo_reapply_embargos', + 'callback' => 'islandora_scholar_embargo_reapply_embargoes', ), ); } @@ -54,9 +54,9 @@ function islandora_scholar_embargo_lift_embargoes_update() { /** * Runs the batch to apply whitelisted roles to embargoed objects. */ -function islandora_scholar_embargo_reapply_embargos() { +function islandora_scholar_embargo_reapply_embargoes() { module_load_include('inc', 'islandora_scholar_embargo', 'includes/batch'); - batch_set(islandora_scholar_embargo_reapply_embargos_batch()); + batch_set(islandora_scholar_embargo_reapply_embargoes_batch()); drush_backend_batch_process(); } diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module index 5802cf4e..7f5ee274 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module @@ -199,7 +199,7 @@ function islandora_scholar_embargo_mail($key, &$message, $params) { * to lift. */ function islandora_scholar_embargo_cron() { - // Detect embargos expiring soon, send out notification. + // Detect embargoes expiring soon, send out notification. $now = time(); $pre_expiry = strtotime("-10DAYS", $now); $now = gmdate("Y-m-d\TH:i:s\Z", $now); @@ -269,7 +269,7 @@ EOQ; islandora_embargo_user_notify($notification['key'], $item, $notification['params']); } - // Inform rules and users of expired embargos. + // Inform rules and users of expired embargoes. $query = << SELECT ?expired ?date From b90ac10997ec7b57e9264d7c018f7f9ec16ce56c Mon Sep 17 00:00:00 2001 From: qadan Date: Wed, 20 Apr 2016 17:10:51 -0300 Subject: [PATCH 03/10] from an earlier idea; forgot to remove this --- .../islandora_scholar_embargo/islandora_scholar_embargo.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module index 7f5ee274..efd41272 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module @@ -12,7 +12,7 @@ define('ISLANDORA_SCHOLAR_EMBARGO_NOTIFICATION_PRED', 'embargo-expiry-notificati define('ISLANDORA_SCHOLAR_EMBARGO_EXPIRY_PRED', 'embargo-until'); define('ISLANDORA_SCHOLAR_EMBARGO_NAMESPACE_ROLE_PREFIX', 'islandora_scholar_embargo_namespace_roles'); define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_OWNED', 'can embargo owned objects'); -define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY', 'can embargo or manage any object'); +define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY', 'can embargo any object'); /** * Implements hook_menu(). From ce16056c5dc541be29168f90a4c4088344da5401 Mon Sep 17 00:00:00 2001 From: qadan Date: Wed, 20 Apr 2016 17:11:35 -0300 Subject: [PATCH 04/10] same type deal --- .../islandora_scholar_embargo/islandora_scholar_embargo.module | 1 - 1 file changed, 1 deletion(-) diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module index efd41272..3ea3ce31 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module @@ -10,7 +10,6 @@ define('ISLANDORA_SCHOLAR_EMBARGO_RELS_URI', 'info:islandora/islandora-system:def/scholar#'); define('ISLANDORA_SCHOLAR_EMBARGO_NOTIFICATION_PRED', 'embargo-expiry-notification-date'); define('ISLANDORA_SCHOLAR_EMBARGO_EXPIRY_PRED', 'embargo-until'); -define('ISLANDORA_SCHOLAR_EMBARGO_NAMESPACE_ROLE_PREFIX', 'islandora_scholar_embargo_namespace_roles'); define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_OWNED', 'can embargo owned objects'); define('ISLANDORA_SCHOLAR_EMBARGO_CAN_EMBARGO_ANY', 'can embargo any object'); From ec41b8f4474c98ee6b324beeaa38f8151573266a Mon Sep 17 00:00:00 2001 From: qadan Date: Fri, 22 Apr 2016 11:14:19 -0300 Subject: [PATCH 05/10] recommended fixes --- .../includes/batch.inc | 59 +++++++++++-------- .../includes/embargo.inc | 53 +++++++++-------- .../includes/embargo_roles_manage.inc | 4 +- .../islandora_scholar_embargo.drush.inc | 4 +- .../islandora_scholar_embargo.module | 1 - 5 files changed, 68 insertions(+), 53 deletions(-) diff --git a/modules/islandora_scholar_embargo/includes/batch.inc b/modules/islandora_scholar_embargo/includes/batch.inc index c852b80a..f5de01c1 100644 --- a/modules/islandora_scholar_embargo/includes/batch.inc +++ b/modules/islandora_scholar_embargo/includes/batch.inc @@ -155,32 +155,45 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) module_load_include('inc', 'islandora_scholar_embargo', 'includes/embargo'); $sandbox =& $context['sandbox']; if (empty($sandbox)) { - $sandbox['total'] = islandora_scholar_embargo_get_all_embargoed(TRUE); $sandbox['completed'] = 0; + $sandbox['offset'] = '1900-01-01T00:00:00.000Z'; } - // Iterate through a set. - $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['completed']); - if (!empty($set)) { - foreach ($set as $embargoed_item) { - $id_parts = explode('/', $embargoed_item['obj']['value']); - $pid = array_shift($id_parts); - $dsid = array_shift($id_parts); - $dsids = is_null($dsid) ? $dsid : array($dsid); - islandora_scholar_embargo_lift_embargo($pid, $dsids); - islandora_scholar_embargo_set_embargo($pid, $dsids, $embargoed_item['date']['value']); - $sandbox['completed']++; - $context['message'] = t('Re-applied embargo to @item of @pid.', array( - '@item' => is_null($dsid) ? t('entire object') : $dsid, - '@pid' => $pid, - )); - } + // It's possible for this number to change during the batch (e.g., if an + // embargo expires or a datastream is modified), so re-grabbing is safest to + // prevent items from being skipped. + $sandbox['total'] = islandora_scholar_embargo_get_all_embargoed(TRUE); + if (!$sandbox['total']) { + $context['message'] = t('No items to embargo found, or embargoed items set was reduced to zero at some point during the batch.'); + return; } - - $context['finished'] = $sandbox['completed'] / $sandbox['total']; - if ($context['finished'] == 1) { - drupal_set_message(t('Re-applied embargoes to @total objects.', array( - '@total' => $sandbox['completed'], - ))); + else { + // Iterate through a set. + $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['offset']); + if (!empty($set)) { + foreach ($set as $embargoed_item) { + $id_parts = explode('/', $embargoed_item['obj']['value']); + $pid = array_shift($id_parts); + $dsid = array_shift($id_parts); + $dsids = is_null($dsid) ? $dsid : array($dsid); + islandora_scholar_embargo_lift_embargo($pid, $dsids); + islandora_scholar_embargo_set_embargo($pid, $dsids, $embargoed_item['expiry']['value']); + $sandbox['completed']++; + $sandbox['offset'] = $embargoed_item['date']['value']; + $context['message'] = t('@count/@total: Re-applied embargo to @item of @pid.', array( + '@count' => $sandbox['completed'], + '@total' => $sandbox['total'], + '@item' => is_null($dsid) ? t('entire object') : $dsid, + '@pid' => $pid, + )); + } + // If the set was empty, $context['finished'] will be 1 anyway. + $context['finished'] == $sandbox['completed'] / $sandbox['total']; + } + if ($context['finished'] >= 1) { + drupal_set_message(t('Re-applied embargoes to @total objects.', array( + '@total' => $sandbox['completed'], + ))); + } } } diff --git a/modules/islandora_scholar_embargo/includes/embargo.inc b/modules/islandora_scholar_embargo/includes/embargo.inc index a2efec2c..a24d7344 100644 --- a/modules/islandora_scholar_embargo/includes/embargo.inc +++ b/modules/islandora_scholar_embargo/includes/embargo.inc @@ -444,23 +444,6 @@ EOQ; return $embargo_results; } -/** - * Gets the base SPARQL for a query requesting all embargoed items. - * - * @return string - * A SPARQL query that can be used to get all embargoed items. - */ -function islandora_scholar_embargo_get_embargoed_items_query() { - return << -SELECT ?obj ?date -FROM <#ri> -WHERE { - ?obj is:embargo-until ?date -} -EOQ; -} - /** * Retrieve all embargoed items. * @@ -471,8 +454,15 @@ EOQ; * An array containing the list of embargoed items. */ function islandora_scholar_embargo_get_all_embargoed($count = FALSE) { - $current_embargo = islandora_scholar_embargo_get_embargoed_items_query(); - if (!$count) { + $current_embargo = << +SELECT ?obj ?date +FROM <#ri> +WHERE { + ?obj is:embargo-until ?date +} +EOQ; + if ($count) { $current_embargo .= << +SELECT DISTINCT ?obj ?expiry ?date +FROM <#ri> +WHERE { + ?obj is:embargo-until ?expiry ; + { ?obj ?date } UNION + { ?obj ?date ; + ?dsid + } + FILTER(?date > "$offset"^^) +} +ORDER BY ASC(?date) EOQ; $connection = islandora_get_tuque_connection(); return $connection->repository->ri->sparqlQuery($current_embargo, $limit); diff --git a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc index 8d2e518d..60a7219a 100644 --- a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc +++ b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc @@ -31,12 +31,12 @@ function islandora_scholar_embargo_manage_roles_admin_form($form, $form_state) { ), ), 'note' => array( - '#markup' => t('Saving and applying will initiate a batch to modify the XACML policies of all embargoed objects. Bear in mind that this can take a long time to execute depending on the number of items to be embargoed, and that it may be more prudent to run the Drush command islandora_scholar_embargo_apply_roles_to_embargoed_objects instead. Number of objects to be embargoed: @number', array( + '#markup' => t('Saving and applying will initiate a batch to modify the XACML policies of all embargoed objects. Bear in mind that this can take a long time to execute depending on the number of items to be embargoed, and that it may be more prudent to run the Drush command islandora_scholar_embargo_apply_roles_to_embargoed_objects instead. Number of embargoed objects at time of page load: @number', array( '@number' => number_format(islandora_scholar_embargo_get_all_embargoed(TRUE)), )), ), 'submit_buttons' => array( - '#type' => 'item', + '#type' => 'actions', 'save' => array( '#type' => 'submit', '#value' => t('Save Without Applying'), diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc b/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc index 52850d1d..0d3870ac 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.drush.inc @@ -26,8 +26,8 @@ function islandora_scholar_embargo_drush_command() { ), 'islandora_scholar_embargo_reapply_embargoes' => array( 'description' => dt('Applies the list of roles that can administer embargoes to all embargoed objects in the repository.'), - 'examples' => array('drush -v -u 1 islandora_scholar_embargo_apply_roles_to_embargoed_objects'), - 'aliases' => array('ise-roles'), + 'examples' => array('drush -v -u 1 islandora_scholar_embargo_reapply_embargoes'), + 'aliases' => array('ise-reapply'), 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, 'callback' => 'islandora_scholar_embargo_reapply_embargoes', ), diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module index 3ea3ce31..406db36e 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module @@ -486,7 +486,6 @@ function islandora_scholar_embargo_set_embargo($param, $dsid = NULL, $end = 'ind } foreach ($roles as $role) { $xacml->viewingRule->addRole($role); - $xacml->managementRule->addRole($role); } } } From 420aa124a087d663d20702cfda37e707c0f5121f Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 25 Apr 2016 11:58:11 -0300 Subject: [PATCH 06/10] offsetting by pid too. also getting that var del in there :/ --- .../includes/batch.inc | 8 ++++--- .../includes/embargo.inc | 22 +++++++++++++++---- .../islandora_scholar_embargo.install | 1 + 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/modules/islandora_scholar_embargo/includes/batch.inc b/modules/islandora_scholar_embargo/includes/batch.inc index f5de01c1..b079d1cb 100644 --- a/modules/islandora_scholar_embargo/includes/batch.inc +++ b/modules/islandora_scholar_embargo/includes/batch.inc @@ -156,7 +156,8 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) $sandbox =& $context['sandbox']; if (empty($sandbox)) { $sandbox['completed'] = 0; - $sandbox['offset'] = '1900-01-01T00:00:00.000Z'; + $sandbox['date_offset'] = '1900-01-01T00:00:00.000Z'; + $sandbox['pid_offset'] = NULL; } // It's possible for this number to change during the batch (e.g., if an @@ -169,7 +170,7 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) } else { // Iterate through a set. - $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['offset']); + $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['date_offset'], $sandbox['pid_offset']); if (!empty($set)) { foreach ($set as $embargoed_item) { $id_parts = explode('/', $embargoed_item['obj']['value']); @@ -179,7 +180,8 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) islandora_scholar_embargo_lift_embargo($pid, $dsids); islandora_scholar_embargo_set_embargo($pid, $dsids, $embargoed_item['expiry']['value']); $sandbox['completed']++; - $sandbox['offset'] = $embargoed_item['date']['value']; + $sandbox['date_offset'] = $embargoed_item['date']['value']; + $sandbox['pid_offset'] = $pid; $context['message'] = t('@count/@total: Re-applied embargo to @item of @pid.', array( '@count' => $sandbox['completed'], '@total' => $sandbox['total'], diff --git a/modules/islandora_scholar_embargo/includes/embargo.inc b/modules/islandora_scholar_embargo/includes/embargo.inc index a24d7344..4345263f 100644 --- a/modules/islandora_scholar_embargo/includes/embargo.inc +++ b/modules/islandora_scholar_embargo/includes/embargo.inc @@ -475,8 +475,11 @@ EOQ; /** * Gets a subset of all embargoed items. * - * @param string $offset + * @param string $offset_date * An offset ISO8601 date string to filter results before. + * @param string|null $offset_pid + * A PID offset to use in the case where there is a tie for dates. Use NULL + * to ignore this case. * @param int $limit * The number of items to return in the subset. * @@ -485,7 +488,16 @@ EOQ; * including 'obj' as the object ID, 'expiry' as the expiry date, and 'date' * as the item's created or last modified date. */ -function islandora_scholar_embargo_get_embargoed_items_subset($offset = '1900-01-01T00:00:00.000Z', $limit = 10) { +function islandora_scholar_embargo_get_embargoed_items_subset($offset_date = '1900-01-01T00:00:00.000Z', $offset_pid = NULL, $limit = 10) { + // Establish some parameters for the query. + $filter = is_null($offset_pid) ? + "FILTER(?date > \"$offset_date\"^^)" : + "FILTER(?date > \"$offset_date\"^^ || (?date = \"$offset_date\"^^ && str(?obj) > \"$offset_pid\"))"; + $order_by = is_null($offset_pid) ? + "ORDER BY ASC(?date)" : + "ORDER BY ASC(?date), ASC(?obj)"; + + // Build the query. $current_embargo = << SELECT DISTINCT ?obj ?expiry ?date @@ -496,10 +508,12 @@ WHERE { { ?obj ?date ; ?dsid } - FILTER(?date > "$offset"^^) + $filter } -ORDER BY ASC(?date) +$order_by EOQ; + + // Use the query. $connection = islandora_get_tuque_connection(); return $connection->repository->ri->sparqlQuery($current_embargo, $limit); } diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.install b/modules/islandora_scholar_embargo/islandora_scholar_embargo.install index 0d010440..5b7850cd 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.install +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.install @@ -11,6 +11,7 @@ function islandora_scholar_embargo_uninstall() { $variables = array( 'islandora_embargo_content_models', + 'islandora_scholar_embargo_whitelisted_roles', ); array_walk($variables, 'variable_del'); } From ba4026a4ca8296984f7720072127f8911519ac6a Mon Sep 17 00:00:00 2001 From: qadan Date: Mon, 25 Apr 2016 13:48:57 -0300 Subject: [PATCH 07/10] always asc those objs --- modules/islandora_scholar_embargo/includes/embargo.inc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/islandora_scholar_embargo/includes/embargo.inc b/modules/islandora_scholar_embargo/includes/embargo.inc index 4345263f..5a64981c 100644 --- a/modules/islandora_scholar_embargo/includes/embargo.inc +++ b/modules/islandora_scholar_embargo/includes/embargo.inc @@ -493,9 +493,6 @@ function islandora_scholar_embargo_get_embargoed_items_subset($offset_date = '19 $filter = is_null($offset_pid) ? "FILTER(?date > \"$offset_date\"^^)" : "FILTER(?date > \"$offset_date\"^^ || (?date = \"$offset_date\"^^ && str(?obj) > \"$offset_pid\"))"; - $order_by = is_null($offset_pid) ? - "ORDER BY ASC(?date)" : - "ORDER BY ASC(?date), ASC(?obj)"; // Build the query. $current_embargo = << Date: Mon, 25 Apr 2016 15:55:43 -0300 Subject: [PATCH 08/10] perfecting the query --- .../includes/batch.inc | 9 +++---- .../includes/embargo.inc | 26 +++++++++---------- 2 files changed, 17 insertions(+), 18 deletions(-) diff --git a/modules/islandora_scholar_embargo/includes/batch.inc b/modules/islandora_scholar_embargo/includes/batch.inc index b079d1cb..36b40e3a 100644 --- a/modules/islandora_scholar_embargo/includes/batch.inc +++ b/modules/islandora_scholar_embargo/includes/batch.inc @@ -156,8 +156,7 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) $sandbox =& $context['sandbox']; if (empty($sandbox)) { $sandbox['completed'] = 0; - $sandbox['date_offset'] = '1900-01-01T00:00:00.000Z'; - $sandbox['pid_offset'] = NULL; + $sandbox['slice'] = array(); } // It's possible for this number to change during the batch (e.g., if an @@ -170,7 +169,7 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) } else { // Iterate through a set. - $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['date_offset'], $sandbox['pid_offset']); + $set = islandora_scholar_embargo_get_embargoed_items_subset($sandbox['slice']); if (!empty($set)) { foreach ($set as $embargoed_item) { $id_parts = explode('/', $embargoed_item['obj']['value']); @@ -180,8 +179,8 @@ function islandora_scholar_embargo_reapply_embargoes_batch_operation(&$context) islandora_scholar_embargo_lift_embargo($pid, $dsids); islandora_scholar_embargo_set_embargo($pid, $dsids, $embargoed_item['expiry']['value']); $sandbox['completed']++; - $sandbox['date_offset'] = $embargoed_item['date']['value']; - $sandbox['pid_offset'] = $pid; + $sandbox['slice']['offset_date'] = $embargoed_item['date']['value']; + $sandbox['slice']['offset_pid'] = $pid; $context['message'] = t('@count/@total: Re-applied embargo to @item of @pid.', array( '@count' => $sandbox['completed'], '@total' => $sandbox['total'], diff --git a/modules/islandora_scholar_embargo/includes/embargo.inc b/modules/islandora_scholar_embargo/includes/embargo.inc index 5a64981c..17335f6c 100644 --- a/modules/islandora_scholar_embargo/includes/embargo.inc +++ b/modules/islandora_scholar_embargo/includes/embargo.inc @@ -475,11 +475,10 @@ EOQ; /** * Gets a subset of all embargoed items. * - * @param string $offset_date - * An offset ISO8601 date string to filter results before. - * @param string|null $offset_pid - * A PID offset to use in the case where there is a tie for dates. Use NULL - * to ignore this case. + * @param array $slice_params + * An associative array containing: + * 'offset_date': an ISO8601 datetime to filter items before + * 'offset_pid': a PID to filter on when a date match is found. * @param int $limit * The number of items to return in the subset. * @@ -488,15 +487,11 @@ EOQ; * including 'obj' as the object ID, 'expiry' as the expiry date, and 'date' * as the item's created or last modified date. */ -function islandora_scholar_embargo_get_embargoed_items_subset($offset_date = '1900-01-01T00:00:00.000Z', $offset_pid = NULL, $limit = 10) { - // Establish some parameters for the query. - $filter = is_null($offset_pid) ? - "FILTER(?date > \"$offset_date\"^^)" : - "FILTER(?date > \"$offset_date\"^^ || (?date = \"$offset_date\"^^ && str(?obj) > \"$offset_pid\"))"; - +function islandora_scholar_embargo_get_embargoed_items_subset($slice_params = array(), $limit = 10) { // Build the query. $current_embargo = << +PREFIX xs: SELECT DISTINCT ?obj ?expiry ?date FROM <#ri> WHERE { @@ -505,9 +500,14 @@ WHERE { { ?obj ?date ; ?dsid } - $filter +EOQ; + // Offset if asked. + if ($slice_params) { + $current_embargo .= "FILTER(?date > '{$slice_params['offset_date']}'^^xs:dateTime || (?date = '{$slice_params['offset_date']}'^^xs:dateTime && xs:string(?obj) > xs:string('info:fedora/{$slice_params['offset_date']}')))"; + } + $current_embargo .= << Date: Tue, 26 Apr 2016 14:43:24 -0300 Subject: [PATCH 09/10] slight misunderstanding of the way #checkboxes work --- .../islandora_scholar_embargo/includes/embargo_roles_manage.inc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc index 60a7219a..648a1211 100644 --- a/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc +++ b/modules/islandora_scholar_embargo/includes/embargo_roles_manage.inc @@ -62,7 +62,7 @@ function islandora_scholar_embargo_manage_roles_admin_form($form, $form_state) { function islandora_scholar_embargo_manage_roles_admin_form_submit($form, &$form_state) { // Attach role names to role IDs to make it more useful as a variable. $whitelist = array(); - foreach ($form_state['values']['islandora_scholar_embargo_whitelisted_roles'] as $rid) { + foreach (array_filter($form_state['values']['islandora_scholar_embargo_whitelisted_roles']) as $rid => $checked) { $whitelist[$rid] = $form['roles_wrapper']['islandora_scholar_embargo_whitelisted_roles']['#options'][$rid]; } variable_set('islandora_scholar_embargo_whitelisted_roles', $whitelist); From 8dcf23c23f6953b583f855eb7f298694315f4377 Mon Sep 17 00:00:00 2001 From: Nigel Banks Date: Mon, 23 May 2016 11:57:34 +0100 Subject: [PATCH 10/10] Add whitelisted roles when embargoing a data-stream. --- .../islandora_scholar_embargo/islandora_scholar_embargo.module | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module index 406db36e..531747f9 100644 --- a/modules/islandora_scholar_embargo/islandora_scholar_embargo.module +++ b/modules/islandora_scholar_embargo/islandora_scholar_embargo.module @@ -516,6 +516,9 @@ function islandora_scholar_embargo_set_embargo($param, $dsid = NULL, $end = 'ind foreach ($users as $user) { $xacml->datastreamRule->addUser($user->name); } + foreach ($roles as $role) { + $xacml->datastreamRule->addRole($role); + } } } }