|
| 1 | +const APIError = require("../../api/APIError"); |
| 2 | +const { PermissionUtil } = require("../auth/PermissionService"); |
| 3 | +const BaseService = require("../BaseService"); |
| 4 | + |
| 5 | +// DO WE HAVE enough information to get the policy for the newer drivers? |
| 6 | +// - looks like it: service:<name of service>:<name of trait> |
| 7 | + |
| 8 | +class DriverUsagePolicyService extends BaseService { |
| 9 | + async get_policies_for_option_ (option) { |
| 10 | + // NOT FINAL: before implementing cascading monthly usage, |
| 11 | + // this return will be removed and the code below it will |
| 12 | + // be uncommented |
| 13 | + return option.path; |
| 14 | + /* |
| 15 | + const svc_systemData = this.services.get('system-data'); |
| 16 | + const svc_su = this.services.get('su'); |
| 17 | + |
| 18 | + const policies = await Promise.all(option.path.map(async path_node => { |
| 19 | + const policy = await svc_su.sudo(async () => { |
| 20 | + return await svc_systemData.interpret(option.data); |
| 21 | + }); |
| 22 | + return { |
| 23 | + ...path_node, |
| 24 | + policy, |
| 25 | + }; |
| 26 | + })); |
| 27 | + return policies; |
| 28 | + */ |
| 29 | + } |
| 30 | + |
| 31 | + async select_best_option_ (options) { |
| 32 | + return options[0]; |
| 33 | + } |
| 34 | + |
| 35 | + // TODO: DRY: This is identical to the method of the same name in |
| 36 | + // DriverService, except after the line with a comment containing |
| 37 | + // the string "[DEVIATION]". |
| 38 | + async get_effective_policy ({ actor, service_name, trait_name }) { |
| 39 | + const svc_permission = this.services.get('permission'); |
| 40 | + const reading = await svc_permission.scan( |
| 41 | + actor, |
| 42 | + PermissionUtil.join('service', service_name, 'ii', trait_name), |
| 43 | + ); |
| 44 | + console.log({ |
| 45 | + perm: PermissionUtil.join('service', service_name, 'ii', trait_name), |
| 46 | + reading: require('util').inspect(reading, { depth: null }), |
| 47 | + }); |
| 48 | + const options = PermissionUtil.reading_to_options(reading); |
| 49 | + console.log('OPTIONS', JSON.stringify(options, undefined, ' ')); |
| 50 | + if ( options.length <= 0 ) { |
| 51 | + return undefined; |
| 52 | + } |
| 53 | + const option = await this.select_best_option_(options); |
| 54 | + const policies = await this.get_policies_for_option_(option); |
| 55 | + console.log('SLA', JSON.stringify(policies, undefined, ' ')); |
| 56 | + |
| 57 | + // NOT FINAL: For now we apply monthly usage logic |
| 58 | + // to the first holder of the permission. Later this |
| 59 | + // will be changed so monthly usage can cascade across |
| 60 | + // multiple actors. I decided not to implement this |
| 61 | + // immediately because it's a hefty time sink and it's |
| 62 | + // going to be some time before we can offer this feature |
| 63 | + // to the end-user either way. |
| 64 | + |
| 65 | + let effective_policy = null; |
| 66 | + for ( const policy of policies ) { |
| 67 | + if ( policy.holder ) { |
| 68 | + effective_policy = policy; |
| 69 | + break; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // === [DEVIATION] In DriverService, this is part of call_new_ === |
| 74 | + const svc_systemData = this.services.get('system-data'); |
| 75 | + const svc_su = this.services.get('su'); |
| 76 | + effective_policy = await svc_su.sudo(async () => { |
| 77 | + return await svc_systemData.interpret(effective_policy.data); |
| 78 | + }); |
| 79 | + |
| 80 | + effective_policy = effective_policy.policy; |
| 81 | + |
| 82 | + return effective_policy; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +module.exports = { |
| 87 | + DriverUsagePolicyService, |
| 88 | +}; |
0 commit comments