Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Admin when empty organization is joined (#135)
Browse files Browse the repository at this point in the history
* When an organization without admins is joined, the user becomes admin.
  • Loading branch information
kad-floriw authored Oct 26, 2021
1 parent f204f60 commit a740ab7
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export class JoinLegalEntityCommandHandler implements ICommandHandler<JoinLegalE
async execute(command: JoinLegalEntityCommand): Promise<void> {
await validateLegalEntity(this.legalEntityRepository, command.legalEntityId);

await this.usersService.grantUserPermissionForOrganization(command.userId, command.legalEntityId);
if (await this.usersService.hasAdmins(command.legalEntityId)) {
await this.usersService.grantUserPermissionForOrganization(command.userId, command.legalEntityId);
} else {
await this.usersService.grantAdminPermissionForOrganization(command.userId, command.legalEntityId);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Logger } from '@nestjs/common';
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs';
import { UserService } from '../../../../command/repositories/user.service';
import { RegisterOidcUserCommand } from '../../../../commons/commands/register-oidc-user.command';
import { UserService } from '../../../repositories/user.service';

@CommandHandler(RegisterOidcUserCommand)
export class RegisterOidcUserCommandHandler implements ICommandHandler<RegisterOidcUserCommand> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class DeviceCountLegalEntityProcessor extends LegalEntityProcessor {
async process(event: LegalEntityEvent, originSync: boolean): Promise<void> {
if (!originSync) {
if (event instanceof LegalEntityRemoved) {
this.listener.processLegalEntityRemoved(event);
await this.listener.processLegalEntityRemoved(event);
}
}
}
Expand Down
14 changes: 9 additions & 5 deletions src/command/repositories/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ export class UserService {

const userInstance = new this.userModel({
_id: id,
email: email,
oidc: oidc,
email,
oidc,
role: 'user',
});

Expand All @@ -35,19 +35,19 @@ export class UserService {

async grantAdminPermissionForOrganization(userId: string, legalEntityId: string): Promise<void> {
this.logger.log(`grant [admin] permissions: [user: ${userId}] [organization: ${legalEntityId}]`);
const userPermissions = { _id: userId, role: UserRole.ADMIN, legalEntityId: legalEntityId };
const userPermissions = { _id: userId, role: UserRole.ADMIN, legalEntityId };
await this.updateUserPermissions({ _id: userId }, userPermissions);
}

async revokeAdminPermissionForOrganization(userId: string, legalEntityId: string): Promise<void> {
this.logger.log(`revoke [admin] permissions: [user: ${userId}] [organization: ${legalEntityId}]`);
const userPermissions = { _id: userId, role: UserRole.USER, legalEntityId: legalEntityId };
const userPermissions = { _id: userId, role: UserRole.USER, legalEntityId };
await this.updateUserPermissions({ _id: userId }, userPermissions);
}

async grantUserPermissionForOrganization(userId: string, legalEntityId: string): Promise<void> {
this.logger.log(`grant [user] permissions: [user: ${userId}] [organization: ${legalEntityId}]`);
const userPermissions = { _id: userId, role: UserRole.USER, legalEntityId: legalEntityId };
const userPermissions = { _id: userId, role: UserRole.USER, legalEntityId };
await this.updateUserPermissions({ _id: userId }, userPermissions);
}

Expand All @@ -69,4 +69,8 @@ export class UserService {
): Promise<IUserPermissions | undefined> {
return this.userPermissionsModel.updateOne(filter, update, { new: true, upsert: true });
}

async hasAdmins(legalEntityId: string): Promise<boolean> {
return (await this.userPermissionsModel.count({ legalEntityId, role: UserRole.ADMIN })) > 0;
}
}

0 comments on commit a740ab7

Please sign in to comment.