2020-11-11 18:34:15 +01:00
|
|
|
const accessLevels = require("../../utilities/security/accessLevels")
|
2020-09-16 15:00:04 +02:00
|
|
|
const userController = require("../../api/controllers/user")
|
2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../../environment")
|
2020-10-08 18:34:41 +02:00
|
|
|
const usage = require("../../utilities/usageQuota")
|
2020-09-16 15:00:04 +02:00
|
|
|
|
|
|
|
module.exports.definition = {
|
|
|
|
description: "Create a new user",
|
|
|
|
tagline: "Create user {{inputs.username}}",
|
2020-10-27 13:33:25 +01:00
|
|
|
icon: "ri-user-add-line",
|
2020-09-16 15:00:04 +02:00
|
|
|
name: "Create User",
|
|
|
|
type: "ACTION",
|
2020-09-16 20:25:52 +02:00
|
|
|
stepId: "CREATE_USER",
|
2020-09-17 13:44:59 +02:00
|
|
|
inputs: {
|
2020-11-13 16:35:20 +01:00
|
|
|
accessLevelId: accessLevels.BUILTIN_LEVEL_IDS.POWER,
|
2020-09-17 13:44:59 +02:00
|
|
|
},
|
2020-09-16 15:00:04 +02:00
|
|
|
schema: {
|
|
|
|
inputs: {
|
|
|
|
properties: {
|
|
|
|
username: {
|
|
|
|
type: "string",
|
|
|
|
title: "Username",
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
type: "string",
|
|
|
|
customType: "password",
|
|
|
|
title: "Password",
|
|
|
|
},
|
|
|
|
accessLevelId: {
|
|
|
|
type: "string",
|
|
|
|
title: "Access Level",
|
2020-11-25 16:11:33 +01:00
|
|
|
enum: accessLevels.BUILTIN_LEVEL_ID_ARRAY,
|
2020-11-13 16:35:20 +01:00
|
|
|
pretty: accessLevels.BUILTIN_LEVEL_NAME_ARRAY,
|
2020-09-16 15:00:04 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
required: ["username", "password", "accessLevelId"],
|
|
|
|
},
|
|
|
|
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"],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
module.exports.run = async function({ inputs, appId, apiKey }) {
|
2020-09-16 15:00:04 +02:00
|
|
|
const { username, password, accessLevelId } = inputs
|
|
|
|
const ctx = {
|
|
|
|
user: {
|
2020-10-29 11:28:27 +01:00
|
|
|
appId: appId,
|
2020-09-16 15:00:04 +02:00
|
|
|
},
|
|
|
|
request: {
|
|
|
|
body: { username, password, accessLevelId },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2020-10-28 21:35:06 +01:00
|
|
|
if (env.CLOUD) {
|
2020-10-08 18:34:41 +02:00
|
|
|
await usage.update(apiKey, usage.Properties.USER, 1)
|
|
|
|
}
|
2020-09-16 15:00:04 +02:00
|
|
|
await userController.create(ctx)
|
|
|
|
return {
|
|
|
|
response: ctx.body,
|
2020-09-21 16:05:24 +02:00
|
|
|
// internal property not returned through the API
|
|
|
|
id: ctx.userId,
|
2020-09-16 15:00:04 +02:00
|
|
|
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
|
|
|
}
|