Skip to content

Commit

Permalink
8.3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pookmish authored Jul 7, 2023
2 parents 9669861 + 3983ecd commit e1a5898
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Stanford SSP

8.3.2
--------------------------------------------------------------------------------
_Release Date: 2023-07-07_

- Add validation for sunetID add user form.

8.3.1
--------------------------------------------------------------------------------
_Release Date: 2023-06-27_
Expand Down
25 changes: 25 additions & 0 deletions src/Form/AddUserForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Password\PasswordGeneratorInterface;
use Drupal\externalauth\AuthmapInterface;
use Drupal\stanford_ssp\Service\StanfordSSPWorkgroupApiInterface;
use Drupal\user\Entity\User;
use Drupal\Component\Utility\EmailValidatorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -77,6 +78,7 @@ public function buildForm(array $form, FormStateInterface $form_state) {
'#title' => $this->t('SUNetID'),
'#description' => $this->t('Enter the SUNetID of the user you wish to add.'),
'#required' => TRUE,
'#element_validate' => [[static::class, 'validateSunetId']],
];

$form['name'] = [
Expand Down Expand Up @@ -119,6 +121,29 @@ public function buildForm(array $form, FormStateInterface $form_state) {
return $form;
}

/**
* SunetID input validation.
*
* @param array $element
* Field element.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* Submitted form state.
* @param array $complete_form
* Complete form.
*/
public static function validateSunetId(array &$element, FormStateInterface $form_state, array &$complete_form){
$value = $element['#value'];
if(!preg_match('/^[a-z0-9]*$/', $value)) {
$form_state->setError($element, t('Invalid SunetID'));
}

/** @var \Drupal\stanford_ssp\Service\StanfordSSPWorkgroupApiInterface $workgroup_api */
$workgroup_api = \Drupal::service('stanford_ssp.workgroup_api');
if ($workgroup_api->connectionSuccessful() && !$workgroup_api->isSunetValid($value)) {
$form_state->setError($element, t('Invalid SunetID'));
}
}

/**
* Get available roles, limited if the role_delegation module is enabled.
*
Expand Down
7 changes: 7 additions & 0 deletions src/Service/StanfordSSPWorkgroupApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ public function isWorkgroupValid(string $workgroup): bool {
return !empty($this->callApi($workgroup));
}

/**
* {@inheritDoc}
*/
public function isSunetValid(string $sunet): bool {
return !!$this->callApi(null, $sunet);
}

/**
* Call the workgroup api and get the response for the workgroup.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Service/StanfordSSPWorkgroupApiInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,15 @@ public function userInAllGroups(array $workgroups, string $name): bool;
*/
public function isWorkgroupValid(string $workgroup): bool;

/**
* Check if the provided sunet exists.
*
* @param string $sunet
* Sunet String to check.
*
* @return bool
* If the user exists.
*/
public function isSunetValid(string $sunet): bool;

}
2 changes: 1 addition & 1 deletion stanford_ssp.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Stanford SimpleSAML PHP
description: Configures SimpleSAML PHP auth to work in Stanford web environment
core_version_requirement: ^9 || ^10
type: module
version: 8.3.1
version: 8.3.2
package: Stanford
dependencies:
- simplesamlphp_auth:simplesamlphp_auth
Expand Down

0 comments on commit e1a5898

Please sign in to comment.