refactor, temp fix for username from initals maybe double

This commit is contained in:
einhornimmond 2024-12-16 14:26:24 +01:00
parent 6b68e78b86
commit 7bbdd8d3d4
5 changed files with 51 additions and 32 deletions

View File

@ -0,0 +1,23 @@
import { User } from '@entity/User'
import { PublishNameLogic } from '@/data/PublishName.logic'
import { PublishNameType } from '@/graphql/enum/PublishNameType'
import { Account } from './Account'
import { Profile } from './Profile'
export abstract class AbstractUser {
public constructor(user: User) {
this.account = new Account(user)
this.profile = new Profile(user)
// temp fix for prevent double usernames in humhub, if the username ist created from initials
const publishNameLogic = new PublishNameLogic(user)
if (publishNameLogic.isUsernameFromInitials(user.humhubPublishName as PublishNameType)) {
this.profile.firstname = this.account.username
this.account.username = user.gradidoID
}
}
account: Account
profile: Profile
}

View File

@ -1,19 +1,15 @@
import { User } from '@entity/User'
import { Account } from './Account'
import { Profile } from './Profile'
import { AbstractUser } from './AbstractUser'
export class GetUser {
export class GetUser extends AbstractUser {
public constructor(user: User, id: number) {
super(user)
this.id = id
this.account = new Account(user)
this.profile = new Profile(user)
}
id: number
guid: string
// eslint-disable-next-line camelcase
display_name: string
account: Account
profile: Profile
}

View File

@ -1,16 +1,7 @@
import { User } from '@entity/User'
import { Account } from './Account'
import { AbstractUser } from './AbstractUser'
import { Password } from './Password'
import { Profile } from './Profile'
export class PostUser {
public constructor(user: User) {
this.account = new Account(user)
this.profile = new Profile(user)
}
account: Account
profile: Profile
// only add password as filed, rest the same as AbstractUser
export class PostUser extends AbstractUser {
password: Password
}

View File

@ -67,17 +67,25 @@ export class PublishNameLogic {
* else return user.firstName[0,2] + user.lastName[0,2] for publishNameType = [PUBLISH_NAME_ALIAS_OR_INITALS, PUBLISH_NAME_INITIALS]
*/
public getUsername(publishNameType: PublishNameType): string {
if (
[
PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS,
PublishNameType.PUBLISH_NAME_INITIALS,
].includes(publishNameType)
) {
return publishNameType === PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS && this.hasAlias()
? this.filterOutInvalidChar(this.user.alias)
: this.firstUpperCaseSecondLowerCase(this.filterOutInvalidChar(this.user.firstName)) +
this.firstUpperCaseSecondLowerCase(this.filterOutInvalidChar(this.user.lastName))
if (this.isUsernameFromInitials(publishNameType)) {
return this.filterOutInvalidChar(
this.firstUpperCaseSecondLowerCase(this.user.firstName) +
this.firstUpperCaseSecondLowerCase(this.user.lastName),
)
} else if (this.isUsernameFromAlias(publishNameType)) {
return this.filterOutInvalidChar(this.user.alias)
}
return this.hasAlias() ? this.user.alias : this.user.gradidoID
return this.user.gradidoID
}
public isUsernameFromInitials(publishNameType: PublishNameType): boolean {
return (
PublishNameType.PUBLISH_NAME_INITIALS === publishNameType ||
(PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS === publishNameType && !this.hasAlias())
)
}
public isUsernameFromAlias(publishNameType: PublishNameType): boolean {
return PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS === publishNameType && this.hasAlias()
}
}

View File

@ -34,6 +34,7 @@ import { updateGmsUser } from '@/apis/gms/GmsClient'
import { GmsUser } from '@/apis/gms/model/GmsUser'
import { HumHubClient } from '@/apis/humhub/HumHubClient'
import { GetUser } from '@/apis/humhub/model/GetUser'
import { PostUser } from '@/apis/humhub/model/PostUser'
import { subscribe } from '@/apis/KlicktippController'
import { encode } from '@/auth/JWT'
import { RIGHTS } from '@/auth/RIGHTS'
@ -170,9 +171,9 @@ export class UserResolver {
let humhubUserPromise: Promise<IRestResponse<GetUser>> | undefined
const klicktippStatePromise = getKlicktippState(dbUser.emailContact.email)
if (CONFIG.HUMHUB_ACTIVE && dbUser.humhubAllowed) {
const publishNameLogic = new PublishNameLogic(dbUser)
const getHumhubUser = new PostUser(dbUser)
humhubUserPromise = HumHubClient.getInstance()?.userByUsernameAsync(
publishNameLogic.getUsername(dbUser.humhubPublishName as PublishNameType),
getHumhubUser.account.username,
)
}