forked from thomas4019/expressa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listeners_collection_permissions.js
32 lines (29 loc) · 1.3 KB
/
listeners_collection_permissions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const util = require('./util')
const debug = require('debug')('expressa')
const eventToPermissionMapping = {
get: 'view',
put: 'edit',
post: 'create',
delete: 'delete'
}
module.exports = function (api) {
api.addCollectionListener('get', 'collection', function viewRelevantCollections (req, collection, data) {
if (req.hasPermission('collection: view relevant')) {
if (req.hasPermission(data._id + ': view') || req.hasPermission(data._id + ': view own')) {
return true // force allow
}
}
})
api.addListener(['get', 'put', 'post', 'delete'], function collectionPermissionCheck (req, collection, data, info) {
const permission = eventToPermissionMapping[info.event]
const editingOwnUser = collection === 'users' && data._id === req.uid
const editingOwnDoc = data.meta && data.meta.owner && data.meta.owner === req.uid
const editingOwn = ((editingOwnUser || editingOwnDoc) &&
req.hasPermission(collection + ': ' + permission + ' own'))
// console.log(editingOwn + ' ' + editingOwnUser + ' ' + editingOwnDoc);
if (!editingOwn && !req.hasPermission(collection + ': ' + permission)) {
debug(`cancelling, missing permission "${collection}: ${permission}"`)
throw new util.ApiError(401, 'You do not have permission to perform this action.')
}
})
}