@@ -9,7 +9,7 @@ import * as $rdf from 'rdflib'
99import ns from '../ns'
1010import kb from '../store.js'
1111import utils from '../utils'
12- import { AgentMapMap , ComboList } from './types'
12+ import { AgentMapMap , AgentMapUnion , ComboList } from './types'
1313import * as debug from '../debug'
1414
1515/**
@@ -22,49 +22,38 @@ export function adoptACLDefault (
2222 doc : $rdf . NamedNode ,
2323 aclDoc : $rdf . NamedNode ,
2424 defaultResource : $rdf . NamedNode ,
25- defaultACLdoc : $rdf . NamedNode
25+ defaultACLDoc : $rdf . NamedNode
2626) : $rdf . IndexedFormula {
2727 const ACL = ns . acl
2828 const isContainer = doc . uri . slice ( - 1 ) === '/' // Give default for all directories
29+
2930 const defaults = kb
30- . each ( undefined , ACL ( 'default' ) , defaultResource , defaultACLdoc )
31- . concat (
32- kb . each ( undefined , ACL ( 'defaultForNew' ) , defaultResource , defaultACLdoc )
33- )
34- let proposed : Array < $rdf . Statement > = [ ]
35- defaults . map ( function ( da ) {
36- proposed = proposed
37- . concat ( kb . statementsMatching ( da , ACL ( 'agent' ) , undefined , defaultACLdoc ) )
38- . concat ( kb . statementsMatching ( da , ACL ( 'agentClass' ) , undefined , defaultACLdoc ) )
39- . concat ( kb . statementsMatching ( da , ACL ( 'agentGroup' ) , undefined , defaultACLdoc ) )
40- . concat ( kb . statementsMatching ( da , ACL ( 'origin' ) , undefined , defaultACLdoc ) )
41- . concat ( kb . statementsMatching ( da , ACL ( 'originClass' ) , undefined , defaultACLdoc ) )
42- . concat ( kb . statementsMatching ( da , ACL ( 'mode' ) , undefined , defaultACLdoc ) )
43- proposed . push ( $rdf . st ( da , ACL ( 'accessTo' ) , doc , defaultACLdoc ) ) // Suppose
44- if ( isContainer ) {
45- // By default, make this apply to folder contents too
46- proposed . push ( $rdf . st ( da , ACL ( 'default' ) , doc , defaultACLdoc ) )
47- }
48- } )
49- const kb2 = $rdf . graph ( ) // Potential - derived is kept apart
50- proposed . map ( function ( st ) {
51- const move = function ( sym ) {
52- const y = defaultACLdoc . uri . length // The default ACL file
53- return $rdf . sym (
54- sym . uri . slice ( 0 , y ) === defaultACLdoc . uri
55- ? aclDoc . uri + sym . uri . slice ( y )
56- : sym . uri
57- )
58- }
59- kb2 . add (
60- move ( st . subject ) ,
61- move ( st . predicate ) ,
62- move ( st . object ) ,
63- $rdf . sym ( aclDoc . uri )
64- )
65- } )
31+ . each ( undefined , ACL ( 'default' ) , defaultResource , defaultACLDoc )
32+ . concat ( kb . each ( undefined , ACL ( 'defaultForNew' ) , defaultResource , defaultACLDoc ) )
33+
34+ const proposed = defaults . reduce ( ( accumulatedStatements , da ) => accumulatedStatements
35+ . concat ( kb . statementsMatching ( da , ns . rdf ( 'type' ) , ACL ( 'Authorization' ) , defaultACLDoc ) )
36+ . concat ( kb . statementsMatching ( da , ACL ( 'agent' ) , undefined , defaultACLDoc ) )
37+ . concat ( kb . statementsMatching ( da , ACL ( 'agentClass' ) , undefined , defaultACLDoc ) )
38+ . concat ( kb . statementsMatching ( da , ACL ( 'agentGroup' ) , undefined , defaultACLDoc ) )
39+ . concat ( kb . statementsMatching ( da , ACL ( 'origin' ) , undefined , defaultACLDoc ) )
40+ . concat ( kb . statementsMatching ( da , ACL ( 'originClass' ) , undefined , defaultACLDoc ) )
41+ . concat ( kb . statementsMatching ( da , ACL ( 'mode' ) , undefined , defaultACLDoc ) )
42+ . concat ( $rdf . st ( da , ACL ( 'accessTo' ) , doc , defaultACLDoc ) )
43+ . concat ( isContainer ? $rdf . st ( da , ACL ( 'default' ) , doc , defaultACLDoc ) : [ ] ) , [ ] )
6644
45+ const kb2 = $rdf . graph ( ) // Potential - derived is kept apart
46+ proposed . forEach ( st => kb2 . add ( move ( st . subject ) , move ( st . predicate ) , move ( st . object ) , $rdf . sym ( aclDoc . uri ) ) )
6747 return kb2
48+
49+ function move ( sym ) {
50+ const y = defaultACLDoc . uri . length // The default ACL file
51+ return $rdf . sym (
52+ sym . uri . slice ( 0 , y ) === defaultACLDoc . uri
53+ ? aclDoc . uri + sym . uri . slice ( y )
54+ : sym . uri
55+ )
56+ }
6857}
6958
7059/**
@@ -73,14 +62,14 @@ export function adoptACLDefault (
7362 * Accumulate the access rights which each agent or class has
7463 */
7564export function readACL (
76- x : $rdf . NamedNode ,
65+ doc : $rdf . NamedNode ,
7766 aclDoc : $rdf . NamedNode ,
7867 kb2 : $rdf . IndexedFormula = kb ,
79- getDefaults ? : boolean
68+ getDefaults : boolean = false
8069) : AgentMapMap {
8170 const auths : Array < $rdf . NamedNode > = getDefaults
8271 ? getDefaultsFallback ( kb2 , ns )
83- : kb2 . each ( undefined , ns . acl ( 'accessTo' ) , x )
72+ : kb2 . each ( undefined , ns . acl ( 'accessTo' ) , doc )
8473
8574 const ACL = ns . acl
8675 const ac = {
@@ -104,15 +93,15 @@ export function readACL (
10493
10594 function getDefaultsFallback ( kb , ns ) {
10695 return kb
107- . each ( undefined , ns . acl ( 'default' ) , x )
108- . concat ( kb . each ( undefined , ns . acl ( 'defaultForNew' ) , x ) )
96+ . each ( undefined , ns . acl ( 'default' ) , doc )
97+ . concat ( kb . each ( undefined , ns . acl ( 'defaultForNew' ) , doc ) )
10998 }
11099}
111100
112101/**
113102 * Compare two ACLs
114103 */
115- export function sameACL ( a : AgentMapMap , b : AgentMapMap ) : boolean {
104+ export function sameACL ( a : AgentMapMap | AgentMapUnion , b : AgentMapMap | AgentMapUnion ) : boolean {
116105 const contains = function ( a , b ) {
117106 for ( const pred in {
118107 agent : true ,
@@ -139,7 +128,7 @@ export function sameACL (a: AgentMapMap, b: AgentMapMap): boolean {
139128/**
140129 * Union N ACLs
141130 */
142- export function ACLunion ( list : Array < AgentMapMap > ) : AgentMapMap {
131+ export function ACLunion ( list : Array < AgentMapMap | AgentMapUnion > ) : AgentMapUnion {
143132 const b = list [ 0 ]
144133 let a , ag
145134 for ( let k = 1 ; k < list . length ; k ++ ) {
@@ -157,13 +146,13 @@ export function ACLunion (list: Array<AgentMapMap>): AgentMapMap {
157146 }
158147 )
159148 }
160- return b
149+ return b as AgentMapUnion
161150}
162151
163152/**
164153 * Merge ACLs lists from things to form union
165154 */
166- export function loadUnionACL ( subjectList : Array < $rdf . NamedNode > , callbackFunction : Function ) : void {
155+ export function loadUnionACL ( subjectList : Array < $rdf . NamedNode > , callbackFunction : loadUnionACLCallback ) : void {
167156 const aclList : Array < AgentMapMap > = [ ]
168157 const doList = function ( list ) {
169158 if ( list . length ) {
@@ -192,13 +181,15 @@ export function loadUnionACL (subjectList: Array<$rdf.NamedNode>, callbackFuncti
192181 doList ( subjectList )
193182}
194183
184+ type loadUnionACLCallback = ( ok : boolean , message ?: string | $rdf . NamedNode | AgentMapUnion | AgentMapMap ) => void
185+
195186/**
196187 * Represents these as an RDF graph by combination of modes
197188 *
198189 * Each agent can only be in one place in this model, one combination of modes.
199190 * Combos are like full control, read append, read only etc.
200191 */
201- export function ACLbyCombination ( ac : AgentMapMap ) : ComboList {
192+ export function ACLbyCombination ( ac : AgentMapMap | AgentMapUnion ) : ComboList {
202193 const byCombo = { }
203194 ; [ 'agent' , 'agentClass' , 'agentGroup' , 'origin' , 'originClass' ] . map ( function ( pred ) {
204195 for ( const agent in ac [ pred ] ) {
@@ -313,9 +304,9 @@ export function makeACLString (x: $rdf.NamedNode, ac: AgentMapMap, aclDoc: $rdf.
313304export function putACLObject (
314305 kb : $rdf . IndexedFormula ,
315306 x : $rdf . NamedNode ,
316- ac : AgentMapMap ,
307+ ac : AgentMapMap | AgentMapUnion ,
317308 aclDoc : $rdf . NamedNode ,
318- callbackFunction
309+ callbackFunction : ( ok : boolean , message ?: string ) => void
319310) : void {
320311 const byCombo = ACLbyCombination ( ac )
321312 return putACLbyCombo ( kb , x , byCombo , aclDoc , callbackFunction )
@@ -358,7 +349,7 @@ export function putACLbyCombo (
358349 *
359350 * All group files must be loaded first
360351 */
361- export function fixIndividualCardACL ( person : $rdf . NamedNode , log : Function , callbackFunction : Function ) : void {
352+ export function fixIndividualCardACL ( person : $rdf . NamedNode , log : Function , callbackFunction : fixIndividualCardACL ) : void {
362353 const groups = kb . each ( undefined , ns . vcard ( 'hasMember' ) , person )
363354 // const doc = person.doc()
364355 if ( groups ) {
@@ -370,10 +361,12 @@ export function fixIndividualCardACL (person: $rdf.NamedNode, log: Function, cal
370361 // @@ if no groups, then use default for People container or the book top container.?
371362}
372363
364+ type fixIndividualCardACL = ( ok : boolean , message ?: string | $rdf . NamedNode | AgentMapUnion | AgentMapMap ) => void
365+
373366/**
374367 * This function is used by [[fixIndividualCardACL]]
375368 */
376- export function fixIndividualACL ( item : $rdf . NamedNode , subjects : Array < $rdf . NamedNode > , log : Function , callbackFunction : Function ) : void {
369+ export function fixIndividualACL ( item : $rdf . NamedNode , subjects : Array < $rdf . NamedNode > , log : Function , callbackFunction : fixIndividualACLCallback ) : void {
377370 log = log || debug . log
378371 const doc = item . doc ( )
379372 getACLorDefault ( doc , function (
@@ -390,7 +383,7 @@ export function fixIndividualACL (item: $rdf.NamedNode, subjects: Array<$rdf.Nam
390383 : readACL ( defaultHolder , defaultACLDoc )
391384 loadUnionACL ( subjects , function ( ok , union ) {
392385 if ( ! ok ) return callbackFunction ( false , union )
393- if ( sameACL ( union , ac ) ) {
386+ if ( sameACL ( union as AgentMapMap | AgentMapUnion , ac ) ) {
394387 log ( 'Nice - same ACL. no change ' + utils . label ( item ) + ' ' + doc )
395388 } else {
396389 log ( 'Group ACLs differ for ' + utils . label ( item ) + ' ' + doc )
@@ -402,7 +395,7 @@ export function fixIndividualACL (item: $rdf.NamedNode, subjects: Array<$rdf.Nam
402395 putACLObject (
403396 kb ,
404397 targetDoc as $rdf . NamedNode ,
405- union ,
398+ union as AgentMapMap | AgentMapUnion ,
406399 targetACLDoc as $rdf . NamedNode ,
407400 callbackFunction
408401 )
@@ -411,6 +404,8 @@ export function fixIndividualACL (item: $rdf.NamedNode, subjects: Array<$rdf.Nam
411404 } )
412405}
413406
407+ type fixIndividualACLCallback = ( ok : boolean , message ?: string | $rdf . NamedNode | AgentMapUnion | AgentMapMap ) => void
408+
414409/**
415410 * Set an ACL
416411 */
0 commit comments