Appeased codeclimate by extracting out external_auth_id group matching
This commit is contained in:
parent
8169c725d5
commit
9bba84684f
3 changed files with 26 additions and 13 deletions
|
@ -9,24 +9,31 @@ class ExternalAuthService
|
||||||
/**
|
/**
|
||||||
* Check a role against an array of group names to see if it matches.
|
* Check a role against an array of group names to see if it matches.
|
||||||
* Checked against role 'external_auth_id' if set otherwise the name of the role.
|
* Checked against role 'external_auth_id' if set otherwise the name of the role.
|
||||||
* @param \BookStack\Auth\Role $role
|
|
||||||
* @param array $groupNames
|
|
||||||
* @return bool
|
|
||||||
*/
|
*/
|
||||||
protected function roleMatchesGroupNames(Role $role, array $groupNames)
|
protected function roleMatchesGroupNames(Role $role, array $groupNames): bool
|
||||||
{
|
{
|
||||||
if ($role->external_auth_id) {
|
if ($role->external_auth_id) {
|
||||||
$externalAuthIds = explode(',', strtolower($role->external_auth_id));
|
return $this->externalIdMatchesGroupNames($role->external_auth_id, $groupNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
$roleName = str_replace(' ', '-', trim(strtolower($role->display_name)));
|
||||||
|
return in_array($roleName, $groupNames);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if the given external auth ID string matches one of the given group names.
|
||||||
|
*/
|
||||||
|
protected function externalIdMatchesGroupNames(string $externalId, array $groupNames): bool
|
||||||
|
{
|
||||||
|
$externalAuthIds = explode(',', strtolower($externalId));
|
||||||
|
|
||||||
foreach ($externalAuthIds as $externalAuthId) {
|
foreach ($externalAuthIds as $externalAuthId) {
|
||||||
if (in_array(trim($externalAuthId), $groupNames)) {
|
if (in_array(trim($externalAuthId), $groupNames)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
$roleName = str_replace(' ', '-', trim(strtolower($role->display_name)));
|
return false;
|
||||||
return in_array($roleName, $groupNames);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -147,10 +147,9 @@ class Saml2Service extends ExternalAuthService
|
||||||
protected function registerUser(array $userDetails): User
|
protected function registerUser(array $userDetails): User
|
||||||
{
|
{
|
||||||
// Create an array of the user data to create a new user instance
|
// Create an array of the user data to create a new user instance
|
||||||
|
|
||||||
$userData = [
|
$userData = [
|
||||||
'name' => $userDetails['name'],
|
'name' => $userDetails['name'],
|
||||||
'email' => $userDetails['email'] ?? '',
|
'email' => $userDetails['email'],
|
||||||
'password' => Str::random(32),
|
'password' => Str::random(32),
|
||||||
'external_auth_id' => $userDetails['external_id'],
|
'external_auth_id' => $userDetails['external_id'],
|
||||||
'email_confirmed' => true,
|
'email_confirmed' => true,
|
||||||
|
|
|
@ -4,6 +4,13 @@ use BookStack\Auth\Permissions\JointPermission;
|
||||||
use BookStack\Auth\Permissions\RolePermission;
|
use BookStack\Auth\Permissions\RolePermission;
|
||||||
use BookStack\Model;
|
use BookStack\Model;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class Role
|
||||||
|
* @property string $display_name
|
||||||
|
* @property string $description
|
||||||
|
* @property string $external_auth_id
|
||||||
|
* @package BookStack\Auth
|
||||||
|
*/
|
||||||
class Role extends Model
|
class Role extends Model
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue