feat(core): Allow user role modification (#7797)

https://linear.app/n8n/issue/PAY-985

```
PATCH /users/:id/role
  unauthenticated user
    ✓ should receive 401 (349 ms)
  member
    ✓ should fail to demote owner to member (349 ms)
    ✓ should fail to demote owner to admin (359 ms)
    ✓ should fail to demote admin to member (381 ms)
    ✓ should fail to promote other member to owner (353 ms)
    ✓ should fail to promote other member to admin (377 ms)
    ✓ should fail to promote self to admin (354 ms)
    ✓ should fail to promote self to owner (371 ms)
  admin
    ✓ should receive 400 on invalid payload (351 ms)
    ✓ should receive 404 on unknown target user (351 ms)
    ✓ should fail to demote owner to admin (349 ms)
    ✓ should fail to demote owner to member (347 ms)
    ✓ should fail to promote member to owner (384 ms)
    ✓ should fail to promote admin to owner (350 ms)
    ✓ should be able to demote admin to member (354 ms)
    ✓ should be able to demote self to member (350 ms)
    ✓ should be able to promote member to admin (349 ms)
  owner
    ✓ should be able to promote member to admin (349 ms)
    ✓ should be able to demote admin to member (349 ms)
    ✓ should fail to demote self to admin (348 ms)
    ✓ should fail to demote self to member (354 ms)
```
This commit is contained in:
Iván Ovejero
2023-11-24 11:40:08 +01:00
committed by GitHub
parent 87fa3c2985
commit 7a86d36068
7 changed files with 384 additions and 19 deletions

View File

@@ -9,7 +9,7 @@ import { TOTPService } from '@/Mfa/totp.service';
import { MfaService } from '@/Mfa/mfa.service';
import { randomApiKey, randomEmail, randomName, randomValidPassword } from '../random';
import { getGlobalMemberRole, getGlobalOwnerRole } from './roles';
import { getGlobalAdminRole, getGlobalMemberRole, getGlobalOwnerRole } from './roles';
/**
* Store a user in the DB, defaulting to a `member`.
@@ -76,6 +76,10 @@ export async function createMember() {
return createUser({ globalRole: await getGlobalMemberRole() });
}
export async function createAdmin() {
return createUser({ globalRole: await getGlobalAdminRole() });
}
export async function createUserShell(globalRole: Role): Promise<User> {
if (globalRole.scope !== 'global') {
throw new Error(`Invalid role received: ${JSON.stringify(globalRole)}`);
@@ -128,6 +132,12 @@ export const getAllUsers = async () =>
relations: ['globalRole', 'authIdentities'],
});
export const getUserById = async (id: string) =>
Container.get(UserRepository).findOneOrFail({
where: { id },
relations: ['globalRole', 'authIdentities'],
});
export const getLdapIdentities = async () =>
Container.get(AuthIdentityRepository).find({
where: { providerType: 'ldap' },