1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00
node-red/packages/node_modules/@node-red/runtime/lib/api/settings.js

288 lines
11 KiB
JavaScript
Raw Normal View History

2018-04-15 12:18:10 +02:00
/**
* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
/**
* @mixin @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
var util = require("util");
var runtime;
function extend(target, source) {
var keys = Object.keys(source);
var i = keys.length;
while(i--) {
var value = source[keys[i]]
var type = typeof value;
if (type === 'string' || type === 'number' || type === 'boolean' || Array.isArray(value)) {
target[keys[i]] = value;
} else if (value === null) {
if (target.hasOwnProperty(keys[i])) {
delete target[keys[i]];
}
} else {
// Object
if (target.hasOwnProperty(keys[i])) {
// Target property exists. Need to handle the case
// where the existing property is a string/number/boolean
// and is being replaced wholesale.
var targetType = typeof target[keys[i]];
if (targetType === 'string' || targetType === 'number' || targetType === 'boolean') {
target[keys[i]] = value;
} else {
target[keys[i]] = extend(target[keys[i]],value);
}
2018-04-15 12:18:10 +02:00
} else {
target[keys[i]] = value;
}
}
}
return target;
}
2018-04-20 21:50:20 +02:00
function getSSHKeyUsername(userObj) {
2018-04-15 12:18:10 +02:00
var username = '__default';
if ( userObj && userObj.username ) {
username = userObj.username;
2018-04-15 12:18:10 +02:00
}
return username;
}
var api = module.exports = {
init: function(_runtime) {
runtime = _runtime;
},
/**
* Gets the runtime settings object
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise<Object>} - the runtime settings
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
getRuntimeSettings: async function(opts) {
var safeSettings = {
httpNodeRoot: runtime.settings.httpNodeRoot||"/",
version: runtime.settings.version
}
if (opts.user) {
safeSettings.user = {}
var props = ["anonymous","username","image","permissions"];
props.forEach(prop => {
if (opts.user.hasOwnProperty(prop)) {
safeSettings.user[prop] = opts.user[prop];
2018-04-20 21:50:20 +02:00
}
})
}
2018-07-30 00:47:19 +02:00
if (!runtime.settings.disableEditor) {
safeSettings.context = runtime.nodes.listContextStores();
2021-04-29 17:53:59 +02:00
if (runtime.settings.editorTheme && runtime.settings.editorTheme.codeEditor) {
safeSettings.codeEditor = runtime.settings.editorTheme.codeEditor || {};
safeSettings.codeEditor.lib = safeSettings.codeEditor.lib || "ace";
safeSettings.codeEditor.options = safeSettings.codeEditor.options || {};
}
2020-12-17 15:35:01 +01:00
safeSettings.libraries = runtime.library.getLibraries();
if (util.isArray(runtime.settings.paletteCategories)) {
safeSettings.paletteCategories = runtime.settings.paletteCategories;
}
2018-04-15 12:18:10 +02:00
if (runtime.settings.flowFilePretty) {
safeSettings.flowFilePretty = runtime.settings.flowFilePretty;
}
2018-04-15 12:18:10 +02:00
if (runtime.settings.editorTheme && runtime.settings.editorTheme.palette) {
if (runtime.settings.editorTheme.palette.upload === false || runtime.settings.editorTheme.palette.editable === false) {
safeSettings.externalModules = {palette: { } }
}
if (runtime.settings.editorTheme.palette.upload === false) {
safeSettings.externalModules.palette.allowUpload = false;
}
if (runtime.settings.editorTheme.palette.editable === false) {
safeSettings.externalModules.palette.allowInstall = false;
safeSettings.externalModules.palette.allowUpload = false;
}
}
if (runtime.settings.externalModules) {
safeSettings.externalModules = extend(safeSettings.externalModules||{},runtime.settings.externalModules);
}
if (!runtime.nodes.installerEnabled()) {
safeSettings.externalModules = safeSettings.externalModules || {};
safeSettings.externalModules.palette = safeSettings.externalModules.palette || {};
safeSettings.externalModules.palette.allowInstall = false;
safeSettings.externalModules.palette.allowUpload = false;
}
if (runtime.storage.projects) {
var activeProject = runtime.storage.projects.getActiveProject();
if (activeProject) {
safeSettings.project = activeProject;
} else if (runtime.storage.projects.flowFileExists()) {
safeSettings.files = {
flow: runtime.storage.projects.getFlowFilename(),
credentials: runtime.storage.projects.getCredentialsFilename()
2018-04-20 21:50:20 +02:00
}
}
safeSettings.git = {
globalUser: runtime.storage.projects.getGlobalGitUser()
}
}
2018-04-15 12:18:10 +02:00
safeSettings.flowEncryptionType = runtime.nodes.getCredentialKeyType();
runtime.settings.exportNodeSettings(safeSettings);
runtime.plugins.exportPluginSettings(safeSettings);
}
2018-04-15 12:18:10 +02:00
return safeSettings;
2018-04-15 12:18:10 +02:00
},
/**
* Gets an individual user's settings object
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise<Object>} - the user settings
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
getUserSettings: async function(opts) {
2018-04-15 12:18:10 +02:00
var username;
if (!opts.user || opts.user.anonymous) {
username = '_';
} else {
username = opts.user.username;
}
return runtime.settings.getUserSettings(username)||{};
2018-04-15 12:18:10 +02:00
},
/**
* Updates an individual user's settings object.
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.settings - the updates to the user settings
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise<Object>} - the user settings
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
updateUserSettings: async function(opts) {
2018-04-15 12:18:10 +02:00
var username;
if (!opts.user || opts.user.anonymous) {
username = '_';
} else {
username = opts.user.username;
}
var currentSettings = runtime.settings.getUserSettings(username)||{};
currentSettings = extend(currentSettings, opts.settings);
try {
return runtime.settings.setUserSettings(username, currentSettings).then(function() {
runtime.log.audit({event: "settings.update",username:username}, opts.req);
return;
}).catch(function(err) {
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
2018-04-15 12:18:10 +02:00
err.status = 400;
throw err;
});
} catch(err) {
runtime.log.warn(runtime.log._("settings.user-not-available",{message:runtime.log._("settings.not-available")}));
runtime.log.audit({event: "settings.update",username:username,error:err.code||"unexpected_error",message:err.toString()}, opts.req);
err.status = 400;
throw err;
}
2018-04-15 12:18:10 +02:00
},
/**
* Gets a list of a user's ssh keys
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise<Object>} - the user's ssh keys
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
getUserKeys: async function(opts) {
var username = getSSHKeyUsername(opts.user);
return runtime.storage.projects.ssh.listSSHKeys(username).catch(function(err) {
err.status = 400;
throw err;
return reject(err);
2018-04-15 12:18:10 +02:00
});
},
/**
* Gets a user's ssh public key
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.id - the id of the key to return
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise<String>} - the user's ssh public key
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
getUserKey: async function(opts) {
var username = getSSHKeyUsername(opts.user);
// console.log('username:', username);
return runtime.storage.projects.ssh.getSSHKey(username, opts.id).then(function(data) {
if (data) {
return data;
} else {
var err = new Error("Key not found");
err.code = "not_found";
err.status = 404;
throw err;
}
}).catch(function(err) {
if (!err.status) {
2018-04-15 12:18:10 +02:00
err.status = 400;
}
throw err;
2018-04-15 12:18:10 +02:00
});
},
/**
* Generates a new ssh key pair
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.name - the id of the key to return
* @param {User} opts.password - (optional) the password for the key pair
* @param {User} opts.comment - (option) a comment to associate with the key pair
* @param {User} opts.size - (optional) the size of the key. Default: 2048
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise<String>} - the id of the generated key
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
generateUserKey: async function(opts) {
var username = getSSHKeyUsername(opts.user);
return runtime.storage.projects.ssh.generateSSHKey(username, opts).catch(function(err) {
err.status = 400;
throw err;
2018-04-15 12:18:10 +02:00
});
},
/**
* Deletes a user's ssh key pair
* @param {Object} opts
* @param {User} opts.user - the user calling the api
* @param {User} opts.id - the id of the key to delete
* @param {Object} opts.req - the request to log (optional)
2018-04-15 12:18:10 +02:00
* @return {Promise} - resolves when deleted
* @memberof @node-red/runtime_settings
2018-04-15 12:18:10 +02:00
*/
removeUserKey: async function(opts) {
var username = getSSHKeyUsername(opts.user);
return runtime.storage.projects.ssh.deleteSSHKey(username, opts.id).catch(function(err) {
err.status = 400;
throw err;
2018-04-15 12:18:10 +02:00
});
}
}