/////////////////////////////////////////////////////////////////////////////// // Copyright (C) 2002-2021, Open Design Alliance (the "Alliance"). // All rights reserved. // // This software and its documentation and related materials are owned by // the Alliance. The software may only be incorporated into application // programs owned by members of the Alliance, subject to a signed // Membership Agreement and Supplemental Software License Agreement with the // Alliance. The structure and organization of this software are the valuable // trade secrets of the Alliance and its suppliers. The software is also // protected by copyright law and international treaty provisions. Application // programs incorporating this software must include the following statement // with their copyright notices: // // This application incorporates Open Design Alliance software pursuant to a // license agreement with Open Design Alliance. // Open Design Alliance Copyright (C) 2002-2021 by Open Design Alliance. // All rights reserved. // // By use of this software, its documentation or related materials, you // acknowledge and accept the above terms. /////////////////////////////////////////////////////////////////////////////// import { IHttpClient } from "./IHttpClient"; import { json } from "./impl/Utils"; /** * A permission provides information about {@link File | file} sharing granted to a specific * {@link User | users}, group, or project {@link Member | member}. */ export class Permission { private _data: any; protected fileId: string; protected httpClient: IHttpClient; /** * @param data - An object that implements permission data storage. * @param fileId - Owner file ID. * @param httpClient - Http client. */ constructor(data: any, fileId: string, httpClient: IHttpClient) { this.httpClient = httpClient; this.fileId = fileId; this.data = data; } private internalGet() { return this.httpClient.get(`/files/${this.fileId}/permissions/${this.id}`); } private internalPut(body?: ArrayBuffer | Blob | globalThis.File | FormData | object | string | null) { return this.httpClient.put(`/files/${this.fileId}/permissions/${this.id}`, body); } private internalDelete() { return this.httpClient.delete(`/files/${this.fileId}/permissions/${this.id}`); } /** * Defines what actions are allowed to be performed on a file with this permission: * * - `read` - The ability to read file description, geometry data and properties * - `readSourceFile` - The ability to read source file * - `write` - The ability to modify file name, description and references * - `readViewpoint` - The ability to read file viewpoints * - `createViewpoint` - The ability to create file viewpoints */ get actions(): string[] { return this.data.actions; } set actions(value: string[]) { this._data.actions = value; } /** * Raw permission data received from the server. * * @readonly */ get data(): any { return this._data; } private set data(value: any) { this._data = value; } /** * Unique permission ID. * * @readonly */ get id(): string { return this.data.id; } /** * Principials are any entity that can be authenticated by the server, such as a any user or * project that will get access to the file. * * @typedef {any} Principial * @property {any} user - The user entry that get access to the file. * @property {string} user.id - User ID. * @property {string} user.name - User name. * @property {any} project - The project entry that get access to the file. * @property {string} project.id - Project ID. * @property {string} project.name - Project name. */ /** * A collection of principials that will get access to the file. */ get grantedTo(): any[] { return this.data.grantedTo; } set grantedTo(value: any[]) { this.data.grantedTo = value; } /** * Specifies whether all users have access to the file or not. */ get public(): boolean { return this.data.public; } set public(value: boolean) { this.data.public = value; } /** * Refresh permission data. * * @async */ async checkout(): Promise { this.data = await json(this.internalGet()); return this; } /** * Update permission data on the server. * * @async * @param data - Raw permission data. */ async update(data: any): Promise { this.data = await json(this.internalPut(data)); return this; } /** * Remove a permission from a file. * * @async * @returns Returns the raw data of a deleted permission. */ delete(): Promise { return json(this.internalDelete()); } /** * Save permission data changes to the server. Call this method to update permission data on * the server after any changes. * * @async */ save(): Promise { return this.update(this.data); } }