Add auths on assign/unassign users/sites + fixed unit tests
MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -178,6 +178,26 @@ export default class SiteService {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleAssignUsersToSite');
+ // Check auth
+ if (action === ServerAction.ADD_USERS_TO_SITE) {
+ if (!Authorizations.canAssignUsersSites(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.ASSIGN, entity: Entity.USERS_SITES,
+ module: MODULE_NAME, method: 'checkAndAssignSiteUsersAuthorizationFilters'
+ });
+ }
+ } else {
+ if (!Authorizations.canUnassignUsersSites(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.UNASSIGN, entity: Entity.USERS_SITES,
+ module: MODULE_NAME, method: 'checkAndAssignSiteUsersAuthorizationFilters'
+ });
+ }
+ }
// Filter
const filteredRequest = SiteSecurity.filterAssignSiteUsers(req.body);
// Check
@@ -193,7 +213,7 @@ export default class SiteService {
}
// Check auth
await AuthorizationService.checkAndAssignSiteUsersAuthorizationFilters(
- action, filteredRequest, req.user, req.tenant);
+ req.tenant, action, req.user, filteredRequest);
// Get the Site
const site = await SiteStorage.getSite(req.user.tenantID, filteredRequest.siteID);
UtilsService.assertObjectExists(action, site, `Site '${filteredRequest.siteID}' does not exist`,
@@ -230,6 +250,15 @@ export default class SiteService {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.SITE, MODULE_NAME, 'handleGetUsersFromSite');
+ // Check auth
+ if (!Authorizations.canListUsersSites(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.LIST, entity: Entity.USERS_SITES,
+ module: MODULE_NAME, method: 'handleGetUsers'
+ });
+ }
// Filter
const filteredRequest = SiteSecurity.filterSiteUsersRequest(req.query);
// Check Mandatory fields
@@ -244,7 +273,7 @@ export default class SiteService {
}
// Check auth
const authorizationFilters = await AuthorizationService.checkAndGetSiteUsersAuthorizationFilters(
- filteredRequest, req.user, req.tenant);
+ req.tenant, req.user, filteredRequest);
if (!authorizationFilters.authorized) {
UtilsService.sendEmptyDataResult(res, next);
return;
@@ -324,12 +353,21 @@ export default class SiteService {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.READ, Entity.SITE, MODULE_NAME, 'handleGetSite');
+ // Check auth
+ if (!Authorizations.canReadSite(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.READ, entity: Entity.SITE,
+ module: MODULE_NAME, method: 'checkAndGetSiteAuthorizationFilters',
+ });
+ }
// Filter
const filteredRequest = SiteSecurity.filterSiteRequest(req.query);
UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetSite', req.user);
// Check auth
const authorizationFilters = await AuthorizationService.checkAndGetSiteAuthorizationFilters(
- filteredRequest.ID, req.user, req.tenant);
+ req.tenant, req.user, filteredRequest.ID);
// Get it
const site = await SiteStorage.getSite(req.user.tenantID, filteredRequest.ID,
{
@@ -350,11 +388,20 @@ export default class SiteService {
// Check if component is active
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.LIST, Entity.SITES, MODULE_NAME, 'handleGetSites');
+ // Check auth
+ if (!Authorizations.canListSites(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.LIST, entity: Entity.SITES,
+ module: MODULE_NAME, method: 'handleGetSites'
+ });
+ }
// Filter
const filteredRequest = SiteSecurity.filterSitesRequest(req.query);
// Check auth
const authorizationFilters = await AuthorizationService.checkAndGetSitesAuthorizationFilters(
- filteredRequest, req.user, req.tenant);
+ req.tenant, req.user, filteredRequest);
if (!authorizationFilters.authorized) {
UtilsService.sendEmptyDataResult(res, next);
return;
@@ -383,7 +430,7 @@ export default class SiteService {
authorizationFilters.project
);
// Add Auth flags
- AuthorizationService.addSitesAuthorizations(req.user, sites.result);
+ await AuthorizationService.addSitesAuthorizations(req.tenant, req.user, sites.result);
// Return
res.json(sites);
next();

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -139,7 +139,7 @@ export default class UserService {
action: action
});
}
- if (!filteredRequest.siteIDs || (filteredRequest.siteIDs && filteredRequest.siteIDs.length <= 0)) {
+ if (!filteredRequest.siteIDs || Utils.isEmptyArray(filteredRequest.siteIDs)) {
throw new AppError({
source: Constants.CENTRAL_SERVER,
errorCode: HTTPError.GENERAL_ERROR,
@@ -681,7 +681,7 @@ export default class UserService {
});
}
// Get authorization filters
- const authorizationFilters = await AuthorizationService.checkAndGetUserAuthorizationFilters(req.user, req.tenant);
+ const authorizationFilters = await AuthorizationService.checkAndGetUserAuthorizationFilters(req.tenant, req.user);
// Get the user
const user = await UserStorage.getUser(req.user.tenantID, userID,
{
@@ -754,28 +754,45 @@ export default class UserService {
public static async handleGetSites(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ORGANIZATION,
Action.UPDATE, Entity.USER, MODULE_NAME, 'handleGetSites');
+ // Check auth
+ if (!Authorizations.canListUsersSites(req.user)) {
+ throw new AppAuthError({
+ errorCode: HTTPAuthError.ERROR,
+ user: req.user,
+ action: Action.LIST, entity: Entity.USERS_SITES,
+ module: MODULE_NAME, method: 'handleGetSites'
+ });
+ }
// Filter
const filteredRequest = UserSecurity.filterUserSitesRequest(req.query);
UtilsService.assertIdIsProvided(action, filteredRequest.UserID, MODULE_NAME, 'handleGetSites', req.user);
- // Get User
- const user = await UserStorage.getUser(req.user.tenantID, filteredRequest.UserID);
- UtilsService.assertObjectExists(action, user, `User with ID '${filteredRequest.UserID}' does not exist`,
- MODULE_NAME, 'handleGetSites', req.user);
// Check auth
- if (!Authorizations.canUpdateUser(req.user, filteredRequest.UserID)) {
+ if (!Authorizations.canReadUser(req.user, filteredRequest.UserID)) {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
action: Action.UPDATE, entity: Entity.USER,
module: MODULE_NAME, method: 'handleGetSites',
- value: user.id
+ value: filteredRequest.UserID
});
}
+ // Get User
+ const user = await UserStorage.getUser(req.user.tenantID, filteredRequest.UserID);
+ UtilsService.assertObjectExists(action, user, `User with ID '${filteredRequest.UserID}' does not exist`,
+ MODULE_NAME, 'handleGetSites', req.user);
+ // Check auth
+ const authorizationFilters = await AuthorizationService.checkAndGetUserSitesAuthorizationFilters(
+ req.tenant, req.user, filteredRequest);
+ if (!authorizationFilters.authorized) {
+ UtilsService.sendEmptyDataResult(res, next);
+ return;
+ }
// Get users
const userSites = await UserStorage.getUserSites(req.user.tenantID,
{
search: filteredRequest.Search,
- userID: filteredRequest.UserID
+ userID: filteredRequest.UserID,
+ ...authorizationFilters.filters
},
{
limit: filteredRequest.Limit,
@@ -783,7 +800,7 @@ export default class UserService {
sort: filteredRequest.Sort,
onlyRecordCount: filteredRequest.OnlyRecordCount
},
- ['site.id', 'site.name', 'site.address.city', 'site.address.country', 'siteAdmin', 'siteOwner', 'userID']
+ authorizationFilters.project
);
// Filter
userSites.result = userSites.result.map((userSite) => ({
@@ -821,7 +838,7 @@ export default class UserService {
}
// Get authorization filters
const authorizationFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizationFilters(
- filteredRequest, req.user, req.tenant);
+ req.tenant, req.user, filteredRequest);
// Get users
const users = await UserStorage.getUsersInError(req.user.tenantID,
{
@@ -839,7 +856,7 @@ export default class UserService {
authorizationFilters.project
);
// Add Auth flags
- AuthorizationService.addUsersAuthorizations(req.user, users.result);
+ await AuthorizationService.addUsersAuthorizations(req.tenant, req.user, users.result);
// Return
res.json(users);
next();
@@ -1136,11 +1153,11 @@ export default class UserService {
private static async getUsers(req: Request): Promise<DataResult<User>> {
// Check auth
- if (!Authorizations.canListUsersSites(req.user)) {
+ if (!Authorizations.canListUsers(req.user)) {
throw new AppAuthError({
errorCode: HTTPAuthError.ERROR,
user: req.user,
- action: Action.LIST, entity: Entity.USERS_SITES,
+ action: Action.LIST, entity: Entity.USERS,
module: MODULE_NAME, method: 'getUsers'
});
}
@@ -1156,8 +1173,8 @@ export default class UserService {
Action.READ, Entity.USER, MODULE_NAME, 'getUsers');
}
// Get authorization filters
- const authorizationFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizationFilters(
- filteredRequest, req.user, req.tenant);
+ const authorizationFilters = await AuthorizationService.checkAndGetUsersAuthorizationFilters(
+ req.tenant, req.user, filteredRequest);
// Get users
const users = await UserStorage.getUsers(req.user.tenantID,
{
@@ -1181,7 +1198,7 @@ export default class UserService {
authorizationFilters.project
);
// Add Auth flags
- AuthorizationService.addUsersAuthorizations(req.user, users.result);
+ await AuthorizationService.addUsersAuthorizations(req.tenant, req.user, users.result);
// Return
return users;
}

MODIFY
test/api/TransactionTest.ts
test/api/TransactionTest.ts
@@ -159,17 +159,6 @@ describe('Transaction tests', function() {
});
- describe('Using function "readAllInError"', () => {
-
- afterEach(async () => {
- await testData.chargingStationContext.cleanUpCreatedData();
- });
-
- it('Cannot find any transactions in error if all transactions are still running', async () => {
- await testData.transactionCommonTests.testReadNoTransactionsInError();
- });
- });
-
describe('Using function "readAllConsumption"', () => {
afterEach(async () => {

Add auths on assign/unassign users/sites + fixed unit tests