/
accountId.ts
executable file
·132 lines (120 loc) · 5.4 KB
/
accountId.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
// 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 { Router, RequestHandler, Request, Response, NextFunction } from 'express';
import { setupMetaverseAPI, finishMetaverseAPI } from '@Route-Tools/middleware';
import { accountFromAuthToken, accountFromParams } from '@Route-Tools/middleware';
import { Perm } from '@Route-Tools/Perm';
import { checkAccessToEntity } from '@Route-Tools/Permissions';
import { buildAccountInfo } from '@Route-Tools/Util';
import { Accounts } from '@Entities/Accounts';
import { VKeyedCollection } from '@Tools/vTypes';
import { Logger } from '@Tools/Logging';
// metaverseServerApp.use(express.urlencoded({ extended: false }));
const procGetAccountId: RequestHandler = async (req: Request, resp: Response, next: NextFunction) => {
if (req.vAuthAccount) {
if (req.vAccount) {
if (checkAccessToEntity(req.vAuthToken, req.vAccount, [ Perm.OWNER, Perm.ADMIN ])) {
req.vRestResp.Data = {
account: await buildAccountInfo(req, req.vAccount)
};
}
else {
req.vRestResp.respondFailure('Unauthorized');
};
}
else {
req.vRestResp.respondFailure('Target account not found');
};
}
else {
req.vRestResp.respondFailure(req.vAccountError ?? 'Not logged in');
};
next();
};
// Set changable values on an account.
// The setter must be either an admin account or the account itself
const procPostAccountId: RequestHandler = async (req: Request, resp: Response, next: NextFunction) => {
if (req.vRestResp) {
if (req.vAuthAccount) {
if (req.vAccount) {
const valuesToSet = req.body.accounts;
const updates: VKeyedCollection = {};
for (const field of [ 'email', 'public_key' ]) {
if (valuesToSet.hasOwnProperty(field)) {
await Accounts.setField(req.vAuthToken, req.vAccount, field, valuesToSet.field, req.vAuthAccount, updates);
};
};
if (valuesToSet.hasOwnProperty('images')) {
if (valuesToSet.images.hero) {
await Accounts.setField(req.vAuthToken, req.vAccount, 'images_hero', valuesToSet.images.hero, req.vAuthAccount, updates);
};
if (valuesToSet.images.tiny) {
await Accounts.setField(req.vAuthToken, req.vAccount, 'images_tiny', valuesToSet.images.tiny, req.vAuthAccount, updates);
};
if (valuesToSet.images.thumbnail) {
await Accounts.setField(req.vAuthToken, req.vAccount, 'images_thumbnail', valuesToSet.images.thumbnail, req.vAuthAccount, updates);
};
};
await Accounts.updateEntityFields(req.vAuthAccount, updates);
}
else {
req.vRestResp.respondFailure(req.vAccountError ?? 'Account not specified');
};
}
else {
req.vRestResp.respondFailure(req.vAccountError ?? 'Not logged in');
};
};
next();
};
// Delete an account.
// The setter must be an admin account.
const procDeleteAccountId: RequestHandler = async (req: Request, resp: Response, next: NextFunction) => {
if (req.vAuthAccount) {
if (req.vAccount) {
if (Accounts.isAdmin(req.vAuthAccount)) {
await Accounts.removeAccount(req.vAccount);
await Accounts.removeAccountContext(req.vAccount);
}
else {
req.vRestResp.respondFailure('Not an administrator');
};
}
else {
req.vRestResp.respondFailure('Target account does not exist');
};
}
else {
req.vRestResp.respondFailure(req.vAccountError ?? 'Not logged in');
};
next();
};
export const name = '/api/v1/account/:accountId';
export const router = Router();
router.get( '/api/v1/account/:accountId', [ setupMetaverseAPI, // req.vRestResp, req.vAuthToken
accountFromAuthToken, // req.vAuthAccount
accountFromParams, // req.vAccount
procGetAccountId,
finishMetaverseAPI ] );
router.post( '/api/v1/account/:accountId', [ setupMetaverseAPI, // req.vRestResp, req.vAuthToken
accountFromAuthToken, // req.vAuthAccount
accountFromParams, // req.vAccount
procPostAccountId,
finishMetaverseAPI ] );
router.delete('/api/v1/account/:accountId', [ setupMetaverseAPI, // req.vRestResp, req.vAuthToken
accountFromAuthToken, // req.vAuthAccount
accountFromParams, // req.vAccount
procDeleteAccountId,
finishMetaverseAPI ] );