Coverage report for Casbin.Adapter.Filesystem.Policy.
Generated at 18/02/2019 12:10:19 by DelphiCodeCoverage - an open source tool for Delphi Code Coverage.
Statistics for Casbin.Adapter.Filesystem.Policy.pas
| Number of lines covered | 66 |
| Number of lines with code gen | 66 |
| Line coverage | 100% |
| 1 | // Copyright 2018 by John Kouraklis and Contributors. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | unit Casbin.Adapter.Filesystem.Policy; |
| 15 | |
| 16 | interface |
| 17 | |
| 18 | uses |
| 19 | Casbin.Adapter.Filesystem, Casbin.Adapter.Policy.Types, Casbin.Core.Base.Types; |
| 20 | |
| 21 | type |
| 22 | TPolicyFileAdapter = class (TFileAdapter, IPolicyAdapter) |
| 23 | private |
| 24 | fCached: Boolean; |
| 25 | fAutosave: Boolean; |
| 26 | fCacheSize: Integer; |
| 27 | fSaved: Boolean; |
| 28 | protected |
| 29 | {$REGION 'IPolicyAdapter'} |
| 30 | procedure add(const aTag: string); |
| 31 | function getAutoSave: Boolean; |
| 32 | function getCached: Boolean; |
| 33 | |
| 34 | procedure setAutoSave(const aValue: Boolean); |
| 35 | procedure setCached(const aValue: Boolean); |
| 36 | |
| 37 | function getCacheSize: Integer; |
| 38 | procedure setCacheSize(const aValue: Integer); |
| 39 | {$ENDREGION} |
| 40 | public |
| 41 | {$REGION 'IAdapter'} |
| 42 | procedure load(const aFilter: TFilterArray = []); override; |
| 43 | procedure save; override; |
| 44 | {$ENDREGION} |
| 45 | {$REGION 'IPolicyAdapter'} |
| 46 | procedure remove(const aPolicyDefinition: string); overload; |
| 47 | {$ENDREGION} |
| 48 | constructor Create(const aFilename: string); override; |
| 49 | end; |
| 50 | |
| 51 | implementation |
| 52 | |
| 53 | uses |
| 54 | System.SysUtils, Casbin.Core.Utilities, System.Classes, System.Generics.Collections, System.IOUtils, System.Types; |
| 55 | |
| 56 | { TPolicyFileAdapter } |
| 57 | |
| 58 | procedure TPolicyFileAdapter.add(const aTag: string); |
| 59 | begin |
| 60 | if Trim(aTag)<>'' then |
| 61 | begin |
| 62 | getAssertions.Add(Trim(aTag)); |
| 63 | fSaved:=False; |
| 64 | end; |
| 65 | end; |
| 66 | |
| 67 | constructor TPolicyFileAdapter.Create(const aFilename: string); |
| 68 | begin |
| 69 | inherited; |
| 70 | fAutosave:=True; |
| 71 | fCached:=False; //PALOFF |
| 72 | fCacheSize:=DefaultCacheSize; |
| 73 | fSaved:=False; |
| 74 | end; |
| 75 | |
| 76 | function TPolicyFileAdapter.getAutoSave: Boolean; |
| 77 | begin |
| 78 | Result:=fAutosave; |
| 79 | end; |
| 80 | |
| 81 | function TPolicyFileAdapter.getCached: Boolean; |
| 82 | begin |
| 83 | Result:=fCached; |
| 84 | end; |
| 85 | |
| 86 | function TPolicyFileAdapter.getCacheSize: Integer; |
| 87 | begin |
| 88 | Result:=fCacheSize; |
| 89 | end; |
| 90 | |
| 91 | procedure TPolicyFileAdapter.load(const aFilter: TFilterArray); |
| 92 | var |
| 93 | policy: string; |
| 94 | filter: string; |
| 95 | i: Integer; |
| 96 | found: Boolean; |
| 97 | begin |
| 98 | // DO NOT CALL inherited when CACHE is implemented |
| 99 | // WE NEED TO MANAGE THE CACHE |
| 100 | // Now, cache is not managed at all |
| 101 | inherited; // <-- This should be removed when Cache is implemented |
| 102 | // But the fFiltered should be managed here |
| 103 | // And the fFilter property |
| 104 | fFilter:=aFilter; |
| 105 | if Length(fFilter)<>0 then |
| 106 | begin |
| 107 | for i:=getAssertions.Count-1 downto 0 do |
| 108 | begin |
| 109 | found:=False; |
| 110 | policy:=getAssertions.Items[i]; |
| 111 | for filter in fFilter do |
| 112 | found:=found or policy.Contains(Trim(filter)); |
| 113 | if not found then |
| 114 | getAssertions.Delete(i); |
| 115 | end; |
| 116 | end; |
| 117 | end; |
| 118 | |
| 119 | procedure TPolicyFileAdapter.remove(const aPolicyDefinition: string); |
| 120 | var |
| 121 | assertion: string; |
| 122 | def: string; |
| 123 | asDef: string; |
| 124 | begin |
| 125 | if Trim(aPolicyDefinition)='' then |
| 126 | Exit; |
| 127 | for assertion in getAssertions do |
| 128 | begin |
| 129 | def:=assertion; |
| 130 | while Pos(#32, def, findStartPos)<>0 do |
| 131 | Delete(def, Pos(#32, def, findStartPos), 1); |
| 132 | asDef:=aPolicyDefinition; |
| 133 | while Pos(#32, asDef, findStartPos)<>0 do |
| 134 | Delete(asDef, Pos(#32, asDef, findStartPos), 1); |
| 135 | if SameText(UpperCase(Trim(def)), UpperCase(trim(asDef))) then |
| 136 | begin |
| 137 | getAssertions.Remove(assertion); |
| 138 | Break; |
| 139 | end; |
| 140 | end; |
| 141 | fSaved:=False; |
| 142 | if fAutosave then |
| 143 | save; |
| 144 | |
| 145 | // We need to remove the role-based policies as well |
| 146 | // For now, if policies are deleted the role rules exists in the file |
| 147 | |
| 148 | end; |
| 149 | |
| 150 | procedure TPolicyFileAdapter.save; |
| 151 | begin |
| 152 | inherited; |
| 153 | if not fSaved then |
| 154 | begin |
| 155 | TFile.WriteAllLines(fFilename, TStringDynArray(getAssertions.ToArray)); |
| 156 | fSaved:=True; |
| 157 | end; |
| 158 | end; |
| 159 | |
| 160 | procedure TPolicyFileAdapter.setAutoSave(const aValue: Boolean); |
| 161 | begin |
| 162 | fAutosave:=aValue; |
| 163 | end; |
| 164 | |
| 165 | procedure TPolicyFileAdapter.setCached(const aValue: Boolean); |
| 166 | begin |
| 167 | fCached:=aValue; |
| 168 | end; |
| 169 | |
| 170 | procedure TPolicyFileAdapter.setCacheSize(const aValue: Integer); |
| 171 | begin |
| 172 | fCacheSize:=aValue; |
| 173 | end; |
| 174 | |
| 175 | end. |