/
AccountEntity.ts
executable file
·391 lines (377 loc) · 13.8 KB
/
AccountEntity.ts
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright 2020 Vircadia Contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict'
import { Entity } from '@Entities/Entity';
import { AuthToken } from '@Entities/AuthToken';
import { Accounts } from '@Entities/Accounts';
import { AccountRoles } from '@Entities/AccountRoles';
import { AccountAvailability } from '@Entities/AccountAvailability';
import { isStringValidator, isBooleanValidator, isPathValidator, isNumberValidator, isSArraySet, isDateValidator } from '@Route-Tools/GetterSetter';
import { simpleGetter, simpleSetter, sArraySetter, dateStringGetter } from '@Route-Tools/GetterSetter';
import { FieldDefn, noGetter, noSetter, verifyAllSArraySetValues } from '@Route-Tools/GetterSetter';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/GetterSetter';
import { VKeyedCollection } from '@Tools/vTypes';
import { IsNullOrEmpty } from '@Tools/Misc';
import { Logger } from '@Tools/Logging';
// NOTE: this class cannot have functions in them as they are just JSON to and from the database
export class AccountEntity implements Entity {
public id: string;
public username: string;
public email: string;
public accountSettings: string; // JSON of client settings
public imagesHero: string;
public imagesThumbnail: string;
public imagesTiny: string;
public locationConnected: boolean;
public locationPath: string; // "/floatX,floatY,floatZ/floatX,floatY,floatZ,floatW"
public locationPlaceId: string; // uuid of place
public locationDomainId: string; // uuid of domain located in
public locationNetworkAddress: string;
public locationNetworkPort: number;
public locationNodeId: string; // sessionId
public availability: string[]; // contains 'none', 'friends', 'connections', 'all'
public connections: string[];
public friends: string[];
public locker: string; // JSON blob stored for user from server
// User authentication
public passwordHash: string;
public passwordSalt: string;
public sessionPublicKey: string; // PEM public key generated for this session
// Old stuff
public xmppPassword: string;
public discourseApiKey: string;
public walletId: string;
// Admin stuff
// ALWAYS USE functions in Roles class to manipulate this list of roles
public roles: string[]; // account roles (like 'admin')
public IPAddrOfCreator: string; // IP address that created this account
public whenCreated: Date; // date of account creation
public timeOfLastHeartbeat: Date; // when we last heard from this user
};
// Get the value of a domain field with the fieldname.
// Checks to make sure the getter has permission to get the values.
// Returns the value. Could be 'undefined' whether the requestor doesn't have permissions or that's
// the actual field value.
export async function getAccountField(pAuthToken: AuthToken, pAccount: AccountEntity,
pField: string, pRequestingAccount?: AccountEntity): Promise<any> {
return getEntityField(accountFields, pAuthToken, pAccount, pField, pRequestingAccount);
};
// Set a domain field with the fieldname and a value.
// Checks to make sure the setter has permission to set.
// Returns 'true' if the value was set and 'false' if the value could not be set.
export async function setAccountField(pAuthToken: AuthToken, // authorization for making this change
pAccount: AccountEntity, // the account being changed
pField: string, pVal: any, // field being changed and the new value
pRequestingAccount?: AccountEntity, // Account associated with pAuthToken, if known
pUpdates?: VKeyedCollection // where to record updates made (optional)
): Promise<boolean> {
return setEntityField(accountFields, pAuthToken, pAccount, pField, pVal, pRequestingAccount, pUpdates);
};
// Generate an 'update' block for the specified field or fields.
// This is a field/value collection that can be passed to the database routines.
// Note that this directly fetches the field value rather than using 'getter' since
// we want the actual value (whatever it is) to go into the database.
// If an existing VKeyedCollection is passed, it is added to an returned.
export function getAccountUpdateForField(pAccount: AccountEntity,
pField: string | string[], pExisting?: VKeyedCollection): VKeyedCollection {
return getEntityUpdateForField(accountFields, pAccount, pField, pExisting);
};
// Naming and access for the fields in a AccountEntity.
// Indexed by request_field_name.
export const accountFields: { [key: string]: FieldDefn } = {
'id': {
entity_field: 'id',
request_field_name: 'id',
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'username': {
entity_field: 'username',
request_field_name: 'username',
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: async (pField: FieldDefn, pEntity: Entity, pVal: any): Promise<boolean> => {
let valid: boolean = false;
if (typeof(pVal) === 'string') {
// Check username for latin alpha-numeric
valid = /^[A-Za-z][A-Za-z0-9+\-_\.]*$/.test(pVal);
};
// Make sure no other account is using this username
if (valid) {
const otherAccount = await Accounts.getAccountWithUsername(pVal);
valid = IsNullOrEmpty(otherAccount) || otherAccount.id === (pEntity as AccountEntity).id;
};
return valid;
},
setter: simpleSetter,
getter: simpleGetter
},
'email': {
entity_field: 'email',
request_field_name: 'email',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: async (pField: FieldDefn, pEntity: Entity, pVal: any): Promise<boolean> => {
let valid: boolean = false;
if (typeof(pVal) === 'string') {
// Check email for sanity
valid = /^[A-Za-z0-9+\-_\.]+@[A-Za-z0-9-\.]+$/.test(pVal);
};
// Make sure no other account is using this email address
if (valid) {
const otherAccount = await Accounts.getAccountWithEmail(pVal);
valid = IsNullOrEmpty(otherAccount) || otherAccount.id === (pEntity as AccountEntity).id;
};
return valid;
},
setter: (pField: FieldDefn, pEntity: Entity, pVal: any): any => {
// emails are stored in lower-case
(pEntity as AccountEntity).email = (pVal as string).toLowerCase();
},
getter: simpleGetter
},
'account_settings': {
entity_field: 'accountSettings',
request_field_name: 'account_settings',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'images_hero': {
entity_field: 'imagesHero',
request_field_name: 'images_hero',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'images_tiny': {
entity_field: 'imagesTiny',
request_field_name: 'images_tiny',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'images_thumbnail': {
entity_field: 'imagesThumbnail',
request_field_name: 'images_thumbnail',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'connected': {
entity_field: 'locationConnected',
request_field_name: 'connected',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isBooleanValidator,
setter: simpleSetter,
getter: simpleGetter
},
'path': {
entity_field: 'locationPath',
request_field_name: 'path',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isPathValidator,
setter: simpleSetter,
getter: simpleGetter
},
'place_id': {
entity_field: 'locationPlaceId',
request_field_name: 'place_id',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'domain_id': {
entity_field: 'locationDomainId',
request_field_name: 'domain_id',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'network_address': {
entity_field: 'locationNetworkAddress',
request_field_name: 'network_address',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'network_port': {
entity_field: 'locationNetworkPort',
request_field_name: 'network_port',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isNumberValidator,
setter: simpleSetter,
getter: simpleGetter
},
'node_id': {
entity_field: 'locationNodeId',
request_field_name: 'node_id',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'availability': {
entity_field: 'availability',
request_field_name: 'availability',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: async (pField: FieldDefn, pEntity: Entity, pVal: any): Promise<boolean> => {
return verifyAllSArraySetValues(pVal, AccountAvailability.KnownAvailability);
},
setter: sArraySetter,
getter: simpleGetter
},
'connections': {
entity_field: 'connections',
request_field_name: 'connections',
get_permissions: [ 'owner', 'admin', 'friend', 'connection' ],
set_permissions: [ 'owner', 'admin' ],
validate: isSArraySet,
setter: sArraySetter,
getter: simpleGetter
},
'friends': {
entity_field: 'friends',
request_field_name: 'friends',
get_permissions: [ 'owner', 'admin', 'friend' ],
set_permissions: [ 'owner', 'admin' ],
validate: isSArraySet,
setter: sArraySetter,
getter: simpleGetter
},
'locker': {
entity_field: 'locker',
request_field_name: 'locker',
get_permissions: [ 'owner', 'admin' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
// User authentication
'password': {
entity_field: 'password',
request_field_name: 'password',
get_permissions: [ 'none' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: (pField: FieldDefn, pEntity: Entity, pVal: any): void => {
Accounts.storePassword((pEntity as AccountEntity), pVal);
},
getter: noGetter,
// An update to the password means updates to hash and salt fields.
updater: (pField: FieldDefn, pEntity: Entity, pUpdates: VKeyedCollection): void => {
pUpdates.passwordHash = (pEntity as AccountEntity).passwordHash;
pUpdates.passwordSalt = (pEntity as AccountEntity).passwordSalt;
}
},
'public_key': {
entity_field: 'sessionPublicKey',
request_field_name: 'public_key',
get_permissions: [ 'all' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
// Old stuff
'xmpp_password': {
entity_field: 'xmppPassword',
request_field_name: 'xmpp_password',
get_permissions: [ 'owner' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'discourse_api_key': {
entity_field: 'discourseApiKey',
request_field_name: 'discourse_api_key',
get_permissions: [ 'owner' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'wallet_id': {
entity_field: 'walletId',
request_field_name: 'wallet_id',
get_permissions: [ 'owner' ],
set_permissions: [ 'owner', 'admin' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
// Admin stuff
'roles': {
entity_field: 'roles',
request_field_name: 'roles',
get_permissions: [ 'all' ],
set_permissions: [ 'admin' ],
validate: async (pField: FieldDefn, pEntity: Entity, pVal: any): Promise<boolean> => {
return verifyAllSArraySetValues(pVal, AccountRoles.KnownRole);
},
setter: sArraySetter,
getter: simpleGetter
},
'ip_addr_of_creator': {
entity_field: 'IPAddrOfCreator',
request_field_name: 'ip_addr_of_creator',
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isStringValidator,
setter: noSetter,
getter: simpleGetter
},
'when_account_created': {
entity_field: 'whenCreated',
request_field_name: 'when_account_created',
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: noSetter,
getter: dateStringGetter
},
'time_of_last_heartbeat': {
entity_field: 'timeOfLastHeartbeat',
request_field_name: 'time_of_last_heartbeat',
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: noSetter,
getter: dateStringGetter
},
};