one lined my check and gets
deleted the if(checkIssuer)
added metadata to users in error
modified user update
fixed user action in check and gets
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -131,7 +131,20 @@ export const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
attributes: [
'id', 'name', 'firstName', 'email', 'role', 'status', 'issuer',
'createdOn', 'lastChangedOn', 'errorCodeDetails', 'errorCode'
- ]
+ ],
+ condition: {
+ Fn: 'custom:dynamicAuthorizations',
+ args: {
+ asserts: [],
+ filters: [],
+ metadata: {
+ status: {
+ visible: true,
+ mandatory: true,
+ }
+ },
+ }
+ }
},
{ resource: Entity.USER, action: Action.SYNCHRONIZE_BILLING_USER },
{

MODIFY
src/server/rest/v1/router/api/UserRouter.ts
src/server/rest/v1/router/api/UserRouter.ts
@@ -50,6 +50,7 @@ export default class UserRouter {
private buildRouteUpdateUser(): void {
this.router.put(`/${RESTServerRoute.REST_USER}`, (req: Request, res: Response, next: NextFunction) => {
+ req.body.id = req.params.id;
void RouterUtils.handleRestServerAction(UserService.handleUpdateUser.bind(this), ServerAction.USER_UPDATE, req, res, next);
});
}

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -262,8 +262,7 @@ export default class AuthorizationService {
return authorizations;
}
- public static async checkAndGetUsersInErrorAuthorizations(tenant: Tenant, userToken: UserToken,
- authAction: Action, filteredRequest: Partial<HttpUsersGetRequest>): Promise<AuthorizationFilter> {
+ public static async checkAndGetUsersInErrorAuthorizations(tenant: Tenant, userToken: UserToken, filteredRequest: Partial<HttpUsersGetRequest>): Promise<AuthorizationFilter> {
const authorizations: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -271,7 +270,7 @@ export default class AuthorizationService {
authorized: false
};
// Check static & dynamic authorization
- await this.canPerformAuthorizationAction(tenant, userToken, Entity.USER, authAction,
+ await this.canPerformAuthorizationAction(tenant, userToken, Entity.USER, Action.IN_ERROR,
authorizations, filteredRequest, null, true);
return authorizations;
}
@@ -313,8 +312,7 @@ export default class AuthorizationService {
Utils.removeCanPropertiesWithFalseValue(user);
}
- public static async checkAndGetUsersAuthorizations(tenant: Tenant, userToken: UserToken,
- authAction: Action, filteredRequest: Partial<HttpUsersGetRequest>, failsWithException = true): Promise<AuthorizationFilter> {
+ public static async checkAndGetUsersAuthorizations(tenant: Tenant, userToken: UserToken, filteredRequest: Partial<HttpUsersGetRequest>, failsWithException = true): Promise<AuthorizationFilter> {
const authorizations: AuthorizationFilter = {
filters: {},
dataSources: new Map(),
@@ -322,8 +320,7 @@ export default class AuthorizationService {
authorized: false,
};
// Check static & dynamic authorization
- await AuthorizationService.canPerformAuthorizationAction(
- tenant, userToken, Entity.USER, authAction, authorizations, filteredRequest, null, failsWithException);
+ await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.USER, Action.LIST, authorizations, filteredRequest, null, failsWithException);
return authorizations;
}

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -40,11 +40,9 @@ export default class SiteService {
});
}
// Check and Get Site
- const site = await UtilsService.checkAndGetSiteAuthorization(
- req.tenant, req.user, filteredRequest.siteID, Action.UPDATE, action);
+ const site = await UtilsService.checkAndGetSiteAuthorization(req.tenant, req.user, filteredRequest.siteID, Action.UPDATE, action);
// Check and Get User
- const user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, filteredRequest.userID, Action.READ, action);
+ const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID, Action.READ, action);
// Update
await SiteStorage.updateSiteUserAdmin(req.tenant, filteredRequest.siteID, filteredRequest.userID, filteredRequest.siteAdmin);
await Logging.logInfo({

MODIFY
src/server/rest/v1/service/TransactionService.ts
src/server/rest/v1/service/TransactionService.ts
@@ -661,7 +661,7 @@ export default class TransactionService {
let user: User;
try {
user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, transaction.userID, Action.READ, action, null, null, true, false);
+ req.tenant, req.user, transaction.userID, Action.READ, action, null, null, true);
} catch (error) {
// Ignore
}

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -110,11 +110,9 @@ export default class UserService {
// Filter request
const filteredRequest = UserValidatorRest.getInstance().validateUserSitesAssignReq(req.body);
// Check and Get User
- const user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, filteredRequest.userID, Action.READ, action);
+ const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.userID, Action.READ, action);
// Check and Get Sites
- const sites = await UtilsService.checkAndGetUserSitesAuthorization(
- req.tenant, req.user, user, filteredRequest.siteIDs, action);
+ const sites = await UtilsService.checkAndGetUserSitesAuthorization(req.tenant, req.user, user, filteredRequest.siteIDs, action);
// Save
if (action === ServerAction.ADD_SITES_TO_USER) {
for (const site of sites) {
@@ -161,8 +159,7 @@ export default class UserService {
// Filter
const userID = UserValidatorRest.getInstance().validateUserDeleteReq(req.query).ID.toString();
// Check and Get User
- const user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, userID, Action.DELETE, action, null, {}, false, false);
+ const user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, userID, Action.DELETE, action, null, {}, false);
// Delete OCPI User
if (!user.issuer) {
// Delete User
@@ -201,10 +198,9 @@ export default class UserService {
public static async handleUpdateUser(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
let statusHasChanged = false;
// Filter
- const filteredRequest = UserValidatorRest.getInstance().validateUserUpdateReq({ ...req.params, ...req.body });
+ const filteredRequest = UserValidatorRest.getInstance().validateUserUpdateReq(req.body);
// Check and Get User
- let user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest);
+ let user = await UtilsService.checkAndGetUserAuthorization(req.tenant, req.user, filteredRequest.id, Action.UPDATE, action, filteredRequest);
// Check email already exists
if (filteredRequest.email) {
const userWithEmail = await UserStorage.getUserByEmail(req.tenant, filteredRequest.email);
@@ -321,7 +317,7 @@ export default class UserService {
const user = await UtilsService.checkAndGetUserAuthorization(
req.tenant, req.user, filteredRequest.ID.toString(), Action.READ, action, null, {
withImage: true
- }, true, false);
+ }, true);
res.json(user);
next();
}
@@ -331,7 +327,7 @@ export default class UserService {
const userID = UserValidatorRest.getInstance().validateUserGetReq(req.query).ID.toString();
// Check and Get User
const user = await UtilsService.checkAndGetUserAuthorization(
- req.tenant, req.user, userID, Action.READ, action, null, null, null, false);
+ req.tenant, req.user, userID, Action.READ, action, null, null, null);
// Get the user image
const userImage = await UserStorage.getUserImage(req.tenant, user.id);
res.json(userImage);
@@ -418,8 +414,7 @@ export default class UserService {
// Filter
const filteredRequest = UserValidatorRest.getInstance().validateUsersInErrorGetReq(req.query);
// Get authorization filters
- const authorizationUserInErrorFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizations(
- req.tenant, req.user, Action.IN_ERROR, filteredRequest);
+ const authorizationUserInErrorFilters = await AuthorizationService.checkAndGetUsersInErrorAuthorizations(req.tenant, req.user, filteredRequest);
if (!authorizationUserInErrorFilters.authorized) {
return Constants.DB_EMPTY_DATA_RESULT;
}
@@ -772,15 +767,13 @@ export default class UserService {
private static async getUsers(req: Request, filteredRequest: HttpUsersGetRequest): Promise<DataResult<User>> {
// Get authorization filters
- const authorizationUsersFilters = await AuthorizationService.checkAndGetUsersAuthorizations(
- req.tenant, req.user, Action.LIST, filteredRequest);
+ const authorizationUsersFilters = await AuthorizationService.checkAndGetUsersAuthorizations(req.tenant, req.user, filteredRequest);
if (!authorizationUsersFilters.authorized) {
return Constants.DB_EMPTY_DATA_RESULT;
}
// Optimization: Get Tag IDs from Visual IDs
if (filteredRequest.VisualTagID) {
- const authorizationTagsFilters = await AuthorizationService.checkAndGetTagsAuthorizations(
- req.tenant, req.user, filteredRequest);
+ const authorizationTagsFilters = await AuthorizationService.checkAndGetTagsAuthorizations(req.tenant, req.user, filteredRequest);
if (!authorizationTagsFilters.authorized) {
return Constants.DB_EMPTY_DATA_RESULT;
}

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -279,12 +279,11 @@ export default class UtilsService {
}
public static async checkAndGetUserAuthorization(tenant: Tenant, userToken: UserToken, userID: string, authAction: Action,
- action: ServerAction, entityData?: EntityData, additionalFilters: Record<string, any> = {}, applyProjectFields = false, checkIssuer = true): Promise<User> {
+ action: ServerAction, entityData?: EntityData, additionalFilters: Record<string, any> = {}, applyProjectFields = false): Promise<User> {
// Check mandatory fields
UtilsService.assertIdIsProvided(action, userID, MODULE_NAME, 'checkAndGetUserAuthorization', userToken);
// Get dynamic auth
- const authorizations = await AuthorizationService.checkAndGetUserAuthorizations(
- tenant, userToken, { ID: userID }, authAction, entityData);
+ const authorizations = await AuthorizationService.checkAndGetUserAuthorizations(tenant, userToken, { ID: userID }, authAction, entityData);
// Get User
const user = await UserStorage.getUser(tenant, userID,
{
@@ -295,17 +294,6 @@ export default class UtilsService {
);
UtilsService.assertObjectExists(action, user, `User ID '${userID}' does not exist`,
MODULE_NAME, 'checkAndGetUserAuthorization', userToken);
- // External User
- // TODO: need alignement of auth definition to remove below check
- if (checkIssuer && !user.issuer) {
- throw new AppError({
- errorCode: HTTPError.GENERAL_ERROR,
- message: `User '${user.name}' with ID '${user.id}' not issued by the organization`,
- module: MODULE_NAME, method: 'checkAndGetUserAuthorization',
- user: userToken,
- action: action
- });
- }
// Assign projected fields
if (authorizations.projectFields) {
user.projectFields = authorizations.projectFields;

one lined my check and gets
deleted the if(checkIssuer)
added metadata to users in error
modified user update
fixed user action in check and gets