@live-change/session-service 0.1.7 → 0.1.12
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/authenticator.js +26 -23
- package/definition.js +4 -1
- package/index.js +0 -46
- package/model.js +1 -119
- package/package.json +3 -2
- package/sessionItem.js +38 -89
- package/sessionProperty.js +57 -60
package/authenticator.js
CHANGED
|
@@ -3,30 +3,33 @@ const app = App.app()
|
|
|
3
3
|
const definition = require('./definition.js')
|
|
4
4
|
const Session = require('./model.js')
|
|
5
5
|
const { createHmac } = require('crypto')
|
|
6
|
+
const config = definition.config
|
|
6
7
|
|
|
7
|
-
definition.authenticator(
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
if(
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
8
|
+
definition.authenticator({
|
|
9
|
+
async prepareCredentials(credentials) {
|
|
10
|
+
const sessionKey = credentials.sessionKey
|
|
11
|
+
if(!sessionKey) throw new Error("sessionKey required!")
|
|
12
|
+
const sessions = await app.dao.get(
|
|
13
|
+
['database', 'indexRange', app.databaseName, Session.tableName + '_byKey', {
|
|
14
|
+
gt: `"${sessionKey}"_`,
|
|
15
|
+
lt: `"${sessionKey}"_\xFF`
|
|
16
|
+
}])
|
|
17
|
+
//console.log("FOUND SESSIONS", sessions)
|
|
18
|
+
let session = sessions[0]?.to
|
|
19
|
+
if(!session) {
|
|
20
|
+
if(config.createSessionOnUpdate) {
|
|
21
|
+
session = createHmac('sha256', config.sessionHmacSecret || 'secret')
|
|
22
|
+
.update(credentials.sessionKey)
|
|
23
|
+
.digest('base64').slice(0, 32)
|
|
24
|
+
} else {
|
|
25
|
+
const createResult = await app.triggerService(definition.name, {
|
|
26
|
+
type: "createSessionKeyIfNotExists",
|
|
27
|
+
sessionKey
|
|
28
|
+
})
|
|
29
|
+
//console.log("CREATE SESSION RESULT", createResult)
|
|
30
|
+
session = createResult.session
|
|
31
|
+
}
|
|
29
32
|
}
|
|
33
|
+
credentials.session = session
|
|
30
34
|
}
|
|
31
|
-
credentials.session = session
|
|
32
35
|
})
|
package/definition.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
const App = require("@live-change/framework")
|
|
2
2
|
const app = App.app()
|
|
3
3
|
|
|
4
|
+
const relationsPlugin = require('@live-change/relations-plugin')
|
|
5
|
+
|
|
4
6
|
const definition = app.createServiceDefinition({
|
|
5
|
-
name: "session"
|
|
7
|
+
name: "session",
|
|
8
|
+
use: [ relationsPlugin ]
|
|
6
9
|
})
|
|
7
10
|
|
|
8
11
|
module.exports = definition
|
package/index.js
CHANGED
|
@@ -3,10 +3,6 @@ const app = App.app()
|
|
|
3
3
|
const definition = require('./definition.js')
|
|
4
4
|
const Session = require('./model.js')
|
|
5
5
|
|
|
6
|
-
|
|
7
|
-
const User = definition.foreignModel('user', 'User')
|
|
8
|
-
|
|
9
|
-
|
|
10
6
|
definition.view({
|
|
11
7
|
name: 'currentSession',
|
|
12
8
|
properties: {},
|
|
@@ -20,7 +16,6 @@ definition.view({
|
|
|
20
16
|
async (input, output, { session, tableName }) => {
|
|
21
17
|
const mapper = (obj) => (obj || {
|
|
22
18
|
id: session,
|
|
23
|
-
user: null,
|
|
24
19
|
roles: []
|
|
25
20
|
})
|
|
26
21
|
let storedObj = undefined
|
|
@@ -81,50 +76,9 @@ definition.trigger({
|
|
|
81
76
|
}
|
|
82
77
|
})
|
|
83
78
|
|
|
84
|
-
definition.action({
|
|
85
|
-
name: "logout",
|
|
86
|
-
properties: {
|
|
87
|
-
},
|
|
88
|
-
async execute({ session }, { client, service }, emit) {
|
|
89
|
-
if(!session) session = client.session
|
|
90
|
-
if(session != client.session) throw new Error("Wrong session id")
|
|
91
|
-
const sessionRow = await Session.get(session)
|
|
92
|
-
if(!sessionRow) throw 'notFound'
|
|
93
|
-
if(!sessionRow.user) throw "loggedOut"
|
|
94
|
-
emit({
|
|
95
|
-
type: "loggedOut",
|
|
96
|
-
session
|
|
97
|
-
})
|
|
98
|
-
await service.trigger({
|
|
99
|
-
type: "OnLogout",
|
|
100
|
-
user: sessionRow.user,
|
|
101
|
-
session: client.session
|
|
102
|
-
})
|
|
103
|
-
return 'loggedOut'
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
|
|
107
|
-
definition.trigger({
|
|
108
|
-
name: "UserDeleted",
|
|
109
|
-
properties: {
|
|
110
|
-
user: {
|
|
111
|
-
type: User,
|
|
112
|
-
idOnly: true
|
|
113
|
-
}
|
|
114
|
-
},
|
|
115
|
-
async execute({ user }, context, emit) {
|
|
116
|
-
emit([{
|
|
117
|
-
type: "UserDeleted",
|
|
118
|
-
user
|
|
119
|
-
}])
|
|
120
|
-
}
|
|
121
|
-
})
|
|
122
|
-
|
|
123
|
-
|
|
124
79
|
require('./authenticator.js')
|
|
125
80
|
require('./localIdValidator.js')
|
|
126
81
|
require('./sessionProperty.js')
|
|
127
82
|
require('./sessionItem.js')
|
|
128
83
|
|
|
129
|
-
|
|
130
84
|
module.exports = definition
|
package/model.js
CHANGED
|
@@ -1,29 +1,15 @@
|
|
|
1
1
|
const definition = require("./definition.js")
|
|
2
2
|
|
|
3
|
-
const User = definition.foreignModel('user', 'User')
|
|
4
|
-
|
|
5
3
|
const Session = definition.model({
|
|
6
4
|
name: "Session",
|
|
7
5
|
properties: {
|
|
8
6
|
key: {
|
|
9
7
|
type: String
|
|
10
|
-
},
|
|
11
|
-
user: {
|
|
12
|
-
type: User
|
|
13
|
-
},
|
|
14
|
-
roles: {
|
|
15
|
-
type: Array,
|
|
16
|
-
of: {
|
|
17
|
-
type: String
|
|
18
|
-
}
|
|
19
8
|
}
|
|
20
9
|
},
|
|
21
10
|
indexes: {
|
|
22
11
|
byKey: {
|
|
23
12
|
property: 'key'
|
|
24
|
-
},
|
|
25
|
-
byUser: {
|
|
26
|
-
property: "user"
|
|
27
13
|
}
|
|
28
14
|
}
|
|
29
15
|
})
|
|
@@ -39,114 +25,10 @@ definition.event({
|
|
|
39
25
|
}
|
|
40
26
|
},
|
|
41
27
|
async execute({ session, key }) {
|
|
42
|
-
console.log("SESSION CREATING!", session, "AT", (new Date()).toISOString())
|
|
43
28
|
await Session.create({
|
|
44
29
|
id: session,
|
|
45
|
-
key: key
|
|
46
|
-
user: null,
|
|
47
|
-
roles: []
|
|
30
|
+
key: key
|
|
48
31
|
})
|
|
49
|
-
console.log("SESSION CREATED!", session, "AT", (new Date()).toISOString())
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
definition.event({
|
|
54
|
-
name: "loggedIn",
|
|
55
|
-
properties: {
|
|
56
|
-
session: {
|
|
57
|
-
type: Session
|
|
58
|
-
},
|
|
59
|
-
user: {
|
|
60
|
-
type: User
|
|
61
|
-
},
|
|
62
|
-
roles: {
|
|
63
|
-
type: Array,
|
|
64
|
-
of: {
|
|
65
|
-
type: String
|
|
66
|
-
}
|
|
67
|
-
},
|
|
68
|
-
expire: {
|
|
69
|
-
type: Date
|
|
70
|
-
}
|
|
71
|
-
},
|
|
72
|
-
async execute({ session, user, roles, expire, language, timezone }) {
|
|
73
|
-
console.log("SESSION UPDATE", session, { user, roles, expire, language, timezone })
|
|
74
|
-
await Session.update(session, { user, roles, expire, language, timezone })
|
|
75
|
-
}
|
|
76
|
-
})
|
|
77
|
-
|
|
78
|
-
definition.event({
|
|
79
|
-
name: "loggedOut",
|
|
80
|
-
properties: {
|
|
81
|
-
session: {
|
|
82
|
-
type: Session
|
|
83
|
-
}
|
|
84
|
-
},
|
|
85
|
-
async execute({ session }) {
|
|
86
|
-
await Session.update(session, [
|
|
87
|
-
{ op: 'reverseMerge', value: { id: session } },
|
|
88
|
-
{ op: 'merge', value: { user: null, roles: [] } }
|
|
89
|
-
])
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
|
|
93
|
-
definition.event({
|
|
94
|
-
name: "UserDeleted",
|
|
95
|
-
properties: {
|
|
96
|
-
user: {
|
|
97
|
-
type: User
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
async execute({ user }) {
|
|
101
|
-
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
102
|
-
async (input, output, { table, index, user }) => {
|
|
103
|
-
const prefix = `"${user}"_`
|
|
104
|
-
await (await input.index(index)).range({
|
|
105
|
-
gte: prefix,
|
|
106
|
-
lte: prefix+"\xFF\xFF\xFF\xFF"
|
|
107
|
-
}).onChange((ind, oldInd) => {
|
|
108
|
-
if(ind && ind.to) {
|
|
109
|
-
output.table(table).update(ind.to, [
|
|
110
|
-
{ op: 'reverseMerge', value: { id: ind.to } },
|
|
111
|
-
{ op: 'merge', value: { user: null, roles: [], expire: null } }
|
|
112
|
-
])
|
|
113
|
-
}
|
|
114
|
-
})
|
|
115
|
-
}
|
|
116
|
-
})`, { table: Session.tableName, index: Session.tableName + '_byUser', user })
|
|
117
|
-
}
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
definition.event({
|
|
121
|
-
name: "rolesUpdated",
|
|
122
|
-
properties: {
|
|
123
|
-
user: {
|
|
124
|
-
type: User
|
|
125
|
-
},
|
|
126
|
-
roles: {
|
|
127
|
-
type: Array,
|
|
128
|
-
of: {
|
|
129
|
-
type: String
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
},
|
|
133
|
-
async execute({ user, roles }) {
|
|
134
|
-
await app.dao.request(['database', 'query'], app.databaseName, `(${
|
|
135
|
-
async (input, output, { table, index, user, roles }) => {
|
|
136
|
-
const prefix = `"${user}"_`
|
|
137
|
-
await (await input.index(index)).range({
|
|
138
|
-
gte: prefix,
|
|
139
|
-
lte: prefix+"\xFF\xFF\xFF\xFF"
|
|
140
|
-
}).onChange((ind, oldInd) => {
|
|
141
|
-
if(ind && ind.to) {
|
|
142
|
-
output.table(table).update(ind.to, [
|
|
143
|
-
{ op: 'reverseMerge', value: { id: session } },
|
|
144
|
-
{ op: 'merge', value: { roles } }
|
|
145
|
-
])
|
|
146
|
-
}
|
|
147
|
-
})
|
|
148
|
-
}
|
|
149
|
-
})`, { table: Session.tableName, index: Session.tableName + '_byUser', user, roles })
|
|
150
32
|
}
|
|
151
33
|
})
|
|
152
34
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/session-service",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
"url": "https://www.viamage.com/"
|
|
22
22
|
},
|
|
23
23
|
"dependencies": {
|
|
24
|
-
"@live-change/framework": "^0.5.7"
|
|
24
|
+
"@live-change/framework": "^0.5.7",
|
|
25
|
+
"@live-change/relations-plugin": "^0.1.10"
|
|
25
26
|
}
|
|
26
27
|
}
|
package/sessionItem.js
CHANGED
|
@@ -12,7 +12,7 @@ definition.processor(function(service, app) {
|
|
|
12
12
|
if (model.properties.session) throw new Error('session property already exists!!!')
|
|
13
13
|
const originalModelProperties = {...model.properties}
|
|
14
14
|
const modelProperties = Object.keys(model.properties)
|
|
15
|
-
const defaults = App.utils.generateDefault(
|
|
15
|
+
const defaults = App.utils.generateDefault(model.properties)
|
|
16
16
|
const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
|
|
17
17
|
|
|
18
18
|
function modelRuntime() {
|
|
@@ -22,28 +22,18 @@ definition.processor(function(service, app) {
|
|
|
22
22
|
const config = model.sessionItem
|
|
23
23
|
const writeableProperties = modelProperties || config.writableProperties
|
|
24
24
|
|
|
25
|
-
console.log("
|
|
25
|
+
console.log("SESSION ITEM", model)
|
|
26
26
|
|
|
27
|
-
model.
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
})
|
|
31
|
-
if(!model.indexes) model.indexes = {}
|
|
32
|
-
model.indexes.bySession = new IndexDefinition({
|
|
33
|
-
property: 'session'
|
|
34
|
-
})
|
|
35
|
-
for(const sortField of config.sortBy) {
|
|
36
|
-
const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
|
|
37
|
-
model.indexes['bySession' + sortFieldUc] = new IndexDefinition({
|
|
38
|
-
property: ['session', sortField]
|
|
39
|
-
})
|
|
27
|
+
model.itemOf = {
|
|
28
|
+
what: Session,
|
|
29
|
+
...config
|
|
40
30
|
}
|
|
41
31
|
|
|
42
|
-
if(config.
|
|
43
|
-
const viewName = '
|
|
32
|
+
if(config.sessionReadAccess) {
|
|
33
|
+
const viewName = 'mySession' + modelName + 's'
|
|
44
34
|
service.views[viewName] = new ViewDefinition({
|
|
45
35
|
name: viewName,
|
|
46
|
-
access: config.
|
|
36
|
+
access: config.sessionReadAccess,
|
|
47
37
|
properties: App.rangeProperties,
|
|
48
38
|
daoPath(range, { client, context }) {
|
|
49
39
|
const path = modelRuntime().indexRangePath('bySession', [client.session], range )
|
|
@@ -52,7 +42,7 @@ definition.processor(function(service, app) {
|
|
|
52
42
|
})
|
|
53
43
|
for(const sortField of config.sortBy) {
|
|
54
44
|
const sortFieldUc = sortField.slice(0, 1).toUpperCase() + sortField.slice(1)
|
|
55
|
-
const viewName = '
|
|
45
|
+
const viewName = 'mySession' + modelName + 'sBy' + sortFieldUc
|
|
56
46
|
service.views[viewName] = new ViewDefinition({
|
|
57
47
|
name: viewName,
|
|
58
48
|
access: config.readAccess,
|
|
@@ -63,45 +53,13 @@ definition.processor(function(service, app) {
|
|
|
63
53
|
})
|
|
64
54
|
}
|
|
65
55
|
}
|
|
66
|
-
if(config.publicAccess) {
|
|
67
|
-
const viewName = 'publicSession' + modelName + 's'
|
|
68
|
-
service.views[viewName] = new ViewDefinition({
|
|
69
|
-
name: viewName,
|
|
70
|
-
access: config.publicAccess,
|
|
71
|
-
properties: App.rangeProperties,
|
|
72
|
-
daoPath(range, { client, context }) {
|
|
73
|
-
return modelRuntime().sortedIndexRangePath('bySession', [range.session], { ...range, session: undefined } )
|
|
74
|
-
}
|
|
75
|
-
})
|
|
76
|
-
for(const sorfField of config.sortBy) {
|
|
77
|
-
const sortFieldUc = sorfField.slice(0, 1).toUpperCase() + sortField.slice(1)
|
|
78
|
-
const viewName = 'publicSession' + modelName + 'sBy' + sortFieldUc
|
|
79
|
-
service.views[viewName] = new ViewDefinition({
|
|
80
|
-
name: viewName,
|
|
81
|
-
access: config.publicAccess,
|
|
82
|
-
properties: App.rangeProperties,
|
|
83
|
-
daoPath(range, { client, context }) {
|
|
84
|
-
return modelRuntime().sortedIndexRangePath('bySession' + sortFieldUc, [client.session], range )
|
|
85
|
-
}
|
|
86
|
-
})
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
56
|
|
|
90
|
-
if(config.
|
|
91
|
-
const eventName = '
|
|
92
|
-
|
|
93
|
-
name: eventName,
|
|
94
|
-
execute(properties) {
|
|
95
|
-
const data = properties.data
|
|
96
|
-
const session = properties.session
|
|
97
|
-
const id = properties[modelPropertyName]
|
|
98
|
-
return modelRuntime().create({ ...data, session, id })
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
const actionName = 'createSession' + modelName
|
|
57
|
+
if(config.sessionCreateAccess || config.sessionWriteAccess) {
|
|
58
|
+
const eventName = 'sessionOwned' + modelName + 'Created'
|
|
59
|
+
const actionName = 'createMySession' + modelName
|
|
102
60
|
service.actions[actionName] = new ActionDefinition({
|
|
103
61
|
name: actionName,
|
|
104
|
-
access: config.
|
|
62
|
+
access: config.sessionCreateAccess || config.sessionWriteAccess,
|
|
105
63
|
properties: {
|
|
106
64
|
...originalModelProperties,
|
|
107
65
|
[modelPropertyName]: {
|
|
@@ -109,35 +67,30 @@ definition.processor(function(service, app) {
|
|
|
109
67
|
validation: ['localId']
|
|
110
68
|
}
|
|
111
69
|
},
|
|
112
|
-
queuedBy: (command) => command.client.session,
|
|
70
|
+
//queuedBy: (command) => command.client.session,
|
|
113
71
|
waitForEvents: true,
|
|
114
72
|
async execute(properties, { client, service }, emit) {
|
|
115
73
|
const id = properties[modelPropertyName] || app.generateUid()
|
|
74
|
+
const entity = await modelRuntime().get(id)
|
|
75
|
+
if(entity) throw 'exists'
|
|
116
76
|
emit({
|
|
117
77
|
type: eventName,
|
|
118
78
|
[modelPropertyName]: id,
|
|
119
|
-
|
|
79
|
+
identifiers: {
|
|
80
|
+
session: client.session,
|
|
81
|
+
},
|
|
120
82
|
data: properties
|
|
121
83
|
})
|
|
122
84
|
return id
|
|
123
85
|
}
|
|
124
86
|
})
|
|
125
87
|
}
|
|
126
|
-
if(config.
|
|
127
|
-
const eventName = '
|
|
128
|
-
|
|
129
|
-
name: eventName,
|
|
130
|
-
execute(properties) {
|
|
131
|
-
const data = properties.data
|
|
132
|
-
const id = properties[modelPropertyName]
|
|
133
|
-
const session = properties.session
|
|
134
|
-
return modelRuntime().update(id, { ...data, id, session })
|
|
135
|
-
}
|
|
136
|
-
})
|
|
137
|
-
const actionName = 'updateSession' + modelName
|
|
88
|
+
if(config.sessionUpdateAccess || config.sessionWriteAccess) {
|
|
89
|
+
const eventName = 'sessionOwned' + modelName + 'Updated'
|
|
90
|
+
const actionName = 'updateMySession' + modelName
|
|
138
91
|
service.actions[actionName] = new ActionDefinition({
|
|
139
92
|
name: actionName,
|
|
140
|
-
access: config.
|
|
93
|
+
access: config.sessionUpdateAccess || config.sessionWriteAccess,
|
|
141
94
|
properties: {
|
|
142
95
|
...originalModelProperties,
|
|
143
96
|
[modelPropertyName]: {
|
|
@@ -150,8 +103,8 @@ definition.processor(function(service, app) {
|
|
|
150
103
|
waitForEvents: true,
|
|
151
104
|
async execute(properties, { client, service }, emit) {
|
|
152
105
|
const entity = await modelRuntime().get(properties[modelPropertyName])
|
|
153
|
-
if(!entity) throw
|
|
154
|
-
if(entity.session != client.session) throw
|
|
106
|
+
if(!entity) throw 'not_found'
|
|
107
|
+
if(entity.session != client.session) throw 'not_authorized'
|
|
155
108
|
let updateObject = {}
|
|
156
109
|
for(const propertyName of writeableProperties) {
|
|
157
110
|
if(properties.hasOwnProperty(propertyName)) {
|
|
@@ -159,12 +112,13 @@ definition.processor(function(service, app) {
|
|
|
159
112
|
}
|
|
160
113
|
}
|
|
161
114
|
const merged = App.utils.mergeDeep({}, entity, updateObject)
|
|
162
|
-
console.log("VALIDATE INTERNAL!!!!", merged)
|
|
163
115
|
await App.validation.validate(merged, validators, { source: action, action, service, app, client })
|
|
164
116
|
emit({
|
|
165
117
|
type: eventName,
|
|
166
118
|
[modelPropertyName]: entity.id,
|
|
167
|
-
|
|
119
|
+
identifiers: {
|
|
120
|
+
session: client.session,
|
|
121
|
+
},
|
|
168
122
|
data: properties
|
|
169
123
|
})
|
|
170
124
|
}
|
|
@@ -172,19 +126,12 @@ definition.processor(function(service, app) {
|
|
|
172
126
|
const action = service.actions[actionName]
|
|
173
127
|
const validators = App.validation.getValidators(action, service, action)
|
|
174
128
|
}
|
|
175
|
-
if(config.
|
|
176
|
-
const eventName = '
|
|
177
|
-
|
|
178
|
-
name: eventName,
|
|
179
|
-
execute(properties) {
|
|
180
|
-
const id = properties[modelPropertyName]
|
|
181
|
-
return modelRuntime().delete(id)
|
|
182
|
-
}
|
|
183
|
-
})
|
|
184
|
-
const actionName = 'deleteSession' + modelName
|
|
129
|
+
if(config.sessionDeleteAccess || config.sessionWriteAccess) {
|
|
130
|
+
const eventName = 'sessionOwned' + modelName + 'Deleted'
|
|
131
|
+
const actionName = 'deleteMySession' + modelName
|
|
185
132
|
service.actions[actionName] = new ActionDefinition({
|
|
186
133
|
name: actionName,
|
|
187
|
-
access: config.
|
|
134
|
+
access: config.sessionDeleteAccess || config.sessionWriteAccess,
|
|
188
135
|
properties: {
|
|
189
136
|
[modelPropertyName]: {
|
|
190
137
|
type: model,
|
|
@@ -195,12 +142,14 @@ definition.processor(function(service, app) {
|
|
|
195
142
|
waitForEvents: true,
|
|
196
143
|
async execute(properties, { client, service }, emit) {
|
|
197
144
|
const entity = await modelRuntime().get(properties[modelPropertyName])
|
|
198
|
-
if(!entity) throw
|
|
199
|
-
if(entity.session != client.session) throw
|
|
145
|
+
if(!entity) throw 'not_found'
|
|
146
|
+
if(entity.session != client.session) throw 'not_authorized'
|
|
200
147
|
emit({
|
|
201
148
|
type: eventName,
|
|
202
|
-
|
|
203
|
-
|
|
149
|
+
[modelPropertyName]: entity.id,
|
|
150
|
+
identifiers: {
|
|
151
|
+
session: client.session
|
|
152
|
+
}
|
|
204
153
|
})
|
|
205
154
|
}
|
|
206
155
|
})
|
package/sessionProperty.js
CHANGED
|
@@ -7,20 +7,28 @@ definition.processor(function(service, app) {
|
|
|
7
7
|
|
|
8
8
|
for(let modelName in service.models) {
|
|
9
9
|
const model = service.models[modelName]
|
|
10
|
+
console.log("SP", modelName)
|
|
10
11
|
|
|
11
12
|
if(model.sessionProperty) {
|
|
12
|
-
console.
|
|
13
|
+
console.log("MODEL " + modelName + " IS SESSION PROPERTY, CONFIG:", model.sessionProperty)
|
|
13
14
|
if (model.properties.session) throw new Error('session property already exists!!!')
|
|
15
|
+
|
|
14
16
|
const originalModelProperties = {...model.properties}
|
|
15
17
|
const modelProperties = Object.keys(model.properties)
|
|
16
|
-
const defaults = App.utils.generateDefault(
|
|
17
|
-
const modelPropertyName = modelName.slice(0, 1).toLowerCase() + modelName.slice(1)
|
|
18
|
+
const defaults = App.utils.generateDefault(model.properties)
|
|
18
19
|
|
|
19
20
|
function modelRuntime() {
|
|
20
21
|
return service._runtime.models[modelName]
|
|
21
22
|
}
|
|
22
23
|
|
|
23
24
|
const config = model.sessionProperty
|
|
25
|
+
const writeableProperties = modelProperties || config.writableProperties
|
|
26
|
+
|
|
27
|
+
model.propertyOf = {
|
|
28
|
+
what: Session,
|
|
29
|
+
...config
|
|
30
|
+
}
|
|
31
|
+
|
|
24
32
|
model.properties.session = new PropertyDefinition({
|
|
25
33
|
type: Session,
|
|
26
34
|
validation: ['nonEmpty']
|
|
@@ -29,78 +37,67 @@ definition.processor(function(service, app) {
|
|
|
29
37
|
model.indexes.bySession = new IndexDefinition({
|
|
30
38
|
property: 'session'
|
|
31
39
|
})
|
|
32
|
-
if(config.
|
|
33
|
-
const viewName = '
|
|
34
|
-
service.views[viewName] = new ViewDefinition({
|
|
35
|
-
name: viewName,
|
|
36
|
-
access: config.readAccess,
|
|
37
|
-
daoPath(params, {client, context}) {
|
|
38
|
-
return modelRuntime().indexObjectPath('bySession', client.session)
|
|
39
|
-
}
|
|
40
|
-
})
|
|
41
|
-
}
|
|
42
|
-
if(config.publicAccess) {
|
|
43
|
-
const viewName = 'publicSession' + modelName
|
|
40
|
+
if(config.sessionReadAccess) {
|
|
41
|
+
const viewName = 'mySession' + modelName
|
|
44
42
|
service.views[viewName] = new ViewDefinition({
|
|
45
43
|
name: viewName,
|
|
46
|
-
access: config.
|
|
47
|
-
daoPath(
|
|
48
|
-
return modelRuntime().
|
|
44
|
+
access: config.sessionReadAccess,
|
|
45
|
+
daoPath(params, { client, context }) {
|
|
46
|
+
return modelRuntime().path(client.session)
|
|
47
|
+
//return modelRuntime().indexObjectPath('bySession', client.session)
|
|
49
48
|
}
|
|
50
49
|
})
|
|
51
50
|
}
|
|
52
51
|
|
|
53
|
-
if(config.
|
|
54
|
-
const eventName = '
|
|
55
|
-
|
|
56
|
-
|
|
52
|
+
if(config.sessionSetAccess || config.sessionWriteAccess) {
|
|
53
|
+
const eventName = 'sessionOwned' + modelName + 'Set'
|
|
54
|
+
const actionName = 'setMySession' + modelName
|
|
55
|
+
service.actions[actionName] = new ActionDefinition({
|
|
56
|
+
name: actionName,
|
|
57
57
|
properties: {
|
|
58
58
|
...originalModelProperties
|
|
59
59
|
},
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const session = properties.session
|
|
63
|
-
return modelRuntime().create({...data, session, id: session})
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
const actionName = 'setSession' + modelName
|
|
67
|
-
service.actions[actionName] = new ActionDefinition({
|
|
68
|
-
name: actionName,
|
|
69
|
-
access: config.createAccess || config.writeAccess,
|
|
60
|
+
access: config.sessionSetAccess || config.sessionWriteAccess,
|
|
61
|
+
skipValidation: true,
|
|
70
62
|
queuedBy: (command) => command.client.session,
|
|
71
63
|
waitForEvents: true,
|
|
72
64
|
async execute(properties, {client, service}, emit) {
|
|
65
|
+
let newObject = {}
|
|
66
|
+
for(const propertyName of writeableProperties) {
|
|
67
|
+
if(properties.hasOwnProperty(propertyName)) {
|
|
68
|
+
newObject[propertyName] = properties[propertyName]
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const data = App.utils.mergeDeep({}, defaults, newObject)
|
|
72
|
+
await App.validation.validate(data, validators, { source: action, action, service, app, client })
|
|
73
73
|
emit({
|
|
74
74
|
type: eventName,
|
|
75
|
-
|
|
76
|
-
|
|
75
|
+
identifiers: {
|
|
76
|
+
session: client.session
|
|
77
|
+
},
|
|
78
|
+
data
|
|
77
79
|
})
|
|
78
80
|
}
|
|
79
81
|
})
|
|
82
|
+
const action = service.actions[actionName]
|
|
83
|
+
const validators = App.validation.getValidators(action, service, action)
|
|
80
84
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
execute(properties) {
|
|
86
|
-
const data = properties.data
|
|
87
|
-
const session = properties.session
|
|
88
|
-
return modelRuntime().update(session, { ...data, session, id: session })
|
|
89
|
-
}
|
|
90
|
-
})
|
|
91
|
-
const actionName = 'updateSession' + modelName
|
|
85
|
+
|
|
86
|
+
if(config.sessionUpdateAccess || config.sessionWriteAccess) {
|
|
87
|
+
const eventName = 'sessionOwned' + modelName + 'Updated'
|
|
88
|
+
const actionName = 'updateMySession' + modelName
|
|
92
89
|
service.actions[actionName] = new ActionDefinition({
|
|
93
90
|
name: actionName,
|
|
94
91
|
properties: {
|
|
95
92
|
...originalModelProperties
|
|
96
93
|
},
|
|
97
|
-
access: config.
|
|
94
|
+
access: config.sessionUpdateAccess || config.sessionWriteAccess,
|
|
98
95
|
skipValidation: true,
|
|
99
96
|
queuedBy: (command) => command.client.session,
|
|
100
97
|
waitForEvents: true,
|
|
101
98
|
async execute(properties, { client, service }, emit) {
|
|
102
99
|
const entity = await modelRuntime().get(client.session)
|
|
103
|
-
if(!entity) throw
|
|
100
|
+
if(!entity) throw 'not_found'
|
|
104
101
|
let updateObject = {}
|
|
105
102
|
for(const propertyName of writeableProperties) {
|
|
106
103
|
if(properties.hasOwnProperty(propertyName)) {
|
|
@@ -111,7 +108,9 @@ definition.processor(function(service, app) {
|
|
|
111
108
|
await App.validation.validate(merged, validators, { source: action, action, service, app, client })
|
|
112
109
|
emit({
|
|
113
110
|
type: eventName,
|
|
114
|
-
|
|
111
|
+
identifiers: {
|
|
112
|
+
session: client.session
|
|
113
|
+
},
|
|
115
114
|
data: properties || {}
|
|
116
115
|
})
|
|
117
116
|
}
|
|
@@ -119,30 +118,28 @@ definition.processor(function(service, app) {
|
|
|
119
118
|
const action = service.actions[actionName]
|
|
120
119
|
const validators = App.validation.getValidators(action, service, action)
|
|
121
120
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
execute({session}) {
|
|
127
|
-
return modelRuntime().delete(session)
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
const actionName = 'resetSession' + modelName
|
|
121
|
+
|
|
122
|
+
if(config.sessionResetAccess || config.sessionWriteAccess) {
|
|
123
|
+
const eventName = 'sessionOwned' + modelName + 'Reset'
|
|
124
|
+
const actionName = 'resetMySession' + modelName
|
|
131
125
|
service.actions[actionName] = new ActionDefinition({
|
|
132
126
|
name: actionName,
|
|
133
|
-
access: config.
|
|
127
|
+
access: config.sessionResetAccess || config.sessionWriteAccess,
|
|
134
128
|
queuedBy: (command) => command.client.session,
|
|
135
129
|
waitForEvents: true,
|
|
136
130
|
async execute(properties, {client, service}, emit) {
|
|
137
131
|
const entity = await modelRuntime().indexObjectGet('bySession', client.session)
|
|
138
|
-
if (!entity) throw
|
|
132
|
+
if (!entity) throw 'not_found'
|
|
139
133
|
emit({
|
|
140
134
|
type: eventName,
|
|
141
|
-
|
|
135
|
+
identifiers: {
|
|
136
|
+
session: client.session
|
|
137
|
+
}
|
|
142
138
|
})
|
|
143
139
|
}
|
|
144
140
|
})
|
|
145
141
|
}
|
|
142
|
+
|
|
146
143
|
}
|
|
147
144
|
}
|
|
148
145
|
|