budibase/packages/server/src/automations/steps/createUser.js

94 lines
2.2 KiB
JavaScript
Raw Normal View History

const roles = require("../../utilities/security/roles")
const userController = require("../../api/controllers/user")
const env = require("../../environment")
const usage = require("../../utilities/usageQuota")
module.exports.definition = {
description: "Create a new user",
2020-12-04 13:22:45 +01:00
tagline: "Create user {{inputs.email}}",
icon: "ri-user-add-line",
name: "Create User",
type: "ACTION",
stepId: "CREATE_USER",
inputs: {
roleId: roles.BUILTIN_ROLE_IDS.POWER,
},
schema: {
inputs: {
properties: {
2020-12-04 13:22:45 +01:00
email: {
type: "string",
2020-12-04 13:22:45 +01:00
customType: "email",
title: "Email",
},
password: {
type: "string",
customType: "password",
title: "Password",
},
roleId: {
type: "string",
title: "Role",
enum: roles.BUILTIN_ROLE_ID_ARRAY,
pretty: roles.BUILTIN_ROLE_NAME_ARRAY,
},
},
2020-12-07 22:20:35 +01:00
required: ["email", "password", "roleId"],
},
outputs: {
properties: {
id: {
type: "string",
description: "The identifier of the new user",
},
revision: {
type: "string",
description: "The revision of the new user",
},
response: {
type: "object",
description: "The response from the user table",
},
success: {
type: "boolean",
description: "Whether the action was successful",
},
},
required: ["id", "revision", "success"],
},
},
}
module.exports.run = async function({ inputs, appId, apiKey, emitter }) {
2020-12-07 22:20:35 +01:00
const { email, password, roleId } = inputs
const ctx = {
user: {
appId: appId,
},
request: {
2020-12-07 22:20:35 +01:00
body: { email, password, roleId },
},
eventEmitter: emitter,
}
try {
if (env.CLOUD) {
await usage.update(apiKey, usage.Properties.USER, 1)
}
await userController.create(ctx)
return {
response: ctx.body,
// internal property not returned through the API
id: ctx.userId,
revision: ctx.body._rev,
success: ctx.status === 200,
}
} catch (err) {
console.error(err)
return {
success: false,
response: err,
}
}
2020-09-21 15:48:24 +02:00
}