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

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,35 @@
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 } = require('./model.js')
6
7
  const access = require('./access.js')(definition)
7
8
 
9
+ definition.event({
10
+ name: 'userInvited',
11
+ async execute({ user, objectType, object, roles, message }) {
12
+ await AccessInvitation.create({
13
+ id: App.encodeIdentifier(['user_User', user, objectType, object]),
14
+ contactOrUserType: 'user_User', contactOrUser: user,
15
+ objectType, object,
16
+ roles, message
17
+ })
18
+ }
19
+ })
20
+
21
+ definition.event({
22
+ name: 'contactInvited',
23
+ async execute({ contactType, contact, objectType, object, roles, message }) {
24
+ await AccessInvitation.create({
25
+ id: App.encodeIdentifier([contactType, contact, objectType, object]),
26
+ contactOrUserType: contactType, contactOrUser: contact,
27
+ objectType, object,
28
+ roles, message
29
+ })
30
+ }
31
+ })
32
+
8
33
  for(const contactType of config.contactTypes) {
9
34
 
10
35
  const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
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,14 +68,7 @@ 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: []
@@ -105,14 +79,7 @@ const AccessRequest = definition.model({
105
79
  })
106
80
 
107
81
  const invitationProperties = {
108
- roles: {
109
- type: Array,
110
- of: {
111
- type: String,
112
- validation: ['nonEmpty']
113
- },
114
- validation: ['elementsNonEmpty']
115
- },
82
+ roles: rolesArrayType,
116
83
  message: {
117
84
  type: String,
118
85
  validation: []
@@ -140,28 +107,4 @@ const AccessInvitation = definition.model({
140
107
  }
141
108
  })
142
109
 
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 }
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.27",
3
+ "version": "0.2.28",
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.0"
25
25
  },
26
- "gitHead": "9a82ff0e7a7003d5b4e34ef9aef1ad4d7d8605dd"
26
+ "gitHead": "34309fef58572e1a8794b52438430dd421c57d65"
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
+ })