Coverage report for Casbin.Policy.Types.

Generated at 18/02/2019 12:10:19 by DelphiCodeCoverage - an open source tool for Delphi Code Coverage.

Statistics for Casbin.Policy.Types.pas

Number of lines covered7
Number of lines with code gen8
Line coverage87%


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.Policy.Types;
15
16
interface
17
18
uses
19
  Casbin.Core.Base.Types, Casbin.Model.Sections.Types,
20
  System.Generics.Collections, System.Rtti, System.Types, Casbin.Watcher.Types, Casbin.Adapter.Types, Casbin.Adapter.Policy.Types;
21
22
const
23
  DefaultDomain = 'default';
24
25
type
26
  TRoleNode = class
27
  private
28
    fDomain: string;
29
    fID: string;
30
    fValue: string;
31
  public
32
    constructor Create(const aValue: String; const aDomain: string = DefaultDomain);
33
    property Domain: string read fDomain;
34
    property Value: string read fValue;
35
    property ID: string read fID write fID;
36
  end;
37
38
  TRoleMode = (
39
    {$REGION 'Returns all the roles and those inferred by the policies'}
40
    /// <summary>
41
    ///   Returns all the roles and those inferred by the policies
42
    /// </summary>
43
    {$ENDREGION}
44
    rmImplicit,
45
    {$REGION 'Returns only the roles'}
46
    /// <summary>
47
    ///   Returns only the roles
48
    /// </summary>
49
    {$ENDREGION}
50
    rmNonImplicit);
51
52
  IPolicyManager = interface (IBaseInterface)
53
    ['{B983A830-6107-4283-A45D-D74CDBB5E2EA}']
54
    function section (const aSlim: Boolean = true): string;
55
    function toOutputString: string;
56
    function getAdapter: IPolicyAdapter;
57
58
    // Policies
59
    function policies: TList<string>;
60
    procedure addPolicy (const aSection: TSectionType; const aTag: string;
61
                              const aAssertion: string); overload;
62
    procedure addPolicy (const aSection: TSectionType;
63
                              const aAssertion: string); overload;
64
    procedure load (const aFilter: TFilterArray = []);
65
    function policy (const aFilter: TFilterArray = []): string;
66
    procedure clear;
67
    function policyExists (const aFilter: TFilterArray = []): Boolean;
68
69
    {$REGION ''}
70
    /// <param name="aRoleMode">
71
    ///   <para>
72
    ///     rmImplicit: Deletes all roles from both 'g' and 'p' sections
73
    ///   </para>
74
    ///   <para>
75
    ///     rmNonImplicit: Deletes roles only in 'g' sections
76
    ///   </para>
77
    /// </param>
78
    {$ENDREGION}
79
    procedure removePolicy (const aFilter: TFilterArray = [];
80
            const aRoleMode: TRoleMode = rmImplicit);
81
82
    // Roles
83
    procedure clearRoles;
84
    function roles: TList<string>;
85
    function domains: TList<string>;
86
    function roleExists (const aFilter: TFilterArray = []): Boolean;
87
88
    {$REGION 'Adds the inheritance link between two roles'}
89
    /// <summary>
90
    ///   Adds the inheritance link between two roles
91
    /// </summary>
92
    /// <example>
93
    ///   addLink(name1, name2) adds a link between role:name 1 and role: name2
94
    /// </example>
95
    {$ENDREGION}
96
    procedure addLink(const aBottom: string; const aTop: string); overload;
97
    procedure addLink(const aBottom: string;
98
                      const aTopDomain: string; const aTop: string); overload;
99
    {$REGION 'Adds the inheritance link between two roles by passign domains'}
100
    /// <summary>
101
    ///   Adds the inheritance link between two roles by passing domains
102
    /// </summary>
103
    /// <example>
104
    ///   addLink(domain1, name1, domain2, name2) adds a link between role:name
105
    ///   1 in domain1 and role: name2 in domain 2
106
    /// </example>
107
    {$ENDREGION}
108
    procedure addLink(const aBottomDomain: string; const aBottom: string;
109
                      const aTopDomain: string; const aTop: string); overload;
110
    procedure removeLink(const aLeft: string; const aRight: string); overload;
111
    procedure removeLink(const aLeft: string;
112
                      const aRightDomain: string; const aRight: string); overload;
113
    procedure removeLink(const aLeftDomain: string; const aLeft: string;
114
                      const aRightDomain: string; const aRight: string); overload;
115
    function linkExists(const aLeft: string; const aRight: string):boolean; overload;
116
    function linkExists(const aLeft: string;
117
                      const aRightDomain: string; const aRight: string):boolean; overload;
118
    function linkExists(const aLeftDomain: string; const aLeft: string;
119
                      const aRightDomain: string; const aRight: string):boolean; overload;
120
121
    function rolesForEntity(const aEntity: string; const aDomain: string = '';
122
        const aRoleMode: TRoleMode = rmNonImplicit): TStringDynArray;
123
124
    function entitiesForRole (const aEntity: string; const aDomain: string =''):TStringDynArray;
125
126
    // Watchers
127
    procedure registerWatcher (const aWatcher: IWatcher);
128
    procedure unregisterWatcher (const aWatcher: IWatcher);
129
    procedure notifyWatchers;
130
131
    // Permissions
132
    {$REGION ''}
133
    /// <remarks>
134
    ///   Assumes the last part of a policy (assertion) statement indicates the
135
    ///   permission
136
    /// </remarks>
137
    {$ENDREGION}
138
    function permissionsForEntity (const aEntity: string): TStringDynArray;
139
    function permissionExists (const aEntity: string; const aPermission: string):
140
                                                              Boolean;
141
142
    // Adapter
143
    property Adapter: IPolicyAdapter read getAdapter;
144
145
  end;
146
147
implementation
148
149
uses
150
  System.SysUtils;
151
152
constructor TRoleNode.Create(const aValue: String; const aDomain: string =
153
    DefaultDomain);
154
var
155
  guid: TGUID;
156
begin
157
  inherited Create;
158
  fValue:=Trim(aValue);
159
  fDomain:=Trim(aDomain);
160
  if CreateGUID(guid) <> 0 then
161
    fID:=GUIDToString(TGUID.Empty)
162
  else
163
    fID:=GUIDToString(guid);
164
end;
165
166
end.