@live-change/access-control-service 0.2.27 → 0.2.30

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/LICENSE.md ADDED
@@ -0,0 +1,11 @@
1
+ Copyright 2019-2022 Michał Łaszczewski
2
+
3
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
4
+
5
+ 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
6
+
7
+ 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
10
+
11
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package/access.js CHANGED
@@ -24,6 +24,10 @@ module.exports = (definition) => {
24
24
  return true
25
25
  }
26
26
 
27
- return { clientHasAnyAccess, clientHasAdminAccess, clientCanInvite, clientCanRequest }
27
+ function clientHasAccessRole(client, { objectType, object }, role) {
28
+ return true
29
+ }
30
+
31
+ return { clientHasAnyAccess, clientHasAdminAccess, clientCanInvite, clientCanRequest, clientHasAccessRole }
28
32
 
29
33
  }
package/index.js CHANGED
@@ -4,5 +4,6 @@ const definition = require('./definition.js')
4
4
 
5
5
  require('./model.js')
6
6
  require('./invite.js')
7
+ require('./request.js')
7
8
 
8
9
  module.exports = definition
package/invite.js CHANGED
@@ -1,10 +1,105 @@
1
- const app = require("@live-change/framework").app()
1
+ const App = require("@live-change/framework")
2
+ const app = App.app()
2
3
  const definition = require('./definition.js')
3
4
  const config = definition.config
4
5
 
5
- const { Invite, invitationProperties } = require('./model.js')
6
+ const { AccessInvitation, invitationProperties, Access } = require('./model.js')
6
7
  const access = require('./access.js')(definition)
7
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
+
22
+ definition.event({
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
+ })
31
+ }
32
+ })
33
+
34
+ definition.event({
35
+ 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
+ })
43
+ }
44
+ })
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
+
8
103
  for(const contactType of config.contactTypes) {
9
104
 
10
105
  const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
@@ -57,7 +152,23 @@ for(const contactType of config.contactTypes) {
57
152
  ...invitationData
58
153
  })
59
154
  } else {
60
- /// 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
+ })
61
172
  emit({
62
173
  type: 'contactInvited',
63
174
  contactType: contactTypeName + '_' + contactTypeUName,
package/model.js CHANGED
@@ -4,6 +4,15 @@ const definition = require('./definition.js')
4
4
  const config = definition.config
5
5
  const access = require('./access.js')(definition)
6
6
 
7
+ const rolesArrayType = {
8
+ type: Array,
9
+ of: {
10
+ type: String,
11
+ validation: ['nonEmpty']
12
+ },
13
+ validation: ['elementsNonEmpty']
14
+ }
15
+
7
16
  const Access = definition.model({
8
17
  name: 'Access',
9
18
  sessionOrUserProperty: {
@@ -17,14 +26,7 @@ const Access = definition.model({
17
26
  visibilityTest || access.clientHasAdminAccess(client, params)
18
27
  },
19
28
  properties: {
20
- roles: {
21
- type: Array,
22
- of: {
23
- type: String,
24
- validation: ['nonEmpty']
25
- },
26
- validation: ['elementsNonEmpty']
27
- },
29
+ roles: rolesArrayType,
28
30
  lastUpdate: {
29
31
  type: Date
30
32
  }
@@ -41,30 +43,9 @@ const PublicAccess = definition.model({
41
43
  visibilityTest || access.clientHasAdminAccess(client, params)
42
44
  },
43
45
  properties: {
44
- userRoles: {
45
- type: Array,
46
- of: {
47
- type: String,
48
- validation: ['nonEmpty']
49
- },
50
- validation: ['elementsNonEmpty']
51
- },
52
- sessionRoles: {
53
- type: Array,
54
- of: {
55
- type: String,
56
- validation: ['nonEmpty']
57
- },
58
- validation: ['elementsNonEmpty']
59
- },
60
- availableRoles: {
61
- type: Array,
62
- of: {
63
- type: String,
64
- validation: ['nonEmpty']
65
- },
66
- validation: ['elementsNonEmpty']
67
- },
46
+ userRoles: rolesArrayType,
47
+ sessionRoles: rolesArrayType,
48
+ availableRoles: rolesArrayType,
68
49
  lastUpdate: {
69
50
  type: Date
70
51
  }
@@ -87,17 +68,13 @@ const AccessRequest = definition.model({
87
68
  visibilityTest || access.clientHasAdminAccess(client, params)
88
69
  },
89
70
  properties: {
90
- roles: {
91
- type: Array,
92
- of: {
93
- type: String,
94
- validation: ['nonEmpty']
95
- },
96
- validation: ['elementsNonEmpty']
97
- },
71
+ roles: rolesArrayType,
98
72
  message: {
99
73
  type: String,
100
74
  validation: []
75
+ },
76
+ lastUpdate: {
77
+ type: Date
101
78
  }
102
79
  },
103
80
  indexes: {
@@ -105,14 +82,7 @@ const AccessRequest = definition.model({
105
82
  })
106
83
 
107
84
  const invitationProperties = {
108
- roles: {
109
- type: Array,
110
- of: {
111
- type: String,
112
- validation: ['nonEmpty']
113
- },
114
- validation: ['elementsNonEmpty']
115
- },
85
+ roles: rolesArrayType,
116
86
  message: {
117
87
  type: String,
118
88
  validation: []
@@ -133,35 +103,14 @@ const AccessInvitation = definition.model({
133
103
  visibilityTest || access.clientHasAdminAccess(client, params)
134
104
  },
135
105
  properties: {
136
- ...invitationProperties
106
+ ...invitationProperties,
107
+ lastUpdate: {
108
+ type: Date
109
+ }
137
110
  },
138
111
  indexes: {
139
112
 
140
113
  }
141
114
  })
142
115
 
143
- definition.event({
144
- name: 'userInvited',
145
- async execute({ user, objectType, object, roles, message }) {
146
- await AccessInvitation.create({
147
- id: App.encodeIdentifier(['user_User', user, objectType, object]),
148
- contactOrUserType: 'user_User', contactOrUser: user,
149
- objectType, object,
150
- roles, message
151
- })
152
- }
153
- })
154
-
155
- definition.event({
156
- name: 'contactInvited',
157
- async execute({ contactType, contact, objectType, object, roles, message }) {
158
- await AccessInvitation.create({
159
- id: App.encodeIdentifier([contactType, contact, objectType, object]),
160
- contactOrUserType: contactType, contactOrUser: contact,
161
- objectType, object,
162
- roles, message
163
- })
164
- }
165
- })
166
-
167
- module.exports = { Access, PublicAccess, AccessRequest, AccessInvitation, invitationProperties }
116
+ module.exports = { Access, PublicAccess, AccessRequest, AccessInvitation, invitationProperties, rolesArrayType }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/access-control-service",
3
- "version": "0.2.27",
3
+ "version": "0.2.30",
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": "9a82ff0e7a7003d5b4e34ef9aef1ad4d7d8605dd"
26
+ "gitHead": "0a3acfcbb3a720c3e2f20bb3c9dc80a0a1aaceaa"
27
27
  }
package/request.js ADDED
@@ -0,0 +1,54 @@
1
+ const App = require("@live-change/framework")
2
+ const app = App.app()
3
+ const definition = require('./definition.js')
4
+ const config = definition.config
5
+
6
+ const { Access, AccessRequest, rolesArrayType } = require('./model.js')
7
+ const access = require('./access.js')(definition)
8
+
9
+ definition.event({
10
+ name: 'accessRequestAccepted',
11
+ async execute({ sessionOrUserType, sessionOrUser, objectType, object, roles }) {
12
+ const id = App.encodeIdentifier([sessionOrUserType, sessionOrUser, objectType, object])
13
+ await Access.create({
14
+ id,
15
+ sessionOrUserType, sessionOrUser, objectType, object, roles
16
+ })
17
+ await AccessRequest.delete(id)
18
+ }
19
+ })
20
+
21
+ definition.action({
22
+ name: 'acceptAccessRequest',
23
+ waitForEvents: true,
24
+ properties: {
25
+ objectType: {
26
+ type: String,
27
+ validation: ['nonEmpty']
28
+ },
29
+ object: {
30
+ type: String,
31
+ validation: ['nonEmpty']
32
+ },
33
+ sessionOrUserType: {
34
+ type: String,
35
+ validation: ['nonEmpty']
36
+ },
37
+ sessionOrUser: {
38
+ type: String,
39
+ validation: ['nonEmpty']
40
+ },
41
+ roles: rolesArrayType
42
+ },
43
+ access: (params, { client, context, visibilityTest }) =>
44
+ visibilityTest || access.clientCanInvite(client, params),
45
+ async execute({ objectType, object, sessionOrUserType, sessionOrUser, roles }, { client, service }, emit) {
46
+ const request = App.encodeIdentifier([ sessionOrUserType, sessionOrUser, objectType, object ])
47
+ const requestData = await AccessRequest.get(request)
48
+ if(!requestData) throw 'not_found'
49
+ emit({
50
+ type: 'accessRequestAccepted',
51
+ objectType, object, sessionOrUserType, sessionOrUser, roles
52
+ })
53
+ }
54
+ })