Browse Source

Assign user role based on admin presence during user creation

pull/5914/head
Germán Martín 2 months ago
parent
commit
6cbf6e2108
  1. 4
      apps/api/src/app/user/user.controller.ts
  2. 7
      apps/api/src/app/user/user.service.ts

4
apps/api/src/app/user/user.controller.ts

@ -126,10 +126,8 @@ export class UserController {
);
}
const hasAdmin = await this.userService.hasAdmin();
const { accessToken, id, role } = await this.userService.createUser({
data: { role: hasAdmin ? 'USER' : 'ADMIN' }
data: {}
});
return {

7
apps/api/src/app/user/user.service.ts

@ -535,6 +535,13 @@ export class UserService {
data.provider = 'ANONYMOUS';
}
// Check if there's already an admin user
// If not, assign ADMIN role to the first user
if (!data.role) {
const hasAdmin = await this.hasAdmin();
data.role = hasAdmin ? 'USER' : 'ADMIN';
}
const user = await this.prismaService.user.create({
data: {
...data,

Loading…
Cancel
Save