-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Partial privacy mode was added (#32)
- Loading branch information
1 parent
e73a30e
commit 9a453d1
Showing
7 changed files
with
114 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...iFactor.Radius.Adapter/Configuration/Features/PrivacyModeFeature/PrivacyModeDescriptor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System; | ||
using System.Linq; | ||
|
||
namespace MultiFactor.Radius.Adapter.Configuration.Features.PrivacyModeFeature | ||
{ | ||
public class PrivacyModeDescriptor | ||
{ | ||
private readonly string[] _fields; | ||
public PrivacyMode Mode { get; } | ||
|
||
public bool HasField(string field) | ||
{ | ||
if (string.IsNullOrWhiteSpace(field)) | ||
{ | ||
return false; | ||
} | ||
|
||
return _fields.Any(x => x.Equals(field, StringComparison.OrdinalIgnoreCase)); | ||
} | ||
|
||
private PrivacyModeDescriptor(PrivacyMode mode, params string[] fields) | ||
{ | ||
Mode = mode; | ||
_fields = fields ?? throw new ArgumentNullException(nameof(fields)); | ||
} | ||
|
||
public static PrivacyModeDescriptor Create(string value) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value)) return new PrivacyModeDescriptor(PrivacyMode.None); | ||
|
||
var mode = GetMode(value); | ||
if (mode != PrivacyMode.Partial) return new PrivacyModeDescriptor(mode); | ||
|
||
var fields = GetFields(value); | ||
return new PrivacyModeDescriptor(mode, fields); | ||
} | ||
|
||
private static PrivacyMode GetMode(string value) | ||
{ | ||
var index = value.IndexOf(':'); | ||
if (index == -1) | ||
{ | ||
if (!Enum.TryParse<PrivacyMode>(value, true, out var parsed1)) throw new Exception("Unexpected privacy-mode value"); | ||
return parsed1; | ||
} | ||
|
||
var sub = value.Substring(0, index); | ||
if (!Enum.TryParse<PrivacyMode>(sub, true, out var parsed2)) throw new Exception("Unexpected privacy-mode value"); | ||
|
||
return parsed2; | ||
} | ||
|
||
private static string[] GetFields(string value) | ||
{ | ||
var index = value.IndexOf(':'); | ||
if (index == -1 || value.Length <= index + 1) | ||
{ | ||
return Array.Empty<string>(); | ||
} | ||
|
||
var sub = value.Substring(index + 1); | ||
return sub.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters