Refactor import & export tags + add flags on root.
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -343,12 +343,14 @@ export default class Authorizations {
return Authorizations.canPerformAction(loggedUser, Entity.TAG, Action.UPDATE);
}
- public static async canImportTags(loggedUser: UserToken): Promise<boolean> {
- return Authorizations.canPerformAction(loggedUser, Entity.TAGS, Action.IMPORT);
+ public static async canImportTags(loggedUser: UserToken, authContext?: AuthorizationContext): Promise<AuthorizationResult> {
+ return Authorizations.can(loggedUser, Entity.TAGS, Action.IMPORT, authContext);
+ // return Authorizations.canPerformAction(loggedUser, Entity.TAGS, Action.IMPORT);
}
- public static async canExportTags(loggedUser: UserToken): Promise<boolean> {
- return Authorizations.canPerformAction(loggedUser, Entity.TAGS, Action.EXPORT);
+ public static async canExportTags(loggedUser: UserToken, authContext?: AuthorizationContext): Promise<AuthorizationResult> {
+ return Authorizations.can(loggedUser, Entity.TAGS, Action.EXPORT, authContext);
+ // return Authorizations.canPerformAction(loggedUser, Entity.TAGS, Action.EXPORT);
}
public static async canReadUser(loggedUser: UserToken, authContext?: AuthorizationContext): Promise<AuthorizationResult> {

MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -568,7 +568,7 @@ const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
},
{ resource: Entity.TAGS, action: Action.EXPORT, attributes: ['*'] },
{
- resource: Entity.TAG, action: Action.UPDATE, attributes: ['*'],
+ resource: Entity.TAG, action: [Action.CREATE, Action.UPDATE, Action.DELETE],
condition: {
Fn: 'custom:dynamicAuthorizationFilters',
args: { filters: ['SiteAdminUsers'] }

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -434,6 +434,8 @@ export default class AuthorizationService {
authorizationFilter: AuthorizationFilter): Promise<void> {
// Add canCreate flag to root
tags.canCreate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.TAG, Action.CREATE, authorizationFilter);
+ tags.canImport = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.TAGS, Action.IMPORT, authorizationFilter);
+ tags.canExport = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.TAGS, Action.EXPORT, authorizationFilter);
// Enrich
for (const tag of tags.result) {
await AuthorizationService.addTagAuthorizations(tenant, userToken, tag, authorizationFilter);

MODIFY
src/server/rest/v1/service/TagService.ts
src/server/rest/v1/service/TagService.ts
@@ -339,7 +339,6 @@ export default class TagService {
next();
}
- // todo: refactor
// eslint-disable-next-line @typescript-eslint/require-await
public static async handleImportTags(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Check auth
@@ -540,15 +539,22 @@ export default class TagService {
}
}
- // todo: refactor
public static async handleExportTags(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
+ // Check auth
+ if (!await Authorizations.canExportTags(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.FORBIDDEN,
+ user: req.user,
+ action: Action.IMPORT, entity: Entity.TAGS,
+ module: MODULE_NAME, method: 'handleImportTags'
+ });
+ }
// Export with users
await UtilsService.exportToCSV(req, res, 'exported-tags.csv',
TagService.getTags.bind(this),
TagService.convertToCSV.bind(this));
}
- // todo: refactor
private static async insertTags(tenantID: string, user: UserToken, action: ServerAction, tagsToBeImported: ImportedTag[], result: ActionsResponse): Promise<void> {
try {
const nbrInsertedTags = await TagStorage.saveImportedTags(tenantID, tagsToBeImported);

MODIFY
src/types/DataResult.ts
src/types/DataResult.ts
@@ -24,6 +24,8 @@ export interface SiteAreaDataResult extends DataResult<SiteArea>{
export interface TagDataResult extends DataResult<Tag>{
canCreate: boolean;
+ canImport: boolean;
+ canExport: boolean;
}
export interface TransactionDataResult {

Refactor import & export tags + add flags on root.