@live-change/user-service 0.2.2

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-2020 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/README.md ADDED
@@ -0,0 +1 @@
1
+ # session-service
@@ -0,0 +1,9 @@
1
+ const App = require('@live-change/framework')
2
+ const app = App.app()
3
+ const definition = require('./definition.js')
4
+
5
+ definition.authenticator({
6
+ async credentialsObservable(credentials) {
7
+
8
+ }
9
+ })
package/definition.js ADDED
@@ -0,0 +1,8 @@
1
+
2
+ const app = require("@live-change/framework").app()
3
+
4
+ const definition = app.createServiceDefinition({
5
+ name: "user"
6
+ })
7
+
8
+ module.exports = definition
package/index.js ADDED
@@ -0,0 +1,99 @@
1
+ const app = require("@live-change/framework").app()
2
+ const definition = require('./definition.js')
3
+
4
+ const { User, AutheticatedUser } = require('./model.js')
5
+ require('./authenticator.js')
6
+ require('./userProperty.js')
7
+ require('./userItem.js')
8
+
9
+ const Session = definition.foreignModel('session', 'Session')
10
+
11
+ definition.trigger({
12
+ name: 'signUp',
13
+ properties: {
14
+ user: {
15
+ type: User
16
+ },
17
+ },
18
+ async execute({ user }, { client, service }, emit) {
19
+ if(!user) {
20
+ user = app.generateUid()
21
+ }
22
+ emit({
23
+ type: "created",
24
+ user
25
+ })
26
+ return user
27
+ }
28
+ })
29
+
30
+ definition.trigger({
31
+ name: 'signIn',
32
+ properties: {
33
+ user: {
34
+ type: User,
35
+ validation: ['nonEmpty']
36
+ },
37
+ session: {
38
+ type: Session,
39
+ validation: ['nonEmpty']
40
+ }
41
+ },
42
+ async execute({ user, session }, { client, service }, emit) {
43
+ emit({
44
+ type: "signedIn",
45
+ user, session
46
+ })
47
+ }
48
+ })
49
+
50
+ definition.action({
51
+ name: 'signOut',
52
+ properties: {
53
+ user: {
54
+ type: User,
55
+ validation: ['nonEmpty']
56
+ },
57
+ session: {
58
+ type: Session,
59
+ validation: ['nonEmpty']
60
+ }
61
+ },
62
+ async execute({ }, { client, service }, emit) {
63
+ if(!client.user) throw "notSignedIn"
64
+ emit({
65
+ type: "signedOut",
66
+ user: client.user,
67
+ session: client.session
68
+ })
69
+ }
70
+ })
71
+
72
+ definition.trigger({
73
+ name: 'signUpAndSignIn',
74
+ properties: {
75
+ user: {
76
+ type: User
77
+ },
78
+ session: {
79
+ type: Session,
80
+ validation: ['nonEmpty']
81
+ }
82
+ },
83
+ async execute({ user, session }, { client, service }, emit) {
84
+ if(!user) {
85
+ user = app.generateUid()
86
+ }
87
+ emit([{
88
+ type: "created",
89
+ user
90
+ },{
91
+ type: "signedIn",
92
+ user, session
93
+ }])
94
+ return user
95
+ }
96
+ })
97
+
98
+
99
+ module.exports = definition
package/model.js ADDED
@@ -0,0 +1,75 @@
1
+ const definition = require('./definition.js')
2
+
3
+ const User = definition.model({
4
+ name: "User",
5
+ properties: {
6
+ roles: {
7
+ type: Array,
8
+ of: {
9
+ type: String
10
+ }
11
+ }
12
+ },
13
+ indexes: {
14
+ }
15
+ })
16
+
17
+ definition.event({
18
+ name: "created",
19
+ properties: {
20
+ user: {
21
+ type: User
22
+ }
23
+ },
24
+ async execute({ user }) {
25
+ await User.create({
26
+ id: user
27
+ })
28
+ }
29
+ })
30
+
31
+ const Session = definition.foreignModel('session', 'Session')
32
+
33
+ const AuthenticatedUser = definition.model({
34
+ name: "LoggedInUser",
35
+ sessionProperty: {
36
+ },
37
+ userItem: {
38
+ userReadAccess: () => true
39
+ }
40
+ })
41
+
42
+ definition.event({
43
+ name: "signedIn",
44
+ properties: {
45
+ user: {
46
+ type: User
47
+ },
48
+ session: {
49
+ type: Session
50
+ }
51
+ },
52
+ async execute({ user, session }) {
53
+ await AuthenticatedUser.create({
54
+ id: session,
55
+ user, session
56
+ })
57
+ }
58
+ })
59
+
60
+ definition.event({
61
+ name: "signedOut",
62
+ properties: {
63
+ user: {
64
+ type: User
65
+ },
66
+ session: {
67
+ type: Session
68
+ }
69
+ },
70
+ async execute({ session }) {
71
+ await AuthenticatedUser.delete(session)
72
+ }
73
+ })
74
+
75
+ module.exports = { User, AuthenticatedUser }
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@live-change/user-service",
3
+ "version": "0.2.2",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "NODE_ENV=test tape tests/*"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/live-change/live-change-services.git"
12
+ },
13
+ "license": "MIT",
14
+ "bugs": {
15
+ "url": "https://github.com/live-change/live-change-services/issues"
16
+ },
17
+ "homepage": "https://github.com/live-change/live-change-services",
18
+ "author": {
19
+ "email": "michal@laszczewski.pl",
20
+ "name": "Michał Łaszczewski",
21
+ "url": "https://www.viamage.com/"
22
+ },
23
+ "dependencies": {
24
+ "@live-change/framework": "^0.5.7",
25
+ "@live-change/relations-plugin": "^0.1.10"
26
+ },
27
+ "gitHead": "7691357c7569a18373668691eb6a81a9e161194d"
28
+ }
package/userItem.js ADDED
@@ -0,0 +1,160 @@
1
+ const definition = require("./definition.js")
2
+ const App = require("@live-change/framework")
3
+ const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
+ const { User } = require("./model.js")
5
+
6
+ definition.processor(function(service, app) {
7
+
8
+ for(let modelName in service.models) {
9
+ const model = service.models[modelName]
10
+ if(model.userItem) {
11
+ console.trace("PROCESS MODEL " + modelName)
12
+ if (model.properties.user) throw new Error('user property already exists!!!')
13
+ const originalModelProperties = {...model.properties}
14
+ const modelProperties = Object.keys(model.properties)
15
+ const defaults = App.utils.generateDefault(model.properties)
16
+ const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
17
+
18
+ function modelRuntime() {
19
+ return service._runtime.models[modelName]
20
+ }
21
+
22
+ const config = model.userItem
23
+ const writeableProperties = modelProperties || config.writableProperties
24
+
25
+ console.log("USER ITEM", model)
26
+
27
+ model.itemOf = {
28
+ what: User,
29
+ ...config
30
+ }
31
+
32
+ if(config.userReadAccess) {
33
+ const viewName = 'myUser' + modelName + 's'
34
+ service.views[viewName] = new ViewDefinition({
35
+ name: viewName,
36
+ access: config.userReadAccess,
37
+ properties: App.rangeProperties,
38
+ daoPath(range, { client, context }) {
39
+ const path = modelRuntime().indexRangePath('byUser', [client.user], range )
40
+ return path
41
+ }
42
+ })
43
+ for(const sortField of config.sortBy || []) {
44
+ const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
45
+ const viewName = 'myUser' + modelName + 'sBy' + sortFieldUc
46
+ service.views[viewName] = new ViewDefinition({
47
+ name: viewName,
48
+ access: config.readAccess,
49
+ properties: App.rangeProperties,
50
+ daoPath(range, { client, context }) {
51
+ return modelRuntime().sortedIndexRangePath('byUser' + sortFieldUc, [client.user], range )
52
+ }
53
+ })
54
+ }
55
+ }
56
+
57
+ if(config.userCreateAccess || config.userWriteAccess) {
58
+ const eventName = 'userOwned' + modelName + 'Created'
59
+ const actionName = 'createMyUser' + modelName
60
+ service.actions[actionName] = new ActionDefinition({
61
+ name: actionName,
62
+ access: config.userCreateAccess || config.userWriteAccess,
63
+ properties: {
64
+ ...originalModelProperties,
65
+ [modelPropertyName]: {
66
+ type: model,
67
+ validation: ['localId']
68
+ }
69
+ },
70
+ //queuedBy: (command) => command.client.user,
71
+ waitForEvents: true,
72
+ async execute(properties, { client, service }, emit) {
73
+ const id = properties[modelPropertyName] || app.generateUid()
74
+ const entity = await modelRuntime().get(id)
75
+ if(entity) throw 'exists'
76
+ emit({
77
+ type: eventName,
78
+ [modelPropertyName]: id,
79
+ identifiers: {
80
+ user: client.user,
81
+ },
82
+ data: properties
83
+ })
84
+ return id
85
+ }
86
+ })
87
+ }
88
+ if(config.userUpdateAccess || config.userWriteAccess) {
89
+ const eventName = 'userOwned' + modelName + 'Updated'
90
+ const actionName = 'updateMyUser' + modelName
91
+ service.actions[actionName] = new ActionDefinition({
92
+ name: actionName,
93
+ access: config.userUpdateAccess || config.userWriteAccess,
94
+ properties: {
95
+ ...originalModelProperties,
96
+ [modelPropertyName]: {
97
+ type: model,
98
+ validation: ['nonEmpty']
99
+ }
100
+ },
101
+ skipValidation: true,
102
+ queuedBy: (command) => command.client.user,
103
+ waitForEvents: true,
104
+ async execute(properties, { client, service }, emit) {
105
+ const entity = await modelRuntime().get(properties[modelPropertyName])
106
+ if(!entity) throw 'not_found'
107
+ if(entity.user != client.user) throw 'not_authorized'
108
+ let updateObject = {}
109
+ for(const propertyName of writeableProperties) {
110
+ if(properties.hasOwnProperty(propertyName)) {
111
+ updateObject[propertyName] = properties[propertyName]
112
+ }
113
+ }
114
+ const merged = App.utils.mergeDeep({}, entity, updateObject)
115
+ await App.validation.validate(merged, validators, { source: action, action, service, app, client })
116
+ emit({
117
+ type: eventName,
118
+ [modelPropertyName]: entity.id,
119
+ identifiers: {
120
+ user: client.user,
121
+ },
122
+ data: properties
123
+ })
124
+ }
125
+ })
126
+ const action = service.actions[actionName]
127
+ const validators = App.validation.getValidators(action, service, action)
128
+ }
129
+ if(config.userDeleteAccess || config.userWriteAccess) {
130
+ const eventName = 'userOwned' + modelName + 'Deleted'
131
+ const actionName = 'deleteMyUser' + modelName
132
+ service.actions[actionName] = new ActionDefinition({
133
+ name: actionName,
134
+ access: config.userDeleteAccess || config.userWriteAccess,
135
+ properties: {
136
+ [modelPropertyName]: {
137
+ type: model,
138
+ validation: ['nonEmpty']
139
+ }
140
+ },
141
+ queuedBy: (command) => command.client.user,
142
+ waitForEvents: true,
143
+ async execute(properties, { client, service }, emit) {
144
+ const entity = await modelRuntime().get(properties[modelPropertyName])
145
+ if(!entity) throw 'not_found'
146
+ if(entity.user != client.user) throw 'not_authorized'
147
+ emit({
148
+ type: eventName,
149
+ [modelPropertyName]: entity.id,
150
+ identifiers: {
151
+ user: client.user
152
+ }
153
+ })
154
+ }
155
+ })
156
+ }
157
+ }
158
+ }
159
+
160
+ })
@@ -0,0 +1,136 @@
1
+ const definition = require("./definition.js")
2
+ const App = require("@live-change/framework")
3
+ const { PropertyDefinition, ViewDefinition, IndexDefinition, ActionDefinition, EventDefinition } = App
4
+ const { User } = require("./model.js")
5
+
6
+ definition.processor(function(service, app) {
7
+
8
+ for(let modelName in service.models) {
9
+ const model = service.models[modelName]
10
+
11
+ if(model.userProperty) {
12
+ console.log("MODEL " + modelName + " IS USER PROPERTY, CONFIG:", model.userProperty)
13
+ if (model.properties.user) throw new Error('user property already exists!!!')
14
+
15
+ const originalModelProperties = { ...model.properties }
16
+ const modelProperties = Object.keys(model.properties)
17
+ const defaults = App.utils.generateDefault(model.properties)
18
+
19
+ function modelRuntime() {
20
+ return service._runtime.models[modelName]
21
+ }
22
+
23
+ const config = model.userProperty
24
+ const writeableProperties = modelProperties || config.writableProperties
25
+
26
+ model.propertyOf = {
27
+ what: User,
28
+ ...config
29
+ }
30
+
31
+ if(config.userReadAccess) {
32
+ const viewName = 'myUser' + modelName
33
+ service.views[viewName] = new ViewDefinition({
34
+ name: viewName,
35
+ access: config.userReadAccess,
36
+ daoPath(params, { client, context }) {
37
+ return modelRuntime().path(client.user)
38
+ }
39
+ })
40
+ }
41
+
42
+ if(config.userSetAccess || config.userWriteAccess) {
43
+ const eventName = 'userOwned' + modelName + 'Set'
44
+ const actionName = 'setMyUser' + modelName
45
+ service.actions[actionName] = new ActionDefinition({
46
+ name: actionName,
47
+ properties: {
48
+ ...originalModelProperties
49
+ },
50
+ access: config.userSetAccess || config.userWriteAccess,
51
+ skipValidation: true,
52
+ queuedBy: (command) => command.client.user,
53
+ waitForEvents: true,
54
+ async execute(properties, {client, service}, emit) {
55
+ let newObject = {}
56
+ for(const propertyName of writeableProperties) {
57
+ if(properties.hasOwnProperty(propertyName)) {
58
+ newObject[propertyName] = properties[propertyName]
59
+ }
60
+ }
61
+ const data = App.utils.mergeDeep({}, defaults, newObject)
62
+ await App.validation.validate(data, validators, { source: action, action, service, app, client })
63
+ emit({
64
+ type: eventName,
65
+ identifiers: {
66
+ user: client.user
67
+ },
68
+ data
69
+ })
70
+ }
71
+ })
72
+ const action = service.actions[actionName]
73
+ const validators = App.validation.getValidators(action, service, action)
74
+ }
75
+
76
+ if(config.userUpdateAccess || config.userWriteAccess) {
77
+ const eventName = 'userOwned' + modelName + 'Updated'
78
+ const actionName = 'updateMyuser' + modelName
79
+ service.actions[actionName] = new ActionDefinition({
80
+ name: actionName,
81
+ properties: {
82
+ ...originalModelProperties
83
+ },
84
+ access: config.userUpdateAccess || config.userWriteAccess,
85
+ skipValidation: true,
86
+ queuedBy: (command) => command.client.user,
87
+ waitForEvents: true,
88
+ async execute(properties, { client, service }, emit) {
89
+ const entity = await modelRuntime().get(client.user)
90
+ if(!entity) throw 'not_found'
91
+ let updateObject = {}
92
+ for(const propertyName of writeableProperties) {
93
+ if(properties.hasOwnProperty(propertyName)) {
94
+ updateObject[propertyName] = properties[propertyName]
95
+ }
96
+ }
97
+ const merged = App.utils.mergeDeep({}, entity, updateObject)
98
+ await App.validation.validate(merged, validators, { source: action, action, service, app, client })
99
+ emit({
100
+ type: eventName,
101
+ identifiers: {
102
+ user: client.user
103
+ },
104
+ data: properties || {}
105
+ })
106
+ }
107
+ })
108
+ const action = service.actions[actionName]
109
+ const validators = App.validation.getValidators(action, service, action)
110
+ }
111
+
112
+ if(config.userResetAccess || config.userWriteAccess) {
113
+ const eventName = 'userOwned' + modelName + 'Reset'
114
+ const actionName = 'resetMyuser' + modelName
115
+ service.actions[actionName] = new ActionDefinition({
116
+ name: actionName,
117
+ access: config.userResetAccess || config.userWriteAccess,
118
+ queuedBy: (command) => command.client.user,
119
+ waitForEvents: true,
120
+ async execute(properties, {client, service}, emit) {
121
+ const entity = await modelRuntime().indexObjectGet('byuser', client.user)
122
+ if (!entity) throw 'not_found'
123
+ emit({
124
+ type: eventName,
125
+ identifiers: {
126
+ user: client.user
127
+ }
128
+ })
129
+ }
130
+ })
131
+ }
132
+
133
+ }
134
+ }
135
+
136
+ })