diff --git a/app/common/UserAPI.ts b/app/common/UserAPI.ts index ff81baea..d9732369 100644 --- a/app/common/UserAPI.ts +++ b/app/common/UserAPI.ts @@ -167,6 +167,7 @@ export interface UserAccessData { // access to the resource. Lack of access to the parent resource is represented by a null value. // If parent has non-inheritable access, this should be null. parentAccess?: roles.BasicRole|null; + anonymous?: boolean; // If set to true, the user is the anonymous user. } /** diff --git a/app/gen-server/lib/HomeDBManager.ts b/app/gen-server/lib/HomeDBManager.ts index 2bf94066..e4c9721f 100644 --- a/app/gen-server/lib/HomeDBManager.ts +++ b/app/gen-server/lib/HomeDBManager.ts @@ -375,16 +375,19 @@ export class HomeDBManager extends EventEmitter { * Convert a user record into the format specified in api. */ public makeFullUser(user: User): FullUser { - if (!(user.logins && user.logins[0].displayEmail)) { + if (!user.logins?.[0]?.displayEmail) { throw new ApiError("unable to find mandatory user email", 400); } - return { + const result: FullUser = { id: user.id, email: user.logins[0].displayEmail, name: user.name, picture: user.picture, - anonymous: this.getAnonymousUserId() === user.id }; + if (this.getAnonymousUserId() === user.id) { + result.anonymous = true; + } + return result; } public async updateUser(userId: number, props: UserProfileChange): Promise { @@ -1915,10 +1918,7 @@ export class HomeDBManager extends EventEmitter { const org: Organization = queryResult.data; const userRoleMap = getMemberUserRoles(org, this.defaultGroupNames); const users = getResourceUsers(org).filter(u => userRoleMap[u.id]).map(u => ({ - id: u.id, - name: u.name, - email: u.logins.map((login: Login) => login.displayEmail)[0], - picture: u.picture, + ...this.makeFullUser(u), access: userRoleMap[u.id] })); return { @@ -1960,10 +1960,7 @@ export class HomeDBManager extends EventEmitter { // Iterate through the org since all users will be in the org. const users: UserAccessData[] = getResourceUsers([workspace, workspace.org]).map(u => { return { - id: u.id, - name: u.name, - email: u.logins.map((login: Login) => login.email)[0], - picture: u.picture, + ...this.makeFullUser(u), access: wsMap[u.id] || null, parentAccess: roles.getEffectiveRole(orgMap[u.id] || null) }; @@ -1998,10 +1995,7 @@ export class HomeDBManager extends EventEmitter { // resource access levels must be tempered by the maxInheritedRole values of their children. const inheritFromOrg = roles.getWeakestRole(orgMap[u.id] || null, wsMaxInheritedRole); return { - id: u.id, - name: u.name, - email: u.logins.map((login: Login) => login.email)[0], - picture: u.picture, + ...this.makeFullUser(u), access: docMap[u.id] || null, parentAccess: roles.getEffectiveRole( roles.getStrongestRole(wsMap[u.id] || null, inheritFromOrg)