Skip to content

Commit

Permalink
Initial commit of the module ported from psul-web.
Browse files Browse the repository at this point in the history
  • Loading branch information
zipymonkey committed Oct 15, 2024
0 parents commit 45129aa
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Development artifacts
.DS_Store
.vscode
/vendor/
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## INTRODUCTION

The PSUL User Auth module is a alter user and authentication for PSU Library
sites.

The primary use case for this module is:

- Ensure username set after authentication is NOT the email.
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "psul-libraries/psul_user_auth",
"description": "Adds functionality to alter users and user authentication",
"type": "drupal-module",
"license": "GPL-2.0-or-later",
"authors": [
{
"name": "Mike Henninger",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {}
}
8 changes: 8 additions & 0 deletions psul_user_auth.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: 'PSUL User Auth'
type: module
description: 'Adds functionality to alter users and user authentication'
package: PSU Libraries
core_version_requirement: ^10 || ^11
dependencies:
- drupal:user
- openid_connect:openid_connect
52 changes: 52 additions & 0 deletions psul_user_auth.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

/**
* @file
* Primary module hooks for PSUL User Auth module.
*/

/**
* Implements hook_openid_connect_userinfo_alter().
*
* Altering the following user data to ensure consist authentication.
* - Email prevent user aliases from being used,
* - Username (should NOT include '@psu.edu'),
* - OIDC Name to include username (e.g. "Henninger, Mike (msh6004)").
*
* All changes are based on the User.Identity.Name (UPN) value.
*/
function psul_user_auth_openid_connect_userinfo_alter(array &$userinfo, array $context) {

// Remove @psu.edu from the username.
preg_match('/^(.+)@([^@]+)$/i', $userinfo['upn'], $matches);

// Nothing else to do if the default email is not an email.
if (!isset($matches[1])) {
return;
}

// Forcing none alias email to be used.
$userinfo['email'] = $userinfo['upn'];

// Adding username to name so that users are unique.
$userinfo['name'] .= " (" . $matches[1] . ")";

// Set the preferred username to strip out "@psu.edu".
$userinfo['preferred_username'] = $matches[1];
}

/**
* Implements hook_user_format_name_alter().
*/
function psul_user_auth_user_format_name_alter(&$name, $account) {
// Ensure that usernames are not displayed if they are email addresses, or if
// they are generated names starting with 'oidc_'.
$oidc_name = \Drupal::service('user.data')->get('openid_connect', $account->id(), 'oidc_name');

if (empty($oidc_name) || strpos($oidc_name, 'oidc_') === 0) {
return;
}

// Always display the oidc_name if it is set.
$name = $oidc_name;
}

0 comments on commit 45129aa

Please sign in to comment.