@live-change/access-control-service 0.2.33 → 0.2.34

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.
Files changed (3) hide show
  1. package/access.js +6 -1
  2. package/invite.js +61 -24
  3. package/package.json +2 -2
package/access.js CHANGED
@@ -28,6 +28,11 @@ module.exports = (definition) => {
28
28
  return true
29
29
  }
30
30
 
31
- return { clientHasAnyAccess, clientHasAdminAccess, clientCanInvite, clientCanRequest, clientHasAccessRole }
31
+ return {
32
+ clientHasAnyAccess, clientHasAdminAccess,
33
+ clientCanInvite,
34
+ clientCanRequest,
35
+ clientHasAccessRole
36
+ }
32
37
 
33
38
  }
package/invite.js CHANGED
@@ -21,25 +21,33 @@ const Session = definition.foreignModel('session', 'Session')
21
21
 
22
22
  definition.event({
23
23
  name: 'userInvited',
24
- async execute({ user, objectType, object, roles, message }) {
25
- await AccessInvitation.create({
26
- id: App.encodeIdentifier([ 'user_User', user, objectType, object ]),
27
- contactOrUserType: 'user_User', contactOrUser: user,
28
- objectType, object,
29
- roles, message
30
- })
24
+ async execute(params) {
25
+ const { user, objectType, object, fromType, from } = params
26
+ const contactOrUserType = 'user_User'
27
+ const contactOrUser = user
28
+ const data = {
29
+ id: App.encodeIdentifier([ contactOrUserType, contactOrUser, objectType, object ]),
30
+ contactOrUserType, contactOrUser,
31
+ objectType, object, fromType, from
32
+ }
33
+ for(const propertyName in invitationProperties) data[propertyName] = params[propertyName]
34
+ await AccessInvitation.create(data)
31
35
  }
32
36
  })
33
37
 
34
38
  definition.event({
35
39
  name: 'contactInvited',
36
- async execute({ contactType, contact, objectType, object, roles, message }) {
37
- await AccessInvitation.create({
38
- id: App.encodeIdentifier([ contactType, contact, objectType, object ]),
39
- contactOrUserType: contactType, contactOrUser: contact,
40
- objectType, object,
41
- roles, message
42
- })
40
+ async execute(params) {
41
+ const { contactType, contact, objectType, object, fromType, from } = params
42
+ const contactOrUserType = contactType
43
+ const contactOrUser = contact
44
+ const data = {
45
+ id: App.encodeIdentifier([ contactOrUserType, contactOrUser, objectType, object ]),
46
+ contactOrUserType, contactOrUser,
47
+ objectType, object, fromType, from
48
+ }
49
+ for(const propertyName in invitationProperties) data[propertyName] = params[propertyName]
50
+ await AccessInvitation.create(data)
43
51
  }
44
52
  })
45
53
 
@@ -88,14 +96,16 @@ definition.trigger({
88
96
  }
89
97
  },
90
98
  async execute({ from, to, objectType, object }, { service }, emit) {
99
+ const invitation = App.encodeIdentifier([from.contactOrUserType, from.contactOrUser, objectType, object])
100
+ const invitationData = await AccessInvitation.get(invitation)
91
101
  if(to.contactOrUserType == 'user_User') {
92
102
  await service.trigger({
103
+ ...invitationData,
93
104
  type: 'notify',
94
105
  sessionOrUserType: 'user_User',
95
106
  sessionOrUser: to.contactOrUser,
96
107
  notificationType: 'accessControl_Invitation',
97
- objectType,
98
- object
108
+ id: undefined
99
109
  })
100
110
  }
101
111
  }
@@ -141,6 +151,34 @@ definition.trigger({
141
151
  }
142
152
  })
143
153
 
154
+ definition.action({
155
+ name: 'acceptInvitation',
156
+ waitForEvents: true,
157
+ properties: {
158
+ objectType: {
159
+ type: String,
160
+ validation: ['nonEmpty']
161
+ },
162
+ object: {
163
+ type: String,
164
+ validation: ['nonEmpty']
165
+ }
166
+ },
167
+ async execute({ objectType, object }, {client, service}, emit) {
168
+ if(!client.user) throw 'not_authorized'
169
+ const user = client.user
170
+ const invitation = App.encodeIdentifier(['user_User', user, objectType, object])
171
+ const invitationData = await AccessInvitation.get(invitation)
172
+ console.log("INVITATION", invitation, invitationData)
173
+ if(!invitationData) throw 'not_found'
174
+ const { roles } = invitationData
175
+ emit({
176
+ type: 'userInvitationAccepted',
177
+ user, objectType, object, roles
178
+ })
179
+ }
180
+ })
181
+
144
182
  for(const contactType of config.contactTypes) {
145
183
 
146
184
  const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
@@ -175,9 +213,10 @@ for(const contactType of config.contactTypes) {
175
213
  access: (params, { client, context, visibilityTest }) =>
176
214
  visibilityTest || access.clientCanInvite(client, params),
177
215
  async execute(params, { client, service }, emit) {
216
+ const [ fromType, from ] = client.user ? ['user_User', client.user] : ['session_Session', client.session]
178
217
  const { [contactTypeName]: contact } = params
179
218
  const { objectType, object } = params
180
- const invitationData = { }
219
+ const invitationData = { fromType, from }
181
220
  for(const propertyName in invitationProperties) invitationData[propertyName] = params[propertyName]
182
221
 
183
222
  const contactData = (await service.trigger({
@@ -192,22 +231,20 @@ for(const contactType of config.contactTypes) {
192
231
  sessionOrUser: user,
193
232
  notificationType: 'accessControl_Invitation',
194
233
  objectType,
195
- object
234
+ object,
235
+ ...invitationData, id: undefined
196
236
  })
197
237
  emit({
198
238
  type: 'userInvited',
199
239
  user,
200
240
  objectType, object,
201
- ...invitationData
241
+ ...invitationData, id: undefined
202
242
  })
203
243
  } else {
204
244
  // Authenticate with message because we will create account later
205
245
  const messageData = {
206
- fromType: client.user ? 'user_User' : 'session_Session',
207
- from: client.user ?? client.session,
208
246
  objectType, object,
209
- roles: params.roles,
210
- message: params.message
247
+ ...invitationData, id: undefined
211
248
  }
212
249
  await service.trigger({
213
250
  type: 'authenticateWithMessage',
@@ -223,7 +260,7 @@ for(const contactType of config.contactTypes) {
223
260
  contactType: contactTypeName + '_' + contactTypeUName,
224
261
  contact,
225
262
  objectType, object,
226
- ...invitationData
263
+ ...invitationData, id: undefined
227
264
  })
228
265
  }
229
266
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/access-control-service",
3
- "version": "0.2.33",
3
+ "version": "0.2.34",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -23,5 +23,5 @@
23
23
  "dependencies": {
24
24
  "@live-change/framework": "0.6.5"
25
25
  },
26
- "gitHead": "8918e5113c031c3d9766df32283de7fb6421e0be"
26
+ "gitHead": "45f52d6c7586d0eaa51b3205c6635a33e32eef6b"
27
27
  }