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

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