-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUnattendedMode.js
More file actions
110 lines (106 loc) · 3.74 KB
/
UnattendedMode.js
File metadata and controls
110 lines (106 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var unattendedMode = Symbol('unattendedMode');
var {RandomUtils} = require('@themost/common');
/**
* Execute a callable function with elevated privileges in unattended mode.
* @param {import('./types').DataContext} context
* @param {function(function(Error?): void)} callable
* @param {function(Error?): void} callback
* @returns {void}
*/
function executeInUnattendedMode(context, callable, callback) {
if (typeof callable !== 'function') {
return callback(new Error('Unattended mode requires a callable function'));
}
// if the context is already in unattended mode
if (context[unattendedMode]) {
try {
// execute callable function
void callable(function (err) {
return callback(err);
});
} catch (err) {
return callback(err);
}
} else {
var interactiveUser;
try {
const account = context.getConfiguration().getSourceAt('settings/auth/unattendedExecutionAccount');
if (account == null) {
return callback(new Error('The unattended execution account is not defined. The operation cannot be completed.'));
}
// enter unattended mode
context[unattendedMode] = true;
// get interactive user
if (context.user) {
interactiveUser = Object.assign({}, context.user);
// set interactive user
context.interactiveUser = interactiveUser;
}
if (account) {
context.user = {name: account, authenticationType: 'Basic'};
}
void callable(function (err) {
// restore user
if (interactiveUser) {
context.user = Object.assign({}, interactiveUser);
}
delete context.interactiveUser;
// exit unattended mode
delete context[unattendedMode];
return callback(err);
});
} catch (err) {
// restore user
if (interactiveUser) {
context.user = Object.assign({}, interactiveUser);
}
delete context.interactiveUser;
// exit unattended mode
delete context[unattendedMode];
return callback(err);
}
}
}
/**
* Execute a callable function with elevated privileges in unattended mode.
* @param {import('./types').DataContext} context
* @param {function(): Promise<void>} callable
* @returns {Promise<void>}
*/
function executeInUnattendedModeAsync(context, callable) {
return new Promise((resolve, reject) => {
void executeInUnattendedMode(context, function (cb) {
return callable().then(function () {
return cb();
}).catch(function (err) {
return cb(err);
});
}, function (err) {
if (err) {
return reject(err);
}
return resolve();
});
});
}
/**
* Enables unattended mode
* @param {{getConfiguration(): import('@themost/common').ConfigurationBase}} app
* @param {string=} executionAccount
*/
function enableUnattendedExecution(app, executionAccount) {
app.getConfiguration().setSourceAt('settings/auth/unattendedExecutionAccount', executionAccount || RandomUtils.randomChars(14));
}
/**
* Disables unattended mode
* @param {{getConfiguration(): import('@themost/common').ConfigurationBase}} app
*/
function disableUnattendedExecution(app) {
app.getConfiguration().setSourceAt('settings/auth/unattendedExecutionAccount', null);
}
module.exports = {
executeInUnattendedMode,
executeInUnattendedModeAsync,
enableUnattendedExecution,
disableUnattendedExecution
}