Skip to content

Commit 9a6bdf3

Browse files
committed
Move resource parameter to constructor.
1 parent 4bb1b8f commit 9a6bdf3

File tree

3 files changed

+38
-35
lines changed

3 files changed

+38
-35
lines changed

lib/acl-checker.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,34 @@ const HTTPError = require('./http-error')
99
const DEFAULT_ACL_SUFFIX = '.acl'
1010

1111
class ACLChecker {
12-
constructor (options = {}) {
12+
constructor (resource, options = {}) {
13+
this.resource = resource
1314
this.debug = options.debug || console.log.bind(console)
1415
this.fetch = options.fetch
1516
this.strictOrigin = options.strictOrigin
1617
this.suffix = options.suffix || DEFAULT_ACL_SUFFIX
1718
}
1819

19-
can (user, mode, resource, options = {}) {
20+
can (user, mode, options = {}) {
2021
const debug = this.debug
21-
debug('Can ' + (user || 'an agent') + ' ' + mode + ' ' + resource + '?')
22+
this.debug(`Can ${user || 'an agent'} ${mode} ${this.resource}?`)
2223
// If this is an ACL, Control mode must be present for any operations
23-
if (this.isAcl(resource)) {
24+
if (this.isAcl(this.resource)) {
2425
mode = 'Control'
2526
}
2627

2728
// Check the permissions within the nearest ACL
28-
return this.getNearestACL(resource)
29+
return this.getNearestACL(this.resource)
2930
.then(nearestAcl => {
30-
const acls = this.getPermissionSet(nearestAcl, resource, options)
31-
return this.checkAccess(acls, user, mode, resource)
31+
const acls = this.getPermissionSet(nearestAcl, options)
32+
return this.checkAccess(acls, user, mode, this.resource)
3233
})
3334
.then(() => { debug('ACL policy found') })
3435
.catch(err => {
3536
debug(`Error: ${err.message}`)
3637
if (!user) {
3738
debug('Authentication required')
38-
throw new HTTPError(401, `Access to ${resource} requires authorization`)
39+
throw new HTTPError(401, `Access to ${this.resource} requires authorization`)
3940
} else {
4041
debug(`${mode} access denied for ${user}`)
4142
throw new HTTPError(403, `Access denied for ${user}`)
@@ -44,10 +45,10 @@ class ACLChecker {
4445
}
4546

4647
// Gets the ACL that applies to the resource
47-
getNearestACL (uri) {
48+
getNearestACL () {
4849
let isContainer = false
4950
let nearestACL = Promise.reject()
50-
for (const acl of this.getPossibleACLs(uri, this.suffix)) {
51+
for (const acl of this.getPossibleACLs()) {
5152
nearestACL = nearestACL.catch(() => new Promise((resolve, reject) => {
5253
this.debug(`Check if ACL exists: ${acl}`)
5354
this.fetch(acl, (err, graph) => {
@@ -65,7 +66,9 @@ class ACLChecker {
6566
}
6667

6768
// Get all possible ACL paths that apply to the resource
68-
getPossibleACLs (uri, suffix) {
69+
getPossibleACLs () {
70+
var uri = this.resource
71+
var suffix = this.suffix
6972
var first = uri.endsWith(suffix) ? uri : uri + suffix
7073
var urls = [first]
7174
var parsedUri = url.parse(uri)
@@ -89,8 +92,8 @@ class ACLChecker {
8992
}
9093

9194
// Tests whether the permissions allow a given operation
92-
checkAccess (permissionSet, user, mode, resource) {
93-
return permissionSet.checkAccess(resource, user, mode)
95+
checkAccess (permissionSet, user, mode) {
96+
return permissionSet.checkAccess(this.resource, user, mode)
9497
.then(hasAccess => {
9598
if (hasAccess) {
9699
this.debug(`${mode} access permitted to ${user}`)
@@ -108,7 +111,7 @@ class ACLChecker {
108111
}
109112

110113
// Gets the permission set for the given resource
111-
getPermissionSet ({ acl, graph, isContainer }, resource, options = {}) {
114+
getPermissionSet ({ acl, graph, isContainer }, options = {}) {
112115
const debug = this.debug
113116
if (!graph || graph.length === 0) {
114117
debug('ACL ' + acl + ' is empty')
@@ -124,7 +127,7 @@ class ACLChecker {
124127
isAcl: uri => this.isAcl(uri),
125128
aclUrlFor: uri => this.aclUrlFor(uri)
126129
}
127-
return new PermissionSet(resource, acl, isContainer, aclOptions)
130+
return new PermissionSet(this.resource, acl, isContainer, aclOptions)
128131
}
129132

130133
aclUrlFor (uri) {

lib/handlers/allow.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,6 @@ function allow (mode) {
1313
if (!ldp.webid) {
1414
return next()
1515
}
16-
var baseUri = utils.uriBase(req)
17-
18-
var acl = new ACL({
19-
debug: debug,
20-
fetch: fetchDocument(req.hostname, ldp, baseUri),
21-
suffix: ldp.suffixAcl,
22-
strictOrigin: ldp.strictOrigin
23-
})
24-
req.acl = acl
2516

2617
var reqPath = res && res.locals && res.locals.path
2718
? res.locals.path
@@ -37,8 +28,17 @@ function allow (mode) {
3728
origin: req.get('origin'),
3829
host: req.protocol + '://' + req.get('host')
3930
}
40-
acl.can(req.session.userId, mode, baseUri + reqPath, options)
41-
.then(next, next)
31+
32+
var baseUri = utils.uriBase(req)
33+
var acl = new ACL(baseUri + reqPath, {
34+
debug: debug,
35+
fetch: fetchDocument(req.hostname, ldp, baseUri),
36+
suffix: ldp.suffixAcl,
37+
strictOrigin: ldp.strictOrigin
38+
})
39+
req.acl = acl
40+
41+
acl.can(req.session.userId, mode, options).then(next, next)
4242
})
4343
}
4444
}

test/unit/acl-checker-test.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ describe('ACLChecker unit test', () => {
2828
})
2929
let graph = {}
3030
let user, mode, resource, aclUrl
31-
let acl = new ACLChecker({ debug })
32-
let acls = acl.getPermissionSet({ graph, acl: aclUrl }, resource)
33-
return expect(acl.checkAccess(acls, user, mode, resource))
31+
let acl = new ACLChecker(resource, { debug })
32+
let acls = acl.getPermissionSet({ graph, acl: aclUrl })
33+
return expect(acl.checkAccess(acls, user, mode))
3434
.to.eventually.be.true
3535
})
3636
it('should callback with error on grant failure', () => {
@@ -39,9 +39,9 @@ describe('ACLChecker unit test', () => {
3939
})
4040
let graph = {}
4141
let user, mode, resource, aclUrl
42-
let acl = new ACLChecker({ debug })
43-
let acls = acl.getPermissionSet({ graph, acl: aclUrl }, resource)
44-
return expect(acl.checkAccess(acls, user, mode, resource))
42+
let acl = new ACLChecker(resource, { debug })
43+
let acls = acl.getPermissionSet({ graph, acl: aclUrl })
44+
return expect(acl.checkAccess(acls, user, mode))
4545
.to.be.rejectedWith('ACL file found but no matching policy found')
4646
})
4747
it('should callback with error on grant error', () => {
@@ -50,9 +50,9 @@ describe('ACLChecker unit test', () => {
5050
})
5151
let graph = {}
5252
let user, mode, resource, aclUrl
53-
let acl = new ACLChecker({ debug })
54-
let acls = acl.getPermissionSet({ graph, acl: aclUrl }, resource)
55-
return expect(acl.checkAccess(acls, user, mode, resource))
53+
let acl = new ACLChecker(resource, { debug })
54+
let acls = acl.getPermissionSet({ graph, acl: aclUrl })
55+
return expect(acl.checkAccess(acls, user, mode))
5656
.to.be.rejectedWith('Error thrown during checkAccess()')
5757
})
5858
})

0 commit comments

Comments
 (0)