Path: blob/main/components/gitpod-protocol/src/permission.ts
2498 views
/**1* Copyright (c) 2020 Gitpod GmbH. All rights reserved.2* Licensed under the GNU Affero General Public License (AGPL).3* See License.AGPL.txt in the project root for license information.4*/56// see below for explanation7export const Permissions = {8developer: undefined,9"registry-access": undefined,10"admin-permissions": undefined,11"admin-users": undefined,12"admin-workspace-content": undefined,13"admin-workspaces": undefined,14"admin-projects": undefined,15"new-workspace-cluster": undefined,16};17export type PermissionName = keyof typeof Permissions;18export const Roles = { devops: undefined, viewer: undefined, admin: undefined };19export type RoleName = keyof typeof Roles;20export type RoleOrPermission = RoleName | PermissionName;2122export namespace RoleName {23export const is = (o: any): o is RoleName => {24return typeof o === "string" && Role.all().some((r) => r.name === o);25};26}2728export interface Role {29name: RoleName;30permissions: PermissionName[];31}3233export namespace RolesOrPermissions {34export function toPermissionSet(rolesOrPermissions: RoleOrPermission[] | undefined): Set<PermissionName> {35rolesOrPermissions = rolesOrPermissions || [];3637const permissions = new Set<PermissionName>();38for (const rop of rolesOrPermissions) {39if (Permission.is(rop)) {40permissions.add(rop);41} else if (RoleName.is(rop)) {42Role.getByName(rop).permissions.forEach((p) => permissions.add(p));43}44}45return permissions;46}47}4849export namespace Permission {50/** The permission to develop on this running Gitpod installation */51export const DEVELOPER: PermissionName = "developer";5253/** The permission for registry access (start workspaces referencing gitpod-internal Docker images) */54export const REGISTRY_ACCESS: PermissionName = "registry-access";5556/** The permission for administration and deletion of user data */57export const ADMIN_PERMISSIONS: PermissionName = "admin-permissions";5859/** The permission for accessing user data */60export const ADMIN_USERS: PermissionName = "admin-users";6162/** The permission for accessing workspace content */63export const ADMIN_WORKSPACE_CONTENT: PermissionName = "admin-workspace-content";6465/** The permission for accessing workspace data */66export const ADMIN_WORKSPACES: PermissionName = "admin-workspaces";6768/** The permission for accessing all projects data */69export const ADMIN_PROJECTS: PermissionName = "admin-projects";7071export const is = (o: any): o is PermissionName => {72return typeof o === "string" && Permission.all().some((p) => p === o);73};7475export const all = (): PermissionName[] => {76return Object.keys(Permissions) as PermissionName[];77};78}7980export namespace Role {81/** A role for people that are allowed to view Gitpod internals */82export const VIEWER: Role = {83name: "viewer",84permissions: [Permission.REGISTRY_ACCESS],85};8687export const ADMIN: Role = {88name: "admin",89permissions: [Permission.ADMIN_USERS, Permission.ADMIN_WORKSPACES, Permission.ADMIN_PROJECTS],90};9192export const getByName = (name: RoleName): Role => {93const result = Role.all().find((r) => r.name === name);94if (!result) {95throw Error("Unknown RoleName: " + name);96}97return result;98};99100export const all = (): Role[] => {101return Object.keys(Role)102.map((k) => (Role as any)[k])103.filter((k) => typeof k === "object");104};105}106107108