-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Expand file tree
/
Copy pathUserGroup.cs
More file actions
197 lines (168 loc) · 5.66 KB
/
Copy pathUserGroup.cs
File metadata and controls
197 lines (168 loc) · 5.66 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System.Collections;
using System.Runtime.Serialization;
using Umbraco.Cms.Core.Models.Entities;
using Umbraco.Cms.Core.Models.Membership.Permissions;
using Umbraco.Cms.Core.Strings;
using Umbraco.Extensions;
namespace Umbraco.Cms.Core.Models.Membership;
/// <summary>
/// Represents a Group for a Backoffice User
/// </summary>
[Serializable]
[DataContract(IsReference = true)]
public class UserGroup : EntityBase, IUserGroup, IReadOnlyUserGroup
{
// Custom comparer for enumerable
private static readonly DelegateEqualityComparer<IEnumerable<string>> _stringEnumerableComparer =
new(
(enum1, enum2) => enum1.UnsortedSequenceEqual(enum2),
enum1 => enum1.GetHashCode());
private static readonly DelegateEqualityComparer<ISet<IGranularPermission>> _granularPermissionSetComparer =
new(
(set1, set2) => Equals(set1, set2),
set => set.GetHashCode());
private readonly IShortStringHelper _shortStringHelper;
private string _alias;
private string? _icon;
private string _name;
private string? _description;
private bool _hasAccessToAllLanguages;
private ISet<string> _permissions;
private ISet<IGranularPermission> _granularPermissions;
private List<string> _sectionCollection;
private List<int> _languageCollection;
private int? _startContentId;
private int? _startMediaId;
/// <summary>
/// Constructor to create a new user group
/// </summary>
public UserGroup(IShortStringHelper shortStringHelper)
{
_alias = string.Empty;
_name = string.Empty;
_shortStringHelper = shortStringHelper;
_sectionCollection = new List<string>();
_languageCollection = new List<int>();
_permissions = new HashSet<string>();
_granularPermissions = new HashSet<IGranularPermission>();
}
/// <summary>
/// Constructor to create an existing user group
/// </summary>
/// <param name="userCount"></param>
/// <param name="alias"></param>
/// <param name="name"></param>
/// <param name="icon"></param>
/// <param name="shortStringHelper"></param>
public UserGroup(
IShortStringHelper shortStringHelper,
int userCount,
string? alias,
string? name,
string? icon)
: this(shortStringHelper)
{
UserCount = userCount;
_alias = alias ?? string.Empty;
_name = name ?? string.Empty;
_icon = icon;
}
[DataMember]
public int? StartMediaId
{
get => _startMediaId;
set => SetPropertyValueAndDetectChanges(value, ref _startMediaId, nameof(StartMediaId));
}
[DataMember]
public int? StartContentId
{
get => _startContentId;
set => SetPropertyValueAndDetectChanges(value, ref _startContentId, nameof(StartContentId));
}
[DataMember]
public string? Icon
{
get => _icon;
set => SetPropertyValueAndDetectChanges(value, ref _icon, nameof(Icon));
}
[DataMember]
public string Alias
{
get => _alias;
set => SetPropertyValueAndDetectChanges(
value.ToCleanString(_shortStringHelper, CleanStringType.Alias | CleanStringType.UmbracoCase), ref _alias!,
nameof(Alias));
}
[DataMember]
public string? Name
{
get => _name;
set => SetPropertyValueAndDetectChanges(value, ref _name!, nameof(Name));
}
[DataMember]
public string? Description
{
get => _description;
set => SetPropertyValueAndDetectChanges(value, ref _description!, nameof(Description));
}
[DataMember]
public bool HasAccessToAllLanguages
{
get => _hasAccessToAllLanguages;
set => SetPropertyValueAndDetectChanges(value, ref _hasAccessToAllLanguages, nameof(HasAccessToAllLanguages));
}
/// <inheritdoc />
public ISet<string> Permissions
{
get => _permissions;
set => SetPropertyValueAndDetectChanges(value, ref _permissions!, nameof(Permissions), _stringEnumerableComparer);
}
public ISet<IGranularPermission> GranularPermissions
{
get => _granularPermissions;
set => SetPropertyValueAndDetectChanges(value, ref _granularPermissions!, nameof(GranularPermissions), _granularPermissionSetComparer);
}
public IEnumerable<string> AllowedSections => _sectionCollection;
public int UserCount { get; }
public void RemoveAllowedSection(string sectionAlias)
{
if (_sectionCollection.Contains(sectionAlias))
{
_sectionCollection.Remove(sectionAlias);
}
}
public void AddAllowedSection(string sectionAlias)
{
if (_sectionCollection.Contains(sectionAlias) == false)
{
_sectionCollection.Add(sectionAlias);
}
}
public IEnumerable<int> AllowedLanguages
{
get => _languageCollection;
}
public void RemoveAllowedLanguage(int languageId)
{
if (_languageCollection.Contains(languageId))
{
_languageCollection.Remove(languageId);
}
}
public void AddAllowedLanguage(int languageId)
{
if (_languageCollection.Contains(languageId) == false)
{
_languageCollection.Add(languageId);
}
}
public void ClearAllowedLanguages() => _languageCollection.Clear();
public void ClearAllowedSections() => _sectionCollection.Clear();
protected override void PerformDeepClone(object clone)
{
base.PerformDeepClone(clone);
var clonedEntity = (UserGroup)clone;
// manually clone the start node props
clonedEntity._sectionCollection = new List<string>(_sectionCollection);
}
}