merge userdb.js into users.js

This commit is contained in:
Girish Ramakrishnan
2021-07-15 09:50:11 -07:00
parent 2840bba4bf
commit a1c61facdc
27 changed files with 1021 additions and 1456 deletions
+18 -21
View File
@@ -28,6 +28,7 @@ const assert = require('assert'),
users = require('./users.js'),
tld = require('tldjs'),
tokens = require('./tokens.js'),
util = require('util'),
_ = require('underscore');
const NOOP_CALLBACK = function (error) { if (error) debug(error); };
@@ -82,7 +83,7 @@ function setup(dnsConfig, sysinfoConfig, auditSource, callback) {
callback(error);
}
users.isActivated(function (error, activated) {
util.callbackify(users.isActivated)(function (error, activated) {
if (error) return done(error);
if (activated) return done(new BoxError(BoxError.CONFLICT, 'Already activated', { activate: true }));
@@ -127,36 +128,32 @@ function setup(dnsConfig, sysinfoConfig, auditSource, callback) {
});
}
function activate(username, password, email, displayName, ip, auditSource, callback) {
async function activate(username, password, email, displayName, ip, auditSource) {
assert.strictEqual(typeof username, 'string');
assert.strictEqual(typeof password, 'string');
assert.strictEqual(typeof email, 'string');
assert.strictEqual(typeof displayName, 'string');
assert.strictEqual(typeof ip, 'string');
assert.strictEqual(typeof auditSource, 'object');
assert.strictEqual(typeof callback, 'function');
debug('activating user:%s email:%s', username, email);
debug(`activate: user: ${username} email:${email}`);
users.createOwner(username, password, email, displayName, auditSource, async function (error, userObject) {
if (error && error.reason === BoxError.ALREADY_EXISTS) return callback(new BoxError(BoxError.CONFLICT, 'Already activated'));
if (error) return callback(error);
const [error, userObject] = await safe(users.createOwner(email, username, password, displayName, auditSource));
if (error && error.reason === BoxError.ALREADY_EXISTS) throw new BoxError(BoxError.CONFLICT, 'Already activated');
if (error) throw error;
const token = { clientId: tokens.ID_WEBADMIN, identifier: userObject.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS };
let result;
[error, result] = await safe(tokens.add(token));
if (error) return callback(error);
const token = { clientId: tokens.ID_WEBADMIN, identifier: userObject.id, expires: Date.now() + constants.DEFAULT_TOKEN_EXPIRATION_MSECS };
const result = await tokens.add(token);
eventlog.add(eventlog.ACTION_ACTIVATE, auditSource, { });
eventlog.add(eventlog.ACTION_ACTIVATE, auditSource, {});
callback(null, {
userId: userObject.id,
token: result.accessToken,
expires: result.expires
});
setImmediate(cloudron.onActivated.bind(null, {}, NOOP_CALLBACK));
setImmediate(cloudron.onActivated.bind(null, {}, NOOP_CALLBACK)); // hack for now to not block the above http response
});
return {
userId: userObject.id,
token: result.accessToken,
expires: result.expires
};
}
function restore(backupConfig, backupId, version, sysinfoConfig, options, auditSource, callback) {
@@ -181,7 +178,7 @@ function restore(backupConfig, backupId, version, sysinfoConfig, options, auditS
callback(error);
}
users.isActivated(async function (error, activated) {
util.callbackify(users.isActivated)(async function (error, activated) {
if (error) return done(error);
if (activated) return done(new BoxError(BoxError.CONFLICT, 'Already activated. Restore with a fresh Cloudron installation.'));
@@ -248,7 +245,7 @@ function restore(backupConfig, backupId, version, sysinfoConfig, options, auditS
function getStatus(callback) {
assert.strictEqual(typeof callback, 'function');
users.isActivated(function (error, activated) {
util.callbackify(users.isActivated)(function (error, activated) {
if (error) return callback(error);
settings.getAll(function (error, allSettings) {