SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IAuthorizations.cs
Go to the documentation of this file.
1 ï»¿// SmartAPI - .Net programmatic access to RedDot servers
2 //
3 // Copyright (C) 2013 erminas GbR
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free Software Foundation,
7 // either version 3 of the License, or (at your option) any later version.
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 // See the GNU General Public License for more details.
12 //
13 // You should have received a copy of the GNU General Public License along with this program.
14 // If not, see <http://www.gnu.org/licenses/>.
15 
16 using System.Collections.Generic;
17 using System.Linq;
18 using System.Xml;
19 using erminas.SmartAPI.CMS.Project.ContentClasses.Elements;
20 using erminas.SmartAPI.CMS.Project.Pages;
21 using erminas.SmartAPI.CMS.Project.Pages.Elements;
22 using erminas.SmartAPI.CMS.ServerManagement;
23 using erminas.SmartAPI.Exceptions;
24 using erminas.SmartAPI.Utils;
25 using erminas.SmartAPI.Utils.CachedCollections;
26 
27 namespace erminas.SmartAPI.CMS.Project.Authorizations
28 {
29  //public interface IBaseAuthorizationPackage<T> : IRDList<T>, IProjectObject where T : class, IRedDotObject
30  //{
31  // /// <summary>
32  // /// Used after setting the Name property to commit the change on the server
33  // /// </summary>
34  // void Commit();
35 
36  // /// <summary>
37  // /// Rename the authorization package on the server.
38  // /// Same as setting the Name property and calling Commit().
39  // /// </summary>
40  // void Rename(string newName);
41 
42  // void Add(T element);
43  // void Remove(T element);
44  //}
45 
46  //public interface IDetailedPageAuthorizationPackage : IBaseAuthorizationPackage<IPage>
47  //{
48  // IUserPageAuthorizations UserAuthorizations { get; }
49  // IGroupPageAuthorizations GroupAuthorizations { get; }
50  //}
51 
52  //public interface IDetailedLinkAuthorizationPackage : IBaseAuthorizationPackage<ILinkElement>
53  //{
54  // IUserLinkAuthorizations UserAuthorizations { get; }
55  // IGroupLinkAuthorizations GroupAuthorizations { get; }
56  //}
57 
58  //public interface IContentClassAuthorizationPackage : IBaseAuthorizationPackage<IContentClass>
59  //{
60  // IUserContentClassAuthorizations UserAuthorizations { get; }
61  // IGroupContentClassAuthorizations GroupAuthorizations { get; }
62  //}
63 
67  public interface IGroupAuthorizations : IIndexedCachedList<string, IGroupAuthorization>
68  {
69  IGroupAuthorization CreateFor(IGroup group);
70  }
71 
75  public interface IUserAuthorizations : IIndexedCachedList<string, IUserAuthorization>
76  {
77  IUserAuthorization CreateFor(IUser user);
78  }
79 
81  {
82  void Commit();
83  IGroupAuthorizations GroupAuthorizations { get; }
84 
85  new string Name { get; set; }
89  void Rename(string newName);
90 
91  int Type { get; }
92  IUserAuthorizations UserAuthorizations { get; }
93  }
94 
96  {
97  void CreateDetailForElement(IPageElement pageElement, string name);
98  void CreateDetailForElement(IContentClassElement element, string name);
99  void CreateDetailForLink(ILinkElement link, string name);
100  void CreateDetailForLink(IContentClassElement element, string name);
101  void CreateDetailForPage(IPage page, string name);
102  void CreateGlobal(string name);
103  IIndexedCachedList<string, IAuthorizationPackage> ForElements { get; }
104  IIndexedCachedList<string, IAuthorizationPackage> ForLinks { get; }
105  IIndexedCachedList<string, IAuthorizationPackage> ForPages { get; }
106  IIndexedCachedList<string, IAuthorizationPackage> Standard { get; }
107  }
108 
109  public enum AuthorizationType
110  {
111  Standard = 0,
112  DetailedPage = 1,
113  DetailedLink = 2,
114  DetailedElement = 4,
115  DetailedAssetManagerAttribute = 8,
116  ContentClass = 16,
117  ProjectVariant = 32,
118  Folder = 64,
119  LanguageVariant = 128
120  }
121 
122  internal class AuthorizationPackages : IAuthorizationPackages
123  {
124  public AuthorizationPackages(IProject project)
125  {
126  Project = project;
127  ForLinks = new NameIndexedRDList<IAuthorizationPackage>(GetForLinks, Caching.Enabled);
128  ForElements = new NameIndexedRDList<IAuthorizationPackage>(GetForElements, Caching.Enabled);
129  ForPages = new NameIndexedRDList<IAuthorizationPackage>(GetForPages, Caching.Enabled);
130  Standard = new NameIndexedRDList<IAuthorizationPackage>(GetStandard, Caching.Enabled);
131  }
132 
133  public void CreateDetailForElement(IPageElement pageElement, string name)
134  {
135  CreateDetail("ELEMENT", pageElement, name, AuthorizationType.DetailedElement);
136  ForElements.InvalidateCache();
137  }
138 
139  public void CreateDetailForElement(IContentClassElement element, string name)
140  {
141  CreateDetail("ELEMENT", element, name, AuthorizationType.DetailedElement);
142  ForElements.InvalidateCache();
143  }
144 
145  public void CreateDetailForLink(ILinkElement link, string name)
146  {
147  CreateDetail("LINK", link, name, AuthorizationType.DetailedLink);
148  ForLinks.InvalidateCache();
149  }
150 
151  public void CreateDetailForLink(IContentClassElement element, string name)
152  {
153  CreateDetail("LINK", element, name, AuthorizationType.DetailedLink);
154  ForLinks.InvalidateCache();
155  ;
156  }
157 
158  public void CreateDetailForPage(IPage page, string name)
159  {
160  CreateDetail("PAGE", page, name, AuthorizationType.DetailedPage);
161  ForPages.InvalidateCache();
162  }
163 
164  public void CreateGlobal(string name)
165  {
166  const string CREATE =
167  @"<AUTHORIZATION><AUTHORIZATIONPACKET action=""addnew"" name=""{0}""/></AUTHORIZATION>";
168  var answer = Project.ExecuteRQL(CREATE.RQLFormat(name));
169  CheckAnswer(name, answer);
170  }
171 
172  public IIndexedCachedList<string, IAuthorizationPackage> ForElements { get; private set; }
173  public IIndexedCachedList<string, IAuthorizationPackage> ForLinks { get; private set; }
174  public IIndexedCachedList<string, IAuthorizationPackage> ForPages { get; private set; }
175  public IProject Project { get; private set; }
176 
177  public ISession Session
178  {
179  get { return Project.Session; }
180  }
181 
182  public IIndexedCachedList<string, IAuthorizationPackage> Standard { get; private set; }
183 
184  private void CheckAnswer(string name, XmlDocument answer)
185  {
186  try
187  {
188  var element = answer.GetSingleElement("AUTHORIZATIONPACKET");
189  if (element.GetName() != name)
190  {
191  throw new SmartAPIException(Session.ServerLogin,
192  string.Format("Could not create authorizationpackage {0}", name));
193  }
194  } catch (SmartAPIInternalException e)
195  {
196  throw new SmartAPIException(Session.ServerLogin,
197  string.Format("Could not create authorizationpackage {0}", name), e);
198  }
199  }
200 
201  private void CreateDetail(string elementtype, IRedDotObject obj, string name, AuthorizationType type)
202  {
203  const string CREATE =
204  @"<AUTHORIZATION><{0} guid=""{1}><AUTHORIZATIONPACKET action=""addnew"" name=""{2}"" guid="" type=""{3}""/></{0}></AUTHORIZATION>";
205 
206  string query = CREATE.RQLFormat(elementtype, obj, name, (int) type);
207 
208  var answer = Project.ExecuteRQL(query);
209  CheckAnswer(name, answer);
210  Standard.InvalidateCache();
211  }
212 
213  private List<IAuthorizationPackage> GetAuthorizationPackages(AuthorizationType type)
214  {
215  const string LIST_PACKAGES =
216  @"<AUTHORIZATION><AUTHORIZATIONS action=""list"" type=""{0}""/></AUTHORIZATION>";
217  var xmlDoc = Project.ExecuteRQL(LIST_PACKAGES.RQLFormat((int) type));
218 
219  return (from XmlElement curelement in xmlDoc.GetElementsByTagName("AUTHORIZATION")
220  select (IAuthorizationPackage) new AuthorizationPackage(Project, curelement)).ToList();
221  }
222 
223  private List<IAuthorizationPackage> GetForElements()
224  {
225  return GetAuthorizationPackages(AuthorizationType.DetailedElement);
226  }
227 
228  private List<IAuthorizationPackage> GetForLinks()
229  {
230  return GetAuthorizationPackages(AuthorizationType.DetailedLink);
231  }
232 
233  private List<IAuthorizationPackage> GetForPages()
234  {
235  return GetAuthorizationPackages(AuthorizationType.DetailedPage);
236  }
237 
238  private List<IAuthorizationPackage> GetStandard()
239  {
240  return GetAuthorizationPackages(AuthorizationType.Standard);
241  }
242  }
243 
244  internal class AuthorizationPackage : RedDotProjectObject, IAuthorizationPackage
245  {
246  public AuthorizationPackage(IProject project, XmlElement xmlElement) : base(project, xmlElement)
247  {
248  Type = xmlElement.GetIntAttributeValue("type").GetValueOrDefault();
249  GroupAuthorizations = new GroupAuthorizations(this, Caching.Enabled);
250  UserAuthorizations = new UserAuthorizations(this, Caching.Enabled);
251  }
252 
253  public void Commit()
254  {
255  const string SAVE_NAME =
256  @"<AUTHORIZATION><AUTHORIZATIONPACKET action=""save"" guid=""{0}"" name=""{1}""/></AUTHORIZATION>";
257  Project.ExecuteRQL(SAVE_NAME.RQLFormat(this, Name));
258  }
259 
260  public IGroupAuthorizations GroupAuthorizations { get; private set; }
261  public new string Name { get { return base.Name; } set { base.Name = value; } }
262 
263  public void Rename(string newName)
264  {
265  Name = newName;
266  Commit();
267  }
268 
269  public int Type { get; private set; }
270 
271  public IUserAuthorizations UserAuthorizations { get; private set; }
272  }
273 
274  internal class GroupAuthorizations : IndexedCachedList<string, IGroupAuthorization>, IGroupAuthorizations
275  {
276  private readonly IAuthorizationPackage _package;
277 
278  public GroupAuthorizations(IAuthorizationPackage package, Caching caching)
279  : base(authorization => authorization.Group.Name, caching)
280  {
281  _package = package;
282  RetrieveFunc = GetAuthorizations;
283  }
284 
285  public IGroupAuthorization CreateFor(IGroup @group)
286  {
287  return new GroupAuthorizationRights(_package, group);
288  }
289 
290  private List<IGroupAuthorization> GetAuthorizations()
291  {
292  const string LOAD_AUTH =
293  @"<AUTHORIZATION><AUTHORIZATIONPACKET action=""load"" guid=""{0}""/></AUTHORIZATION>";
294 
295  var xmlDoc = _package.Project.ExecuteRQL(LOAD_AUTH.RQLFormat(_package));
296 
297  IGroup curGroup = null;
298  return (from XmlElement curElement in xmlDoc.GetElementsByTagName("GROUP")
299  where _package.Project.AssignedGroups.TryGetByGuid(curElement.GetGuid(), out curGroup)
300  select (IGroupAuthorization) new GroupAuthorizationRights(_package, curGroup, curElement)).ToList();
301  }
302  }
303 
304  internal class UserAuthorizations : IndexedCachedList<string, IUserAuthorization>, IUserAuthorizations
305  {
306  private readonly IAuthorizationPackage _package;
307 
308  public UserAuthorizations(IAuthorizationPackage package, Caching caching)
309  : base(authorization => authorization.User.Name, caching)
310  {
311  _package = package;
312  RetrieveFunc = GetAuthorizations;
313  }
314 
315  public IUserAuthorization CreateFor(IUser @group)
316  {
317  return new UserAuthorizationRights(_package, group);
318  }
319 
320  private List<IUserAuthorization> GetAuthorizations()
321  {
322  const string LOAD_AUTH =
323  @"<AUTHORIZATION><AUTHORIZATIONPACKET action=""load"" guid=""{0}""/></AUTHORIZATION>";
324 
325  var xmlDoc = _package.Project.ExecuteRQL(LOAD_AUTH.RQLFormat(_package));
326 
327  IUserProjectAssignment curUser = null;
328  return (from XmlElement curElement in xmlDoc.GetElementsByTagName("USER")
329  where _package.Project.Users.TryGetByUserGuid(curElement.GetGuid(), out curUser)
330  select (IUserAuthorization) new UserAuthorizationRights(_package, curUser.User, curElement)).ToList();
331  }
332  }
333 
335  {
336  IAuthorizationPackage AuthorizationPackage { get; }
337 
338  void Commit();
339  int DeniedAssetManagerAttributeRights { get; set; }
340  int DeniedAssetManagerFolderRights { get; set; }
341  int DeniedContentClassRights { get; set; }
342 
343  int DeniedElementRights { get; set; }
344  int DeniedGlobalRights { get; set; }
345  int DeniedPageRights { get; set; }
346  int DeniedProjectOrLanguageVariantRights { get; set; }
347 
348  int DeniedStructuralElementRights { get; set; }
349 
350  int GrantedAssetManagerAttributeRights { get; set; }
351  int GrantedAssetManagerFolderRights { get; set; }
352 
353  int GrantedContentClassRights { get; set; }
354  int GrantedElementRights { get; set; }
355 
356  int GrantedGlobalRights { get; set; }
357  int GrantedPageRights { get; set; }
358  int GrantedProjectOrLanguageVariantRights { get; set; }
359  int GrantedStructuralElementRights { get; set; }
360 
361  //PageRights GrantedPageRights { get; set; }
362  //PageRights DeniedPageRights { get; set; }
363 
364  //ElementRights GrantedElementRights { get; set; }
365  //ElementRights DeniedElementRights { get; set; }
366 
367  //ContentClassRights GrantedContentClassRights { get; set; }
368  //ContentClassRights DeniedContentClassRights { get; set; }
369 
370  //ProjectVariantRights GrantedProjectVariantRights { get; set; }
371  //ProjectVariantRights DeniedProjectVariantRights { get; set; }
372 
373  //LanguageVariantRights GrantedLanguageVariantRights { get; set; }
374  //LanguageVariantRights DeniedLanguageVariantRights { get; set; }
375  }
376 
378  {
379  IUser User { get; }
380  }
381 
383  {
384  IGroup Group { get; }
385  }
386 
387  internal abstract class AuthorizationRights : IAuthorizationRights
388  {
389  private readonly IAuthorizationPackage _package;
390 
391  public AuthorizationRights(IAuthorizationPackage package, XmlElement element)
392  {
393  _package = package;
394 
395  GrantedPageRights = element.GetIntAttributeValue("right1").GetValueOrDefault();
396  DeniedPageRights = element.GetIntAttributeValue("deny1").GetValueOrDefault();
397 
398  GrantedStructuralElementRights = element.GetIntAttributeValue("right2").GetValueOrDefault();
399  DeniedStructuralElementRights = element.GetIntAttributeValue("deny2").GetValueOrDefault();
400 
401  GrantedElementRights = element.GetIntAttributeValue("right3").GetValueOrDefault();
402  DeniedElementRights = element.GetIntAttributeValue("deny3").GetValueOrDefault();
403 
404  GrantedGlobalRights = element.GetIntAttributeValue("right4").GetValueOrDefault();
405  DeniedGlobalRights = element.GetIntAttributeValue("deny4").GetValueOrDefault();
406 
407  GrantedAssetManagerAttributeRights = element.GetIntAttributeValue("right5").GetValueOrDefault();
408  DeniedAssetManagerAttributeRights = element.GetIntAttributeValue("deny5").GetValueOrDefault();
409 
410  GrantedContentClassRights = element.GetIntAttributeValue("right6").GetValueOrDefault();
411  DeniedContentClassRights = element.GetIntAttributeValue("deny6").GetValueOrDefault();
412 
413  GrantedProjectOrLanguageVariantRights = element.GetIntAttributeValue("right7").GetValueOrDefault();
414  DeniedProjectOrLanguageVariantRights = element.GetIntAttributeValue("deny7").GetValueOrDefault();
415 
416  GrantedAssetManagerFolderRights = element.GetIntAttributeValue("right8").GetValueOrDefault();
417  DeniedAssetManagerFolderRights = element.GetIntAttributeValue("deny8").GetValueOrDefault();
418  }
419 
420  protected AuthorizationRights(IAuthorizationPackage package)
421  {
422  _package = package;
423  }
424 
425  public IAuthorizationPackage AuthorizationPackage
426  {
427  get { return _package; }
428  }
429 
430  public abstract void Commit();
431  public int DeniedAssetManagerAttributeRights { get; set; }
432  public int DeniedAssetManagerFolderRights { get; set; }
433  public int DeniedContentClassRights { get; set; }
434  public int DeniedElementRights { get; set; }
435  public int DeniedGlobalRights { get; set; }
436  public int DeniedPageRights { get; set; }
437  public int DeniedProjectOrLanguageVariantRights { get; set; }
438  public int DeniedStructuralElementRights { get; set; }
439  public int GrantedAssetManagerAttributeRights { get; set; }
440  public int GrantedAssetManagerFolderRights { get; set; }
441  public int GrantedContentClassRights { get; set; }
442  public int GrantedElementRights { get; set; }
443  public int GrantedGlobalRights { get; set; }
444  public int GrantedPageRights { get; set; }
445  public int GrantedProjectOrLanguageVariantRights { get; set; }
446  public int GrantedStructuralElementRights { get; set; }
447 
448  public IProject Project
449  {
450  get { return _package.Project; }
451  }
452 
453  public ISession Session
454  {
455  get { return _package.Session; }
456  }
457  }
458 
459  internal class UserAuthorizationRights : AuthorizationRights, IUserAuthorization
460  {
461  public UserAuthorizationRights(IAuthorizationPackage package, IUser user, XmlElement element)
462  : base(package, element)
463  {
464  User = user;
465  }
466 
467  public UserAuthorizationRights(IAuthorizationPackage package, IUser user) : base(package)
468  {
469  User = user;
470  }
471 
472  public override void Commit()
473  {
474  const string SAVE_USER_RIGHTS =
475  @"<AUTHORIZATION><AUTHORIZATIONPACKET action=""save"" guid=""{0}""><USERS><USER guid=""{1}"" right1=""{2}"" right2=""{3}"" right3=""{4}"" right4=""{5}"" right5=""{6}"" right6=""{7}"" right7=""{8}"" right8=""{9}"" deny1=""{10}"" deny2=""{11}"" deny3=""{12}"" deny4=""{13}"" deny5=""{14}"" deny6=""{15}"" deny7=""{16}"" deny8=""{17}""/></USERS></AUTHORIZATIONPACKET></AUTHORIZATION>";
476 
477  string query = SAVE_USER_RIGHTS.RQLFormat(AuthorizationPackage, User, GrantedPageRights,
478  GrantedStructuralElementRights, GrantedElementRights,
479  GrantedGlobalRights, GrantedAssetManagerAttributeRights,
480  GrantedContentClassRights, GrantedProjectOrLanguageVariantRights,
481  GrantedAssetManagerFolderRights, DeniedPageRights,
482  DeniedStructuralElementRights, DeniedElementRights,
483  DeniedGlobalRights, DeniedAssetManagerAttributeRights,
484  DeniedContentClassRights, DeniedProjectOrLanguageVariantRights,
485  DeniedAssetManagerFolderRights);
486 
487  Project.ExecuteRQL(query);
488  }
489 
490  public IUser User { get; private set; }
491  }
492 
493  internal class GroupAuthorizationRights : AuthorizationRights, IGroupAuthorization
494  {
495  public GroupAuthorizationRights(IAuthorizationPackage package, IGroup group, XmlElement element)
496  : base(package, element)
497  {
498  Group = @group;
499  }
500 
501  public GroupAuthorizationRights(IAuthorizationPackage package, IGroup group) : base(package)
502  {
503  Group = group;
504  }
505 
506  public override void Commit()
507  {
508  const string SAVE_GROUP_RIGHTS =
509  @"<AUTHORIZATION><AUTHORIZATIONPACKET action=""save"" guid=""{0}""><GROUPS><GROUP guid=""{1}"" right1=""{2}"" right2=""{3}"" right3=""{4}"" right4=""{5}"" right5=""{6}"" right6=""{7}"" right7=""{8}"" right8=""{9}"" deny1=""{10}"" deny2=""{11}"" deny3=""{12}"" deny4=""{13}"" deny5=""{14}"" deny6=""{15}"" deny7=""{16}"" deny8=""{17}""/></GROUPS></AUTHORIZATIONPACKET></AUTHORIZATION>";
510 
511  string query = SAVE_GROUP_RIGHTS.RQLFormat(AuthorizationPackage, Group, GrantedPageRights,
512  GrantedStructuralElementRights, GrantedElementRights,
513  GrantedGlobalRights, GrantedAssetManagerAttributeRights,
514  GrantedContentClassRights, GrantedProjectOrLanguageVariantRights,
515  GrantedAssetManagerFolderRights, DeniedPageRights,
516  DeniedStructuralElementRights, DeniedElementRights,
517  DeniedGlobalRights, DeniedAssetManagerAttributeRights,
518  DeniedContentClassRights, DeniedProjectOrLanguageVariantRights,
519  DeniedAssetManagerFolderRights);
520 
521  Project.ExecuteRQL(query);
522  }
523 
524  public IGroup Group { get; private set; }
525  }
526 
527  //public interface IAuthorizations<T> : IRDList<T> where T : class, IRedDotObject
528  //{
529  // void Add(T element);
530  // void AddAll(IEnumerable<T> elements);
531  // void Remove(T element);
532  // void RemoveAll(IEnumerable<T> elements);
533  //}
534 }