Skip to content

Commit

Permalink
Apply permissions on Exposed form and Query itself. Check for Anonymous
Browse files Browse the repository at this point in the history
  • Loading branch information
DiegoPino committed Jun 20, 2024
1 parent 820f3eb commit adc699b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 16 deletions.
12 changes: 11 additions & 1 deletion src/Plugin/views/filter/StrawberryRunnersMLImagefilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,11 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
);
}

public function isExposed()
{
return parent::isExposed() && ((!$this->currentUser->isAnonymous() && $this->currentUser->hasPermission('execute Image ML queries')) || $this->currentUser->hasRole('administrator'));
}

protected function valueForm(&$form, FormStateInterface $form_state) {
// At this stage $this->value is not set?
$this->value = is_array($this->value) ? $this->value : (array) $this->value;
Expand All @@ -400,7 +405,8 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
'#type' => 'textarea',
'#title' => t('JSON used to query public form'),
'#prefix' => '<div class="views-group-box">',
'#suffix' => '</div>'
'#suffix' => '</div>',
'#access' => !$this->currentUser->isAnonymous() && $this->currentUser->hasPermission('execute Image ML queries') || $this->currentUser->hasRole('administrator'),
] ;
}
}
Expand All @@ -427,6 +433,10 @@ public function buildExposeForm(&$form, FormStateInterface $form_state) {


public function query() {
if ($this->currentUser->isAnonymous() || (!$this->currentUser->hasPermission('execute Image ML queries') && !$this->currentUser->hasRole('administrator'))) {
return;
}

if (empty($this->value) || empty($this->validated_exposed_input) || !$this->getQuery()) {
// basically not validated, not present as a value and also someone cancelled/nuklled the query before?
return;
Expand Down
34 changes: 19 additions & 15 deletions src/Plugin/views/filter/StrawberryRunnersMLTextfilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,23 +309,33 @@ public function submitOptionsForm(&$form, FormStateInterface $form_state) {
);
}

/**
* @inheritDoc
*/
public function isExposed()
{
return parent::isExposed() && ((!$this->currentUser->isAnonymous() && $this->currentUser->hasPermission('execute Text ML queries')) || $this->currentUser->hasRole('administrator'));
}


protected function valueForm(&$form, FormStateInterface $form_state) {
// At this stage $this->value is not set?
$this->value = is_array($this->value) ? $this->value : (array) $this->value;
if (!$form_state->get('exposed')) {
$form['value'] = [
'#type' => 'textarea',
'#title' => t('JSON used to query internal form'),
'#title' => t('Text query to be Vectorized'),
'#prefix' => '<div class="views-group-box">',
'#suffix' => '</div>'
];
}
elseif ($this->isExposed()) {
elseif ($this->isExposed() ) {
$form['value'] = [
'#type' => 'textarea',
'#title' => t('JSON used to query public form'),
'#title' => t('Text query to be vectorized'),
'#prefix' => '<div class="views-group-box">',
'#suffix' => '</div>'
'#suffix' => '</div>',
'#access' => !$this->currentUser->isAnonymous() && $this->currentUser->hasPermission('execute Text ML queries') || $this->currentUser->hasRole('administrator'),
] ;
}
}
Expand All @@ -352,19 +362,13 @@ public function buildExposeForm(&$form, FormStateInterface $form_state) {


public function query() {
if (empty($this->value) || empty($this->validated_exposed_input) || !$this->getQuery()) {
// basically not validated, not present as a value and also someone cancelled/nuklled the query before?
if (empty($this->value) || empty($this->validated_exposed_input) || !$this->getQuery() ||
($this->currentUser->isAnonymous() || (!$this->currentUser->hasPermission('execute Text ML queries') && !$this->currentUser->hasRole('administrator')))
) {
// basically not validated, not present as a value or not the right permisisons.
return;
}
/*
* $this->value = {stdClass}
iiif_image_id = "s3://3b9%2Fimage-dcpl-p034-npsncr-00015-rexported-f2c69aeb-7bcb-434a-a781-e580cb3695b7.tiff"
bbox = {stdClass}
x = {float} 0.0
y = {float} 0.0
w = {float} 1.0
h = {float} 1.0
*/

// Just to be sure here bc we have our own way. Who knows if some external code decides to alter the value
$this->value = $this->validated_exposed_input;
// We should only be at this stage if we have validation
Expand Down

0 comments on commit adc699b

Please sign in to comment.