mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-06-07 01:51:36 -04:00
feat(auth): refactor auth, add oidc
Some checks are pending
Docker / build-and-push (frontend) (push) Waiting to run
Docker / build-and-push (backend) (push) Waiting to run
Deploy HD2 docs to Netlify / Deploys to netlify (push) Waiting to run
E2E Tests / backend-sqlite (push) Waiting to run
E2E Tests / backend-mariadb (push) Waiting to run
E2E Tests / backend-postgres (push) Waiting to run
E2E Tests / Build test build of frontend (push) Waiting to run
E2E Tests / frontend-cypress (1) (push) Blocked by required conditions
E2E Tests / frontend-cypress (2) (push) Blocked by required conditions
E2E Tests / frontend-cypress (3) (push) Blocked by required conditions
Lint and check format / Lint files and check formatting (push) Waiting to run
REUSE Compliance Check / reuse (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Static Analysis / Njsscan code scanning (push) Waiting to run
Static Analysis / CodeQL analysis (push) Waiting to run
Run tests & build / Test and build with NodeJS 20 (push) Waiting to run
Some checks are pending
Docker / build-and-push (frontend) (push) Waiting to run
Docker / build-and-push (backend) (push) Waiting to run
Deploy HD2 docs to Netlify / Deploys to netlify (push) Waiting to run
E2E Tests / backend-sqlite (push) Waiting to run
E2E Tests / backend-mariadb (push) Waiting to run
E2E Tests / backend-postgres (push) Waiting to run
E2E Tests / Build test build of frontend (push) Waiting to run
E2E Tests / frontend-cypress (1) (push) Blocked by required conditions
E2E Tests / frontend-cypress (2) (push) Blocked by required conditions
E2E Tests / frontend-cypress (3) (push) Blocked by required conditions
Lint and check format / Lint files and check formatting (push) Waiting to run
REUSE Compliance Check / reuse (push) Waiting to run
Scorecard supply-chain security / Scorecard analysis (push) Waiting to run
Static Analysis / Njsscan code scanning (push) Waiting to run
Static Analysis / CodeQL analysis (push) Waiting to run
Run tests & build / Test and build with NodeJS 20 (push) Waiting to run
Thanks to all HedgeDoc team members for the time discussing, helping with weird Nest issues, providing feedback and suggestions! Co-authored-by: Philip Molares <philip.molares@udo.edu> Signed-off-by: Philip Molares <philip.molares@udo.edu> Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
parent
1609f3e01f
commit
7f665fae4b
109 changed files with 2927 additions and 1700 deletions
|
@ -1,12 +1,14 @@
|
|||
/*
|
||||
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
|
||||
* SPDX-FileCopyrightText: 2024 The HedgeDoc developers (see AUTHORS file)
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { REGEX_USERNAME } from '@hedgedoc/commons';
|
||||
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository } from 'typeorm';
|
||||
|
||||
import AuthConfiguration, { AuthConfig } from '../config/auth.config';
|
||||
import { AlreadyInDBError, NotInDBError } from '../errors/errors';
|
||||
import { ConsoleLoggerService } from '../logger/console-logger.service';
|
||||
import { Username } from '../utils/username';
|
||||
|
@ -22,6 +24,8 @@ import { User } from './user.entity';
|
|||
export class UsersService {
|
||||
constructor(
|
||||
private readonly logger: ConsoleLoggerService,
|
||||
@Inject(AuthConfiguration.KEY)
|
||||
private authConfig: AuthConfig,
|
||||
@InjectRepository(User) private userRepository: Repository<User>,
|
||||
) {
|
||||
this.logger.setContext(UsersService.name);
|
||||
|
@ -32,11 +36,24 @@ export class UsersService {
|
|||
* Create a new user with a given username and displayName
|
||||
* @param {Username} username - the username the new user shall have
|
||||
* @param {string} displayName - the display name the new user shall have
|
||||
* @param {string} [email] - the email the new user shall have
|
||||
* @param {string} [photoUrl] - the photoUrl the new user shall have
|
||||
* @return {User} the user
|
||||
* @throws {BadRequestException} if the username contains invalid characters or is too short
|
||||
* @throws {AlreadyInDBError} the username is already taken.
|
||||
*/
|
||||
async createUser(username: Username, displayName: string): Promise<User> {
|
||||
const user = User.create(username, displayName);
|
||||
async createUser(
|
||||
username: Username,
|
||||
displayName: string,
|
||||
email?: string,
|
||||
photoUrl?: string,
|
||||
): Promise<User> {
|
||||
if (!REGEX_USERNAME.test(username)) {
|
||||
throw new BadRequestException(
|
||||
`The username '${username}' is not a valid username.`,
|
||||
);
|
||||
}
|
||||
const user = User.create(username, displayName, email, photoUrl);
|
||||
try {
|
||||
return await this.userRepository.save(user);
|
||||
} catch {
|
||||
|
@ -66,13 +83,51 @@ export class UsersService {
|
|||
|
||||
/**
|
||||
* @async
|
||||
* Change the displayName of the specified user
|
||||
* @param {User} user - the user to be changed
|
||||
* @param displayName - the new displayName
|
||||
* Update the given User with the given information.
|
||||
* Use {@code null} to clear the stored value (email or profilePicture).
|
||||
* Use {@code undefined} to keep the stored value.
|
||||
* @param {User} user - the User to update
|
||||
* @param {string | undefined} displayName - the displayName to update the user with
|
||||
* @param {string | null | undefined} email - the email to update the user with
|
||||
* @param {string | null | undefined} profilePicture - the profilePicture to update the user with
|
||||
*/
|
||||
async changeDisplayName(user: User, displayName: string): Promise<void> {
|
||||
user.displayName = displayName;
|
||||
await this.userRepository.save(user);
|
||||
async updateUser(
|
||||
user: User,
|
||||
displayName?: string,
|
||||
email?: string | null,
|
||||
profilePicture?: string | null,
|
||||
): Promise<User> {
|
||||
let shouldSave = false;
|
||||
if (displayName !== undefined) {
|
||||
user.displayName = displayName;
|
||||
shouldSave = true;
|
||||
}
|
||||
if (email !== undefined) {
|
||||
user.email = email;
|
||||
shouldSave = true;
|
||||
}
|
||||
if (profilePicture !== undefined) {
|
||||
user.photo = profilePicture;
|
||||
shouldSave = true;
|
||||
// ToDo: handle LDAP images (https://github.com/hedgedoc/hedgedoc/issues/5032)
|
||||
}
|
||||
if (shouldSave) {
|
||||
return await this.userRepository.save(user);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* @async
|
||||
* Checks if the user with the specified username exists
|
||||
* @param username - the username to check
|
||||
* @return {boolean} true if the user exists, false otherwise
|
||||
*/
|
||||
async checkIfUserExists(username: Username): Promise<boolean> {
|
||||
const user = await this.userRepository.findOne({
|
||||
where: { username: username },
|
||||
});
|
||||
return user !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue