update side panel to enable selecting of creator role

This commit is contained in:
Peter Clement 2023-08-24 11:40:00 +01:00
parent f7e43a766d
commit 6b14353dcb
4 changed files with 83 additions and 22 deletions

View File

@ -1,6 +1,8 @@
<script> <script>
import { Select } from "@budibase/bbui" import { Select } from "@budibase/bbui"
import { roles } from "stores/backend" import { roles } from "stores/backend"
import { licensing } from "stores/portal"
import { Constants, RoleUtils } from "@budibase/frontend-core" import { Constants, RoleUtils } from "@budibase/frontend-core"
import { createEventDispatcher } from "svelte" import { createEventDispatcher } from "svelte"
@ -15,17 +17,41 @@
export let align export let align
export let footer = null export let footer = null
export let allowedRoles = null export let allowedRoles = null
export let allowCreator = false
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const RemoveID = "remove" const RemoveID = "remove"
const CreatorID = "CREATOR"
$: options = getOptions($roles, allowPublic, allowRemove, allowedRoles) $: options = getOptions(
$roles,
const getOptions = (roles, allowPublic, allowRemove, allowedRoles) => { allowPublic,
allowRemove,
allowedRoles,
allowCreator
)
const getOptions = (
roles,
allowPublic,
allowRemove,
allowedRoles,
allowCreator
) => {
if (allowedRoles?.length) { if (allowedRoles?.length) {
return roles.filter(role => allowedRoles.includes(role._id)) return roles.filter(role => allowedRoles.includes(role._id))
} }
let newRoles = [...roles] let newRoles = [...roles]
if (allowCreator) {
newRoles = [
{
_id: CreatorID,
name: "Creator",
tag: $licensing.perAppBuildersEnabled && null,
},
...newRoles,
]
}
if (allowRemove) { if (allowRemove) {
newRoles = [ newRoles = [
...newRoles, ...newRoles,
@ -56,8 +82,11 @@
} }
const onChange = e => { const onChange = e => {
console.log(e.detail)
if (allowRemove && e.detail === RemoveID) { if (allowRemove && e.detail === RemoveID) {
dispatch("remove") dispatch("remove")
} else if (e.detail === CreatorID) {
dispatch("addcreator")
} else { } else {
dispatch("change", e.detail) dispatch("change", e.detail)
} }
@ -77,6 +106,13 @@
getOptionValue={role => role._id} getOptionValue={role => role._id}
getOptionColour={getColor} getOptionColour={getColor}
getOptionIcon={getIcon} getOptionIcon={getIcon}
isOptionEnabled={option => {
if (option._id == CreatorID && !$licensing.perAppBuildersEnabled) {
return false
} else {
return true
}
}}
{placeholder} {placeholder}
{error} {error}
/> />

View File

@ -40,7 +40,6 @@
let userLimitReachedModal let userLimitReachedModal
let inviteFailureResponse = "" let inviteFailureResponse = ""
$: queryIsEmail = emailValidator(query) === true $: queryIsEmail = emailValidator(query) === true
$: prodAppId = apps.getProdAppID($store.appId) $: prodAppId = apps.getProdAppID($store.appId)
$: promptInvite = showInvite( $: promptInvite = showInvite(
@ -108,10 +107,16 @@
await usersFetch.refresh() await usersFetch.refresh()
filteredUsers = $usersFetch.rows.map(user => { filteredUsers = $usersFetch.rows.map(user => {
const isAdminOrBuilder = sdk.users.isAdminOrBuilder(user, prodAppId) const isAdminOrGlobalBuilder = sdk.users.isAdminOrGlobalBuilder(
user,
prodAppId
)
const isAppBuilder = sdk.users.hasAppBuilderPermissions(user, prodAppId)
let role = undefined let role = undefined
if (isAdminOrBuilder) { if (isAdminOrGlobalBuilder) {
role = Constants.Roles.ADMIN role = Constants.Roles.ADMIN
} else if (isAppBuilder) {
role = Constants.Roles.CREATOR
} else { } else {
const appRole = Object.keys(user.roles).find(x => x === prodAppId) const appRole = Object.keys(user.roles).find(x => x === prodAppId)
if (appRole) { if (appRole) {
@ -122,7 +127,8 @@
return { return {
...user, ...user,
role, role,
isAdminOrBuilder, isAdminOrGlobalBuilder,
isAppBuilder,
} }
}) })
} }
@ -160,6 +166,12 @@
if (user.role === role) { if (user.role === role) {
return return
} }
if (user.isAppBuilder) {
await removeAppBuilder(user._id, prodAppId)
}
if (role === Constants.Roles.CREATOR) {
await removeAppBuilder(user._id, prodAppId)
}
await updateAppUser(user, role) await updateAppUser(user, role)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
@ -373,6 +385,14 @@
}) })
} }
const addAppBuilder = async userId => {
await users.addAppBuilder(userId, prodAppId)
}
const removeAppBuilder = async userId => {
await users.removeAppBuilder(userId, prodAppId)
}
const initSidePanel = async sidePaneOpen => { const initSidePanel = async sidePaneOpen => {
if (sidePaneOpen === true) { if (sidePaneOpen === true) {
await groups.actions.init() await groups.actions.init()
@ -388,22 +408,12 @@
} }
} }
const userTitle = user => {
if (sdk.users.isAdmin(user)) {
return "Admin"
} else if (sdk.users.isBuilder(user, prodAppId)) {
return "Developer"
} else {
return "App user"
}
}
const getRoleFooter = user => { const getRoleFooter = user => {
if (user.group) { if (user.group) {
const role = $roles.find(role => role._id === user.role) const role = $roles.find(role => role._id === user.role)
return `This user has been given ${role?.name} access from the ${user.group} group` return `This user has been given ${role?.name} access from the ${user.group} group`
} }
if (user.isAdminOrBuilder) { if (user.isAdminOrGlobalBuilder) {
return "This user's role grants admin access to all apps" return "This user's role grants admin access to all apps"
} }
return null return null
@ -594,9 +604,6 @@
<div class="user-email" title={user.email}> <div class="user-email" title={user.email}>
{user.email} {user.email}
</div> </div>
<div class="auth-entity-meta">
{userTitle(user)}
</div>
</div> </div>
<div class="auth-entity-access" class:muted={user.group}> <div class="auth-entity-access" class:muted={user.group}>
<RoleSelect <RoleSelect
@ -605,7 +612,11 @@
value={user.role} value={user.role}
allowRemove={user.role && !user.group} allowRemove={user.role && !user.group}
allowPublic={false} allowPublic={false}
allowCreator
quiet={true} quiet={true}
on:addcreator={() => {
addAppBuilder(user._id)
}}
on:change={e => { on:change={e => {
onUpdateUser(user, e.detail) onUpdateUser(user, e.detail)
}} }}
@ -614,7 +625,7 @@
}} }}
autoWidth autoWidth
align="right" align="right"
allowedRoles={user.isAdminOrBuilder allowedRoles={user.isAdminOrGlobalBuilder
? [Constants.Roles.ADMIN] ? [Constants.Roles.ADMIN]
: null} : null}
/> />

View File

@ -125,6 +125,9 @@ export const createLicensingStore = () => {
const syncAutomationsEnabled = license.features.includes( const syncAutomationsEnabled = license.features.includes(
Constants.Features.SYNC_AUTOMATIONS Constants.Features.SYNC_AUTOMATIONS
) )
const perAppBuildersEnabled = license.features.includes(
Constants.Features.APP_BUILDERS
)
store.update(state => { store.update(state => {
return { return {
...state, ...state,
@ -140,6 +143,7 @@ export const createLicensingStore = () => {
auditLogsEnabled, auditLogsEnabled,
enforceableSSO, enforceableSSO,
syncAutomationsEnabled, syncAutomationsEnabled,
perAppBuildersEnabled,
} }
}) })
}, },

View File

@ -112,6 +112,14 @@ export function createUsersStore() {
return await API.saveUser(user) return await API.saveUser(user)
} }
async function addAppBuilder(userId, appId) {
return await API.addAppBuilder({ userId, appId })
}
async function removeAppBuilder(userId, appId) {
return await API.removeAppBuilder({ userId, appId })
}
const getUserRole = user => const getUserRole = user =>
sdk.users.isAdmin(user) sdk.users.isAdmin(user)
? "admin" ? "admin"
@ -139,6 +147,8 @@ export function createUsersStore() {
getInvites, getInvites,
updateInvite, updateInvite,
getUserCountByApp, getUserCountByApp,
addAppBuilder,
removeAppBuilder,
// any operation that adds or deletes users // any operation that adds or deletes users
acceptInvite, acceptInvite,
create: refreshUsage(create), create: refreshUsage(create),