@live-change/access-control-service 0.2.26 → 0.2.29

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
@@ -4,16 +4,30 @@ module.exports = (definition) => {
4
4
  const Access = definition.foreignModel('access-control', 'Access')
5
5
  const PublicAccess = definition.foreignModel('access-control', 'PublicAccess')
6
6
 
7
- function clientHasAnyAccess(client, toType, toId) {
7
+ function clientHasAnyAccess(client, { objectType, object }) {
8
8
  /// TODO: access control
9
9
  return true
10
10
  }
11
11
 
12
- function clientHasAdminAccess(client, toType, toId) {
12
+ function clientHasAdminAccess(client, { objectType, object }) {
13
13
  /// TODO: access control
14
14
  return true
15
15
  }
16
16
 
17
- return {clientHasAnyAccess, clientHasAdminAccess }
17
+ function clientCanInvite(client, { roles, objectType, object }) {
18
+ /// TODO: access control
19
+ return true
20
+ }
21
+
22
+ function clientCanRequest(client, { roles, objectType, object }) {
23
+ /// TODO: access control
24
+ return true
25
+ }
26
+
27
+ function clientHasAccessRole(client, { objectType, object }, role) {
28
+ return true
29
+ }
30
+
31
+ return { clientHasAnyAccess, clientHasAdminAccess, clientCanInvite, clientCanRequest, clientHasAccessRole }
18
32
 
19
33
  }
package/index.js CHANGED
@@ -3,5 +3,7 @@ const app = require("@live-change/framework").app()
3
3
  const definition = require('./definition.js')
4
4
 
5
5
  require('./model.js')
6
+ require('./invite.js')
7
+ require('./request.js')
6
8
 
7
9
  module.exports = definition
package/invite.js ADDED
@@ -0,0 +1,183 @@
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 { AccessInvitation, invitationProperties, Access } = require('./model.js')
7
+ const access = require('./access.js')(definition)
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
+
103
+ for(const contactType of config.contactTypes) {
104
+
105
+ const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
106
+
107
+ const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
108
+
109
+ const contactTypeName = contactConfig.name
110
+ const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
111
+
112
+ const contactTypeProperties = {
113
+ [contactType]: {
114
+ type: String,
115
+ validation: ['nonEmpty', contactTypeName]
116
+ }
117
+ }
118
+
119
+ definition.action({
120
+ name: 'invite' + contactTypeUpperCaseName,
121
+ waitForEvents: true,
122
+ properties: {
123
+ objectType: {
124
+ type: String,
125
+ validation: ['nonEmpty']
126
+ },
127
+ object: {
128
+ type: String,
129
+ validation: ['nonEmpty']
130
+ },
131
+ ...contactTypeProperties,
132
+ ...invitationProperties
133
+ },
134
+ access: (params, { client, context, visibilityTest }) =>
135
+ visibilityTest || access.clientCanInvite(client, params),
136
+ async execute(params, { client, service }, emit) {
137
+ const { [contactTypeName]: contact } = params
138
+ const { objectType, object } = params
139
+ const invitationData = { }
140
+ for(const propertyName in invitationProperties) invitationData[propertyName] = params[propertyName]
141
+
142
+ const contactData = (await service.trigger({
143
+ type: 'get' + contactTypeUName + 'OrNull',
144
+ [contactType]: contact,
145
+ }))[0]
146
+ if(contactData?.user) { // user exists
147
+ /// TODO: Trigger notification
148
+ emit({
149
+ type: 'userInvited',
150
+ user: contactData.user,
151
+ objectType, object,
152
+ ...invitationData
153
+ })
154
+ } else {
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
+ })
172
+ emit({
173
+ type: 'contactInvited',
174
+ contactType: contactTypeName + '_' + contactTypeUName,
175
+ contact,
176
+ objectType, object,
177
+ ...invitationData
178
+ })
179
+ }
180
+ }
181
+ })
182
+
183
+ }
package/model.js CHANGED
@@ -1,28 +1,32 @@
1
+ const App = require("@live-change/framework")
2
+ const app = App.app()
1
3
  const definition = require('./definition.js')
2
4
  const config = definition.config
3
5
  const access = require('./access.js')(definition)
4
6
 
7
+ const rolesArrayType = {
8
+ type: Array,
9
+ of: {
10
+ type: String,
11
+ validation: ['nonEmpty']
12
+ },
13
+ validation: ['elementsNonEmpty']
14
+ }
15
+
5
16
  const Access = definition.model({
6
17
  name: 'Access',
7
18
  sessionOrUserProperty: {
8
19
  extendedWith: ['object'],
9
20
  ownerReadAccess: () => true,
10
21
  readAccess: (params, { client, context, visibilityTest }) =>
11
- visibilityTest || access.clientHasAnyAccess(client, params.objectType, params.object),
22
+ visibilityTest || access.clientHasAnyAccess(client, params),
12
23
  updateAccess: (params, { client, context, visibilityTest }) =>
13
- visibilityTest || access.clientHasAdminAccess(client, params.objectType, params.object),
24
+ visibilityTest || access.clientHasAdminAccess(client, params),
14
25
  resetAccess: (params, { client, context, visibilityTest }) =>
15
- visibilityTest || access.clientHasAdminAccess(client, params.objectType, params.object)
26
+ visibilityTest || access.clientHasAdminAccess(client, params)
16
27
  },
17
28
  properties: {
18
- roles: {
19
- type: Array,
20
- of: {
21
- type: String,
22
- validation: ['nonEmpty']
23
- },
24
- validation: ['elementsNonEmpty']
25
- },
29
+ roles: rolesArrayType,
26
30
  lastUpdate: {
27
31
  type: Date
28
32
  }
@@ -34,27 +38,14 @@ const PublicAccess = definition.model({
34
38
  propertyOfAny: {
35
39
  to: 'object',
36
40
  readAccess: (params, { client, context, visibilityTest }) =>
37
- visibilityTest || access.clientHasAnyAccess(client, params.objectType, params.object),
41
+ visibilityTest || access.clientHasAnyAccess(client, params),
38
42
  writeAccess: (params, { client, context, visibilityTest }) =>
39
- visibilityTest || access.clientHasAdminAccess(client, params.objectType, params.object)
43
+ visibilityTest || access.clientHasAdminAccess(client, params)
40
44
  },
41
45
  properties: {
42
- userRoles: {
43
- type: Array,
44
- of: {
45
- type: String,
46
- validation: ['nonEmpty']
47
- },
48
- validation: ['elementsNonEmpty']
49
- },
50
- sessionRoles: {
51
- type: Array,
52
- of: {
53
- type: String,
54
- validation: ['nonEmpty']
55
- },
56
- validation: ['elementsNonEmpty']
57
- },
46
+ userRoles: rolesArrayType,
47
+ sessionRoles: rolesArrayType,
48
+ availableRoles: rolesArrayType,
58
49
  lastUpdate: {
59
50
  type: Date
60
51
  }
@@ -65,22 +56,19 @@ const PublicAccess = definition.model({
65
56
 
66
57
  const AccessRequest = definition.model({
67
58
  name: 'AccessRequest',
68
- sessionOrUserItem: {
69
- },
70
- relatedToAny: {
71
- to: 'object',
59
+ sessionOrUserProperty: {
60
+ extendedWith: ['object'],
61
+ ownerReadAccess: () => true,
62
+ ownerResetAccess: () => true,
72
63
  readAccess: (params, { client, context, visibilityTest }) =>
73
- visibilityTest || access.clientHasAdminAccess(client, params.objectType, params.object)
64
+ visibilityTest || access.clientHasAnyAccess(client, params),
65
+ updateAccess: (params, { client, context, visibilityTest }) =>
66
+ visibilityTest || access.clientHasAdminAccess(client, params),
67
+ resetAccess: (params, { client, context, visibilityTest }) =>
68
+ visibilityTest || access.clientHasAdminAccess(client, params)
74
69
  },
75
70
  properties: {
76
- roles: {
77
- type: Array,
78
- of: {
79
- type: String,
80
- validation: ['nonEmpty']
81
- },
82
- validation: ['elementsNonEmpty']
83
- },
71
+ roles: rolesArrayType,
84
72
  message: {
85
73
  type: String,
86
74
  validation: []
@@ -90,32 +78,33 @@ const AccessRequest = definition.model({
90
78
  }
91
79
  })
92
80
 
81
+ const invitationProperties = {
82
+ roles: rolesArrayType,
83
+ message: {
84
+ type: String,
85
+ validation: []
86
+ }
87
+ }
93
88
 
94
- const AccessInvite = definition.model({
95
- name: 'AccessInvite',
96
- contactOrUserItem: {},
97
- relatedToAny: {
98
- to: 'object',
99
- readAccess: (params, {client, context, visibilityTest}) =>
100
- visibilityTest || access.clientHasAdminAccess(client, params.objectType, params.object)
89
+ const AccessInvitation = definition.model({
90
+ name: 'AccessInvitation',
91
+ contactOrUserProperty: {
92
+ extendedWith: ['object'],
93
+ ownerReadAccess: () => true,
94
+ ownerResetAccess: () => true,
95
+ readAccess: (params, { client, context, visibilityTest }) =>
96
+ visibilityTest || access.clientHasAnyAccess(client, params),
97
+ updateAccess: (params, { client, context, visibilityTest }) =>
98
+ visibilityTest || access.clientHasAdminAccess(client, params),
99
+ resetAccess: (params, { client, context, visibilityTest }) =>
100
+ visibilityTest || access.clientHasAdminAccess(client, params)
101
101
  },
102
102
  properties: {
103
- roles: {
104
- type: Array,
105
- of: {
106
- type: String,
107
- validation: ['nonEmpty']
108
- },
109
- validation: ['elementsNonEmpty']
110
- },
111
- message: {
112
- type: String,
113
- validation: []
114
- }
103
+ ...invitationProperties
115
104
  },
116
105
  indexes: {
117
106
 
118
107
  }
119
108
  })
120
109
 
121
- module.exports = { Access, PublicAccess, AccessRequest, AccessInvite }
110
+ 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.26",
3
+ "version": "0.2.29",
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.3"
25
25
  },
26
- "gitHead": "f3d7b9b6c689b9d87df3cb7f64cd75fc72339b7a"
26
+ "gitHead": "37d229ac05adf5e045ae3dcc826c6945d5dc3670"
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
+ })