Building out the role assignment/unassignment APIs as new components of the public API.
This commit is contained in:
parent
cb49906d36
commit
a6a70c2d09
|
@ -1,30 +1,30 @@
|
||||||
import env from "../environment"
|
import env from "../environment"
|
||||||
import * as eventHelpers from "./events"
|
import * as eventHelpers from "./events"
|
||||||
import * as accounts from "../accounts"
|
import * as accounts from "../accounts"
|
||||||
|
import * as accountSdk from "../accounts"
|
||||||
import * as cache from "../cache"
|
import * as cache from "../cache"
|
||||||
import { getIdentity, getTenantId, getGlobalDB } from "../context"
|
import { getGlobalDB, getIdentity, getTenantId } from "../context"
|
||||||
import * as dbUtils from "../db"
|
import * as dbUtils from "../db"
|
||||||
import { EmailUnavailableError, HTTPError } from "../errors"
|
import { EmailUnavailableError, HTTPError } from "../errors"
|
||||||
import * as platform from "../platform"
|
import * as platform from "../platform"
|
||||||
import * as sessions from "../security/sessions"
|
import * as sessions from "../security/sessions"
|
||||||
import * as usersCore from "./users"
|
import * as usersCore from "./users"
|
||||||
import {
|
import {
|
||||||
|
Account,
|
||||||
AllDocsResponse,
|
AllDocsResponse,
|
||||||
BulkUserCreated,
|
BulkUserCreated,
|
||||||
BulkUserDeleted,
|
BulkUserDeleted,
|
||||||
|
isSSOAccount,
|
||||||
|
isSSOUser,
|
||||||
RowResponse,
|
RowResponse,
|
||||||
SaveUserOpts,
|
SaveUserOpts,
|
||||||
User,
|
User,
|
||||||
Account,
|
|
||||||
isSSOUser,
|
|
||||||
isSSOAccount,
|
|
||||||
UserStatus,
|
UserStatus,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import * as accountSdk from "../accounts"
|
|
||||||
import {
|
import {
|
||||||
validateUniqueUser,
|
|
||||||
getAccountHolderFromUserIds,
|
getAccountHolderFromUserIds,
|
||||||
isAdmin,
|
isAdmin,
|
||||||
|
validateUniqueUser,
|
||||||
} from "./utils"
|
} from "./utils"
|
||||||
import { searchExistingEmails } from "./lookup"
|
import { searchExistingEmails } from "./lookup"
|
||||||
import { hash } from "../utils"
|
import { hash } from "../utils"
|
||||||
|
@ -179,6 +179,14 @@ export class UserDB {
|
||||||
return user
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async bulkGet(userIds: string[]) {
|
||||||
|
return await usersCore.bulkGetGlobalUsersById(userIds)
|
||||||
|
}
|
||||||
|
|
||||||
|
static async bulkUpdate(users: User[]) {
|
||||||
|
return await usersCore.bulkUpdateGlobalUsers(users)
|
||||||
|
}
|
||||||
|
|
||||||
static async save(user: User, opts: SaveUserOpts = {}): Promise<User> {
|
static async save(user: User, opts: SaveUserOpts = {}): Promise<User> {
|
||||||
// default booleans to true
|
// default booleans to true
|
||||||
if (opts.hashPassword == null) {
|
if (opts.hashPassword == null) {
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 7394675d71f1a0f8c5471644ed03a079683297a2
|
Subproject commit 1fc0fe26ec843794c4b80d25e593e3f6edd34840
|
|
@ -3,6 +3,13 @@ import Resource from "./utils/Resource"
|
||||||
|
|
||||||
const roleSchema = object(
|
const roleSchema = object(
|
||||||
{
|
{
|
||||||
|
appBuilder: object({
|
||||||
|
appId: {
|
||||||
|
description:
|
||||||
|
"The app that the users should have app builder privileges granted for.",
|
||||||
|
type: "string",
|
||||||
|
},
|
||||||
|
}),
|
||||||
builder: object(
|
builder: object(
|
||||||
{
|
{
|
||||||
global: {
|
global: {
|
||||||
|
|
|
@ -1,12 +1,29 @@
|
||||||
import { UserCtx } from "@budibase/types"
|
import {
|
||||||
|
UserCtx,
|
||||||
|
RoleAssignmentResponse,
|
||||||
|
RoleAssignmentRequest,
|
||||||
|
} from "@budibase/types"
|
||||||
import { Next } from "koa"
|
import { Next } from "koa"
|
||||||
|
import { sdk } from "@budibase/pro"
|
||||||
|
|
||||||
async function assign(ctx: UserCtx, next: Next) {
|
async function assign(
|
||||||
ctx.body = { message: "roles assigned" }
|
ctx: UserCtx<RoleAssignmentRequest, RoleAssignmentResponse>,
|
||||||
|
next: Next
|
||||||
|
) {
|
||||||
|
const { userIds, ...assignmentProps } = ctx.request.body
|
||||||
|
await sdk.publicApi.roles.assign(userIds, assignmentProps)
|
||||||
|
ctx.body = { userIds }
|
||||||
|
await next()
|
||||||
}
|
}
|
||||||
|
|
||||||
async function unAssign(ctx: UserCtx, next: Next) {
|
async function unAssign(
|
||||||
ctx.body = { message: "roles un-assigned" }
|
ctx: UserCtx<RoleAssignmentRequest, RoleAssignmentResponse>,
|
||||||
|
next: Next
|
||||||
|
) {
|
||||||
|
const { userIds, ...unAssignmentProps } = ctx.request.body
|
||||||
|
await sdk.publicApi.roles.unAssign(userIds, unAssignmentProps)
|
||||||
|
ctx.body = { userIds }
|
||||||
|
await next()
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|
|
@ -35,7 +35,7 @@ export async function search(ctx: UserCtx, next: Next) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create(ctx: UserCtx, next: Next) {
|
export async function create(ctx: UserCtx, next: Next) {
|
||||||
ctx = publicApiUserFix(await sdk.publicApi.userSaveUpdate(ctx))
|
ctx = publicApiUserFix(await sdk.publicApi.users.roleCheck(ctx))
|
||||||
const response = await saveGlobalUser(ctx)
|
const response = await saveGlobalUser(ctx)
|
||||||
ctx.body = await getUser(ctx, response._id)
|
ctx.body = await getUser(ctx, response._id)
|
||||||
await next()
|
await next()
|
||||||
|
@ -52,7 +52,7 @@ export async function update(ctx: UserCtx, next: Next) {
|
||||||
...ctx.request.body,
|
...ctx.request.body,
|
||||||
_rev: user._rev,
|
_rev: user._rev,
|
||||||
}
|
}
|
||||||
ctx = publicApiUserFix(await sdk.publicApi.userSaveUpdate(ctx, user))
|
ctx = publicApiUserFix(await sdk.publicApi.users.roleCheck(ctx, user))
|
||||||
const response = await saveGlobalUser(ctx)
|
const response = await saveGlobalUser(ctx)
|
||||||
ctx.body = await getUser(ctx, response._id)
|
ctx.body = await getUser(ctx, response._id)
|
||||||
await next()
|
await next()
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
export * from "./account"
|
export * from "./account"
|
||||||
export * from "./web"
|
export * from "./web"
|
||||||
|
export * from "./public"
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
export * from "./roles"
|
|
@ -0,0 +1,16 @@
|
||||||
|
export interface RoleAssignmentRequest {
|
||||||
|
role?: {
|
||||||
|
appId: string
|
||||||
|
roleId: string
|
||||||
|
}
|
||||||
|
appBuilder?: {
|
||||||
|
appId: string
|
||||||
|
}
|
||||||
|
builder?: boolean
|
||||||
|
admin?: boolean
|
||||||
|
userIds: string[]
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RoleAssignmentResponse {
|
||||||
|
userIds: string[]
|
||||||
|
}
|
Loading…
Reference in New Issue