@@ -4,6 +4,7 @@ import { inject as service } from '@ember/service';
44import { tracked } from '@glimmer/tracking' ;
55import { A } from '@ember/array' ;
66import { namedNode } from 'rdflib' ;
7+ import { n3reasoner } from 'eyereasoner' ;
78
89export default class IndexController extends Controller {
910 queryParams = [ 'form' ] ;
@@ -26,6 +27,11 @@ export default class IndexController extends Controller {
2627 @tracked
2728 fields = this . loadFields ( ) ;
2829
30+ @tracked policyTypeError = '' ;
31+ @tracked policyURLError = '' ;
32+ @tracked policyMethodError = '' ;
33+ @tracked policyContentTypeError = '' ;
34+
2935 loadFields ( ) {
3036 let fields ;
3137 if (
@@ -156,7 +162,7 @@ export default class IndexController extends Controller {
156162 }
157163
158164 // Remove all N3 rules from the resource.
159- const matches = await this . model . removeN3RulesFromResource ( ) ;
165+ let matches = await this . model . removeN3RulesFromResource ( ) ;
160166
161167 this . fields . forEach ( ( field , i ) => {
162168 field . order = i ;
@@ -185,6 +191,38 @@ export default class IndexController extends Controller {
185191
186192 await this . store . persist ( ) ;
187193
194+ // Apply the policy configuration to the rules.
195+ matches . rules = await matches . rules . filter (
196+ ( rule ) => ! this . isEventSubmitRule ( rule , matches . prefixes )
197+ ) ;
198+ matches . rules . push ( `
199+ {
200+ _:id ex:event ex:Submit.
201+ } => {
202+ ex:HttpPolicy pol:policy ex:Pol .
203+ ex:Pol a fno:Execution ;
204+ fno:executes ex:httpRequest ;
205+ ex:method "${ this . model . policyMethod } " ;
206+ ex:url <${ this . model . policyURL } > ;
207+ ex:contentType "${ this . model . policyContentType } " .
208+ } .
209+ ` ) ;
210+ matches . prefixes = this . addIfNotIncluded (
211+ matches . prefixes ,
212+ 'ex' ,
213+ 'http://example.org/'
214+ ) ;
215+ matches . prefixes = this . addIfNotIncluded (
216+ matches . prefixes ,
217+ 'fno' ,
218+ 'https://w3id.org/function/ontology#'
219+ ) ;
220+ matches . prefixes = this . addIfNotIncluded (
221+ matches . prefixes ,
222+ 'pol' ,
223+ 'https://www.example.org/ns/policy#'
224+ ) ;
225+
188226 // Re-add the N3 rules to the resource.
189227 await this . model . addN3RulesToResource ( matches ) ;
190228
@@ -220,6 +258,27 @@ export default class IndexController extends Controller {
220258 }
221259 }
222260 } ) ;
261+
262+ if ( ! this . model . policyURL . trim ( ) ) {
263+ this . policyURLError = 'Please fill in a URL.' ;
264+ valid = false ;
265+ }
266+ if ( ! this . model . policyContentType . trim ( ) ) {
267+ this . policyContentTypeError = 'Please fill in a Content-Type.' ;
268+ valid = false ;
269+ }
270+ if (
271+ ! [ 'GET' , 'POST' , 'PUT' , 'DELETE' , 'PATCH' ] . includes (
272+ this . model . policyMethod
273+ )
274+ ) {
275+ this . policyMethodError = 'Please choose a valid HTTP method.' ;
276+ valid = false ;
277+ }
278+ if ( ! [ 'HTTP' ] . includes ( this . model . policyType ) ) {
279+ this . policyTypeError = 'Please choose a valid policy type.' ;
280+ valid = false ;
281+ }
223282 return valid ;
224283 }
225284
@@ -481,4 +540,47 @@ export default class IndexController extends Controller {
481540 await this . solidAuth . ensureLogout ( ) ;
482541 this . model . refresh ( ) ;
483542 }
543+
544+ async isEventSubmitRule ( rule , prefixes ) {
545+ const options = { blogic : false , outputType : 'string' } ;
546+ const query = `${ prefixes ? prefixes . join ( '\n' ) : '' } \n${ rule } ` ;
547+ const reasonerResult = await n3reasoner (
548+ '_:id <http://example.org/event> <http://example.org/Submit> .' ,
549+ query ,
550+ options
551+ ) ;
552+ return reasonerResult . length > 0 ;
553+ }
554+ addIfNotIncluded ( prefixes , prefix , url ) {
555+ let alreadyIncluded = false ;
556+ prefixes . forEach ( ( p ) => {
557+ if ( p . includes ( prefix ) && p . includes ( url ) ) {
558+ alreadyIncluded = true ;
559+ }
560+ } ) ;
561+ if ( ! alreadyIncluded ) {
562+ prefixes . push ( `@prefix ${ prefix } : <${ url } > .` ) ;
563+ }
564+ return prefixes ;
565+ }
566+
567+ @action
568+ updatePolicyType ( event ) {
569+ this . model . policyType = event . target . value ?. trim ( ) ;
570+ }
571+
572+ @action
573+ updatePolicyURL ( event ) {
574+ this . model . policyURL = event . target . value ?. trim ( ) ;
575+ }
576+
577+ @action
578+ updatePolicyMethod ( event ) {
579+ this . model . policyMethod = event . target . value ?. trim ( ) ;
580+ }
581+
582+ @action
583+ updatePolicyContentType ( event ) {
584+ this . model . policyContentType = event . target . value ?. trim ( ) ;
585+ }
484586}
0 commit comments