Clear Site Admin flag when User's role is changed to Admin
MODIFY
src/assets/schemas/user/user.json
src/assets/schemas/user/user.json
"type": "string",
"minLength": 1,
"maxLength": 100,
+ "transform": ["toUpperCase"],
"sanitize": "mongo"
},
"firstName": {

MODIFY
src/server/rest/v1/service/UserService.ts
src/server/rest/v1/service/UserService.ts
@@ -7,7 +7,7 @@ import { DataResult, UserDataResult } from '../../../../types/DataResult';
import { HTTPAuthError, HTTPError } from '../../../../types/HTTPError';
import { NextFunction, Request, Response } from 'express';
import Tenant, { TenantComponents } from '../../../../types/Tenant';
-import User, { ImportedUser, UserRequiredImportProperties } from '../../../../types/User';
+import User, { ImportedUser, UserRequiredImportProperties, UserRole } from '../../../../types/User';
import AppAuthError from '../../../../exception/AppAuthError';
import AppError from '../../../../exception/AppError';
@@ -196,12 +196,6 @@ export default class UserService {
lastChangedBy: lastChangedBy,
lastChangedOn: lastChangedOn,
};
- if (filteredRequest.name) {
- user.name = filteredRequest.name.toUpperCase();
- }
- if (filteredRequest.email) {
- user.email = filteredRequest.email.toLowerCase();
- }
// Update User (override TagIDs because it's not of the same type as in filteredRequest)
await UserStorage.saveUser(req.tenant, user, true);
// Save User's password
@@ -636,8 +630,8 @@ export default class UserService {
// Create
const newUser: User = {
...filteredRequest,
- name: filteredRequest.name.toUpperCase(),
- email: filteredRequest.email.toLowerCase(),
+ name: filteredRequest.name,
+ email: filteredRequest.email,
createdBy: { id: req.user.id },
createdOn: new Date(),
issuer: true,
@@ -967,6 +961,10 @@ export default class UserService {
if (Utils.objectHasProperty(user, 'role') &&
projectFields.includes('role')) {
await UserStorage.saveUserRole(tenant, user.id, user.role);
+ // Check Admin
+ if (user.role === UserRole.ADMIN) {
+ await UserStorage.clearUserSiteAdmin(tenant, user.id);
+ }
}
// Save Admin Data
if (projectFields.includes('plateID') ||

MODIFY
src/storage/mongodb/UserStorage.ts
src/storage/mongodb/UserStorage.ts
@@ -165,6 +165,20 @@ export default class UserStorage {
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'addSitesToUser', startTime, siteIDs);
}
+ public static async clearUserSiteAdmin(tenant: Tenant, userID: string): Promise<void> {
+ const startTime = Logging.traceDatabaseRequestStart();
+ DatabaseUtils.checkTenantObject(tenant);
+ // Execute
+ await global.database.getCollection<User>(tenant.id, 'siteusers').updateMany(
+ { userID: DatabaseUtils.convertToObjectID(userID) },
+ {
+ $set: {
+ siteAdmin: false
+ }
+ });
+ await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'clearUserSiteAdmin', startTime, { userID });
+ }
+
public static async addSiteToUser(tenant: Tenant, userID: string, siteID: string): Promise<string> {
const startTime = Logging.traceDatabaseRequestStart();
DatabaseUtils.checkTenantObject(tenant);

Clear Site Admin flag when User's role is changed to Admin