feat(users): add updateAdmin endpoint and enhance role assignment
- Introduced `PATCH /admin/:uuid` endpoint for admin-specific user updates. - Updated `update` logic to handle role assignment via `rbacService`. - Refactored `findAll` method in repository for improved readability.
This commit is contained in:
@@ -64,7 +64,7 @@ export class UsersRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async findAll(limit: number, offset: number) {
|
async findAll(limit: number, offset: number) {
|
||||||
return await this.databaseService.db
|
const result = await this.databaseService.db
|
||||||
.select({
|
.select({
|
||||||
uuid: users.uuid,
|
uuid: users.uuid,
|
||||||
username: users.username,
|
username: users.username,
|
||||||
@@ -77,6 +77,8 @@ export class UsersRepository {
|
|||||||
.from(users)
|
.from(users)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.offset(offset);
|
.offset(offset);
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async findByUsername(username: string) {
|
async findByUsername(username: string) {
|
||||||
|
|||||||
@@ -112,6 +112,16 @@ export class UsersController {
|
|||||||
return this.usersService.remove(uuid);
|
return this.usersService.remove(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Patch("admin/:uuid")
|
||||||
|
@UseGuards(AuthGuard, RolesGuard)
|
||||||
|
@Roles("admin")
|
||||||
|
updateAdmin(
|
||||||
|
@Param("uuid") uuid: string,
|
||||||
|
@Body() updateUserDto: UpdateUserDto,
|
||||||
|
) {
|
||||||
|
return this.usersService.update(uuid, updateUserDto);
|
||||||
|
}
|
||||||
|
|
||||||
// Double Authentification (2FA)
|
// Double Authentification (2FA)
|
||||||
@Post("me/2fa/setup")
|
@Post("me/2fa/setup")
|
||||||
@UseGuards(AuthGuard)
|
@UseGuards(AuthGuard)
|
||||||
|
|||||||
@@ -100,7 +100,14 @@ export class UsersService {
|
|||||||
|
|
||||||
async update(uuid: string, data: UpdateUserDto) {
|
async update(uuid: string, data: UpdateUserDto) {
|
||||||
this.logger.log(`Updating user profile for ${uuid}`);
|
this.logger.log(`Updating user profile for ${uuid}`);
|
||||||
const result = await this.usersRepository.update(uuid, data);
|
|
||||||
|
const { role, ...userData } = data;
|
||||||
|
|
||||||
|
const result = await this.usersRepository.update(uuid, userData);
|
||||||
|
|
||||||
|
if (role) {
|
||||||
|
await this.rbacService.assignRoleToUser(uuid, role);
|
||||||
|
}
|
||||||
|
|
||||||
if (result[0]) {
|
if (result[0]) {
|
||||||
await this.clearUserCache(result[0].username);
|
await this.clearUserCache(result[0].username);
|
||||||
|
|||||||
Reference in New Issue
Block a user