authentication

This commit is contained in:
Bastian Wagner
2024-09-13 21:14:09 +02:00
parent c00aad559d
commit b4a5f04505
65 changed files with 1140 additions and 77 deletions

View File

@@ -0,0 +1,5 @@
import { Expose } from 'class-transformer';
export function AdminGroup() {
return Expose({ groups: ['admin'] }); // Setzt die Gruppe 'admin' automatisch
}

View File

@@ -1,2 +1,3 @@
export * from './sso.user.entity';
export * from './user.entity';
export * from './role.entity';

View File

@@ -0,0 +1,19 @@
import {
Column,
Entity,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { User } from './user.entity';
@Entity()
export class Role {
@PrimaryGeneratedColumn('uuid')
id: string;
@OneToMany(() => User, (user) => user.role)
user: User[];
@Column({ nullable: true })
name: string;
}

View File

@@ -1,14 +1,17 @@
import { Exclude } from 'class-transformer';
import { Exclude, Transform } from 'class-transformer';
import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
import { IUser } from '../interface';
import { SSOUser } from './sso.user.entity';
import { IsEmail } from 'class-validator';
import { Role } from './role.entity';
@Entity()
export class User implements IUser {
@@ -35,10 +38,14 @@ export class User implements IUser {
@OneToOne(() => SSOUser, (sso) => sso.user, { eager: true, cascade: true })
external: SSOUser;
@Exclude()
@Column({ default: true })
isActive: boolean;
@ManyToOne(() => Role, (role) => role.user, { cascade: true })
@JoinColumn()
@Transform(({ value }) => value.name)
role: Role;
accessToken?: string;
refreshToken?: string;
}

View File

@@ -1,3 +1,5 @@
import { Role } from '../entitites';
export interface IUser {
id: string;
username: string;
@@ -7,4 +9,6 @@ export interface IUser {
accessToken?: string;
refreshToken?: string;
role?: string | Role;
}

View File

@@ -1,2 +1,3 @@
export * from './user.repository';
export * from './ssouser.repository';
export * from './role.repository';

View File

@@ -0,0 +1,14 @@
import { Injectable } from '@nestjs/common';
import { Repository, DataSource } from 'typeorm';
import { Role } from '../entitites';
@Injectable()
export class RoleRepository extends Repository<Role> {
constructor(dataSource: DataSource) {
super(Role, dataSource.createEntityManager());
}
getStandardRole(): Promise<Role> {
return this.findOne({ where: { name: 'develop' } });
}
}

View File

@@ -3,12 +3,14 @@ import { Repository, DataSource } from 'typeorm';
import { User } from '../entitites';
import { CreateUserDto } from '../dto/create-user.dto';
import { SsoUserRepository } from './ssouser.repository';
import { RoleRepository } from './role.repository';
@Injectable()
export class UserRepository extends Repository<User> {
constructor(
dataSource: DataSource,
private ssoRepo: SsoUserRepository,
private roleRepo: RoleRepository,
) {
super(User, dataSource.createEntityManager());
}
@@ -38,6 +40,7 @@ export class UserRepository extends Repository<User> {
externalId: createUserDto.externalId,
});
created.external = sso;
created.role = await this.roleRepo.getStandardRole();
const user = await this.save(created);
sso.user = user;
this.ssoRepo.save(sso);