Authorization concept without roles final version
MODIFY
src/authorization/Authorizations.ts
src/authorization/Authorizations.ts
@@ -757,6 +757,21 @@ export default class Authorizations {
return user.role === UserRole.DEMO;
}
+ public static async can(loggedUser: UserToken, entity: Entity, action: Action, context?: AuthorizationContext): Promise<AuthorizationResult> {
+ // Check
+ const authDefinition = AuthorizationsDefinition.getInstance();
+ const result = await authDefinition.canPerformAction(loggedUser.rolesACL, entity, action, context);
+ if (!result.authorized && Authorizations.getConfiguration().debug) {
+ void Logging.logSecurityInfo({
+ tenantID: loggedUser.tenantID, user: loggedUser,
+ action: ServerAction.AUTHORIZATIONS,
+ module: MODULE_NAME, method: 'canPerformAction',
+ message: `Role ${loggedUser.role} Cannot ${action} on ${entity} with context ${JSON.stringify(context)}`,
+ });
+ }
+ return result;
+ }
+
private static async isTagIDAuthorizedOnChargingStation(tenantID: string, chargingStation: ChargingStation,
transaction: Transaction, tagID: string, action: ServerAction, authAction: Action): Promise<User> {
// Get the Organization component
@@ -1054,19 +1069,4 @@ export default class Authorizations {
}
return authorized;
}
-
- private static async can(loggedUser: UserToken, entity: Entity, action: Action, context?: AuthorizationContext): Promise<AuthorizationResult> {
- // Check
- const authDefinition = AuthorizationsDefinition.getInstance();
- const result = await authDefinition.canPerformAction(loggedUser.rolesACL, entity, action, context);
- if (!result.authorized && Authorizations.getConfiguration().debug) {
- void Logging.logSecurityInfo({
- tenantID: loggedUser.tenantID, user: loggedUser,
- action: ServerAction.AUTHORIZATIONS,
- module: MODULE_NAME, method: 'canPerformAction',
- message: `Role ${loggedUser.role} Cannot ${action} on ${entity} with context ${JSON.stringify(context)}`,
- });
- }
- return result;
- }
}

MODIFY
src/authorization/DynamicAuthorizationFactory.ts
src/authorization/DynamicAuthorizationFactory.ts
@@ -7,7 +7,9 @@ import DynamicAuthorizationFilter from './DynamicAuthorizationFilter';
export default class DynamicAuthorizationFactory {
public static async getDynamicFilter(tenantID: string, userID: string,
- filter: DynamicAuthorizationFilterName): Promise<DynamicAuthorizationFilter> {
+ filter: DynamicAuthorizationFilterName,
+ existingDataSources?: Map<DynamicAuthorizationDataSourceName, DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>>):
+ Promise<DynamicAuthorizationFilter> {
let dynamicFilter: DynamicAuthorizationFilter;
switch (filter) {
case DynamicAuthorizationFilterName.ASSIGNED_SITES_COMPANIES:
@@ -15,21 +17,27 @@ export default class DynamicAuthorizationFactory {
}
// Init Data Source
if (dynamicFilter) {
- await DynamicAuthorizationFactory.initFilterDataSources(tenantID, userID, dynamicFilter);
+ await DynamicAuthorizationFactory.initFilterDataSources(tenantID, userID, dynamicFilter, existingDataSources);
}
return dynamicFilter;
}
private static async initFilterDataSources(tenantID: string, userID: string,
- dynamicFilter: DynamicAuthorizationFilter): Promise<void> {
+ dynamicFilter: DynamicAuthorizationFilter,
+ existingDataSources?: Map<DynamicAuthorizationDataSourceName, DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>>): Promise<void> {
// Get Data Source
const dataSourceNames = dynamicFilter.getApplicableDataSources();
for (const dataSourceName of dataSourceNames) {
+ let dataSource = existingDataSources.get(dataSourceName);
+ if (!dataSource) {
// Create the data source
- const dataSource = DynamicAuthorizationFactory.getDynamicDataSource(
+ dataSource = DynamicAuthorizationFactory.getDynamicDataSource(
tenantID, userID, dataSourceName);
// Load data
await dataSource.loadData();
+ // Add
+ existingDataSources.set(dataSourceName, dataSource);
+ }
// Set
dynamicFilter.setDataSource(dataSourceName, dataSource);
}

MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -33,7 +33,7 @@ import _ from 'lodash';
const MODULE_NAME = 'AuthorizationService';
export default class AuthorizationService {
- public static canPerfomAuthorizationAction(entity: AuthorizationActions, authAction: Action): boolean {
+ public static canPerfomAction(entity: AuthorizationActions, authAction: Action): boolean {
switch (authAction) {
case Action.READ:
return entity.canRead;
@@ -52,6 +52,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'issuer', 'image', 'address', 'companyID', 'company.name', 'autoUserSiteAssignment', 'public'
],
@@ -102,6 +103,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'address', 'companyID', 'company.name', 'autoUserSiteAssignment', 'issuer',
'autoUserSiteAssignment', 'distanceMeters', 'public', 'createdOn', 'lastChangedOn',
@@ -138,6 +140,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'user.id', 'user.name', 'user.firstName', 'user.email', 'user.role', 'siteAdmin', 'siteOwner', 'siteID'
],
@@ -156,6 +159,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpUserSitesRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'site.id', 'site.name', 'site.address.city', 'site.address.country', 'siteAdmin', 'siteOwner', 'userID'
],
@@ -174,6 +178,7 @@ export default class AuthorizationService {
tenant: Tenant, action: ServerAction, userToken: UserToken, filteredRequest: HttpSiteAssignUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [],
authorized: userToken.role === UserRole.ADMIN,
};
@@ -207,6 +212,7 @@ export default class AuthorizationService {
tenant: Tenant, action: ServerAction, userToken: UserToken, siteArea: SiteArea, filteredRequest: HttpAssignAssetsToSiteAreaRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [],
authorized: userToken.role === UserRole.ADMIN,
};
@@ -241,6 +247,7 @@ export default class AuthorizationService {
filteredRequest: HttpAssignChargingStationToSiteAreaRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [],
authorized: userToken.role === UserRole.ADMIN,
};
@@ -274,6 +281,7 @@ export default class AuthorizationService {
tenant: Tenant, action: ServerAction, userToken: UserToken, filteredRequest: HttpUserAssignSitesRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [],
authorized: userToken.role === UserRole.ADMIN,
};
@@ -310,6 +318,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'firstName', 'email', 'role', 'status', 'issuer',
'createdOn', 'lastChangedOn', 'errorCodeDetails', 'errorCode'
@@ -346,6 +355,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpUsersRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'firstName', 'email', 'role', 'status', 'issuer', 'createdOn', 'createdBy',
'lastChangedOn', 'lastChangedBy', 'eulaAcceptedOn', 'eulaAcceptedVersion', 'locale',
@@ -366,6 +376,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpAssetsRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'siteAreaID', 'siteArea.id', 'siteArea.name', 'siteArea.siteID', 'siteID', 'assetType', 'coordinates',
'dynamicAsset', 'connectionID', 'meterID', 'currentInstantWatts', 'currentStateOfCharge'
@@ -385,6 +396,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpUserRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'firstName', 'email', 'role', 'status', 'issuer', 'locale', 'deleted', 'plateID',
'notificationsActive', 'notifications', 'phone', 'mobile', 'iNumber', 'costCenter', 'address'
@@ -404,6 +416,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpTagsRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'userID', 'active', 'ocpiToken', 'description', 'issuer', 'default',
'createdOn', 'lastChangedOn'
@@ -427,6 +440,7 @@ export default class AuthorizationService {
tenant: Tenant, userToken: UserToken, filteredRequest: HttpByIDRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: ['id', 'userID', 'issuer', 'active', 'description', 'default', 'deleted', 'user.id', 'user.name', 'user.firstName', 'user.email'],
authorized: userToken.role === UserRole.ADMIN || userToken.role === UserRole.SUPER_ADMIN,
};
@@ -439,72 +453,52 @@ export default class AuthorizationService {
return authorizationFilters;
}
- public static async addCompaniesAuthorizations(tenant: Tenant, userToken: UserToken, companies: CompanyDataResult): Promise<void> {
- // Get Site Admins
- const { siteAdminIDs, siteOwnerIDs } = await AuthorizationService.getSiteAdminOwnerIDs(tenant, userToken);
- // Set to user
- userToken.sitesAdmin = siteAdminIDs;
- userToken.sitesOwner = siteOwnerIDs;
+ public static async checkAndGetCompaniesAuthorizationFilters(tenant: Tenant, userToken: UserToken,
+ filteredRequest: HttpCompaniesRequest): Promise<AuthorizationFilter> {
+ const authorizationFilters: AuthorizationFilter = {
+ filters: {},
+ dataSources: new Map(),
+ projectFields: [],
+ authorized: false,
+ };
+ // Check static & dynamic authorization
+ await this.canPerformAuthorizationAction(
+ tenant, userToken, Entity.COMPANIES, Action.LIST, authorizationFilters, filteredRequest);
+ return authorizationFilters;
+ }
+
+ public static async addCompaniesAuthorizations(tenant: Tenant, userToken: UserToken,
+ companies: CompanyDataResult, authorizationFilter: AuthorizationFilter): Promise<void> {
// Add canCreate flag to root
- companies.canCreate = await Authorizations.canCreateCompany(userToken);
+ companies.canCreate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.COMPANY, Action.CREATE, authorizationFilter);
// Enrich
for (const company of companies.result) {
- await AuthorizationService.addCompanyAuthorizations(tenant, userToken, company);
+ await AuthorizationService.addCompanyAuthorizations(tenant, userToken, company, authorizationFilter);
}
}
- public static async addCompanyAuthorizations(tenant: Tenant, userToken: UserToken, company: Company): Promise<void> {
- // TODO: Use the Data Provider in the Extra context
+ public static async addCompanyAuthorizations(tenant: Tenant, userToken: UserToken, company: Company, authorizationFilter: AuthorizationFilter): Promise<void> {
// Enrich
if (!company.issuer) {
company.canRead = true;
company.canUpdate = false;
company.canDelete = false;
} else {
- company.canRead = (await Authorizations.canReadCompany(userToken)).authorized;
- company.canDelete = await Authorizations.canDeleteCompany(userToken);
- company.canUpdate = await Authorizations.canUpdateCompany(userToken);
+ company.canRead = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.COMPANY, Action.READ, authorizationFilter);
+ company.canDelete = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.COMPANY, Action.DELETE, authorizationFilter);
+ company.canUpdate = await AuthorizationService.canPerformAuthorizationAction(tenant, userToken, Entity.COMPANY, Action.UPDATE, authorizationFilter);
}
}
- public static async checkAndGetCompaniesAuthorizationFilters(tenant: Tenant, userToken: UserToken,
- filteredRequest: HttpCompaniesRequest): Promise<AuthorizationFilter> {
- const authorizationFilters: AuthorizationFilter = {
- filters: {},
- projectFields: [],
- authorized: false,
- };
- // Check static auth
- const authorizationContext: AuthorizationContext = {};
- const authResult = await Authorizations.canListCompanies(userToken, authorizationContext);
- authorizationFilters.authorized = authResult.authorized;
- // Check
- if (!authorizationFilters.authorized) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.FORBIDDEN,
- user: userToken,
- action: Action.LIST, entity: Entity.COMPANIES,
- module: MODULE_NAME, method: 'checkAndGetCompaniesAuthorizationFilters'
- });
- }
- // Process dynamic filters
- await AuthorizationService.processDynamicFilters(tenant, userToken, Action.LIST, Entity.COMPANIES,
- authorizationFilters, authorizationContext, filteredRequest);
- // Filter projected fields
- authorizationFilters.projectFields = AuthorizationService.filterProjectFields(
- authResult.fields, filteredRequest.ProjectFields);
- return authorizationFilters;
- }
-
public static async checkAndGetCompanyAuthorizationFilters(tenant: Tenant, userToken: UserToken,
filteredRequest: HttpCompanyRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [],
authorized: false,
};
// Check static auth
- // TODO: Can get here the Data Provider (AssignedCompanyIDs) + Intantiate them + Get the Data: JSon: { assignedCompanyIDs: [...]}
const authorizationContext: AuthorizationContext = {};
const authResult = await Authorizations.canReadCompany(userToken, authorizationContext);
authorizationFilters.authorized = authResult.authorized;
@@ -518,7 +512,6 @@ export default class AuthorizationService {
});
}
// Process dynamic filters
- // TODO: Pass here the JSon Data from all the Data Provider to the extra filter
await AuthorizationService.processDynamicFilters(tenant, userToken, Action.READ, Entity.COMPANY,
authorizationFilters, authorizationContext, { CompanyID: filteredRequest.ID });
// Filter projected fields
@@ -530,6 +523,7 @@ export default class AuthorizationService {
public static async checkAndGetSiteAreaAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteAreaRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'issuer', 'image', 'address', 'maximumPower', 'numberOfPhases',
'voltage', 'smartCharging', 'accessControl', 'connectorStats', 'siteID', 'site.name'
@@ -552,6 +546,7 @@ export default class AuthorizationService {
public static async checkAndGetSiteAreasAuthorizationFilters(tenant: Tenant, userToken: UserToken, filteredRequest: HttpSiteAreasRequest): Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'name', 'siteID', 'maximumPower', 'voltage', 'numberOfPhases', 'accessControl', 'smartCharging', 'address',
'site.id', 'site.name', 'issuer', 'distanceMeters', 'createdOn', 'createdBy', 'lastChangedOn', 'lastChangedBy'
@@ -618,6 +613,7 @@ export default class AuthorizationService {
filteredRequest: HttpChargingStationRequest):Promise<AuthorizationFilter> {
const authorizationFilters: AuthorizationFilter = {
filters: {},
+ dataSources: new Map(),
projectFields: [
'id', 'inactive', 'public', 'chargingStationURL', 'issuer', 'maximumPower', 'excludeFromSmartCharging', 'lastReboot',
'siteAreaID', 'siteArea.id', 'siteArea.name', 'siteArea.smartCharging', 'siteArea.siteID',
@@ -809,13 +805,13 @@ export default class AuthorizationService {
}
private static async processDynamicFilters(tenant: Tenant, userToken: UserToken, action: Action, entity: Entity,
- authorizationFilters: AuthorizationFilter, authorizationContext: AuthorizationContext, extraFilters?: any): Promise<void> {
+ authorizationFilters: AuthorizationFilter, authorizationContext: AuthorizationContext, extraFilters?: Record<string, any>): Promise<void> {
if (!Utils.isEmptyArray(authorizationContext.filters)) {
for (const filter of authorizationContext.filters) {
// Reset to false
authorizationFilters.authorized = false;
// Get the filter
- const dynamicFilter = await DynamicAuthorizationFactory.getDynamicFilter(tenant.id, userToken.id, filter);
+ const dynamicFilter = await DynamicAuthorizationFactory.getDynamicFilter(tenant.id, userToken.id, filter, authorizationFilters.dataSources);
if (!dynamicFilter) {
// Filter not found -> Not authorized (all auth filter MUST work)
throw new AppAuthError({
@@ -834,4 +830,22 @@ export default class AuthorizationService {
}
}
}
+
+ private static async canPerformAuthorizationAction(tenant: Tenant, userToken: UserToken,
+ entity: Entity, action: Action, authorizationFilters: AuthorizationFilter, filteredRequest?: Record<string, any>): Promise<boolean> {
+ // Check static auth
+ const authorizationContext: AuthorizationContext = {};
+ const authResult = await Authorizations.can(userToken, entity, action, authorizationContext);
+ authorizationFilters.authorized = authResult.authorized;
+ if (!authorizationFilters.authorized) {
+ return false;
+ }
+ // Check Dynamic Auth
+ await AuthorizationService.processDynamicFilters(tenant, userToken, Action.LIST, Entity.COMPANIES,
+ authorizationFilters, authorizationContext, filteredRequest);
+ // Filter projected fields
+ authorizationFilters.projectFields = AuthorizationService.filterProjectFields(
+ authResult.fields, filteredRequest?.ProjectFields);
+ return authorizationFilters.authorized;
+ }
}

MODIFY
src/server/rest/v1/service/CompanyService.ts
src/server/rest/v1/service/CompanyService.ts
@@ -119,7 +119,7 @@ export default class CompanyService {
authorizationCompaniesFilter.projectFields
);
// Add Auth flags
- await AuthorizationService.addCompaniesAuthorizations(req.tenant, req.user, companies as CompanyDataResult);
+ await AuthorizationService.addCompaniesAuthorizations(req.tenant, req.user, companies as CompanyDataResult, authorizationCompaniesFilter);
// Return
res.json(companies);
next();

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
@@ -106,7 +106,6 @@ export default class UtilsService {
// Check mandatory fields
UtilsService.assertIdIsProvided(action, companyID, MODULE_NAME, 'checkAndGetCompanyAuthorization', userToken);
// Get dynamic auth
- // TODO: Should have Provider data in the authorizationFilter
const authorizationFilter = await AuthorizationService.checkAndGetCompanyAuthorizationFilters(
tenant, userToken, { ID: companyID });
if (!authorizationFilter.authorized) {
@@ -137,10 +136,9 @@ export default class UtilsService {
});
}
// Add actions
- // TODO: Pass the data from Data Provider as extraFilters
- await AuthorizationService.addCompanyAuthorizations(tenant, userToken, company);
+ await AuthorizationService.addCompanyAuthorizations(tenant, userToken, company, authorizationFilter);
// Check
- const authorized = AuthorizationService.canPerfomAuthorizationAction(company, authAction);
+ const authorized = AuthorizationService.canPerfomAction(company, authAction);
if (!authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -203,7 +201,7 @@ export default class UtilsService {
// Add actions
await AuthorizationService.addUserAuthorizations(tenant, userToken, user);
// Check
- const authorized = AuthorizationService.canPerfomAuthorizationAction(user, authAction);
+ const authorized = AuthorizationService.canPerfomAction(user, authAction);
if (!authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -266,7 +264,7 @@ export default class UtilsService {
// Add actions
await AuthorizationService.addSiteAuthorizations(tenant, userToken, site);
// Check
- const authorized = AuthorizationService.canPerfomAuthorizationAction(site, authAction);
+ const authorized = AuthorizationService.canPerfomAction(site, authAction);
if (!authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,
@@ -481,7 +479,7 @@ export default class UtilsService {
// Add actions
await AuthorizationService.addSiteAreaAuthorizations(tenant, userToken, siteArea);
// Check
- const authorized = AuthorizationService.canPerfomAuthorizationAction(siteArea, authAction);
+ const authorized = AuthorizationService.canPerfomAction(siteArea, authAction);
if (!authorized) {
throw new AppAuthError({
errorCode: HTTPAuthError.FORBIDDEN,

MODIFY
src/types/Authorization.ts
src/types/Authorization.ts
+import DynamicAuthorizationDataSource from '../authorization/DynamicAuthorizationDataSource';
+
export interface AuthorizationDefinition {
superAdmin: {
grants: Grant[];
@@ -33,7 +35,8 @@ export interface AuthorizationResult {
export interface AuthorizationFilter {
filters: Record<string, any>;
projectFields: string[];
- authorized?: boolean;
+ authorized: boolean;
+ dataSources: Map<DynamicAuthorizationDataSourceName, DynamicAuthorizationDataSource<DynamicAuthorizationDataSourceData>>;
}
export interface Grant {

MODIFY
src/types/configuration/LoggingConfiguration.ts
src/types/configuration/LoggingConfiguration.ts
export default interface LoggingConfiguration {
logLevel: string;
- consoleLogLevel: string;
trace: boolean;
traceLogOnlyStatistics: boolean;
traceStatisticInterval: number;
moduleDetails: {
ChargingStation: {
logLevel: string;
- consoleLogLevel: string;
};
Authorizations: {
logLevel: string;
- consoleLogLevel: string;
};
};
}

MODIFY
src/utils/Logging.ts
src/utils/Logging.ts
@@ -738,11 +738,10 @@ export default class Logging {
// Log
private static async _log(log: Log): Promise<string> {
let moduleConfig = null;
+ // Check Log Level
const loggingConfig = Logging.getConfiguration();
// Default Log Level
let logLevel = loggingConfig.logLevel ? loggingConfig.logLevel : LogLevel.DEBUG;
- // Default Console Level
- let consoleLogLevel = loggingConfig.consoleLogLevel ? loggingConfig.consoleLogLevel : LogLevel.NONE;
// Module Provided?
if (log.module && loggingConfig.moduleDetails) {
// Yes: Check the Module
@@ -755,41 +754,8 @@ export default class Logging {
logLevel = moduleConfig.logLevel;
}
}
- // Check Console Log Level
- if (moduleConfig.consoleLogLevel) {
- // Default?
- if (moduleConfig.consoleLogLevel !== LogLevel.DEFAULT) {
- // No
- consoleLogLevel = moduleConfig.consoleLogLevel;
}
}
- }
- }
- // Log Level takes precedence over console log
- switch (LogLevel[consoleLogLevel]) {
- case LogLevel.NONE:
- break;
- // Keep up to error filter out debug
- case LogLevel.ERROR:
- if (log.level === LogLevel.INFO || log.level === LogLevel.WARNING || log.level === LogLevel.DEBUG) {
- break;
- }
- // Keep up to warning filter out debug
- case LogLevel.WARNING: // eslint-disable-line no-fallthrough
- if (log.level === LogLevel.INFO || log.level === LogLevel.DEBUG) {
- break;
- }
- // Keep all log messages just filter out DEBUG
- case LogLevel.INFO: // eslint-disable-line no-fallthrough
- if (log.level === LogLevel.DEBUG) {
- break;
- }
- // Keep all messages
- case LogLevel.DEBUG: // eslint-disable-line no-fallthrough
- default: // If we did not break then it means we have to console log it
- Logging._consoleLog(log);
- break;
- }
// Do not log to DB simple string messages
if (log['simpleMessage']) {
return;
@@ -825,9 +791,7 @@ export default class Logging {
// Timestamp
log.timestamp = new Date();
// Source
- if (!log.source) {
- log.source = `${Constants.CENTRAL_SERVER}`;
- }
+ log.source = log.source ?? `${Constants.CENTRAL_SERVER}`;
// Host
log.host = Utils.getHostname();
// Process
@@ -835,8 +799,10 @@ export default class Logging {
// Check
if (log.detailedMessages) {
// Anonymize message
+ if (!Utils.isDevelopmentEnv()) {
log.detailedMessages = Utils.cloneObject(log.detailedMessages);
log.detailedMessages = await Logging.anonymizeSensitiveData(log.detailedMessages);
+ }
// Array?
if (!Array.isArray(log.detailedMessages)) {
log.detailedMessages = [log.detailedMessages];
@@ -855,13 +821,11 @@ export default class Logging {
if (!log.tenantID || log.tenantID === '') {
log.tenantID = Constants.DEFAULT_TENANT;
}
-
// Log in Cloud Foundry
if (Configuration.isCloudFoundry()) {
// Bind to express app
CFLog.logMessage(Logging.getCFLogLevel(log.level), log.message);
}
-
// Log
return LoggingStorage.saveLog(log.tenantID, log);
}

Authorization concept without roles final version