@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/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 (sessionState) {
4
- return !!sessionState.user
5
- }
6
- export function assertAuthenticated (sessionState) {
7
- if (!isAuthenticated(sessionState)) { throw httpError(401) }
8
- }
9
- export function assertAdminMode (sessionState) {
10
- assertAuthenticated(sessionState)
11
- // TODO: use sessionState.locale to internationalize error message
12
- if (!sessionState.user.adminMode) { throw httpError(403, 'super admin only') }
13
- }
14
- function matchAccount (userAccount, resourceAccount, acceptDepAsRoot = false) {
15
- if (userAccount.type !== resourceAccount.type) { return false }
16
- if (userAccount.id !== resourceAccount.id) { return false }
17
- if (!acceptDepAsRoot) {
18
- if (userAccount.department && userAccount.department !== resourceAccount.department) { return false }
19
- }
20
- return true
21
- }
22
- export function getAccountRole (sessionState, account, options = {}) {
23
- if (!isAuthenticated(sessionState)) { return null }
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
- } else {
32
- if (matchAccount(sessionState.account, account, options.acceptDepAsRoot)) { return sessionState.accountRole }
33
- }
34
- return null
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 (sessionState, account, roles, options = {}) {
37
- if (typeof roles === 'string') { roles = [roles] }
38
- const accountRole = getAccountRole(sessionState, account, options)
39
- if (!accountRole || !roles.includes(accountRole)) { throw httpError(403, `requires ${roles.join(', ')} role(s)`) }
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 (type) {
42
- return ['user', 'organization'].includes(type)
54
+ export function isValidAccountType(type) {
55
+ return ['user', 'organization'].includes(type);
43
56
  }
44
- export function assertValidAccountType (type) {
45
- if (!isValidAccountType(type)) { throw httpError(400, 'invalid account type') }
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
- $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: {
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
- adminMode: {
113
- type: 'integer',
114
- enum: [1]
15
+ account: {
16
+ $ref: '#/$defs/account'
115
17
  },
116
- asAdmin: {
117
- $ref: '#/$defs/userRef'
18
+ accountRole: {
19
+ type: 'string'
118
20
  },
119
- pd: {
120
- type: 'string',
121
- format: 'date'
21
+ siteRole: {
22
+ type: 'string'
122
23
  },
123
- ipa: {
124
- type: 'integer',
125
- title: 'short for ignorePersonalAccount',
126
- enum: [1]
24
+ lang: {
25
+ type: 'string'
127
26
  },
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'
27
+ dark: {
28
+ deprecated: true,
29
+ type: 'boolean'
144
30
  }
145
- }
146
31
  },
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'
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
+ };