Add auth flags in User / Site list
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -257,7 +257,10 @@ const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
{ resource: Entity.USER, action: [Action.READ], attributes: ['*'] },
{ resource: Entity.USERS_SITES, action: Action.LIST, attributes: ['*'] },
{ resource: Entity.USERS_SITES, action: Action.UNASSIGN, attributes: ['*'] },
- { resource: Entity.SITE, action: [Action.UPDATE], attributes: ['*'] },
+ {
+ resource: Entity.SITE, action: [Action.UPDATE], attributes: ['*'],
+ condition: { Fn: 'LIST_CONTAINS', args: { 'sitesAdmin': '$.site' } }
+ },
{
resource: Entity.SITE_AREA, action: [Action.CREATE, Action.UPDATE, Action.DELETE], attributes: ['*'],
condition: { Fn: 'LIST_CONTAINS', args: { 'sites': '$.site' } }

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -382,6 +382,9 @@ export default class SiteService {
},
authorizationFilters.project
);
+ // Add Auth flags
+ AuthorizationService.addSitesAuthorizations(req.user, sites.result);
+ // Return
res.json(sites);
next();
}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -681,7 +681,7 @@ export default class UserService {
});
}
// Get authorization filters
- const authorizationFilters = await AuthorizationService.getUserAuthorizationFilters(req.user, req.tenant);
+ const authorizationFilters = await AuthorizationService.checkAndGetUserAuthorizationFilters(req.user, req.tenant);
// Get the user
const user = await UserStorage.getUser(req.user.tenantID, userID,
{
@@ -820,7 +820,7 @@ export default class UserService {
Action.READ, Entity.USER, MODULE_NAME, 'handleGetUsersInError');
}
// Get authorization filters
- const authorizationFilters = await AuthorizationService.getUsersInErrorAuthorizationFilters(
+ const authorizationFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizationFilters(
filteredRequest, req.user, req.tenant);
// Get users
const users = await UserStorage.getUsersInError(req.user.tenantID,
@@ -838,6 +838,8 @@ export default class UserService {
},
authorizationFilters.project
);
+ // Add Auth flags
+ AuthorizationService.addUsersAuthorizations(req.user, users.result);
// Return
res.json(users);
next();
@@ -1154,7 +1156,7 @@ export default class UserService {
Action.READ, Entity.USER, MODULE_NAME, 'getUsers');
}
// Get authorization filters
- const authorizationFilters = await AuthorizationService.getUsersAuthorizationFilters(
+ const authorizationFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizationFilters(
filteredRequest, req.user, req.tenant);
// Get users
const users = await UserStorage.getUsers(req.user.tenantID,
@@ -1178,6 +1180,9 @@ export default class UserService {
},
authorizationFilters.project
);
+ // Add Auth flags
+ AuthorizationService.addUsersAuthorizations(req.user, users.result);
+ // Return
return users;
}
}

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
@@ -146,3 +146,10 @@ export interface AuthorizationContext {
asset?: string;
assets?: string[];
}
+
+export interface AuthorizationActions {
+ canRead?: boolean;
+ canCreate?: boolean;
+ canUpdate?: boolean;
+ canDelete?: boolean;
+}

MODIFY
src/types/Site.ts
src/types/Site.ts
import Address from './Address';
+import { AuthorizationActions } from './Authorization';
import Company from './Company';
import ConnectorStats from './ConnectorStats';
import CreatedUpdatedProps from './CreatedUpdatedProps';
import SiteArea from './SiteArea';
-export default interface Site extends CreatedUpdatedProps {
+export default interface Site extends CreatedUpdatedProps, AuthorizationActions {
id: string;
name: string;
issuer: boolean;

MODIFY
src/types/User.ts
src/types/User.ts
import Address from './Address';
+import { AuthorizationActions } from './Authorization';
import { BillingUserData } from './Billing';
import CreatedUpdatedProps from './CreatedUpdatedProps';
import UserNotifications from './UserNotifications';
-export default interface User extends CreatedUpdatedProps {
+export default interface User extends CreatedUpdatedProps, AuthorizationActions {
id: string;
issuer: boolean;
name: string;

Add auth flags in User / Site list