Skip to content

Commit

Permalink
Merge pull request #6 from digitalutsc/issue#2#3
Browse files Browse the repository at this point in the history
Issue#2#3
  • Loading branch information
kylehuynh205 authored May 10, 2023
2 parents 32a9a5c + 83be2f4 commit 7cde5d8
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
10 changes: 10 additions & 0 deletions config/schema/group_solr.schema.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
views.filter.access_control_with_group_filter:
type: views.filter.in_operator
label: 'Select access control by Group'
mapping:
value:
type: sequence
label: 'Select access control by Group'
sequence:
type: string
label: 'Select access control by Group'
17 changes: 17 additions & 0 deletions group_solr.module
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,20 @@ function group_solr_theme() {
],
];
}

/**
* Implements hook_views_data().
*/
function group_solr_views_data_alter(array &$data) {

$data['views']['access_control_with_group_filter'] = [
'title' => t('Access Control Filter - Configurable with Group'),
'group' => t('Access control'),
'filter' => [
'title' => t('Access Control Filter - Configurable with Group'),
'field' => 'group_access_control',
'id' => 'access_control_with_group_filter',
],
];

}
4 changes: 3 additions & 1 deletion src/Plugin/search_api/processor/AccessControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public function addFieldValues(ItemInterface $item) {
$value = "200";
}
else {
$value = "403";
$groups = Utilities::getGroupsByNode($entity->id());
sort($groups);
$value = implode(",", $groups);
}


Expand Down
74 changes: 74 additions & 0 deletions src/Plugin/views/filter/AccessControlWithGroupFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Drupal\group_solr\Plugin\views\filter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\filter\StringFilter;
use Drupal\views\Plugin\views\filter\BooleanOperator;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\filter\InOperator;
use Drupal\views\Plugin\views\filter\Standard;
use Drupal\views\ViewExecutable;

/**
* Filter by start and end date.
*
* @ingroup views_filter_handlers
*
* @ViewsFilter("access_control_with_group_filter")
*/
class AccessControlWithGroupFilter extends Standard{

/**
* {@inheritdoc}
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
}

/**
* Override the query so that no filtering takes place if the user doesn't
* select any options.
*/
public function query() {
$query = $this->query;

// Get current user
$uid = \Drupal::currentUser()->id();
$current_user = \Drupal\user\Entity\User::load($uid);

// Get groups which this user is belonged to
$groups = array();
$grp_membership_service = \Drupal::service('group.membership_loader');
$grps = $grp_membership_service->loadByUser($current_user);
foreach ($grps as $grp) {
$groups[$grp->getGroup()->id()] = $grp->getGroup()->label();
}

if (count($groups) > 0) {
// - from those group, get the taxonomy term in field_access_terms
// - from those terms, get term ids
$terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree("islandora_access");
$conditions = $query->createConditionGroup('OR');
$conditions->addCondition("group_access_control", '200', "=");
foreach ($terms as $term) {
if (in_array($term->name, $groups)) {
$conditions->addCondition('group_access_control', $term->name, "IN");
}
}
$query->addConditionGroup($conditions);
}
else {
$query->addCondition('group_access_control', "200", '=');
}
}

public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form["filter-descritpion"] = [
"#markup" => $this->t("Adding this fitler, it will check current user
with the existing access control with Groups and filter the results.")
];
unset($form['expose_button']);
}
}

0 comments on commit 7cde5d8

Please sign in to comment.