@data-fair/lib-common-types 1.16.0 → 1.16.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/account/index.js +1 -1
- package/account/schema.js +37 -37
- package/application/index.js +1 -1
- package/application/schema.js +124 -124
- package/catalog/index.js +1 -1
- package/catalog/schema.js +180 -180
- package/event/index.js +1 -1
- package/event/schema.js +199 -199
- package/notification/index.js +1 -1
- package/notification/schema.js +83 -83
- package/package.json +1 -1
- package/session/index.js +55 -41
- package/session/schema.js +164 -164
- package/theme/index.js +326 -306
- package/theme/schema.d.ts +384 -0
- package/theme/schema.js +809 -368
package/session/index.js
CHANGED
|
@@ -1,46 +1,60 @@
|
|
|
1
|
-
import { httpError } from '@data-fair/lib-utils/http-errors.js'
|
|
2
|
-
export * from './.type/index.js'
|
|
3
|
-
export function isAuthenticated
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export function assertAuthenticated
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (sessionState.user.adminMode) { return 'admin' }
|
|
25
|
-
// user is always admin of themself even if currently switched in an orga
|
|
26
|
-
if (account.type === 'user' && sessionState.user.id === account.id) { return 'admin' }
|
|
27
|
-
if (options.allAccounts) {
|
|
28
|
-
for (const org of sessionState.user.organizations) {
|
|
29
|
-
if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account, options.acceptDepAsRoot)) { return org.role }
|
|
1
|
+
import { httpError } from '@data-fair/lib-utils/http-errors.js';
|
|
2
|
+
export * from './.type/index.js';
|
|
3
|
+
export function isAuthenticated(sessionState) {
|
|
4
|
+
return !!sessionState.user;
|
|
5
|
+
}
|
|
6
|
+
export function assertAuthenticated(sessionState) {
|
|
7
|
+
if (!isAuthenticated(sessionState))
|
|
8
|
+
throw httpError(401);
|
|
9
|
+
}
|
|
10
|
+
export function assertAdminMode(sessionState) {
|
|
11
|
+
assertAuthenticated(sessionState);
|
|
12
|
+
// TODO: use sessionState.locale to internationalize error message
|
|
13
|
+
if (!sessionState.user.adminMode)
|
|
14
|
+
throw httpError(403, 'super admin only');
|
|
15
|
+
}
|
|
16
|
+
function matchAccount(userAccount, resourceAccount, acceptDepAsRoot = false) {
|
|
17
|
+
if (userAccount.type !== resourceAccount.type)
|
|
18
|
+
return false;
|
|
19
|
+
if (userAccount.id !== resourceAccount.id)
|
|
20
|
+
return false;
|
|
21
|
+
if (!acceptDepAsRoot) {
|
|
22
|
+
if (userAccount.department && userAccount.department !== resourceAccount.department)
|
|
23
|
+
return false;
|
|
30
24
|
}
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
return true;
|
|
26
|
+
}
|
|
27
|
+
export function getAccountRole(sessionState, account, options = {}) {
|
|
28
|
+
if (!isAuthenticated(sessionState))
|
|
29
|
+
return null;
|
|
30
|
+
if (sessionState.user.adminMode)
|
|
31
|
+
return 'admin';
|
|
32
|
+
// user is always admin of themself even if currently switched in an orga
|
|
33
|
+
if (account.type === 'user' && sessionState.user.id === account.id)
|
|
34
|
+
return 'admin';
|
|
35
|
+
if (options.allAccounts) {
|
|
36
|
+
for (const org of sessionState.user.organizations) {
|
|
37
|
+
if (matchAccount({ type: 'organization', id: org.id, department: org.department }, account, options.acceptDepAsRoot))
|
|
38
|
+
return org.role;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
if (matchAccount(sessionState.account, account, options.acceptDepAsRoot))
|
|
43
|
+
return sessionState.accountRole;
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
35
46
|
}
|
|
36
|
-
export function assertAccountRole
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
47
|
+
export function assertAccountRole(sessionState, account, roles, options = {}) {
|
|
48
|
+
if (typeof roles === 'string')
|
|
49
|
+
roles = [roles];
|
|
50
|
+
const accountRole = getAccountRole(sessionState, account, options);
|
|
51
|
+
if (!accountRole || !roles.includes(accountRole))
|
|
52
|
+
throw httpError(403, `requires ${roles.join(', ')} role(s)`);
|
|
40
53
|
}
|
|
41
|
-
export function isValidAccountType
|
|
42
|
-
|
|
54
|
+
export function isValidAccountType(type) {
|
|
55
|
+
return ['user', 'organization'].includes(type);
|
|
43
56
|
}
|
|
44
|
-
export function assertValidAccountType
|
|
45
|
-
|
|
57
|
+
export function assertValidAccountType(type) {
|
|
58
|
+
if (!isValidAccountType(type))
|
|
59
|
+
throw httpError(400, 'invalid account type');
|
|
46
60
|
}
|
package/session/schema.js
CHANGED
|
@@ -1,175 +1,175 @@
|
|
|
1
1
|
export default {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
$ref: '#/$defs/organizationMembership'
|
|
14
|
-
},
|
|
15
|
-
account: {
|
|
16
|
-
$ref: '#/$defs/account'
|
|
17
|
-
},
|
|
18
|
-
accountRole: {
|
|
19
|
-
type: 'string'
|
|
20
|
-
},
|
|
21
|
-
siteRole: {
|
|
22
|
-
type: 'string'
|
|
23
|
-
},
|
|
24
|
-
lang: {
|
|
25
|
-
type: 'string'
|
|
26
|
-
},
|
|
27
|
-
dark: {
|
|
28
|
-
deprecated: true,
|
|
29
|
-
type: 'boolean'
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
$defs: {
|
|
33
|
-
organizationMembership: {
|
|
34
|
-
type: 'object',
|
|
35
|
-
additionalProperties: false,
|
|
36
|
-
required: [
|
|
37
|
-
'id',
|
|
38
|
-
'name',
|
|
39
|
-
'role'
|
|
40
|
-
],
|
|
41
|
-
properties: {
|
|
42
|
-
id: {
|
|
43
|
-
type: 'string'
|
|
44
|
-
},
|
|
45
|
-
name: {
|
|
46
|
-
type: 'string'
|
|
47
|
-
},
|
|
48
|
-
role: {
|
|
49
|
-
type: 'string'
|
|
50
|
-
},
|
|
51
|
-
roleLabel: {
|
|
52
|
-
type: 'string'
|
|
53
|
-
},
|
|
54
|
-
department: {
|
|
55
|
-
type: 'string'
|
|
56
|
-
},
|
|
57
|
-
departmentName: {
|
|
58
|
-
type: 'string'
|
|
59
|
-
},
|
|
60
|
-
dflt: {
|
|
61
|
-
type: 'integer',
|
|
62
|
-
enum: [1]
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
},
|
|
66
|
-
userRef: {
|
|
67
|
-
type: 'object',
|
|
68
|
-
additionalProperties: false,
|
|
69
|
-
required: [
|
|
70
|
-
'id',
|
|
71
|
-
'name'
|
|
72
|
-
],
|
|
73
|
-
properties: {
|
|
74
|
-
id: {
|
|
75
|
-
type: 'string'
|
|
76
|
-
},
|
|
77
|
-
name: {
|
|
78
|
-
type: 'string'
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
user: {
|
|
83
|
-
type: 'object',
|
|
84
|
-
additionalProperties: false,
|
|
85
|
-
required: [
|
|
86
|
-
'email',
|
|
87
|
-
'id',
|
|
88
|
-
'name',
|
|
89
|
-
'organizations'
|
|
90
|
-
],
|
|
91
|
-
properties: {
|
|
92
|
-
email: {
|
|
93
|
-
type: 'string',
|
|
94
|
-
format: 'email'
|
|
95
|
-
},
|
|
96
|
-
id: {
|
|
97
|
-
type: 'string'
|
|
98
|
-
},
|
|
99
|
-
name: {
|
|
100
|
-
type: 'string'
|
|
101
|
-
},
|
|
102
|
-
organizations: {
|
|
103
|
-
type: 'array',
|
|
104
|
-
items: {
|
|
2
|
+
$id: 'https://github.com/data-fair/lib/session-state',
|
|
3
|
+
'x-exports': ['types', 'validate'],
|
|
4
|
+
type: 'object',
|
|
5
|
+
title: 'session state',
|
|
6
|
+
additionalProperties: false,
|
|
7
|
+
required: ['lang'],
|
|
8
|
+
properties: {
|
|
9
|
+
user: {
|
|
10
|
+
$ref: '#/$defs/user'
|
|
11
|
+
},
|
|
12
|
+
organization: {
|
|
105
13
|
$ref: '#/$defs/organizationMembership'
|
|
106
|
-
}
|
|
107
|
-
},
|
|
108
|
-
isAdmin: {
|
|
109
|
-
type: 'integer',
|
|
110
|
-
enum: [1]
|
|
111
14
|
},
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
enum: [1]
|
|
15
|
+
account: {
|
|
16
|
+
$ref: '#/$defs/account'
|
|
115
17
|
},
|
|
116
|
-
|
|
117
|
-
|
|
18
|
+
accountRole: {
|
|
19
|
+
type: 'string'
|
|
118
20
|
},
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
format: 'date'
|
|
21
|
+
siteRole: {
|
|
22
|
+
type: 'string'
|
|
122
23
|
},
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
title: 'short for ignorePersonalAccount',
|
|
126
|
-
enum: [1]
|
|
24
|
+
lang: {
|
|
25
|
+
type: 'string'
|
|
127
26
|
},
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
enum: [1]
|
|
132
|
-
},
|
|
133
|
-
os: {
|
|
134
|
-
type: 'integer',
|
|
135
|
-
title: 'short for orgStorage',
|
|
136
|
-
enum: [1]
|
|
137
|
-
},
|
|
138
|
-
rememberMe: {
|
|
139
|
-
type: 'integer',
|
|
140
|
-
enum: [1]
|
|
141
|
-
},
|
|
142
|
-
siteOwner: {
|
|
143
|
-
$ref: '#/$defs/account'
|
|
27
|
+
dark: {
|
|
28
|
+
deprecated: true,
|
|
29
|
+
type: 'boolean'
|
|
144
30
|
}
|
|
145
|
-
}
|
|
146
31
|
},
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
32
|
+
$defs: {
|
|
33
|
+
organizationMembership: {
|
|
34
|
+
type: 'object',
|
|
35
|
+
additionalProperties: false,
|
|
36
|
+
required: [
|
|
37
|
+
'id',
|
|
38
|
+
'name',
|
|
39
|
+
'role'
|
|
40
|
+
],
|
|
41
|
+
properties: {
|
|
42
|
+
id: {
|
|
43
|
+
type: 'string'
|
|
44
|
+
},
|
|
45
|
+
name: {
|
|
46
|
+
type: 'string'
|
|
47
|
+
},
|
|
48
|
+
role: {
|
|
49
|
+
type: 'string'
|
|
50
|
+
},
|
|
51
|
+
roleLabel: {
|
|
52
|
+
type: 'string'
|
|
53
|
+
},
|
|
54
|
+
department: {
|
|
55
|
+
type: 'string'
|
|
56
|
+
},
|
|
57
|
+
departmentName: {
|
|
58
|
+
type: 'string'
|
|
59
|
+
},
|
|
60
|
+
dflt: {
|
|
61
|
+
type: 'integer',
|
|
62
|
+
enum: [1]
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
userRef: {
|
|
67
|
+
type: 'object',
|
|
68
|
+
additionalProperties: false,
|
|
69
|
+
required: [
|
|
70
|
+
'id',
|
|
71
|
+
'name'
|
|
72
|
+
],
|
|
73
|
+
properties: {
|
|
74
|
+
id: {
|
|
75
|
+
type: 'string'
|
|
76
|
+
},
|
|
77
|
+
name: {
|
|
78
|
+
type: 'string'
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
},
|
|
82
|
+
user: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
additionalProperties: false,
|
|
85
|
+
required: [
|
|
86
|
+
'email',
|
|
87
|
+
'id',
|
|
88
|
+
'name',
|
|
89
|
+
'organizations'
|
|
90
|
+
],
|
|
91
|
+
properties: {
|
|
92
|
+
email: {
|
|
93
|
+
type: 'string',
|
|
94
|
+
format: 'email'
|
|
95
|
+
},
|
|
96
|
+
id: {
|
|
97
|
+
type: 'string'
|
|
98
|
+
},
|
|
99
|
+
name: {
|
|
100
|
+
type: 'string'
|
|
101
|
+
},
|
|
102
|
+
organizations: {
|
|
103
|
+
type: 'array',
|
|
104
|
+
items: {
|
|
105
|
+
$ref: '#/$defs/organizationMembership'
|
|
106
|
+
}
|
|
107
|
+
},
|
|
108
|
+
isAdmin: {
|
|
109
|
+
type: 'integer',
|
|
110
|
+
enum: [1]
|
|
111
|
+
},
|
|
112
|
+
adminMode: {
|
|
113
|
+
type: 'integer',
|
|
114
|
+
enum: [1]
|
|
115
|
+
},
|
|
116
|
+
asAdmin: {
|
|
117
|
+
$ref: '#/$defs/userRef'
|
|
118
|
+
},
|
|
119
|
+
pd: {
|
|
120
|
+
type: 'string',
|
|
121
|
+
format: 'date'
|
|
122
|
+
},
|
|
123
|
+
ipa: {
|
|
124
|
+
type: 'integer',
|
|
125
|
+
title: 'short for ignorePersonalAccount',
|
|
126
|
+
enum: [1]
|
|
127
|
+
},
|
|
128
|
+
idp: {
|
|
129
|
+
type: 'integer',
|
|
130
|
+
title: 'Is the user coming from a core ID provider ?',
|
|
131
|
+
enum: [1]
|
|
132
|
+
},
|
|
133
|
+
os: {
|
|
134
|
+
type: 'integer',
|
|
135
|
+
title: 'short for orgStorage',
|
|
136
|
+
enum: [1]
|
|
137
|
+
},
|
|
138
|
+
rememberMe: {
|
|
139
|
+
type: 'integer',
|
|
140
|
+
enum: [1]
|
|
141
|
+
},
|
|
142
|
+
siteOwner: {
|
|
143
|
+
$ref: '#/$defs/account'
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
account: {
|
|
148
|
+
type: 'object',
|
|
149
|
+
additionalProperties: false,
|
|
150
|
+
required: [
|
|
151
|
+
'type',
|
|
152
|
+
'id',
|
|
153
|
+
'name'
|
|
154
|
+
],
|
|
155
|
+
properties: {
|
|
156
|
+
type: {
|
|
157
|
+
type: 'string',
|
|
158
|
+
enum: ['user', 'organization']
|
|
159
|
+
},
|
|
160
|
+
id: {
|
|
161
|
+
type: 'string'
|
|
162
|
+
},
|
|
163
|
+
name: {
|
|
164
|
+
type: 'string'
|
|
165
|
+
},
|
|
166
|
+
department: {
|
|
167
|
+
type: 'string'
|
|
168
|
+
},
|
|
169
|
+
departmentName: {
|
|
170
|
+
type: 'string'
|
|
171
|
+
}
|
|
172
|
+
}
|
|
171
173
|
}
|
|
172
|
-
}
|
|
173
174
|
}
|
|
174
|
-
|
|
175
|
-
}
|
|
175
|
+
};
|