-
Notifications
You must be signed in to change notification settings - Fork 5
/
users.js
189 lines (149 loc) · 5.01 KB
/
users.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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const fs = require('fs');
const bcrypt = require('bcrypt');
const userspath = __dirname + '/server-data/users.json';
let userdb = undefined;
function load() {
fs.readFile(userspath, (err, data) => {
if (err) {
console.error(err);
return;
}
userdb = JSON.parse(data);
// console.log(JSON.parse(data));
console.log("Users Loaded")
});
};
load();
async function save() {
if (userdb == undefined) {
console.log("Userdb undefined");
return;
}
let usersJson = JSON.stringify(userdb, null, 2);
fs.writeFile(userspath, usersJson, err => {
if (err) {
console.error(err);
// return false;
} else {
console.log("Saved Userdb to file");
}
});
};
exports.save = save;
/**
* Updates the user in the database and saves it to file. IF the password is changes
* it will compute the new password hash and updated the db. You cannot change the username or user id
*
* @param {number} id The id of the usere you want to update
* @param {user} newuser The updated user object
*/
exports.update = function (id, newuser) {
let i = userdb.users.findIndex(user => user.id == id);
if (userdb.users[i].username != newuser.username) throw "You Cant Change Userenames";
if (userdb.users[i].id != newuser.id) throw "You Cant Change User Id";
if (userdb.users[i].password != newuser.password) throw "User changePasswor(id, oldpw, newpw) to Change password";
userdb.users[i] = newuser;
console.log("Updated" + userdb.users[i].username);
save();
};
function changePassword(id, oldpw, newpw) {
let i = userdb.users.findIndex(user => user.id == id);
let same = bcrypt.compareSync(oldpw, userdb.users[i].password);
console.log(same);
if (!same) return "Current Password Incorrect";
if (oldpw == newpw) return true;
let hashedPassword = bcrypt.hashSync(newpw, 10);
userdb.users[i].password = hashedPassword;
// console.log(hashedPassword);
// console.log(userdb.users[i].password);
save();
return true
}
exports.changePassword = changePassword;
function set_password(id, newpw) {
let i = userdb.users.findIndex(user => user.id == id);
let hashedPassword = bcrypt.hashSync(newpw, 10);
userdb.users[i].password = hashedPassword;
// console.log(hashedPassword);
// console.log(userdb.users[i].password);
save();
return true
}
function addUser(req) {
if (userdb.users.some(u => u.username == req.body.username)) {
throw "Username Already Exists";
}
let hashedPassword = bcrypt.hashSync(req.body.password, 10);
console.log(hashedPassword);
let newUser = {
id: userdb.nextId++, //get next and increments
username: req.body.username,
displayName: req.body.username,
email: req.body.email,
role: 'guest',
password: hashedPassword
};
console.log('Adding new User:', newUser);
userdb.users.push(newUser);
save();
return newUser
}
exports.addUser = addUser;
exports.findByUsername = function (username, cb) {
let u = userdb.users.find(user => user.username == username);
// console.log(userdb.users);
// console.log(u);
if (u == undefined) u = null;
cb(null, u);
};
exports.get_user_by_id = function (id) {
return userdb.users.find(user => user.id == id);
};
exports.get_user_by_username = function (username) {
return userdb.users.find(user => user.username == username);
};
exports.findById = function (id, cb) {
let u = userdb.users.find(user => user.id == id);
if (u == undefined) u = null;
cb(null, u);
};
exports.addRoutes = function (app, auth) {
app.get('/api/get_users', auth.apiAuthenticatedRole('user'), (req, res) => {
res.json(userdb);
});
app.post('/api/edit_user', auth.apiAuthenticatedRole('admin'), (req, res) => {
// console.log(req.file.path);
// console.log(req.file.encoding);
// console.log(req.file.mimetype);
console.log(req.body);
let u;
if (req.body.id == -1) {
u = addUser(req);
u.role = req.body.role || 'guest';
u.displayName = req.body.displayName || req.body.username;
r = {
users: userdb,
updated_user: u
};
res.send(JSON.stringify(r));
return;
} else {
u = get_user_by_id(req.body.id);
}
u.username = req.body.username || u.username;
u.displayName = req.body.displayName || u.displayName;
u.email = req.body.password || u.password;
u.role = req.body.role || u.role;
update(u.id, u);
if (req.body.password) {
set_password(u.id, req.body.password)
}
// if(typeof req.body.disabled != "undefined") {
//if the disabled flag is not sent to the server the drone will be not disabled
r = {
users: userdb,
updated_user: u
};
res.send(JSON.stringify(r));
});
}