@@ -9,33 +9,34 @@ const HTTPError = require('./http-error')
99const DEFAULT_ACL_SUFFIX = '.acl'
1010
1111class 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 ) {
0 commit comments