Add resource service helpers, add user service, add ancestors-service
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 23 Aug 2018 15:57:59 +0000 (17:57 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 23 Aug 2018 15:57:59 +0000 (17:57 +0200)
Feature #14102

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/models/user.ts
src/services/ancestors-service/ancestors-service.ts [new file with mode: 0644]
src/services/services.ts
src/services/user-service/user-service.ts [new file with mode: 0644]

index 4cc29ba779cc6dfd677c965d0c485e7bc18fb0fe..c2f21e582798dacd5597872696ff7fc1685d62e7 100644 (file)
@@ -2,6 +2,8 @@
 //
 // SPDX-License-Identifier: AGPL-3.0
 
+import { Resource, ResourceKind } from '~/models/resource';
+
 export interface User {
     email: string;
     firstName: string;
@@ -12,4 +14,18 @@ export interface User {
 
 export const getUserFullname = (user?: User) => {
     return user ? `${user.firstName} ${user.lastName}` : "";
-};
\ No newline at end of file
+};
+
+export interface UserResource extends Resource {
+    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
diff --git a/src/services/ancestors-service/ancestors-service.ts b/src/services/ancestors-service/ancestors-service.ts
new file mode 100644 (file)
index 0000000..5eab842
--- /dev/null
@@ -0,0 +1,44 @@
+import { GroupsService } from "~/services/groups-service/groups-service";
+import { UserService } from '../user-service/user-service';
+import { GroupResource } from '~/models/group';
+import { UserResource } from '../../models/user';
+import { extractUuidObjectType, ResourceObjectType } from "~/models/resource";
+
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+export class AncestorService {
+    constructor(
+        private groupsService: GroupsService,
+        private userService: UserService
+    ) { }
+
+    async ancestors(uuid: string, rootUuid: string): Promise<Array<UserResource | GroupResource>> {
+        const service = this.getService(extractUuidObjectType(uuid));
+        if (service) {
+            const resource = await service.get(uuid);
+            if (uuid === rootUuid) {
+                return [resource];
+            } else {
+                return [
+                    ...await this.ancestors(resource.ownerUuid, rootUuid),
+                    resource
+                ];
+            }
+        } else {
+            return [];
+        }
+    }
+
+    private getService = (objectType?: string) => {
+        switch (objectType) {
+            case ResourceObjectType.GROUP:
+                return this.groupsService;
+            case ResourceObjectType.USER:
+                return this.userService;
+            default:
+                return undefined;
+        }
+    }
+}
\ No newline at end of file
index 0e1f4b438bc239ee02578b9d8aebe751b18f096c..6295527bfb40d5c326b56a4a4d09dfe27d22e85e 100644 (file)
@@ -14,6 +14,9 @@ import { CollectionFilesService } from "./collection-files-service/collection-fi
 import { KeepService } from "./keep-service/keep-service";
 import { WebDAV } from "../common/webdav";
 import { Config } from "../common/config";
+import { UserService } from './user-service/user-service';
+import { AncestorService } from "~/services/ancestors-service/ancestors-service";
+import { ResourceKind } from "~/models/resource";
 
 export type ServiceRepository = ReturnType<typeof createServices>;
 
@@ -33,6 +36,8 @@ export const createServices = (config: Config) => {
     const collectionService = new CollectionService(apiClient, webdavClient, authService);
     const tagService = new TagService(linkService);
     const collectionFilesService = new CollectionFilesService(collectionService);
+    const userService = new UserService(apiClient);
+    const ancestorsService = new AncestorService(groupsService, userService);
 
     return {
         apiClient,
@@ -45,6 +50,21 @@ export const createServices = (config: Config) => {
         favoriteService,
         collectionService,
         tagService,
-        collectionFilesService
+        collectionFilesService,
+        userService,
+        ancestorsService,
     };
 };
+
+export const getResourceService = (kind?: ResourceKind) => (serviceRepository: ServiceRepository) => {
+    switch (kind) {
+        case ResourceKind.USER:
+            return serviceRepository.userService;
+        case ResourceKind.GROUP:
+            return serviceRepository.groupsService;
+        case ResourceKind.COLLECTION:
+            return serviceRepository.collectionService;
+        default:
+            return undefined;
+    }
+};
\ No newline at end of file
diff --git a/src/services/user-service/user-service.ts b/src/services/user-service/user-service.ts
new file mode 100644 (file)
index 0000000..3c09a87
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
+import { AxiosInstance } from "axios";
+import { CommonResourceService } from "~/common/api/common-resource-service";
+import { UserResource } from "~/models/user";
+
+export class UserService extends CommonResourceService<UserResource> {
+    constructor(serverApi: AxiosInstance) {
+        super(serverApi, "users");
+    }
+}
\ No newline at end of file