Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor optimization to DefaultRoleService bootstrapAdminUser #412

Merged
merged 1 commit into from
Oct 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
]
}

}
Loading