@live-change/access-control-service 0.2.25 → 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
@@ -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,97 @@
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 } = require('./model.js')
7
+ const access = require('./access.js')(definition)
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
+
33
+ for(const contactType of config.contactTypes) {
34
+
35
+ const contactTypeUpperCaseName = contactType[0].toUpperCase() + contactType.slice(1)
36
+
37
+ const contactConfig = (typeof contactType == "string") ? { name: contactType } : contactType
38
+
39
+ const contactTypeName = contactConfig.name
40
+ const contactTypeUName = contactTypeName[0].toUpperCase() + contactTypeName.slice(1)
41
+
42
+ const contactTypeProperties = {
43
+ [contactType]: {
44
+ type: String,
45
+ validation: ['nonEmpty', contactTypeName]
46
+ }
47
+ }
48
+
49
+ definition.action({
50
+ name: 'invite' + contactTypeUpperCaseName,
51
+ waitForEvents: true,
52
+ properties: {
53
+ objectType: {
54
+ type: String,
55
+ validation: ['nonEmpty']
56
+ },
57
+ object: {
58
+ type: String,
59
+ validation: ['nonEmpty']
60
+ },
61
+ ...contactTypeProperties,
62
+ ...invitationProperties
63
+ },
64
+ access: (params, { client, context, visibilityTest }) =>
65
+ visibilityTest || access.clientCanInvite(client, params),
66
+ async execute(params, { client, service }, emit) {
67
+ const { [contactTypeName]: contact } = params
68
+ const { objectType, object } = params
69
+ const invitationData = { }
70
+ for(const propertyName in invitationProperties) invitationData[propertyName] = params[propertyName]
71
+
72
+ const contactData = (await service.trigger({
73
+ type: 'get' + contactTypeUName + 'OrNull',
74
+ [contactType]: contact,
75
+ }))[0]
76
+ if(contactData?.user) { // user exists
77
+ /// TODO: Trigger notification
78
+ emit({
79
+ type: 'userInvited',
80
+ user: contactData.user,
81
+ objectType, object,
82
+ ...invitationData
83
+ })
84
+ } else {
85
+ /// TODO: Send message to contact
86
+ emit({
87
+ type: 'contactInvited',
88
+ contactType: contactTypeName + '_' + contactTypeUName,
89
+ contact,
90
+ objectType, object,
91
+ ...invitationData
92
+ })
93
+ }
94
+ }
95
+ })
96
+
97
+ }
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
- sessionOrUserItem: {
8
- ownerReadAccess: () => true
9
- },
10
- relatedToAny: {
11
- to: 'object',
18
+ sessionOrUserProperty: {
19
+ extendedWith: ['object'],
20
+ ownerReadAccess: () => true,
12
21
  readAccess: (params, { client, context, visibilityTest }) =>
13
- visibilityTest || access.clientHasAnyAccess(client, params.ownerType, params.owner),
14
- writeAccess: (params, { client, context, visibilityTest }) =>
15
- visibilityTest || access.clientHasAdminAccess(client, params.ownerType, params.owner)
22
+ visibilityTest || access.clientHasAnyAccess(client, params),
23
+ updateAccess: (params, { client, context, visibilityTest }) =>
24
+ visibilityTest || access.clientHasAdminAccess(client, params),
25
+ resetAccess: (params, { client, context, visibilityTest }) =>
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.ownerType, params.owner),
41
+ visibilityTest || access.clientHasAnyAccess(client, params),
38
42
  writeAccess: (params, { client, context, visibilityTest }) =>
39
- visibilityTest || access.clientHasAdminAccess(client, params.ownerType, params.owner)
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.ownerType, params.owner)
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,33 +78,33 @@ const AccessRequest = definition.model({
90
78
  }
91
79
  })
92
80
 
93
- /*
94
- const AccessInvite = definition.model({
95
- name: 'AccessInvite',
96
- userOrContactItem: {},
97
- relatedToAny: {
98
- to: 'object',
99
- readAccess: (params, {client, context, visibilityTest}) =>
100
- visibilityTest || access.clientHasAdminAccess(client, params.ownerType, params.owner)
81
+ const invitationProperties = {
82
+ roles: rolesArrayType,
83
+ message: {
84
+ type: String,
85
+ validation: []
86
+ }
87
+ }
88
+
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
- */
121
109
 
122
- module.exports = { Access, PublicAccess, AccessRequest }
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.25",
3
+ "version": "0.2.28",
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.5.27"
24
+ "@live-change/framework": "0.6.0"
25
25
  },
26
- "gitHead": "4ccbe06b5a1ad1c46a907ac079e31a6d72a8591e"
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
+ })