|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | import { CoreEnforcer } from './coreEnforcer';
|
16 |
| -import { BatchAdapter } from './persist/batchAdapter'; |
| 16 | +import { BatchAdapter } from './persist'; |
| 17 | +import { UpdatableAdapter } from './persist'; |
17 | 18 | import { PolicyOp } from './model';
|
18 | 19 |
|
19 | 20 | /**
|
@@ -85,6 +86,43 @@ export class InternalEnforcer extends CoreEnforcer {
|
85 | 86 | return ok;
|
86 | 87 | }
|
87 | 88 |
|
| 89 | + /** |
| 90 | + * updatePolicyInternal updates a rule from the current policy. |
| 91 | + */ |
| 92 | + public async updatePolicyInternal(sec: string, ptype: string, oldRule: string[], newRule: string[]): Promise<boolean> { |
| 93 | + if (!this.model.hasPolicy(sec, ptype, oldRule)) { |
| 94 | + return false; |
| 95 | + } |
| 96 | + |
| 97 | + if (this.autoSave) { |
| 98 | + if ('updatePolicy' in this.adapter) { |
| 99 | + try { |
| 100 | + await this.adapter.updatePolicy(sec, ptype, oldRule, newRule); |
| 101 | + } catch (e) { |
| 102 | + if (e.message !== 'not implemented') { |
| 103 | + throw e; |
| 104 | + } |
| 105 | + } |
| 106 | + } else { |
| 107 | + throw new Error('cannot to update policy, the adapter does not implement the UpdatableAdapter'); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + if (this.watcher && this.autoNotifyWatcher) { |
| 112 | + // In fact I think it should wait for the respond, but they implement add_policy() like this |
| 113 | + // error intentionally ignored |
| 114 | + this.watcher.update(); |
| 115 | + } |
| 116 | + |
| 117 | + const ok = this.model.updatePolicy(sec, ptype, oldRule, newRule); |
| 118 | + if (sec === 'g' && ok) { |
| 119 | + await this.buildIncrementalRoleLinks(PolicyOp.PolicyRemove, ptype, [oldRule]); |
| 120 | + await this.buildIncrementalRoleLinks(PolicyOp.PolicyAdd, ptype, [newRule]); |
| 121 | + } |
| 122 | + |
| 123 | + return ok; |
| 124 | + } |
| 125 | + |
88 | 126 | /**
|
89 | 127 | * removePolicyInternal removes a rule from the current policy.
|
90 | 128 | */
|
|
0 commit comments