diff --git a/plugins/access/user_in_reference.inc b/plugins/access/user_in_reference.inc new file mode 100644 index 0000000..ad40955 --- /dev/null +++ b/plugins/access/user_in_reference.inc @@ -0,0 +1,122 @@ + t("User: in reference field"), + 'description' => t('Check if a user is within the values of a reference field'), + 'callback' => 'ctools_user_in_reference_access_check', + 'settings form' => 'ctools_user_in_reference_ctools_access_settings', + 'summary' => 'ctools_user_in_reference_ctools_access_summary', + 'get child' => 'ctools_user_in_reference_ctools_access_get_child', + 'get children' => 'ctools_user_in_reference_ctools_access_get_children', + 'required context' => array( + new ctools_context_required(t('User'), 'user'), + ), +); + +function ctools_user_in_reference_ctools_access_get_child($plugin, $parent, $child) { + $plugins = &drupal_static(__FUNCTION__, array()); + if (empty($plugins[$parent . ':' . $child])) { + list($entity_type, $bundle_type, $field_name) = explode(':', $child); + $entity = entity_get_info($entity_type); + $bundle = $entity['bundles'][$bundle_type]; + $field_instances = field_info_instances($entity_type, $bundle_type); + $field = $field_instances[$field_name]; + $plugins[$parent . ':' . $child] = _ctools_user_in_reference_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field); + } + + return $plugins[$parent . ':' . $child]; +} + +function ctools_user_in_reference_ctools_access_get_children($plugin, $parent) { + $plugins = &drupal_static(__FUNCTION__, array()); + + if (!empty($plugins)) { + return $plugins; + } + + $entities = entity_get_info(); + foreach ($entities as $entity_type => $entity) { + foreach ($entity['bundles'] as $bundle_type => $bundle) { + foreach (field_info_instances($entity_type, $bundle_type) as $field_name => $field) { + $field_info = field_info_field($field_name); + if (($field_info['type'] == 'user_reference') || (($field_info['type'] == 'entityreference') && ($field_info['settings']['target_type'] == 'user'))) { + $key = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name; + if (!isset($plugins[$key])) { + $plugins[$key] = _ctools_user_in_reference_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field); + } + } + } + } + } + + return $plugins; +} + +function _ctools_user_in_reference_ctools_access_get_child($plugin, $parent, $entity_type, $bundle_type, $field_name, $entity, $bundle, $field) { + $plugin['title'] = t('User in @entity @type: @field Field', array('@entity' => $entity['label'], '@type' => $bundle_type, '@field' => $field['label'])); + $plugin['keyword'] = $entity_type; + $plugin['description'] = t('Control access by @field field.', array('@field' => $field['label'])); + $plugin['name'] = $parent . ':' . $entity_type . ':' . $bundle_type . ':' . $field_name; + $plugin['required context'][] = new ctools_context_required(t(ucfirst($entity_type)), $entity_type, array('type' => $bundle_type)); + return $plugin; +} + +/** + * Settings form for the 'user in reference' access plugin. + */ +function ctools_user_in_reference_ctools_access_settings($form, &$form_state, $conf) { + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $form_state['plugin']['name']); + + $form['helptext'] = array( + '#markup' => t('Grant access based on comparison of a user context with a reference field.'), + '#weight' => -10, + ); + + return $form; +} + +/** + * Check for access. + */ +function ctools_user_in_reference_access_check($conf, $context, $plugin) { + if (empty($context) || count($context) != 2 || empty($context[0]->data) || empty($context[1]->data)) { + return FALSE; + } + + $account = $context[0]->data; + $node = $context[1]->data; + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); + + if ($field_items = field_get_items($entity_type, $node, $field_name)) { + + // Entityreference and user_reference use a different key. + $field_info = field_info_field($field_name); + $key = ($field_info['type'] == 'entityreference')?'target_id':'uid'; + + foreach ($field_items as $field_value) { + if ($field_value[$key] == $account->uid) { + return TRUE; + } + } + } + + return FALSE; +} + +/** + * Describe an instance of this plugin. + */ +function ctools_user_in_reference_ctools_access_summary($conf, $context, $plugin) { + list($parent, $entity_type, $bundle_type, $field_name) = explode(':', $plugin['name']); + return t('@id1 in @id2:@field', array('@id1' => $context[0]->identifier, '@id2' => $context[1]->identifier, '@field' => $field_name)); +}