-
Notifications
You must be signed in to change notification settings - Fork 0
/
entry.js
251 lines (230 loc) · 9.67 KB
/
entry.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// NoService/services/youservice/entry.js
// Description:
// "youservice/entry.js" description.
// Copyright 2018 NOOXY. All Rights Reserved.
'use strict';
let NoUser = require('./NoUser');
let fs = require('fs');
const MAX_USERNAME = 10;
function Service(Me, NoService) {
// Your service entry point
// Get the service socket of your service
let ss = NoService.Service.ServiceSocket;
// BEWARE! To prevent callback error crash the system.
// If you call an callback function which is not API provided. Such as setTimeout(callback, timeout).
// You need to wrap the callback funciton by NoService.SafeCallback.
// E.g. setTimeout(NoService.SafeCallback(callback), timeout)
let safec = NoService.SafeCallback;
// Your settings in manifest file.
let settings = Me.Settings;
let country_list = Me.Settings.country_list;
let nouser = new NoUser();
nouser.importUtils(NoService.Utils);
nouser.importCountries(country_list);
// JSONfunction is a function that can be defined, which others entities can call.
// It is a NOOXY Service Framework Standard
ss.def('createUser', (json, entityID, returnJSON)=>{
// Code here for JSONfunciton
// Return Value for JSONfunction call. Otherwise remote will not recieve funciton return value.
let json_be_returned = {
s: 'Succeessfully created.'
}
// First parameter for error, next is JSON to be returned.
if (json.pw != json.cp) {
json_be_returned.e = true;
json_be_returned.s = 'Error: password not match.';
returnJSON(false, json_be_returned);
}
else {
NoService.Authenticity.createUser(json.un, json.dn, json.pw, 1, json.dt, json.fn, json.ln, (err)=>{
if(err) {
json_be_returned.e = true;
json_be_returned.s = err.toString();
}
returnJSON(false, json_be_returned);
});
}
});
ss.def('returnUserMeta', (json, entityID, returnJSON)=>{
NoService.Service.Entity.getEntityOwner(entityID, (err, username)=> {
NoService.Authorization.Authby.Token(entityID, (err, valid)=>{
if(valid) {
NoService.Authenticity.getUserMetaByUsername(username, (err, meta1)=>{
NoService.Authenticity.getUserIdByUsername(username, (err, userid) => {
nouser.getUserMeta(userid, (err, meta2)=>{
let meta = Object.assign({}, meta1, meta2);
delete meta['pwdhash'];
delete meta['token'];
delete meta['tokenexpire'];
delete meta['privilege'];
returnJSON(false, meta);
})
});
})
}
else {
returnJSON(false, {});
}
});
})
});
ss.def('getUserMetaByUserId', (json, entityID, returnJSON)=>{
NoService.Authorization.Authby.Token(entityID, (err, valid)=>{
if(valid) {
NoService.Authenticity.getUserMetaByUserId(json.i, (err, meta1)=>{
nouser.getUserMeta(json.i, (err, meta2)=>{
let meta = Object.assign({}, meta1, meta2);
delete meta['pwdhash'];
delete meta['token'];
delete meta['tokenexpire'];
delete meta['privilege'];
delete meta['detail'];
delete meta['createdate'];
delete meta['modifydate'];
returnJSON(false, meta);
})
});
}
else {
returnJSON(false, {});
}
});
});
ss.def('searchUsersByUsername', (json, entityID, returnJSON)=>{
NoService.Authorization.Authby.Token(entityID, (err, valid)=>{
if(valid) {
if(json.n&&json.n.length>0) {
NoService.Authenticity.searchUsersByUsernameNRows(json.n+'%', MAX_USERNAME,(err, rows)=>{
let list = [];
for(let i in rows) {
let meta = rows[i];
delete meta['pwdhash'];
delete meta['token'];
delete meta['tokenexpire'];
delete meta['privilege'];
delete meta['detail'];
delete meta['createdate'];
delete meta['modifydate'];
list.push(meta);
}
returnJSON(false, {e: err, r:list});
});
}
else {
returnJSON(false, {e: err, r:[]});
}
}
else {
returnJSON(false, {});
}
});
});
ss.on('close', (entityID, callback) => {callback(false)});
// Your service entry point
this.start = ()=> {
nouser.importModel(NoService.Database.Model, (err)=> {
NoService.Daemon.getSettings((err, DaemonSettings)=>{
// Access another service on this daemon
NoService.Service.ActivitySocket.createDefaultAdminDeamonSocket('NoMailer', (err, NoMailersocket)=> {
// JSONfunction is a function that can be defined, which others entities can call.
// It is a NOOXY Service Framework Standard
ss.def('updateUser', (json, entityID, returnJSON)=>{
NoService.Service.Entity.getEntityOwner(entityID, (err, username)=> {
let json_be_returned = {
s: 'Succeessfully updated.'
}
NoService.Authorization.Authby.Password(entityID, (err, valid)=>{
if(valid) {
for(let i in json) {
if(json[i] == '') {
json[i] = null;
}
}
// First parameter for error, next is JSON to be returned.
if (json.pw != json.cp) {
json_be_returned.e = true;
json_be_returned.s = 'Error: password not match.';
returnJSON(false, json_be_returned);
}
else {
NoService.Authenticity.updatePasswordByUsername(username, json.pw, (err)=>{
if(err&&json.pw!=null) {
json_be_returned.e = true;
json_be_returned.s = err.toString();
returnJSON(false, json_be_returned);
}
else {
if(json.firstname != null && json.lastname!= null) {
NoService.Authenticity.updateNameByUsername(username, json.firstname, json.lastname, (err)=>{
if(err) {
json_be_returned.e = true;
json_be_returned.s = err.toString();
returnJSON(false, json_be_returned);
}
else {
NoService.Authenticity.getUserIdByUsername(username, (err, userid) => {
nouser.updateUser(userid, json, (err)=>{
if(err) {
json_be_returned.e = true;
json_be_returned.s = err.toString();
}
else {
NoService.Service.Entity.getEntityMetaData(entityID, (err, emeta)=>{
// accessing other service
NoMailersocket.call('sendMail', {
to: json.email,
subject: DaemonSettings.daemon_display_name+" account security.",
text: 'Hi! '+json.firstname+', your account has been modify.\n\n If you have no idea what happened. Please change your password!\n\nTime:'+(new Date())+'\nEntity detail:\n'+JSON.stringify(emeta, null, 2)+'\n\n'+DaemonSettings.copyright
}, (error, info)=> {
if(error) {
console.log(error);
};
});
});
}
returnJSON(false, json_be_returned);
});
});
}
});
}
else {
json_be_returned.e = true;
json_be_returned.s = 'Error: Please enter your name.';
returnJSON(false, json_be_returned);
}
}
});
}
}
else {
NoService.Service.Entity.getEntityMetaData(entityID, (err, emeta)=>{
// accessing other service
NoMailersocket.call('sendMail', {
to: json.email,
subject: DaemonSettings.daemon_display_name+" account security.",
text: 'Hi! '+json.firstname+', your account is being modifed by someone.\n\n If you have no idea what happened. Please change your password!\n\nTime:'+(new Date())+'\nEntity detail:\n'+JSON.stringify(emeta, null, 2)+'\n\n'+DaemonSettings.copyright
}, (error, info)=> {
if(error) {
console.log(error);
};
});
});
json_be_returned.e = true;
json_be_returned.s = 'Error: Auth failed.';
returnJSON(false, json_be_returned);
}
});
});
});
});
});
});
}
// If the daemon stop, your service recieve close signal here.
this.close = ()=> {
nouser.close();
}
}
// Export your work for system here.
module.exports = Service;