@@ -24,29 +24,9 @@ class ACLChecker {
2424 mode = 'Control'
2525 }
2626
27- // Find nearest ACL
28- let accessType = 'accessTo'
29- let nearestACL = Promise . reject ( )
30- for ( const acl of ACLChecker . possibleACLs ( resource , this . suffix ) ) {
31- nearestACL = nearestACL . catch ( ( ) => new Promise ( ( resolve , reject ) => {
32- debug ( 'Check if acl exist: ' + acl )
33- this . fetch ( acl , function ( err , graph ) {
34- if ( err || ! graph || ! graph . length ) {
35- if ( err ) debug ( `Error reading ${ acl } : ${ err } ` )
36- accessType = 'defaultForNew'
37- reject ( err )
38- } else {
39- resolve ( { acl, graph } )
40- }
41- } )
42- } ) )
43- }
44- nearestACL = nearestACL . catch ( ( ) => {
45- throw new Error ( 'No ACL resource found' )
46- } )
47-
48- // Check the permissions within the ACL
49- return nearestACL . then ( ( { acl, graph } ) =>
27+ // Check the permissions within the nearest ACL
28+ return this . getNearestACL ( resource )
29+ . then ( ( { acl, graph, accessType } ) =>
5030 this . checkAccess (
5131 graph , // The ACL graph
5232 user , // The webId of the user
@@ -70,6 +50,51 @@ class ACLChecker {
7050 } )
7151 }
7252
53+ // Gets the ACL that applies to the resource
54+ getNearestACL ( uri ) {
55+ let accessType = 'accessTo'
56+ let nearestACL = Promise . reject ( )
57+ for ( const acl of this . getPossibleACLs ( uri , this . suffix ) ) {
58+ nearestACL = nearestACL . catch ( ( ) => new Promise ( ( resolve , reject ) => {
59+ this . debug ( `Check if ACL exists: ${ acl } ` )
60+ this . fetch ( acl , ( err , graph ) => {
61+ if ( err || ! graph || ! graph . length ) {
62+ if ( err ) this . debug ( `Error reading ${ acl } : ${ err } ` )
63+ accessType = 'defaultForNew'
64+ reject ( err )
65+ } else {
66+ resolve ( { acl, graph, accessType } )
67+ }
68+ } )
69+ } ) )
70+ }
71+ return nearestACL . catch ( e => { throw new Error ( 'No ACL resource found' ) } )
72+ }
73+
74+ // Get all possible ACL paths that apply to the resource
75+ getPossibleACLs ( uri , suffix ) {
76+ var first = uri . endsWith ( suffix ) ? uri : uri + suffix
77+ var urls = [ first ]
78+ var parsedUri = url . parse ( uri )
79+ var baseUrl = ( parsedUri . protocol ? parsedUri . protocol + '//' : '' ) +
80+ ( parsedUri . host || '' )
81+ if ( baseUrl + '/' === uri ) {
82+ return urls
83+ }
84+
85+ var times = parsedUri . pathname . split ( '/' ) . length
86+ // TODO: improve temporary solution to stop recursive path walking above root
87+ if ( parsedUri . pathname . endsWith ( '/' ) ) {
88+ times --
89+ }
90+
91+ for ( var i = 0 ; i < times - 1 ; i ++ ) {
92+ uri = path . dirname ( uri )
93+ urls . push ( uri + ( uri [ uri . length - 1 ] === '/' ? suffix : '/' + suffix ) )
94+ }
95+ return urls
96+ }
97+
7398 /**
7499 * Tests whether a graph (parsed .acl resource) allows a given operation
75100 * for a given user. Calls the provided callback with `null` if the user
@@ -135,29 +160,6 @@ class ACLChecker {
135160 return false
136161 }
137162 }
138-
139- static possibleACLs ( uri , suffix ) {
140- var first = uri . endsWith ( suffix ) ? uri : uri + suffix
141- var urls = [ first ]
142- var parsedUri = url . parse ( uri )
143- var baseUrl = ( parsedUri . protocol ? parsedUri . protocol + '//' : '' ) +
144- ( parsedUri . host || '' )
145- if ( baseUrl + '/' === uri ) {
146- return urls
147- }
148-
149- var times = parsedUri . pathname . split ( '/' ) . length
150- // TODO: improve temporary solution to stop recursive path walking above root
151- if ( parsedUri . pathname . endsWith ( '/' ) ) {
152- times --
153- }
154-
155- for ( var i = 0 ; i < times - 1 ; i ++ ) {
156- uri = path . dirname ( uri )
157- urls . push ( uri + ( uri [ uri . length - 1 ] === '/' ? suffix : '/' + suffix ) )
158- }
159- return urls
160- }
161163}
162164
163165module . exports = ACLChecker
0 commit comments