Compare commits

...

2 Commits

Author SHA1 Message Date
a.bouhuolia
920c8ea95c feat(server): add user transformer 2023-04-14 03:43:39 +02:00
a.bouhuolia
8de3717587 fix(server): prevent deleting last user in the tenant 2023-04-14 03:41:11 +02:00
8 changed files with 105 additions and 38 deletions

View File

@@ -92,6 +92,7 @@ export default class InviteUsersController extends BaseController {
try { try {
await this.inviteUsersService.sendInvite(tenantId, sendInviteDTO, user); await this.inviteUsersService.sendInvite(tenantId, sendInviteDTO, user);
return res.status(200).send({ return res.status(200).send({
type: 'success', type: 'success',
code: 'INVITE.SENT.SUCCESSFULLY', code: 'INVITE.SENT.SUCCESSFULLY',

View File

@@ -25,6 +25,7 @@ import SyncSystemSendInvite from '@/services/InviteUsers/SyncSystemSendInvite';
import InviteSendMainNotification from '@/services/InviteUsers/InviteSendMailNotification'; import InviteSendMainNotification from '@/services/InviteUsers/InviteSendMailNotification';
import SyncTenantAcceptInvite from '@/services/InviteUsers/SyncTenantAcceptInvite'; import SyncTenantAcceptInvite from '@/services/InviteUsers/SyncTenantAcceptInvite';
import SyncTenantUserMutate from '@/services/Users/SyncTenantUserSaved'; import SyncTenantUserMutate from '@/services/Users/SyncTenantUserSaved';
import { SyncTenantUserDelete } from '@/services/Users/SyncTenantUserDeleted';
import OrgSyncTenantAdminUserSubscriber from '@/subscribers/Organization/SyncTenantAdminUser'; import OrgSyncTenantAdminUserSubscriber from '@/subscribers/Organization/SyncTenantAdminUser';
import OrgBuildSmsNotificationSubscriber from '@/subscribers/Organization/BuildSmsNotification'; import OrgBuildSmsNotificationSubscriber from '@/subscribers/Organization/BuildSmsNotification';
import PurgeUserAbilityCache from '@/services/Users/PurgeUserAbilityCache'; import PurgeUserAbilityCache from '@/services/Users/PurgeUserAbilityCache';
@@ -113,6 +114,7 @@ export const susbcribers = () => {
SyncTenantAcceptInvite, SyncTenantAcceptInvite,
InviteSendMainNotification, InviteSendMainNotification,
SyncTenantUserMutate, SyncTenantUserMutate,
SyncTenantUserDelete,
OrgSyncTenantAdminUserSubscriber, OrgSyncTenantAdminUserSubscriber,
OrgBuildSmsNotificationSubscriber, OrgBuildSmsNotificationSubscriber,
PurgeUserAbilityCache, PurgeUserAbilityCache,

View File

@@ -8,7 +8,7 @@ import { Inject, Service } from 'typedi';
@Service() @Service()
export default class InviteSendMainNotificationSubscribe { export default class InviteSendMainNotificationSubscribe {
@Inject('agenda') @Inject('agenda')
agenda: any; private agenda: any;
/** /**
* Attaches events with handlers. * Attaches events with handlers.

View File

@@ -3,7 +3,6 @@ import uniqid from 'uniqid';
import moment from 'moment'; import moment from 'moment';
import { ServiceError } from '@/exceptions'; import { ServiceError } from '@/exceptions';
import TenancyService from '@/services/Tenancy/TenancyService'; import TenancyService from '@/services/Tenancy/TenancyService';
import InviteUsersMailMessages from '@/services/InviteUsers/InviteUsersMailMessages';
import events from '@/subscribers/events'; import events from '@/subscribers/events';
import { import {
ISystemUser, ISystemUser,
@@ -13,7 +12,6 @@ import {
IUserInvitedEventPayload, IUserInvitedEventPayload,
IUserInviteResendEventPayload, IUserInviteResendEventPayload,
} from '@/interfaces'; } from '@/interfaces';
import TenantsManagerService from '@/services/Tenancy/TenantsManager';
import { ERRORS } from './constants'; import { ERRORS } from './constants';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import RolesService from '@/services/Roles/RolesService'; import RolesService from '@/services/Roles/RolesService';
@@ -21,25 +19,13 @@ import RolesService from '@/services/Roles/RolesService';
@Service() @Service()
export default class InviteTenantUserService implements IInviteUserService { export default class InviteTenantUserService implements IInviteUserService {
@Inject() @Inject()
eventPublisher: EventPublisher; private eventPublisher: EventPublisher;
@Inject() @Inject()
tenancy: TenancyService; private tenancy: TenancyService;
@Inject('logger')
logger: any;
@Inject() @Inject()
mailMessages: InviteUsersMailMessages; private rolesService: RolesService;
@Inject('repositories')
sysRepositories: any;
@Inject()
tenantsManager: TenantsManagerService;
@Inject()
rolesService: RolesService;
/** /**
* Sends invite mail to the given email from the given tenant and user. * Sends invite mail to the given email from the given tenant and user.
@@ -99,8 +85,6 @@ export default class InviteTenantUserService implements IInviteUserService {
): Promise<{ ): Promise<{
user: ITenantUser; user: ITenantUser;
}> { }> {
const { User } = this.tenancy.models(tenantId);
// Retrieve the user by id or throw not found service error. // Retrieve the user by id or throw not found service error.
const user = await this.getUserByIdOrThrowError(tenantId, userId); const user = await this.getUserByIdOrThrowError(tenantId, userId);

View File

@@ -10,18 +10,18 @@ export class RoleTransformer extends Transformer {
}; };
/** /**
* * Retrieves the localized role name if is predefined or stored name.
* @param role * @param role
* @returns * @returns {string}
*/ */
public name(role) { public name(role) {
return role.predefined ? this.context.i18n.__(role.name) : role.name; return role.predefined ? this.context.i18n.__(role.name) : role.name;
} }
/** /**
* * Retrieves the localized role description if is predefined or stored description.
* @param role * @param role
* @returns * @returns {string}
*/ */
public description(role) { public description(role) {
return role.predefined return role.predefined

View File

@@ -0,0 +1,26 @@
import events from '@/subscribers/events';
import { ITenantUserDeletedPayload } from '@/interfaces';
import { SystemUser } from '@/system/models';
export class SyncTenantUserDelete {
/**
* Attaches events with handlers.
* @param bus
*/
public attach(bus) {
bus.subscribe(
events.tenantUser.onDeleted,
this.syncSystemUserOnceUserDeleted
);
}
/**
* Deletes the system user once tenant user be deleted.
* @param {ITenantUserDeletedPayload} payload -
*/
private syncSystemUserOnceUserDeleted = async ({
tenantUser,
}: ITenantUserDeletedPayload) => {
await SystemUser.query().where('id', tenantUser.systemUserId).delete();
};
}

View File

@@ -0,0 +1,50 @@
import { Transformer } from '@/lib/Transformer/Transformer';
export class UserTransformer extends Transformer {
/**
* Exclude these attributes from user object.
* @returns {Array}
*/
public excludeAttributes = (): string[] => {
return ['role'];
};
/**
* Includeded attributes.
* @returns {string[]}
*/
public includeAttributes = (): string[] => {
return ['roleName', 'roleDescription', 'roleSlug'];
};
/**
* Retrieves the localized role name if is predefined or stored name.
* @param role
* @returns {string}
*/
public roleName(user) {
return user.role.predefined
? this.context.i18n.__(user.role.name)
: user.role.name;
}
/**
* Retrieves the localized role description if is predefined or stored description.
* @param user
* @returns {string}
*/
public roleDescription(user) {
return user.role.predefined
? this.context.i18n.__(user.role.description)
: user.role.description;
}
/**
* Retrieves the role slug.
* @param user
* @returns {string}
*/
public roleSlug(user) {
return user.role.slug;
}
}

View File

@@ -14,12 +14,11 @@ import RolesService from '@/services/Roles/RolesService';
import HasTenancyService from '@/services/Tenancy/TenancyService'; import HasTenancyService from '@/services/Tenancy/TenancyService';
import { ERRORS } from './constants'; import { ERRORS } from './constants';
import { EventPublisher } from '@/lib/EventPublisher/EventPublisher'; import { EventPublisher } from '@/lib/EventPublisher/EventPublisher';
import { TransformerInjectable } from '@/lib/Transformer/TransformerInjectable';
import { UserTransformer } from './UserTransformer';
@Service() @Service()
export default class UsersService { export default class UsersService {
@Inject('repositories')
private repositories: any;
@Inject() @Inject()
private rolesService: RolesService; private rolesService: RolesService;
@@ -29,6 +28,9 @@ export default class UsersService {
@Inject() @Inject()
private eventPublisher: EventPublisher; private eventPublisher: EventPublisher;
@Inject()
private transformer: TransformerInjectable;
/** /**
* Creates a new user. * Creates a new user.
* @param {number} tenantId - Tenant id. * @param {number} tenantId - Tenant id.
@@ -91,9 +93,10 @@ export default class UsersService {
// Retrieve user details or throw not found service error. // Retrieve user details or throw not found service error.
const tenantUser = await this.getTenantUserOrThrowError(tenantId, userId); const tenantUser = await this.getTenantUserOrThrowError(tenantId, userId);
// Validate the delete user should not be the last user. // Validate the delete user should not be the last active user.
await this.validateNotLastUserDelete(tenantId); if (tenantUser.isInviteAccepted) {
await this.validateNotLastUserDelete(tenantId);
}
// Delete user from the storage. // Delete user from the storage.
await User.query().findById(userId).delete(); await User.query().findById(userId).delete();
@@ -183,7 +186,7 @@ export default class UsersService {
const users = await User.query().withGraphFetched('role'); const users = await User.query().withGraphFetched('role');
return users; return this.transformer.transform(tenantId, users, new UserTransformer());
} }
/** /**
@@ -223,11 +226,13 @@ export default class UsersService {
* @param {number} tenantId * @param {number} tenantId
*/ */
private async validateNotLastUserDelete(tenantId: number) { private async validateNotLastUserDelete(tenantId: number) {
const { systemUserRepository } = this.repositories; const { User } = this.tenancy.models(tenantId);
const usersFound = await systemUserRepository.find({ tenantId }); const inviteAcceptedUsers = await User.query()
.select(['id'])
.whereNotNull('invite_accepted_at');
if (usersFound.length === 1) { if (inviteAcceptedUsers.length === 1) {
throw new ServiceError(ERRORS.CANNOT_DELETE_LAST_USER); throw new ServiceError(ERRORS.CANNOT_DELETE_LAST_USER);
} }
} }
@@ -291,9 +296,9 @@ export default class UsersService {
/** /**
* Validate the authorized user cannot mutate its role. * Validate the authorized user cannot mutate its role.
* @param {ITenantUser} oldTenantUser * @param {ITenantUser} oldTenantUser
* @param {IEditUserDTO} editUserDTO * @param {IEditUserDTO} editUserDTO
* @param {ISystemUser} authorizedUser * @param {ISystemUser} authorizedUser
*/ */
validateMutateRoleNotAuthorizedUser( validateMutateRoleNotAuthorizedUser(
oldTenantUser: ITenantUser, oldTenantUser: ITenantUser,
@@ -307,5 +312,4 @@ export default class UsersService {
throw new ServiceError(ERRORS.CANNOT_AUTHORIZED_USER_MUTATE_ROLE); throw new ServiceError(ERRORS.CANNOT_AUTHORIZED_USER_MUTATE_ROLE);
} }
} }
} }