Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / models / user.ts
index 60d598d57bd637c0728476f6b6b5f18a77909270..0df6eac24158809ce426560359bab96b0985fe1c 100644 (file)
@@ -2,7 +2,7 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { Resource, ResourceKind } from '~/models/resource';
+import { Resource, ResourceKind, RESOURCE_UUID_REGEX } from 'models/resource';
 
 export type UserPrefs = {
     profile?: {
@@ -20,25 +20,48 @@ export interface User {
     lastName: string;
     uuid: string;
     ownerUuid: string;
-    identityUrl: string;
+    username: string;
     prefs: UserPrefs;
     isAdmin: boolean;
+    isActive: boolean;
+    canWrite: boolean;
+    canManage: boolean;
 }
 
-export const getUserFullname = (user?: User) => {
-    return user ? `${user.firstName} ${user.lastName}` : "";
+export const getUserFullname = (user: User) => {
+    return user.firstName && user.lastName
+        ? `${user.firstName} ${user.lastName}`
+        : "";
 };
 
-export interface UserResource extends Resource {
+export const getUserDisplayName = (user: User, withEmail = false, withUuid = false) => {
+    const displayName = getUserFullname(user) || user.email || user.username || user.uuid;
+    let parts: string[] = [displayName];
+    if (withEmail && user.email && displayName !== user.email) {
+        parts.push(`<${user.email}>`);
+    }
+    if (withUuid) {
+        parts.push(`(${user.uuid})`);
+    }
+    return parts.join(' ');
+};
+
+export const getUserDetailsString = (user: User) => {
+    let parts: string[] = [];
+    const userCluster = getUserClusterID(user);
+    user.username.length && parts.push(user.username);
+    user.email.length && parts.push(`<${user.email}>`);
+    userCluster && userCluster.length && parts.push(`(${userCluster})`);
+    return parts.join(' ');
+};
+
+export const getUserClusterID = (user: User): string | undefined => {
+    const match = RESOURCE_UUID_REGEX.exec(user.uuid);
+    const parts = match ? match[0].split('-') : [];
+    return parts.length === 3 ? parts[0] : undefined;
+};
+
+export interface UserResource extends Resource, User {
     kind: ResourceKind.USER;
-    email: string;
-    username: string;
-    firstName: string;
-    lastName: string;
-    identityUrl: string;
-    isAdmin: boolean;
-    prefs: string;
     defaultOwnerUuid: string;
-    isActive: boolean;
-    writableBy: string[];
-}
\ No newline at end of file
+}