Get owner & admin info from DB instead of token.
MODIFY
src/server/rest/v1/service/AuthorizationService.ts
src/server/rest/v1/service/AuthorizationService.ts
@@ -69,12 +69,14 @@ export default class AuthorizationService {
public static async addSitesAuthorizations(tenant: Tenant, userToken: UserToken, sites: Site[]): Promise<void> {
// Get Site Admins
- // TODO: Should be done like company
const { siteAdminIDs, siteOwnerIDs } = await AuthorizationService.getSiteAdminOwnerIDs(tenant, userToken);
// Set to user
userToken.sitesAdmin = siteAdminIDs;
userToken.sitesOwner = siteOwnerIDs;
// Enrich
+ const assignedSites = await AuthorizationService.getAssignedUserSites(tenant.id, userToken.id);
+ const sitesOwner = assignedSites.filter((site) => site.siteOwner === true).map((site) => site.id);
+ const sitesAdmin = assignedSites.filter((site) => site.siteAdmin === true).map((site) => site.id);
for (const site of sites) {
if (userToken.role === UserRole.ADMIN) {
site.canRead = true;
@@ -82,10 +84,8 @@ export default class AuthorizationService {
site.canDelete = true;
} else {
site.canRead = Authorizations.canReadSite(userToken);
- site.canUpdate = Authorizations.canUpdateSite(userToken)
- && (userToken.sitesAdmin.includes(site.id) || userToken.sitesOwner.includes(site.id));
- site.canDelete = Authorizations.canDeleteSite(userToken)
- && (userToken.sitesAdmin.includes(site.id) || userToken.sitesOwner.includes(site.id));
+ site.canUpdate = Authorizations.canUpdateSite(userToken) && (sitesAdmin.includes(site.id) || sitesOwner.includes(site.id));
+ site.canDelete = Authorizations.canDeleteSite(userToken) && (sitesAdmin.includes(site.id));
}
}
}
@@ -507,6 +507,16 @@ export default class AuthorizationService {
return userSites.result.map((userSite) => userSite.siteID);
}
+ private static async getAssignedUserSites(tenantID: string, userID: string) {
+ // Get the Sites assigned to the user - id, siteOwner, siteAdmin
+ const sites = await UserStorage.getUserSites(tenantID, { userID: userID }, Constants.DB_PARAMS_MAX_LIMIT);
+ return sites.result.map((siteUser) => ({
+ id: siteUser.site.id,
+ siteAdmin: siteUser.siteAdmin,
+ siteOwner: siteUser.siteOwner
+ }));
+ }
+
private static async getAssignedSiteIDs(tenantID: string, userToken: UserToken, siteID?: string): Promise<string[]> {
// Get the Sites where the user is Site Admin
const sites = await SiteStorage.getSites(tenantID,

Get owner & admin info from DB instead of token.