18 using erminas.SmartAPI.CMS.Project;
19 using erminas.SmartAPI.Exceptions;
20 using erminas.SmartAPI.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
23 namespace erminas.SmartAPI.CMS.ServerManagement
46 bool IsTemplateEditor {
get;
set; }
47 bool IsTranslationEditor {
get;
set; }
54 internal class UserProjectAssignment : IUserProjectAssignment
57 private readonly IUser _user;
58 private bool _isInitialized;
59 private bool? _isTemplateEditor;
60 private bool? _isTranslationEditor;
63 internal UserProjectAssignment(IUser user,
IProject project)
70 internal UserProjectAssignment(IUser user, XmlElement projectAssignment)
73 _session = _user.Session;
74 LoadXml(projectAssignment);
75 _isInitialized =
true;
85 IsTranslationEditor = extendedUserRoles.HasFlag(
ExtendedUserRoles.TranslationEditor);
86 _isInitialized =
true;
91 EnsureInitialization();
92 const string SAVE_USER_RIGHTS =
93 @"<ADMINISTRATION><USER action=""save"" guid=""{0}""><PROJECTS><PROJECT guid=""{1}"" checked=""1"" lm=""{2}"" te=""{3}"" userlevel=""{4}""/></PROJECTS></USER></ADMINISTRATION>";
95 User.Session.ExecuteRQL(SAVE_USER_RIGHTS.RQLFormat(User, Project, IsTranslationEditor, IsTemplateEditor,
101 Delete(Project, User);
104 public void InvalidateCache()
107 _isTemplateEditor = null;
108 _isTranslationEditor = null;
109 _isInitialized =
false;
112 public bool IsTemplateEditor
116 EnsureInitialization();
118 return _isTemplateEditor.Value;
121 set { _isTemplateEditor = value; }
124 public bool IsTranslationEditor
128 EnsureInitialization();
130 return _isTranslationEditor.Value;
133 set { _isTranslationEditor = value; }
136 public IProject Project {
get;
private set; }
138 public void Refresh()
141 EnsureInitialization();
144 public IUserProjectAssignment Refreshed()
150 public ISession Session
152 get {
return _session; }
157 get {
return _user; }
164 EnsureInitialization();
166 return _userRole.Value;
169 set { _userRole = value; }
172 internal static IUserProjectAssignment Create(IUser user,
IProject project,
UserRole role,
175 var assignment =
new UserProjectAssignment(user, project, role, extendedUserRoles);
178 user.Projects.InvalidateCache();
179 project.
Users.InvalidateCache();
184 internal static void Delete(
IProject project, IUser user)
186 const string UNASSING_PROJECT =
187 @"<ADMINISTRATION><USER action=""save"" guid=""{0}""><PROJECTS><PROJECT guid=""{1}"" checked=""0""/></PROJECTS><CCSCONNECTIONS/></USER></ADMINISTRATION>";
191 project.
Users.InvalidateCache();
192 user.Projects.InvalidateCache();
195 private void EnsureInitialization()
199 var xml = RetrieveObjectElement();
201 _isInitialized =
true;
205 private bool HasRight(XmlElement projectElement,
string attributeName)
207 var intAttributeValue = projectElement.GetIntAttributeValue(attributeName);
208 if (intAttributeValue == null)
211 string.Format(
"Missing attribute '{0}' in user/project assignment",
215 return intAttributeValue.Value == -1;
218 private void LoadXml(XmlElement projectAssignment)
222 Project =
new Project.Project(_user.Session, projectAssignment.GetGuid())
224 Name = projectAssignment.GetName()
229 if (_userRole == null)
231 _userRole = (
UserRole) projectAssignment.GetIntAttributeValue(
"userlevel").GetValueOrDefault();
233 if (_isTemplateEditor == null)
235 _isTemplateEditor = HasRight(projectAssignment,
"templateeditorright");
237 if (_isTranslationEditor == null)
239 _isTranslationEditor = HasRight(projectAssignment,
"languagemanagerright");
243 private XmlElement RetrieveObjectElement()
245 const string LOAD_ACCESS_LEVEL =
246 @"<ADMINISTRATION><USER guid=""{0}"" ><PROJECT guid=""{1}"" action=""load"" extendedinfo=""1""/></USER></ADMINISTRATION>";
247 var xmlDoc = Project.ExecuteRQL(LOAD_ACCESS_LEVEL.RQLFormat(User, Project));
248 return xmlDoc.GetSingleElement(
"PROJECT");