Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.
TypeScript cloud-alternativecollaborationcollaborative-editingdropboxfile-serverfile-sharingfile-storagefile-syncfull-text-searchhomelabnextcloudon-premiseopen-sourceprivacy-firstprivate-cloudrealtimesecureself-hostedstorage-serverwebdav
feat(backend:auth): add `adminGroup` support and improve LDAP user role assignment
johaven9074145c9c86
diff --git a/backend/src/authentication/auth.config.ts b/backend/src/authentication/auth.config.tsindex ac2d33e3..37f0ceda 100644--- a/backend/src/authentication/auth.config.ts+++ b/backend/src/authentication/auth.config.ts@@ -145,6 +145,10 @@ export class AuthMethodLdapConfig {@ValidateNested()@Type(() => AuthMethodLdapAttributesConfig)attributes: AuthMethodLdapAttributesConfig = new AuthMethodLdapAttributesConfig()++ @IsOptional()+ @IsString()+ adminGroup?: string}export class AuthConfig {diff --git a/backend/src/authentication/constants/auth-ldap.ts b/backend/src/authentication/constants/auth-ldap.tsindex eccbd7c3..24c7b11c 100644--- a/backend/src/authentication/constants/auth-ldap.ts+++ b/backend/src/authentication/constants/auth-ldap.ts@@ -15,7 +15,8 @@ export const LDAP_COMMON_ATTR = {GIVEN_NAME: 'givenName',SN: 'sn',CN: 'cn',- DISPLAY_NAME: 'displayName'+ DISPLAY_NAME: 'displayName',+ MEMBER_OF: 'memberOf'} as constexport const ALL_LDAP_ATTRIBUTES = [...Object.values(LDAP_LOGIN_ATTR), ...Object.values(LDAP_COMMON_ATTR)]diff --git a/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts b/backend/src/authentication/services/auth-methods/auth-method-ldap.service.tsindex 439c4dc9..bc8e9526 100644--- a/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts+++ b/backend/src/authentication/services/auth-methods/auth-method-ldap.service.ts@@ -23,11 +23,12 @@ type LdapUserEntry = Entry & Record<LDAP_LOGIN_ATTR | (typeof LDAP_COMMON_ATTR)[@Injectable()export class AuthMethodLdapService implements AuthMethod {private readonly logger = new Logger(AuthMethodLdapService.name)- private readonly loginAttribute: LDAP_LOGIN_ATTR = configuration.auth.ldap.attributes.login- private readonly emailAttribute: string = configuration.auth.ldap.attributes.emailprivate readonly servers: string[] = configuration.auth.ldap.serversprivate readonly baseDN: string = configuration.auth.ldap.baseDNprivate readonly filter: string = configuration.auth.ldap.filter+ private readonly loginAttribute: LDAP_LOGIN_ATTR = configuration.auth.ldap.attributes.login+ private readonly emailAttribute: string = configuration.auth.ldap.attributes.email+ private readonly adminGroup: string = configuration.auth.ldap.adminGroupprivate clientOptions: ClientOptions = { timeout: 6000, connectTimeout: 6000, url: '' }constructor(@@ -141,7 +142,8 @@ export class AuthMethodLdapService implements AuthMethod {private async updateOrCreateUser(identity: CreateUserDto, user: UserModel): Promise<UserModel> {if (user === null) {- const createdUser = await this.adminUsersManager.createUserOrGuest(identity, USER_ROLE.USER)+ // create+ const createdUser = await this.adminUsersManager.createUserOrGuest(identity, identity.role)const freshUser = await this.usersManager.fromUserId(createdUser.id)if (!freshUser) {this.logger.error(`${this.updateOrCreateUser.name} - user was not found : ${createdUser.login} (${createdUser.id})`)@@ -153,7 +155,7 @@ export class AuthMethodLdapService implements AuthMethod {this.logger.error(`${this.updateOrCreateUser.name} - user login mismatch : ${identity.login} !== ${user.login}`)throw new HttpException('Account matching error', HttpStatus.FORBIDDEN)}- // check if user information has changed+ // update: check if user information has changedconst identityHasChanged: UpdateUserDto = Object.fromEntries((await Promise.all(@@ -169,7 +171,15 @@ export class AuthMethodLdapService implements AuthMethod {)if (Object.keys(identityHasChanged).length > 0) {try {+ if (identityHasChanged?.role != null) {+ if (user.role === USER_ROLE.ADMINISTRATOR && !this.adminGroup) {+ // Prevent removing admin role when adminGroup was removed or not defined+ delete identityHasChanged.role+ }+ }+ // Update user propertiesawait this.adminUsersManager.updateUserOrGuest(user.id, identityHasChanged)+ // Extra stuffif (identityHasChanged?.password) {delete identityHasChanged.password}@@ -187,7 +197,15 @@ export class AuthMethodLdapService implements AuthMethod {private convertToLdapUserEntry(entry: Entry): LdapUserEntry {for (const attr of ALL_LDAP_ATTRIBUTES) {+ if (attr === LDAP_COMMON_ATTR.MEMBER_OF && entry[attr]) {+ entry[attr] = (Array.isArray(entry[attr]) ? entry[attr] : entry[attr] ? [entry[attr]] : [])+ .filter((v: any) => typeof v === 'string')+ .map((v) => v.match(/cn\s*=\s*([^,]+)/i)?.[1]?.trim())+ .filter(Boolean)+ continue+ }if (Array.isArray(entry[attr])) {+ // Keep only the first value for all other attributes (e.g., email)entry[attr] = entry[attr].length > 0 ? entry[attr][0] : null}}@@ -195,10 +213,12 @@ export class AuthMethodLdapService implements AuthMethod {}private createIdentity(entry: LdapUserEntry, password: string): CreateUserDto {+ const isAdmin = typeof this.adminGroup === 'string' && this.adminGroup && entry[LDAP_COMMON_ATTR.MEMBER_OF]?.includes(this.adminGroup)return {login: this.normalizeLogin(entry[this.loginAttribute]),email: entry[this.emailAttribute] as string,password: password,+ role: isAdmin ? USER_ROLE.ADMINISTRATOR : USER_ROLE.USER,...this.getFirstNameAndLastName(entry)} satisfies CreateUserDto}diff --git a/environment/environment.dist.yaml b/environment/environment.dist.yamlindex a96aa4d8..34e5b581 100755--- a/environment/environment.dist.yaml+++ b/environment/environment.dist.yaml@@ -115,7 +115,7 @@ auth:ldap:# e.g: [ldap://localhost:389, ldaps://localhost:636] (array required)servers: []- # baseDN: distinguished name ( e.g.ou=people,dc=ldap,dc=sync-in,dc=com)+ # baseDN: distinguished name (e.g., ou=people,dc=ldap,dc=sync-in,dc=com)baseDN:# filter, e.g: (acl=admin)filter:@@ -126,6 +126,8 @@ auth:# email attribute: `mail` or `email`# default: `mail`email: mail+ # adminGroup: The CN of a group containing Sync-in administrators (e.g., administrators)+ adminGroup:applications:files:# required