Factorized code for assigning users to sites
MODIFY
src/server/rest/v1/service/AuthService.ts
src/server/rest/v1/service/AuthService.ts
@@ -187,8 +187,7 @@ export default class AuthService {
};
await TagStorage.saveTag(tenant, tag);
// Save User password
- await UserStorage.saveUserPassword(tenant, newUser.id,
- {
+ await UserStorage.saveUserPassword(tenant, newUser.id, {
password: newPasswordHashed,
passwordWrongNbrTrials: 0,
passwordResetHash: null,
@@ -197,23 +196,13 @@ export default class AuthService {
// Save User Account Verification
await UserStorage.saveUserAccountVerification(tenant, newUser.id, { verificationToken });
// Save User EULA
- await UserStorage.saveUserEULA(tenant, newUser.id,
- {
+ await UserStorage.saveUserEULA(tenant, newUser.id, {
eulaAcceptedOn: new Date(),
eulaAcceptedVersion: endUserLicenseAgreement.version,
eulaAcceptedHash: endUserLicenseAgreement.hash
});
- // Assign user to all sites with auto-assign flag set
- const sites = await SiteStorage.getSites(tenant,
- { withAutoUserAssignment: true },
- Constants.DB_PARAMS_MAX_LIMIT
- );
- if (sites.count > 0) {
- const siteIDs = sites.result.map((site) => site.id);
- if (siteIDs && siteIDs.length > 0) {
- await UserStorage.addSitesToUser(tenant, newUser.id, siteIDs);
- }
- }
+ // Assign user to all Sites with auto-assign flag
+ await UtilsService.assignCreatedUserToSites(tenant, newUser);
await Logging.logInfo({
tenantID: tenant.id,
user: newUser, action: action,

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -666,7 +666,7 @@ export default class UserService {
// Update User Admin Data
await UserService.updateUserAdminData(req.tenant, newUser, authorizationFilter.projectFields);
// Assign Site to new User
- await UserService.assignCreatedUserToSites(req.tenant, req.user, newUser, authorizationFilter);
+ await UtilsService.assignCreatedUserToSites(req.tenant, newUser, authorizationFilter);
// Update Billing
await UserService.updateUserBilling(ServerAction.USER_CREATE, req.tenant, req.user, newUser);
// Log
@@ -947,24 +947,6 @@ export default class UserService {
}
}
- private static async assignCreatedUserToSites(tenant: Tenant, loggedUser: UserToken, user: User, authorizationFilter: AuthorizationFilter) {
- // Assign user to sites
- if (Utils.isComponentActiveFromToken(loggedUser, TenantComponents.ORGANIZATION)) {
- let siteIDs = [];
- if (!Utils.isEmptyArray(authorizationFilter.filters.siteIDs)) {
- siteIDs = authorizationFilter.filters.siteIDs;
- } else {
- // Assign user to all sites with auto-assign flag set
- const sites = await SiteStorage.getSites(tenant,
- { withAutoUserAssignment: true },
- Constants.DB_PARAMS_MAX_LIMIT
- );
- siteIDs = sites.result.map((site) => site.id);
- }
- await UserStorage.addSitesToUser(tenant, user.id, siteIDs);
- }
- }
-
private static async updateUserAdminData(tenant: Tenant, user: User, projectFields: string[]) {
// Save User Status
if (Utils.objectHasProperty(user, 'status') &&

MODIFY
src/server/rest/v1/service/UtilsService.ts
src/server/rest/v1/service/UtilsService.ts
-import { Action, Entity } from '../../../../types/Authorization';
+import { Action, AuthorizationFilter, Entity } from '../../../../types/Authorization';
import { Car, CarCatalog } from '../../../../types/Car';
import ChargingStation, { ChargePoint, Voltage } from '../../../../types/ChargingStation';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
@@ -52,6 +52,23 @@ import moment from 'moment';
const MODULE_NAME = 'UtilsService';
export default class UtilsService {
+ public static async assignCreatedUserToSites(tenant: Tenant, user: User, authorizationFilter?: AuthorizationFilter) {
+ // Assign user to sites
+ if (Utils.isTenantComponentActive(tenant, TenantComponents.ORGANIZATION)) {
+ let siteIDs = [];
+ if (!Utils.isEmptyArray(authorizationFilter.filters.siteIDs)) {
+ siteIDs = authorizationFilter.filters.siteIDs;
+ } else {
+ // Assign user to all sites with auto-assign flag set
+ const sites = await SiteStorage.getSites(tenant,
+ { withAutoUserAssignment: true },
+ Constants.DB_PARAMS_MAX_LIMIT
+ );
+ siteIDs = sites.result.map((site) => site.id);
+ }
+ await UserStorage.addSitesToUser(tenant, user.id, siteIDs);
+ }
+ }
public static async checkReCaptcha(tenant: Tenant, action: ServerAction, method: string,
centralSystemRestConfig: CentralSystemRestServiceConfiguration, captcha: string, remoteAddress: string): Promise<void> {

Factorized code for assigning users to sites