Skip to content

Commit

Permalink
Minor optimization to DefaultRoleService bootstrapAdminUser
Browse files Browse the repository at this point in the history
+ Resolve possible bootstrapAdminUser setting once, on init, and log if active.
+ Simplify `getRolesForUser` accordingly - this can get called in hot loops with permission checks, want to do anything we can to keep it lean and mean.
  • Loading branch information
amcclain committed Oct 12, 2024
1 parent 981272d commit d9395fd
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions src/main/groovy/io/xh/hoist/role/provided/DefaultRoleService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ import static java.util.Collections.*
*/
class DefaultRoleService extends BaseRoleService {

static clearCachesConfigs = ['xhRoleModuleConfig']

ConfigService configService
LdapService ldapService
DefaultRoleUpdateService defaultRoleUpdateService
Expand All @@ -95,11 +97,22 @@ class DefaultRoleService extends BaseRoleService {
// Local state for primary when computing role assignment
protected Map<String, Object> _usersForDirectoryGroups = emptyMap()

static clearCachesConfigs = ['xhRoleModuleConfig']
// Support granting key Hoist admin roles to an instance-configured user in local dev only,
// for initial bootstrapping during development when databased roles not yet created.
private String bootstrapAdminUser = null
private final Set<String> bootstrapAdminRoles = ['HOIST_ADMIN', 'HOIST_ADMIN_READER', 'HOIST_ROLE_MANAGER']

void init() {
ensureRequiredConfigAndRolesCreated()

if (isLocalDevelopment && !isProduction) {
bootstrapAdminUser = getInstanceConfig('bootstrapAdminUser')?.toLowerCase()
if (bootstrapAdminUser) {
logInfo("$bootstrapAdminUser configured as local development bootstrapAdminUser - will be granted $bootstrapAdminRoles")
}
}


timer = createTimer(
name: 'refreshRoles',
runFn: this.&refreshRoleAssignments,
Expand Down Expand Up @@ -129,13 +142,11 @@ class DefaultRoleService extends BaseRoleService {
}
ret = _roleAssignmentsByUser[username] = unmodifiableSet(userRoles) as Set<String>
}
if (
getInstanceConfig('bootstrapAdminUser')?.toLowerCase() == username &&
isLocalDevelopment &&
!isProduction
) {
ret += ['HOIST_ADMIN', 'HOIST_ADMIN_READER', 'HOIST_ROLE_MANAGER']

if (bootstrapAdminUser == username) {
ret += bootstrapAdminRoles
}

ret
}

Expand Down Expand Up @@ -189,7 +200,7 @@ class DefaultRoleService extends BaseRoleService {
protected Map<String, Object> doLoadUsersForDirectoryGroups(Set<String> groups, boolean strictMode) {
if (!groups) return emptyMap()
if (!ldapService.enabled) {
return groups.collectEntries{[it, 'LdapService not enabled in this application']}
return groups.collectEntries { [it, 'LdapService not enabled in this application'] }
}

def foundGroups = new HashSet(),
Expand All @@ -198,7 +209,7 @@ class DefaultRoleService extends BaseRoleService {
// 1) Determine valid groups
ldapService
.lookupGroups(groups, strictMode)
.each {name, group ->
.each { name, group ->
if (group) {
foundGroups << name
} else {
Expand All @@ -209,7 +220,7 @@ class DefaultRoleService extends BaseRoleService {
// 2) Search for members of valid groups
ldapService
.lookupGroupMembers(foundGroups, strictMode)
.each {name, members ->
.each { name, members ->
ret[name] = members.collect(new HashSet()) { it.samaccountname?.toLowerCase() }
// Exclude members without a samaccountname (e.g. email-only contacts within a DL)
ret[name].remove(null)
Expand All @@ -234,7 +245,7 @@ class DefaultRoleService extends BaseRoleService {
xhRoleModuleConfig: [
valueType : 'json',
defaultValue: [
refreshIntervalSecs : 300
refreshIntervalSecs: 300
],
groupName : 'xh.io',
note : 'Configures built-in role management via DefaultRoleService.'
Expand Down Expand Up @@ -344,8 +355,8 @@ class DefaultRoleService extends BaseRoleService {

roles.collectEntries { role ->
Set<Role> effectiveRoles = getEffectiveRoles(role),
users = new HashSet(),
groups = new HashSet()
users = new HashSet(),
groups = new HashSet()

effectiveRoles.each { effRole ->
if (userAssignmentSupported) users.addAll(effRole.users)
Expand Down Expand Up @@ -396,10 +407,12 @@ class DefaultRoleService extends BaseRoleService {
}


Map getAdminStats() {[
roleAssignments: allRoleAssignments?.size(),
roleAssignmentsByUser: _roleAssignmentsByUser?.size(),
usersForDirectoryGroups: _usersForDirectoryGroups?.size()
]}
Map getAdminStats() {
[
roleAssignments : allRoleAssignments?.size(),
roleAssignmentsByUser : _roleAssignmentsByUser?.size(),
usersForDirectoryGroups: _usersForDirectoryGroups?.size()
]
}

}

0 comments on commit d9395fd

Please sign in to comment.