17 using System.Collections;
18 using System.Collections.Generic;
21 using erminas.SmartAPI.Utils;
22 using erminas.SmartAPI.Utils.CachedCollections;
24 namespace erminas.SmartAPI.CMS.ServerManagement
33 CreateGroups = 1048576,
34 DeleteGroups = 524288,
35 AssignUsersToGroups = 2097152,
36 AdministerUserDefinedJobs = 16777216,
37 AdministerPlugins = 8388608,
38 AdministerDatabaseServers = 33554432,
39 AdministerApplicationServers = 67108864,
40 AdministerDirectoryServices = 134217728,
41 AdministerProjects = 268435456,
42 AdministerXCMSProjects = 536870912,
43 AdministerDeliveryServer = 1073741824
49 bool HasAccessToAssetManager {
get;
set; }
50 bool HasAccessToSmartEdit {
get;
set; }
51 bool HasAccessToSmartTree {
get;
set; }
53 bool IsServerManager {
get;
set; }
54 bool IsTemplateEditor {
get;
set; }
55 bool IsTranslationEditor {
get;
set; }
58 void SetIsModuleAssigned(
ModuleType moduleType,
bool assign);
68 internal class UserModuleAssignment : IUserModuleAssignment
70 private readonly IndexedRDList<ModuleType, IModule> _assignedModules;
72 private readonly User _user;
74 internal UserModuleAssignment(User user)
77 Session = _user.Session;
78 _assignedModules =
new IndexedRDList<ModuleType, IModule>(GetAssignedModules, module => module.Type,
87 public IEnumerator<IModule> GetEnumerator()
89 return _assignedModules.GetEnumerator();
92 public bool HasAccessToAssetManager
94 get {
return IsModuleAssigned(
ModuleType.Translation); }
95 set { SetIsModuleAssigned(
ModuleType.Translation, value); }
98 public bool HasAccessToSmartEdit
100 get {
return IsModuleAssigned(
ModuleType.SmartEdit); }
101 set { SetIsModuleAssigned(
ModuleType.SmartEdit, value); }
104 public bool HasAccessToSmartTree
106 get {
return IsModuleAssigned(
ModuleType.SmartTree); }
107 set { SetIsModuleAssigned(
ModuleType.SmartTree, value); }
110 public void InvalidateCache()
112 _assignedModules.InvalidateCache();
115 public bool IsModuleAssigned(
ModuleType moduleType)
117 return _assignedModules.ContainsKey(moduleType);
120 public bool IsServerManager
122 get {
return IsModuleAssigned(
ModuleType.ServerManager); }
123 set { SetIsModuleAssigned(
ModuleType.ServerManager, value); }
126 public bool IsTemplateEditor
128 get {
return IsModuleAssigned(
ModuleType.TemplateEditor); }
129 set { SetIsModuleAssigned(
ModuleType.TemplateEditor, value); }
132 public bool IsTranslationEditor
134 get {
return IsModuleAssigned(
ModuleType.Translation); }
135 set { SetIsModuleAssigned(
ModuleType.Translation, value); }
138 public void Refresh()
140 _assignedModules.Refresh();
152 IModule serverManagerModule;
153 if (!_assignedModules.TryGet(
ModuleType.ServerManager, out serverManagerModule))
158 return GetServerManagerRights(serverManagerModule);
162 var serverManagerModule = _user.Session.ServerManager.Modules[
ModuleType.ServerManager];
166 const string SAVE_SERVER_MANAGER_RIGHTS =
167 @"<MODULE guid=""{0}"" checked=""{1}"" servermanagerflag=""{2}""/>";
169 ExecuteSaveModules(SAVE_SERVER_MANAGER_RIGHTS.RQLFormat(serverManagerModule,
171 _assignedModules.InvalidateCache();
175 public ISession Session {
get;
private set; }
177 public void SetIsModuleAssigned(
ModuleType moduleType,
bool assign)
179 var module = _user.Session.ServerManager.Modules[moduleType];
181 var modulesToAssign =
new List<IModule> {module};
182 if (IsModuleDependentOnSmartEdit(moduleType))
184 var smartEditModule = _user.Session.ServerManager.Modules[
ModuleType.SmartEdit];
185 modulesToAssign.Add(smartEditModule);
187 string moduleAssignmentSubString = CreateModuleAssignmentSubString(modulesToAssign, assign);
188 ExecuteSaveModules(moduleAssignmentSubString);
191 public void SetModuleAssignment(IUserModuleAssignment otherAssignment)
193 var modulesToUnassign = this.Except(otherAssignment,
new NameEqualityComparer<IModule>());
195 string unassign = CreateModuleAssignmentSubString(modulesToUnassign,
false);
196 string assign = CreateModuleAssignmentSubString(otherAssignment,
true);
198 ExecuteSaveModules(unassign + assign);
203 private static string CreateModuleAssignmentSubString(IEnumerable<IModule> modulesToUnassign,
bool value)
205 return modulesToUnassign.Aggregate(
"",
207 s +
@"<MODULE guid=""{0}"" checked=""{1}"" />".RQLFormat(module, value));
210 private void ExecuteSaveModules(
string modules)
212 const string SAVE_MODULES =
213 @"<ADMINISTRATION><USER action=""save"" guid=""{0}""><MODULES>{1}</MODULES></USER></ADMINISTRATION>";
214 _user.Session.ExecuteRQL(SAVE_MODULES.RQLFormat(_user, modules));
215 _assignedModules.InvalidateCache();
218 private List<IModule> GetAssignedModules()
220 const string LIST_ASSIGNED_MODULES =
221 @"<ADMINISTRATION><MODULES action=""list"" userguid=""{0}"" countlicense=""1""/></ADMINISTRATION>";
222 var xmlDoc = _user.Session.ExecuteRQL(LIST_ASSIGNED_MODULES.RQLFormat(_user));
225 var modules = xmlDoc.GetElementsByTagName(
"MODULE").Cast<XmlElement>().ToList();
228 (from curModule in modules
229 where IsAssignedModule(curModule)
230 select (IModule)
new Module(Session, curModule)).ToList();
233 IEnumerator IEnumerable.GetEnumerator()
235 return GetEnumerator();
242 ((Module) serverManagerModule).XmlElement.GetIntAttributeValue(
"servermanagerflag").GetValueOrDefault();
245 private static bool IsAssignedModule(XmlElement curModule)
247 return curModule.GetBoolAttributeValue(
"checked").GetValueOrDefault();
250 private static bool IsModuleDependentOnSmartEdit(
ModuleType moduleType)
261 (rights & DEPENDENT_ON_ASSIGN_USER_TO_GROUP) != 0)
290 value = ResolveAssignUsersToGroupDependencies(value);
291 value = ResolveEditUsersDependencies(value);
292 value = ResolveDepenciesOfAdministerDirectoryServices(value);