forward to settings on auto link generation, check first if user exist and is enabled

This commit is contained in:
einhornimmond 2024-05-10 14:30:06 +02:00
parent 83361ea17d
commit 7650304981
5 changed files with 31 additions and 13 deletions

View File

@ -1,4 +1,3 @@
import { User } from '@entity/User'
import { SignJWT } from 'jose'
import { IRequestOptions, IRestResponse, RestClient } from 'typed-rest-client'
@ -6,6 +5,7 @@ import { CONFIG } from '@/config'
import { LogError } from '@/server/LogError'
import { backendLogger as logger } from '@/server/logger'
import { PostUserLoggingView } from './logging/PostUserLogging.view'
import { GetUser } from './model/GetUser'
import { PostUser } from './model/PostUser'
import { UsersResponse } from './model/UsersResponse'
@ -59,9 +59,8 @@ export class HumHubClient {
return token
}
public async createAutoLoginUrl(user: User) {
public async createAutoLoginUrl(username: string) {
const secret = new TextEncoder().encode(CONFIG.HUMHUB_JWT_KEY)
const username = user.alias ?? user.gradidoID
logger.info(`user ${username} as username for humhub auto-login`)
const token = await new SignJWT({ username })
.setProtectedHeader({ alg: 'HS256' })
@ -109,6 +108,11 @@ export class HumHubClient {
return this.restClient.get<GetUser>('/api/v1/user/get-by-email', options)
}
public async userByUsernameAsync(username: string): Promise<IRestResponse<GetUser>> {
const options = await this.createRequestOptions({ username })
return this.restClient.get<GetUser>('/api/v1/user/get-by-username', options)
}
/**
* get user by username
* https://marketplace.humhub.com/module/rest/docs/html/user.html#tag/User/paths/~1user~1get-by-username/get
@ -130,7 +134,7 @@ export class HumHubClient {
* @param user for saving on humhub instance
*/
public async createUser(user: PostUser): Promise<void> {
logger.info('create new humhub user', user)
logger.info('create new humhub user', new PostUserLoggingView(user))
const options = await this.createRequestOptions()
try {
const response = await this.restClient.create('/api/v1/user', user, options)
@ -153,7 +157,7 @@ export class HumHubClient {
* @returns updated user object on success
*/
public async updateUser(user: PostUser, humhubUserId: number): Promise<GetUser | null> {
logger.info('update humhub user', user)
logger.info('update humhub user', new PostUserLoggingView(user))
const options = await this.createRequestOptions()
const response = await this.restClient.update<GetUser>(
`/api/v1/user/${humhubUserId}`,

View File

@ -168,7 +168,7 @@ export class UserResolver {
let humhubUserPromise: Promise<IRestResponse<GetUser>> | undefined
const klicktippStatePromise = getKlicktippState(dbUser.emailContact.email)
if (CONFIG.HUMHUB_ACTIVE && dbUser.humhubAllowed) {
humhubUserPromise = HumHubClient.getInstance()?.userByEmailAsync(email)
humhubUserPromise = HumHubClient.getInstance()?.userByUsernameAsync(email)
}
if (dbUser.passwordEncryptionType !== PasswordEncryptionType.GRADIDO_ID) {
@ -726,7 +726,15 @@ export class UserResolver {
if (!humhubClient) {
throw new LogError('cannot create humhub client')
}
return await humhubClient.createAutoLoginUrl(dbUser)
const username = dbUser.alias ?? dbUser.gradidoID
const humhubUser = await humhubClient.userByUsername(username)
if (!humhubUser) {
throw new LogError("user don't exist (any longer) on humhub")
}
if (humhubUser.account.status !== 1) {
throw new LogError('user status is not 1', humhubUser.account.status)
}
return await humhubClient.createAutoLoginUrl(username)
}
@Authorized([RIGHTS.SEARCH_ADMIN_USERS])

View File

@ -28,7 +28,7 @@ export async function syncHumhub(
return
}
logger.debug('retrieve user from humhub')
const humhubUser = await humhubClient.userByEmail(user.emailContact.email)
const humhubUser = await humhubClient.userByUsername(user.alias ?? user.gradidoID)
const humhubUsers = new Map<string, GetUser>()
if (humhubUser) {
humhubUsers.set(user.emailContact.email, humhubUser)

View File

@ -59,8 +59,11 @@ export default {
this.enableButton = true
})
.catch(() => {
this.toastError('authenticateHumhubAutoLogin failed!')
// this.toastError('authenticateHumhubAutoLogin failed!')
this.enableButton = true
// something went wrong with login link so we disable humhub
this.$store.commit('humhubAllowed', false)
this.$router.push('/settings/extern')
})
},
},

View File

@ -80,9 +80,9 @@
</b-row>
</b-tab>
<div v-if="isCommunityService">
<b-tab :title="$t('settings.community')">
<b-tab class="community-service-tabs" :title="$t('settings.community')">
<div class="h2">{{ $t('settings.allow-community-services') }}</div>
<div v-if="isHumhub" class="">
<div v-if="isHumhub" class="mt-3">
<b-row>
<b-col cols="12" md="6" lg="6">
<div class="h3">{{ $t('Humhub.title') }}</div>
@ -113,7 +113,7 @@
</b-col>
</b-row>
</div>
<div v-if="isGMS">
<div v-if="isGMS" class="mt-3">
<b-row>
<b-col cols="12" md="6" lg="6">
<div class="h3 text-muted">{{ $t('GMS.title') }}</div>
@ -129,7 +129,7 @@
/>
</b-col>
</b-row>
<div class="h4 mt-3">{{ $t('GMS.desc') }}</div>
<div class="h4 mt-3 text-muted">{{ $t('GMS.desc') }}</div>
<div v-if="gmsAllowed">
<b-row class="mb-4">
<b-col cols="12" md="6" lg="6">
@ -287,6 +287,9 @@ export default {
}
</script>
<style>
.community-service-tabs {
min-height: 315px;
}
.card-border-radius {
border-radius: 0px 5px 5px 0px !important;
}