This repository has been archived by the owner on Dec 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelevate.js
44 lines (37 loc) · 1.51 KB
/
elevate.js
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
const mongoose = require('mongoose');
const commandLineArgs = process.argv.slice(2);
if (commandLineArgs.length > 1) {
const port = process.env.MONGO_PORT || 27017;
const host = process.env.MONGO_HOST || 'localhost';
const database = process.env.MONGO_DATABASE || 'sensrnet';
const url = `mongodb://${host}:${port}/${database}`;
const connectOptions = {
useCreateIndex: true,
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
}
mongoose.connect(url, connectOptions).then();
const UserPermissionsSchema = new mongoose.Schema({
_id: { type: String, required: true },
legalEntityId: { type: String, required: true },
role: { type: Number, required: false },
});
const UserPermissionsModel = mongoose.model('User', UserPermissionsSchema);
const userId = commandLineArgs[0];
const legalEntityId = commandLineArgs[1];
const role = commandLineArgs[2];
console.log(`Elevating ${userId} to ${role}.`);
const filterKwargs = {_id: userId};
const userPermissions = {_id: userId, legalEntityId: legalEntityId, role: role};
UserPermissionsModel.updateOne(filterKwargs, userPermissions).then(() => {
console.log(`Successfully elevated ${userId} to ${role}.`);
process.exit();
}, () => {
console.log(`Failed to elevate ${userId} to ${role}.`);
process.exit();
});
} else {
console.log('Supply command line arguments (1) email (2) role.');
process.exit();
}