Merge branch '21128-toolbar-context-menu'
[arvados-workbench2.git] / src / models / user.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { Resource, ResourceKind, RESOURCE_UUID_REGEX } from 'models/resource';
6
7 export type UserPrefs = {
8     profile?: {
9         organization?: string,
10         organization_email?: string,
11         lab?: string,
12         website_url?: string,
13         role?: string
14     }
15 };
16
17 export interface User {
18     email: string;
19     firstName: string;
20     lastName: string;
21     uuid: string;
22     ownerUuid: string;
23     username: string;
24     prefs: UserPrefs;
25     isAdmin: boolean;
26     isActive: boolean;
27     canWrite: boolean;
28     canManage: boolean;
29 }
30
31 export const getUserFullname = (user: User) => {
32     return user.firstName && user.lastName
33         ? `${user.firstName} ${user.lastName}`
34         : "";
35 };
36
37 export const getUserDisplayName = (user: User, withEmail = false, withUuid = false) => {
38     const displayName = getUserFullname(user) || user.email || user.username || user.uuid;
39     let parts: string[] = [displayName];
40     if (withEmail && user.email && displayName !== user.email) {
41         parts.push(`<${user.email}>`);
42     }
43     if (withUuid) {
44         parts.push(`(${user.uuid})`);
45     }
46     return parts.join(' ');
47 };
48
49 export const getUserDetailsString = (user: User) => {
50     let parts: string[] = [];
51     const userCluster = getUserClusterID(user);
52     user.username.length && parts.push(user.username);
53     user.email.length && parts.push(`<${user.email}>`);
54     userCluster && userCluster.length && parts.push(`(${userCluster})`);
55     return parts.join(' ');
56 };
57
58 export const getUserClusterID = (user: User): string | undefined => {
59     const match = RESOURCE_UUID_REGEX.exec(user.uuid);
60     const parts = match ? match[0].split('-') : [];
61     return parts.length === 3 ? parts[0] : undefined;
62 };
63
64 export interface UserResource extends Resource, User {
65     kind: ResourceKind.USER;
66     defaultOwnerUuid: string;
67 }