@live-change/access-control-service 0.2.28 → 0.2.31

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/invite.js +90 -4
  2. package/model.js +7 -1
  3. package/package.json +3 -3
package/invite.js CHANGED
@@ -3,14 +3,27 @@ const app = App.app()
3
3
  const definition = require('./definition.js')
4
4
  const config = definition.config
5
5
 
6
- const { AccessInvitation, invitationProperties } = require('./model.js')
6
+ const { AccessInvitation, invitationProperties, Access } = require('./model.js')
7
7
  const access = require('./access.js')(definition)
8
8
 
9
+ const contactProperties = {
10
+ contactType: {
11
+ type: String,
12
+ validation: ['nonEmpty']
13
+ },
14
+ contact: {
15
+ type: String,
16
+ validation: ['nonEmpty']
17
+ }
18
+ }
19
+
20
+ const Session = definition.foreignModel('session', 'Session')
21
+
9
22
  definition.event({
10
23
  name: 'userInvited',
11
24
  async execute({ user, objectType, object, roles, message }) {
12
25
  await AccessInvitation.create({
13
- id: App.encodeIdentifier(['user_User', user, objectType, object]),
26
+ id: App.encodeIdentifier([ 'user_User', user, objectType, object ]),
14
27
  contactOrUserType: 'user_User', contactOrUser: user,
15
28
  objectType, object,
16
29
  roles, message
@@ -22,7 +35,7 @@ definition.event({
22
35
  name: 'contactInvited',
23
36
  async execute({ contactType, contact, objectType, object, roles, message }) {
24
37
  await AccessInvitation.create({
25
- id: App.encodeIdentifier([contactType, contact, objectType, object]),
38
+ id: App.encodeIdentifier([ contactType, contact, objectType, object ]),
26
39
  contactOrUserType: contactType, contactOrUser: contact,
27
40
  objectType, object,
28
41
  roles, message
@@ -30,6 +43,63 @@ definition.event({
30
43
  }
31
44
  })
32
45
 
46
+ definition.event({
47
+ name: 'userInvitationAccepted',
48
+ async execute({ user, objectType, object, roles }) {
49
+ await AccessInvitation.delete(
50
+ App.encodeIdentifier([ 'user_User', user, objectType, object ])
51
+ )
52
+ await Access.create({
53
+ id: App.encodeIdentifier([ 'user_User', user, objectType, object ]),
54
+ sessionOrUserType: 'user_User',
55
+ sessionOrUser: user,
56
+ objectType,
57
+ object,
58
+ roles
59
+ })
60
+ }
61
+ })
62
+
63
+ definition.trigger({
64
+ name: 'inviteWithMessageAuthenticated',
65
+ waitForEvents: true,
66
+ properties: {
67
+ ...contactProperties,
68
+ session: {
69
+ type: Session,
70
+ validation: ['nonEmpty']
71
+ },
72
+ actionProperties: {
73
+ type: Object
74
+ }
75
+ },
76
+ async execute({ contactType, contact, session, objectType, object }, { service }, emit) {
77
+ const contactTypeUpperCase = contactType[0].toUpperCase() + contactType.slice(1)
78
+ /// Load invitation
79
+ const invitation = App.encodeIdentifier([ contactType + '_' + contactTypeUpperCase, contact, objectType, object ])
80
+ console.log("INVITATION", invitation)
81
+ const invitationData = await AccessInvitation.get(invitation)
82
+ if(!invitationData) throw 'not_found'
83
+ const { roles } = invitation
84
+ /// Create account and sign-in:
85
+ const user = app.generateUid()
86
+ await service.trigger({
87
+ type: 'connect' + contactTypeUpperCase,
88
+ [contactType]: contact,
89
+ user
90
+ })
91
+ await service.trigger({
92
+ type: 'signUpAndSignIn',
93
+ user, session
94
+ })
95
+ emit({
96
+ type: 'userInvitationAccepted',
97
+ user, objectType, object, roles
98
+ })
99
+ return user
100
+ }
101
+ })
102
+
33
103
  for(const contactType of config.contactTypes) {
34
104
 
35
105
  const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
@@ -82,7 +152,23 @@ for(const contactType of config.contactTypes) {
82
152
  ...invitationData
83
153
  })
84
154
  } else {
85
- /// TODO: Send message to contact
155
+ // Authenticate with message because we will create account later
156
+ const messageData = {
157
+ fromType: client.user ? 'user_User' : 'session_Session',
158
+ from: client.user ?? client.session,
159
+ objectType, object,
160
+ roles: params.roles,
161
+ message: params.message
162
+ }
163
+ await service.trigger({
164
+ type: 'authenticateWithMessage',
165
+ contactType,
166
+ contact,
167
+ messageData,
168
+ action: 'inviteWithMessage',
169
+ actionProperties: { objectType, object },
170
+ targetPage: { name: 'access:invitationAccepted', objectType, object }
171
+ })
86
172
  emit({
87
173
  type: 'contactInvited',
88
174
  contactType: contactTypeName + '_' + contactTypeUName,
package/model.js CHANGED
@@ -72,6 +72,9 @@ const AccessRequest = definition.model({
72
72
  message: {
73
73
  type: String,
74
74
  validation: []
75
+ },
76
+ lastUpdate: {
77
+ type: Date
75
78
  }
76
79
  },
77
80
  indexes: {
@@ -100,7 +103,10 @@ const AccessInvitation = definition.model({
100
103
  visibilityTest || access.clientHasAdminAccess(client, params)
101
104
  },
102
105
  properties: {
103
- ...invitationProperties
106
+ ...invitationProperties,
107
+ lastUpdate: {
108
+ type: Date
109
+ }
104
110
  },
105
111
  indexes: {
106
112
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/access-control-service",
3
- "version": "0.2.28",
3
+ "version": "0.2.31",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,7 +21,7 @@
21
21
  "url": "https://www.viamage.com/"
22
22
  },
23
23
  "dependencies": {
24
- "@live-change/framework": "0.6.0"
24
+ "@live-change/framework": "0.6.4"
25
25
  },
26
- "gitHead": "34309fef58572e1a8794b52438430dd421c57d65"
26
+ "gitHead": "be1924699748fe7abcddabf207f9290e734bac6b"
27
27
  }