Coverage report for Casbin.Functions.
Generated at 18/02/2019 12:10:19 by DelphiCodeCoverage - an open source tool for Delphi Code Coverage.
Statistics for Casbin.Functions.pas
Number of lines covered | 55 |
Number of lines with code gen | 67 |
Line coverage | 82% |
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.Functions; |
15 | |
16 | interface |
17 | |
18 | uses |
19 | Casbin.Core.Base.Types, Casbin.Functions.Types, |
20 | System.Generics.Collections, System.Classes, System.Types, System.Rtti; |
21 | |
22 | type |
23 | TFunctions = class (TBaseInterfacedObject, IFunctions) |
24 | private |
25 | fDictionary: TDictionary<string, TCasbinFunc>; |
26 | fObjDictionary: TDictionary<string, TCasbinObjectFunc>; |
27 | fList: TStringList; |
28 | procedure loadBuiltInFunctions; |
29 | procedure loadCustomFunctions; //PALOFF |
30 | private |
31 | {$REGION 'Interface'} |
32 | procedure registerFunction(const aName: string; |
33 | const aFunc: TCasbinFunc); overload; |
34 | procedure registerFunction(const aName: string; |
35 | const aFunc: TCasbinObjectFunc); overload; |
36 | function retrieveFunction(const aName: string): TCasbinFunc; |
37 | function retrieveObjFunction(const aName: string): TCasbinObjectFunc; |
38 | function list: TStringList; |
39 | procedure refreshFunctions; |
40 | {$ENDREGION} |
41 | public |
42 | constructor Create; |
43 | destructor Destroy; override; |
44 | end; |
45 | |
46 | implementation |
47 | |
48 | uses |
49 | System.SysUtils, System.RegularExpressions, System.StrUtils; |
50 | |
51 | constructor TFunctions.Create; |
52 | begin |
53 | inherited; |
54 | fDictionary:=TDictionary<string, TCasbinFunc>.Create; |
55 | fObjDictionary:=TDictionary<string, TCasbinObjectFunc>.Create; |
56 | fList:=TStringList.Create; |
57 | fList.Sorted:=true; |
58 | loadBuiltInFunctions; |
59 | loadCustomFunctions; |
60 | end; |
61 | |
62 | destructor TFunctions.Destroy; |
63 | var |
64 | item: string; |
65 | begin |
66 | for item in fDictionary.Keys do |
67 | fDictionary.AddOrSetValue(item, nil); |
68 | for item in fDictionary.Keys do |
69 | fObjDictionary.AddOrSetValue(item, nil); |
70 | fDictionary.Free; |
71 | fObjDictionary.Free; |
72 | fList.Free; |
73 | inherited; |
74 | end; |
75 | |
76 | procedure TFunctions.refreshFunctions; |
77 | begin |
78 | fDictionary.Clear; |
79 | loadBuiltInFunctions; |
80 | loadCustomFunctions; |
81 | end; |
82 | |
83 | procedure TFunctions.registerFunction(const aName: string; |
84 | const aFunc: TCasbinObjectFunc); |
85 | begin |
86 | if Trim(aName)='' then |
87 | raise Exception.Create('Obj Function to register must have a name'); |
88 | if not Assigned(aFunc) then |
89 | raise Exception.Create('Obj Function to register is nil'); |
90 | if Assigned(aFunc) then |
91 | fObjDictionary.AddOrSetValue(Trim(aName), aFunc); |
92 | end; |
93 | |
94 | function TFunctions.retrieveFunction(const aName: string): TCasbinFunc; |
95 | begin |
96 | if not fDictionary.ContainsKey(Trim(aName)) then |
97 | raise Exception.Create('Function '+aName+' is not registered'); |
98 | Result:=fDictionary.Items[Trim(aName)]; |
99 | end; |
100 | |
101 | procedure TFunctions.registerFunction(const aName: string; |
102 | const aFunc: TCasbinFunc); |
103 | begin |
104 | if Trim(aName)='' then |
105 | raise Exception.Create('Function to register must have a name'); |
106 | if not Assigned(aFunc) then |
107 | raise Exception.Create('Function to register is nil'); |
108 | if Assigned(aFunc) then |
109 | fDictionary.AddOrSetValue(Trim(aName), aFunc); |
110 | end; |
111 | |
112 | function TFunctions.retrieveObjFunction(const aName: string): TCasbinObjectFunc; |
113 | begin |
114 | if not fObjDictionary.ContainsKey(Trim(aName)) then |
115 | raise Exception.Create('Function '+aName+' is not registered'); |
116 | Result:=fObjDictionary.Items[Trim(aName)]; |
117 | end; |
118 | |
119 | // Built-in functions |
120 | // In this section, built-in functions are imported |
121 | {$I ..\SourceCode\Common\Functions\Casbin.Functions.KeyMatch.pas} |
122 | {$I ..\SourceCode\Common\Functions\Casbin.Functions.KeyMatch2.pas} |
123 | {$I ..\SourceCode\Common\Functions\Casbin.Functions.KeyMatch3.pas} |
124 | {$I ..\SourceCode\Common\Functions\Casbin.Functions.RegExMatch.pas} |
125 | {$I ..\SourceCode\Common\Functions\Casbin.Functions.IPMatch.pas} |
126 | |
127 | function TFunctions.list: TStringList; |
128 | var |
129 | name: string; |
130 | begin |
131 | fList.Clear; |
132 | for name in fDictionary.Keys do |
133 | fList.add(name); |
134 | for name in fObjDictionary.Keys do |
135 | fList.add(name); |
136 | Result:=fList; |
137 | end; |
138 | |
139 | procedure TFunctions.loadBuiltInFunctions; |
140 | begin |
141 | fDictionary.Add('KeyMatch', KeyMatch); |
142 | fDictionary.Add('KeyMatch2', KeyMatch2); |
143 | fDictionary.Add('KeyMatch3', KeyMatch2); |
144 | fDictionary.Add('RegExMatch', regexMatch); |
145 | fDictionary.Add('IPMatch', IPMatch); |
146 | end; |
147 | |
148 | // Custom functions |
149 | // If you want to add more functions include the files here |
150 | // Then register it in loadCustomFunctions |
151 | // (see loadBuiltInFunctions for examples) |
152 | |
153 | { $I ..\SourceCode\Common\Functions\NewCustomFunction.pas} |
154 | |
155 | procedure TFunctions.loadCustomFunctions; |
156 | begin |
157 | // Add call to fDictionary.Add to register a custom function |
158 | end; |
159 | |
160 | end. |