Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Role constants from frontend core to shared core #13940

Closed
wants to merge 13 commits into from
11 changes: 2 additions & 9 deletions packages/backend-core/src/security/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,11 @@ import {
doWithDB,
} from "../db"
import { getAppDB } from "../context"
import { Screen, Role as RoleDoc } from "@budibase/types"
import { Screen, Role as RoleDoc, Roles } from "@budibase/types"
melohagan marked this conversation as resolved.
Show resolved Hide resolved
import cloneDeep from "lodash/fp/cloneDeep"

export const BUILTIN_ROLE_IDS = {
ADMIN: "ADMIN",
POWER: "POWER",
BASIC: "BASIC",
PUBLIC: "PUBLIC",
}

const BUILTIN_IDS = {
...BUILTIN_ROLE_IDS,
...Roles,
BUILDER: "BUILDER",
}

Expand Down
8 changes: 4 additions & 4 deletions packages/backend-core/src/users/test/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { User, UserGroup } from "@budibase/types"
import { Roles, User, UserGroup } from "@budibase/types"
import { generator, structures } from "../../../tests"
import { DBTestConfiguration } from "../../../tests/extra"
import { getGlobalDB } from "../../context"
Expand Down Expand Up @@ -28,12 +28,12 @@ describe("Users", () => {
})

it("User is a creator if it has CREATOR permission in some application", async () => {
const user: User = structures.users.user({ roles: { app1: "CREATOR" } })
const user: User = structures.users.user({ roles: { app1: Roles.CREATOR } })
expect(await isCreator(user)).toBe(true)
})

it("User is a creator if it has ADMIN permission in some application", async () => {
const user: User = structures.users.user({ roles: { app1: "ADMIN" } })
const user: User = structures.users.user({ roles: { app1: Roles.ADMIN } })
expect(await isCreator(user)).toBe(true)
})

Expand All @@ -42,7 +42,7 @@ describe("Users", () => {
const groupId = "gr_17abffe89e0b40268e755b952f101a59"
const group: UserGroup = {
...structures.userGroups.userGroup(),
...{ _id: groupId, roles: { app1: "ADMIN" } },
...{ _id: groupId, roles: { app1: Roles.ADMIN } },
}
const users: User[] = []
for (let i = 0; i < usersInGroup; i++) {
Expand Down
9 changes: 7 additions & 2 deletions packages/backend-core/src/users/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import { CloudAccount, ContextUser, User, UserGroup } from "@budibase/types"
import {
CloudAccount,
ContextUser,
User,
UserGroup,
Roles as BUILTIN_ROLE_IDS,
} from "@budibase/types"
import * as accountSdk from "../accounts"
import env from "../environment"
import { getPlatformUser } from "./lookup"
import { EmailUnavailableError } from "../errors"
import { getTenantId } from "../context"
import { sdk } from "@budibase/shared-core"
import { getAccountByTenantId } from "../accounts"
import { BUILTIN_ROLE_IDS } from "../security/roles"
import * as context from "../context"

// extract from shared-core to make easily accessible from backend-core
Expand Down
17 changes: 8 additions & 9 deletions packages/builder/src/components/common/RoleSelect.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { licensing } from "stores/portal"

import { Constants, RoleUtils } from "@budibase/frontend-core"
import { Roles } from "@budibase/types"
import { createEventDispatcher } from "svelte"
import { capitalise } from "helpers"

Expand Down Expand Up @@ -51,9 +52,9 @@
name: enrichLabel(role.name),
_id: role._id,
}))
if (allowedRoles.includes(Constants.Roles.CREATOR)) {
if (allowedRoles.includes(Roles.CREATOR)) {
options.push({
_id: Constants.Roles.CREATOR,
_id: Roles.CREATOR,
name: "Can edit",
enabled: false,
})
Expand All @@ -70,7 +71,7 @@
// Add creator if required
if (allowCreator) {
options.unshift({
_id: Constants.Roles.CREATOR,
_id: Roles.CREATOR,
name: "Can edit",
tag:
!$licensing.perAppBuildersEnabled &&
Expand All @@ -88,15 +89,15 @@

// Remove public if not allowed
if (!allowPublic) {
options = options.filter(role => role._id !== Constants.Roles.PUBLIC)
options = options.filter(role => role._id !== Roles.PUBLIC)
}

return options
}

const getColor = role => {
// Creator and remove options have no colors
if (role._id === Constants.Roles.CREATOR || role._id === RemoveID) {
if (role._id === Roles.CREATOR || role._id === RemoveID) {
return null
}
return RoleUtils.getRoleColour(role._id)
Expand Down Expand Up @@ -135,8 +136,7 @@
getOptionColour={getColor}
getOptionIcon={getIcon}
isOptionEnabled={option =>
option._id !== Constants.Roles.CREATOR ||
$licensing.perAppBuildersEnabled}
option._id !== Roles.CREATOR || $licensing.perAppBuildersEnabled}
{placeholder}
{error}
/>
Expand All @@ -155,8 +155,7 @@
getOptionColour={getColor}
getOptionIcon={getIcon}
isOptionEnabled={option =>
(option._id !== Constants.Roles.CREATOR ||
$licensing.perAppBuildersEnabled) &&
(option._id !== Roles.CREATOR || $licensing.perAppBuildersEnabled) &&
option.enabled !== false}
{placeholder}
{error}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script>
import { Label, notifications, Select } from "@budibase/bbui"
import { permissions, roles } from "stores/builder"
import { Constants } from "@budibase/frontend-core"
import { Roles } from "@budibase/types"

export let query
export let label
Expand Down Expand Up @@ -35,14 +35,14 @@
}
fetched = queryToFetch
if (!queryToFetch || !queryToFetch._id) {
roleId = Constants.Roles.BASIC
roleId = Roles.BASIC
loaded = true
return
}
try {
roleId = (await permissions.forResource(queryToFetch._id))["read"].role
} catch (err) {
roleId = Constants.Roles.BASIC
roleId = Roles.BASIC
}
loaded = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
RoleUtils,
} from "@budibase/frontend-core"
import { sdk } from "@budibase/shared-core"
import { Roles } from "@budibase/types"
import { API } from "api"
import GroupIcon from "../../../portal/users/groups/_components/GroupIcon.svelte"
import RoleSelect from "components/common/RoleSelect.svelte"
Expand All @@ -49,7 +50,7 @@
let error
let form
let creationRoleType = Constants.BudibaseRoles.AppUser
let creationAccessType = Constants.Roles.BASIC
let creationAccessType = Roles.BASIC

let appInvites = []
let filteredInvites = []
Expand Down Expand Up @@ -133,9 +134,9 @@
const isAppBuilder = user.builder?.apps?.includes(prodAppId)
let role
if (isAdminOrGlobalBuilder) {
role = Constants.Roles.ADMIN
role = Roles.ADMIN
} else if (isAppBuilder) {
role = Constants.Roles.CREATOR
role = Roles.CREATOR
} else {
const appRole = user.roles[prodAppId]
if (appRole) {
Expand Down Expand Up @@ -218,7 +219,7 @@
if (user.isAppBuilder) {
await removeAppBuilder(user._id, prodAppId)
}
if (role === Constants.Roles.CREATOR) {
if (role === Roles.CREATOR) {
await removeAppBuilder(user._id, prodAppId)
}
await updateAppUser(user, role)
Expand Down Expand Up @@ -296,7 +297,7 @@
return {
...group,
role: group?.builder?.apps.includes(prodAppId)
? Constants.Roles.CREATOR
? Roles.CREATOR
: group.roles?.[
groups.actions.getGroupAppIds(group).find(x => x === prodAppId)
],
Expand Down Expand Up @@ -376,7 +377,7 @@
]

const notCreatingAdmin = creationRoleType !== Constants.BudibaseRoles.Admin
const isCreator = creationAccessType === Constants.Roles.CREATOR
const isCreator = creationAccessType === Roles.CREATOR
if (notCreatingAdmin && isCreator) {
payload[0].builder.apps = [prodAppId]
} else if (notCreatingAdmin && !isCreator) {
Expand Down Expand Up @@ -446,11 +447,11 @@
},
}

if (role === Constants.Roles.CREATOR) {
if (role === Roles.CREATOR) {
updateBody.builder = updateBody.builder || {}
updateBody.builder.apps = [...(updateBody.builder.apps ?? []), prodAppId]
delete updateBody?.apps?.[prodAppId]
} else if (role !== Constants.Roles.CREATOR && invite?.builder?.apps) {
} else if (role !== Roles.CREATOR && invite?.builder?.apps) {
invite.builder.apps = []
}
await users.updateInvite(updateBody)
Expand Down Expand Up @@ -505,7 +506,7 @@
(invite.info?.admin?.global && invite.info?.builder?.global) ||
invite.info?.builder?.apps?.includes(prodAppId)
) {
return Constants.Roles.CREATOR
return Roles.CREATOR
}
return invite.info.apps?.[prodAppId]
}
Expand All @@ -523,7 +524,7 @@

const parseRole = user => {
if (user.isAdminOrGlobalBuilder) {
return Constants.Roles.CREATOR
return Roles.CREATOR
}
return user.role
}
Expand All @@ -532,11 +533,11 @@
// Ensure we don't get into an invalid combo of tenant role and app access
if (
e.detail === Constants.BudibaseRoles.AppUser &&
creationAccessType === Constants.Roles.CREATOR
creationAccessType === Roles.CREATOR
) {
creationAccessType = Constants.Roles.BASIC
creationAccessType = Roles.BASIC
} else if (e.detail === Constants.BudibaseRoles.Admin) {
creationAccessType = Constants.Roles.CREATOR
creationAccessType = Roles.CREATOR
}
}
</script>
Expand Down Expand Up @@ -660,7 +661,7 @@
autoWidth
align="right"
allowedRoles={user.isAdminOrGlobalBuilder
? [Constants.Roles.CREATOR]
? [Roles.CREATOR]
: null}
labelPrefix="Can use as"
/>
Expand Down Expand Up @@ -706,7 +707,7 @@
allowRemove={group.role}
allowPublic={false}
quiet={true}
allowCreator={group.role === Constants.Roles.CREATOR}
allowCreator={group.role === Roles.CREATOR}
on:change={e => {
onUpdateGroup(group, e.detail)
}}
Expand Down Expand Up @@ -747,7 +748,7 @@
quiet={true}
on:addcreator={() => {}}
on:change={e => {
if (e.detail === Constants.Roles.CREATOR) {
if (e.detail === Roles.CREATOR) {
addAppBuilder(user._id)
} else {
onUpdateUser(user, e.detail)
Expand All @@ -759,7 +760,7 @@
autoWidth
align="right"
allowedRoles={user.isAdminOrGlobalBuilder
? [Constants.Roles.CREATOR]
? [Roles.CREATOR]
: null}
labelPrefix="Can use as"
/>
Expand Down Expand Up @@ -832,7 +833,7 @@
align="right"
fancySelect
allowedRoles={creationRoleType === Constants.BudibaseRoles.Admin
? [Constants.Roles.CREATOR]
? [Roles.CREATOR]
: null}
footer={getRoleFooter({
isAdminOrGlobalBuilder:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import NavItem from "./NavItem.svelte"
import { generate } from "shortid"
import { getSequentialName } from "helpers/duplicate"
import { Constants } from "@budibase/frontend-core"
import { Roles } from "@budibase/types"

export let bindings

Expand Down Expand Up @@ -52,7 +52,7 @@
getName: x => x.text,
}),
url: "",
roleId: Constants.Roles.BASIC,
roleId: Roles.BASIC,
type: "link",
},
])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import GroupIcon from "./_components/GroupIcon.svelte"
import GroupUsers from "./_components/GroupUsers.svelte"
import { sdk } from "@budibase/shared-core"
import { Constants } from "@budibase/frontend-core"
import { Roles } from "@budibase/types"

export let groupId

Expand Down Expand Up @@ -60,7 +60,7 @@
.map(app => ({
...app,
role: group?.builder?.apps.includes(appsStore.getProdAppID(app.devId))
? Constants.Roles.CREATOR
? Roles.CREATOR
: group?.roles?.[appsStore.getProdAppID(app.devId)],
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import AppNameTableRenderer from "./_components/AppNameTableRenderer.svelte"
import AppRoleTableRenderer from "./_components/AppRoleTableRenderer.svelte"
import { sdk } from "@budibase/shared-core"
import { Roles } from "@budibase/types"
import ActiveDirectoryInfo from "../_components/ActiveDirectoryInfo.svelte"

export let userId
Expand Down Expand Up @@ -136,11 +137,11 @@

const getRole = (prodAppId, roles) => {
if (privileged) {
return Constants.Roles.ADMIN
return Roles.ADMIN
}

if (user?.builder?.apps?.includes(prodAppId)) {
return Constants.Roles.CREATOR
return Roles.CREATOR
}

return roles[prodAppId]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<script>
import { StatusLight } from "@budibase/bbui"
import { RoleUtils, Constants } from "@budibase/frontend-core"
import { RoleUtils } from "@budibase/frontend-core"
import { roles } from "stores/builder"
import { capitalise } from "helpers"
import { Roles } from "@budibase/types"

export let value

const getRoleLabel = roleId => {
const role = $roles.find(x => x._id === roleId)
return roleId === Constants.Roles.CREATOR
? capitalise(Constants.Roles.CREATOR.toLowerCase())
return roleId === Roles.CREATOR
? capitalise(Roles.CREATOR.toLowerCase())
: role?.name || "Custom role"
}
</script>

{#if value === Constants.Roles.CREATOR}
{#if value === Roles.CREATOR}
Can edit
{:else}
<StatusLight square color={RoleUtils.getRoleColour(value)}>
Expand Down
Loading
Loading