Add dynamic filter; remove todo leftovers.
MODIFY
src/authorization/AuthorizationsDefinition.ts
src/authorization/AuthorizationsDefinition.ts
@@ -303,7 +303,13 @@ const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
'miscSegment', 'miscIsofixSeats', 'chargeStandardPower', 'chargeStandardPhase', 'chargeAlternativePower', 'hash',
'chargeAlternativePhase', 'chargeOptionPower', 'chargeOptionPhase', 'image', 'chargeOptionPhaseAmp', 'chargeAlternativePhaseAmp'
]
- }, { resource: Entity.CAR, action: [Action.CREATE, Action.UPDATE, Action.DELETE], attributes: ['*'] },
+ }, {
+ resource: Entity.CAR, action: [Action.CREATE, Action.UPDATE, Action.DELETE], attributes: ['*'],
+ condition: {
+ Fn: 'custom:dynamicAuthorizationFilters',
+ args: { filters: ['OwnUser'] }
+ }
+ },
{
resource: Entity.CAR, action: Action.READ, attributes: [
'id', 'type', 'vin', 'licensePlate', 'converter', 'default', 'owner', 'createdOn', 'lastChangedOn',
@@ -311,14 +317,20 @@ const AUTHORIZATION_DEFINITION: AuthorizationDefinition = {
'carCatalog.chargeStandardPower', 'carCatalog.chargeStandardPhaseAmp', 'carCatalog.chargeStandardPhase',
'carCatalog.chargeAlternativePower', 'carCatalog.chargeAlternativePhaseAmp', 'carCatalog.chargeAlternativePhase',
'carCatalog.chargeOptionPower', 'carCatalog.chargeOptionPhaseAmp', 'carCatalog.chargeOptionPhase'
- ]
+ ], condition: {
+ Fn: 'custom:dynamicAuthorizationFilters',
+ args: { filters: ['OwnUser'] }
+ }
},
{
resource: Entity.CARS, action: Action.LIST, attributes: [
'id', 'type', 'vin', 'licensePlate', 'converter', 'default', 'owner', 'createdOn', 'lastChangedOn',
'carCatalog.id', 'carCatalog.vehicleMake', 'carCatalog.vehicleModel', 'carCatalog.vehicleModelVersion',
'carCatalog.image', 'carCatalog.fastChargePowerMax', 'carCatalog.batteryCapacityFull'
- ]
+ ], condition: {
+ Fn: 'custom:dynamicAuthorizationFilters',
+ args: { filters: ['OwnUser'] }
+ }
},
{
resource: Entity.COMPANIES, action: Action.LIST,

MODIFY
src/server/rest/v1/service/CarService.ts
src/server/rest/v1/service/CarService.ts
@@ -330,10 +330,8 @@ export default class CarService {
// Check and Get Car
const car = await UtilsService.checkAndGetCarAuthorization(req.tenant, req.user, filteredRequest.id, Action.UPDATE, action,
{
- withUsers: Authorizations.isBasic(req.user) ? true : false,
- userIDs: Authorizations.isBasic(req.user) ? [req.user.id] : null
+ withUsers: Authorizations.isBasic(req.user) ? true : false
}, true);
- // todo: dynamic filter?
// Check Owner if Basic
let carUser: UserCar;
if (Authorizations.isBasic(req.user)) {
@@ -362,7 +360,6 @@ export default class CarService {
});
}
}
- // todo: dynamic filter?
// Owner?
if (Authorizations.isAdmin(req.user) || (Authorizations.isBasic(req.user) && carUser.owner)) {
// Ok: Update & Save
@@ -415,17 +412,15 @@ export default class CarService {
if (await Authorizations.canListUsers(req.user)) {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName',
'carUsers.user.id', 'carUsers.user.name', 'carUsers.user.firstName', 'carUsers.owner', 'carUsers.default' ];
- // todo: is it ok like this?
authorizationCarsFilter.projectFields = authorizationCarsFilter.projectFields.concat(userProject);
}
// Get cars
const cars = await CarStorage.getCars(req.user.tenantID,
{
search: filteredRequest.Search,
- // todo: should this be replaced by a dynamic filter?
- userIDs: Authorizations.isBasic(req.user) ? [req.user.id] : filteredRequest.UserID ? filteredRequest.UserID.split('|') : null,
carMakers: filteredRequest.CarMaker ? filteredRequest.CarMaker.split('|') : null,
- withUsers: filteredRequest.WithUsers
+ withUsers: filteredRequest.WithUsers,
+ ...authorizationCarsFilter.filters
},
{
limit: filteredRequest.Limit,
@@ -452,7 +447,6 @@ export default class CarService {
// Check and get Car
const car = await UtilsService.checkAndGetCarAuthorization(req.tenant, req.user, filteredRequest.ID, Action.READ, action, {
withUsers: true ,
- userIDs: Authorizations.isBasic(req.user) ? [req.user.id] : null // todo: dynamic filter?
}, true);
// Return
res.json(car);
@@ -509,10 +503,8 @@ export default class CarService {
// Check and Get Car
const car = await UtilsService.checkAndGetCarAuthorization(req.tenant, req.user, carId, Action.DELETE, action,
{
- withUsers: Authorizations.isBasic(req.user) ? true : false,
- userIDs: Authorizations.isBasic(req.user) ? [req.user.id] : null
+ withUsers: Authorizations.isBasic(req.user) ? true : false
}, true);
- // todo: dynamic filter?
// Check Owner if Basic
let carUser: UserCar;
if (Authorizations.isBasic(req.user)) {
@@ -528,7 +520,6 @@ export default class CarService {
});
}
}
- // todo: dynamic filter?
// Basic User
if (Authorizations.isBasic(req.user) && !carUser.owner) {
// Delete the association
@@ -541,7 +532,6 @@ export default class CarService {
detailedMessages: { car }
});
}
- // todo: dynamic filter?
// Owner?
if (Authorizations.isAdmin(req.user) || (Authorizations.isBasic(req.user) && carUser.owner)) {
// Check if Transaction exist (to Be implemented later)

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -552,7 +552,6 @@ export default class UtilsService {
userProject = [ 'createdBy.name', 'createdBy.firstName', 'lastChangedBy.name', 'lastChangedBy.firstName', 'carUsers.id',
'carUsers.user.id', 'carUsers.user.name', 'carUsers.user.firstName', 'carUsers.user.email', 'carUsers.default', 'carUsers.owner'
];
- // todo: is it ok like this?
authorizationFilter.projectFields = authorizationFilter.projectFields.concat(userProject);
}
// Get Car
@@ -609,7 +608,6 @@ export default class UtilsService {
// Check it exists
UtilsService.assertObjectExists(action, carCatalog, `Car Catalog ID '${carCatalogID}' does not exist`,
MODULE_NAME, 'checkAndGetCarCatalogAuthorization', userToken);
- // todo: check if extra auth is needed?
// Return
return carCatalog;
}

Add dynamic filter; remove todo leftovers.