forked from PeterWaher/IoTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrivilegePattern.cs
More file actions
87 lines (78 loc) · 2.04 KB
/
PrivilegePattern.cs
File metadata and controls
87 lines (78 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Text.RegularExpressions;
using Waher.Events;
using Waher.Persistence.Attributes;
namespace Waher.Security.Users
{
/// <summary>
/// Contains a reference to a privilege
/// </summary>
[TypeName(TypeNameSerialization.None)]
public sealed class PrivilegePattern
{
private string expression = string.Empty;
private bool include = true;
private Regex regex = null;
/// <summary>
/// Contains a reference to a privilege
/// </summary>
public PrivilegePattern()
{
}
/// <summary>
/// Contains a reference to a privilege
/// </summary>
/// <param name="Pattern">Regular expression.</param>
/// <param name="Include">If privileges matching the pattern should be included (true) or excluded (false).</param>
public PrivilegePattern(string Pattern, bool Include)
{
this.expression = Pattern;
this.regex = new Regex(Pattern, RegexOptions.Singleline);
this.include = Include;
}
/// <summary>
/// Privilege ID regular expression to match against.
/// </summary>
public string Expression
{
get => this.expression;
set
{
this.expression = value;
try
{
this.regex = new Regex(this.expression, RegexOptions.Singleline);
}
catch (Exception ex)
{
Log.Error("Invalid regular expression:\r\n" + this.expression + "\r\n\r\nError reported.\r\n" + ex.Message);
this.regex = null;
}
}
}
/// <summary>
/// If privileges matching the pattern are included (true) or excluded (false).
/// </summary>
[DefaultValue(true)]
public bool Include
{
get => this.include;
set => this.include = value;
}
/// <summary>
/// If the privilege is included.
/// </summary>
/// <param name="Privilege">Full Privilege Id</param>
/// <returns>true=yes, false=no, null=not applicable</returns>
public bool? IsIncluded(string Privilege)
{
if (this.regex is null)
return null;
Match M = this.regex.Match(Privilege);
if (M.Success && M.Index == 0 && M.Length == Privilege.Length)
return this.include;
else
return null;
}
}
}