-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathglobal.js
214 lines (188 loc) · 6.15 KB
/
global.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"use strict";
var hashpwd = require("./hashpwd.js");
class globalVars {
constructor(app) {
this.userArray = [];
//this.Homey = require("homey");
this.Homey = app.homey;
this.logmodule = app.logmodule;
this.OnInit();
}
OnInit() {
this.initVars();
}
/*
initVars() is called as soon as the app is loaded and it will
initialise the unload event that will persist the userArray and fenceArray
It also loads the userArray and fenceArray from file and puts them in the
array.
*/
initVars() {
const ref = this;
this.logmodule.writelog('debug', "initVars called");
require('fs').readFile('/userdata/broker-users.json', 'utf8', function (err, data) {
if (err) {
ref.logmodule.writelog('error', "Retreiving broker data failed: "+ err);
} else {
try {
ref.userArray = JSON.parse(data);
} catch (err) {
ref.logmodule.writelog('error', "Parsing broker data failed: "+ err);
ref.userArray = [];
}
}
});
}
/*
saveUserData() saves the user data into a JSON file on the filesystem
*/
saveUserData() {
const ref = this;
this.logmodule.writelog('info', "saveUserData called");
require('fs').writeFile("/userdata/broker-users.json", JSON.stringify(this.userArray), function (err) {
if (err) {
ref.logmodule.writelog('error', "Persisting brokerArray failed: "+ err);
}
});
}
/*
deletePersistancyFiles() deletes the saved arrays from the filesystem. This
can be used when the persistency files were borked.
*/
deletePresistancyFiles() {
const ref = this;
var returnValue = false;
try {
require('fs').unlinkSync('/userdata/broker-users.json');
} catch(err) {
ref.logmodule.writelog('error', err);
returnValue = true;
}
return returnValue;
}
/*
Return the userArray
*/
getUserArray() {
this.logmodule.writelog('debug', "getUserArray called");
return this.userArray;
}
/*
Return the data for the given user
*/
getUser(userName) {
for (var i=0; i < this.userArray.length; i++) {
if (this.userArray[i].userName === userName) {
return this.userArray[i];
}
}
// User has not been found, so return null
return null
}
/*
Update the user, or if the user does not exist, add the user
to the user array
*/
setUser(userData, persistUser) {
const ref = this;
try {
var entryArray = ref.getUser(userData.userName);
if (entryArray !== null) {
entryArray = userData;
} else {
// User has not been found, so assume this is a new user
ref.userArray.push(userData);
if (persistUser == true) {
ref.saveUserData();
}
ref.Homey.notifications.createNotification({
excerpt: ref.Homey.__("notifications.user_added", {"name": userData.userName})
}, function( err, notification ) {
if( err ) return console.error( err );
console.log( 'Notification added' );
});
}
} catch(err) {
ref.logmodule.writelog('error', "setUser: " +err);
}
}
/*
Create a password hash
*/
hashedPassword(userPassword) {
var ref = this;
var hashedPassword = hashpwd.hashPassword(userPassword);
this.logmodule.writelog('debug', "hashedPassword: "+hashedPassword);
return hashedPassword;
}
createEmptyUser(userName, userPassword) {
try {
var newUser = {};
newUser.userName = userName;
if (this.Homey.settings.get('disable_hashing') == false ) {
newUser.userPassword = this.hashedPassword(userPassword);
} else {
newUser.userPassword = userPassword;
}
return newUser;
} catch(err) {
this.logmodule.writelog('error', "createEmptyUser: " +err);
return null;
}
}
/*
addNewUser is called from the settings page when a new user is added
or when the token needs to be refreshed.
*/
addNewUser(body) {
const ref = this;
try {
ref.logmodule.writelog('debug', "New user called: "+ body.userName);
if (body.userName !== null && body.userName !== undefined && body.userName !== "" ) {
var currentUser = ref.getUser(body.userName);
if (currentUser == null) {
var newUser = ref.createEmptyUser(body.userName, body.userPassword);
ref.setUser(newUser, true);
ref.logmodule.writelog('info', "New user added: "+ newUser.userName);
return true;
} else {
if (this.homey.settings.get('disable_hashing') == false ) {
currentUser.userPassword = this.hashedPassword(body.userPassword);
} else {
currentUser.userPassword = body.userPassword;
}
ref.logmodule.writelog('debug', "userPassword: " + currentUser.userPassword);
ref.saveUserData();
}
}
return false;
} catch(err) {
ref.logmodule.writelog('error', "addNewUser: " +err);
return err;
}
}
/*
deleteUser is called from the settings page when a user is deleted
by pressing the - button
*/
deleteUser(body) {
const ref = this;
try {
ref.logmodule.writelog('debug', "Delete user called: "+ body.userName);
var result = false;
for (var i=0; i < ref.userArray.length; i++) {
if (ref.userArray[i].userName === body.userName) {
var deletedUser = ref.userArray.splice(i, 1);
ref.logmodule.writelog('info', "Deleted user: " + deletedUser.userName);
result = true;
}
}
ref.saveUserData();
return result;
} catch(err) {
logmodule.writelog('error', "deleteUser: " +err);
return err;
}
}
}
module.exports = globalVars;