SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IProjectUsers.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;
17 using System.Collections.Generic;
18 using System.Linq;
19 using System.Xml;
20 using erminas.SmartAPI.CMS.ServerManagement;
21 using erminas.SmartAPI.Exceptions;
22 using erminas.SmartAPI.Utils.CachedCollections;
23 
24 namespace erminas.SmartAPI.CMS.Project
25 {
26  public interface IProjectUsers : IIndexedCachedList<string, IUserProjectAssignment>, IProjectObject
27  {
28  IUserProjectAssignment AddOrSet(IUser user, UserRole role, ExtendedUserRoles extendedUserRoles);
29  bool ContainsUser(IUser user);
30  bool ContainsUserGuid(Guid userGuid);
31  bool ContainsUserName(string userName);
32  IUserProjectAssignment GetByUserGuid(Guid userGuid);
33  IUserProjectAssignment GetByUserName(string userName);
34  IUserProjectAssignment this[IUser user] { get; }
35  void Remove(IUser user);
36  bool TryGetByUser(IUser user, out IUserProjectAssignment assignment);
37  bool TryGetByUserGuid(Guid userGuid, out IUserProjectAssignment assignment);
38  bool TryGetByUserName(string userName, out IUserProjectAssignment assignment);
39  }
40 
41  internal class ProjectUsers : IndexedCachedList<string, IUserProjectAssignment>, IProjectUsers
42  {
43  private readonly IProject _project;
44 
45  internal ProjectUsers(IProject project, Caching caching) : base(assignment => assignment.User.Name, caching)
46  {
47  _project = project;
48  RetrieveFunc = GetUsersOfProject;
49  }
50 
51  public IUserProjectAssignment AddOrSet(IUser user, UserRole role, ExtendedUserRoles extendedUserRoles)
52  {
53  return UserProjectAssignment.Create(user, _project, role, extendedUserRoles);
54  }
55 
56  public bool ContainsUser(IUser user)
57  {
58  return ContainsUserGuid(user.Guid);
59  }
60 
61  public bool ContainsUserGuid(Guid userGuid)
62  {
63  return this.Any(assignment => assignment.User.Guid == userGuid);
64  }
65 
66  public bool ContainsUserName(string userName)
67  {
68  return this.Any(assignment => assignment.User.Name == userName);
69  }
70 
71  public IUserProjectAssignment GetByUserGuid(Guid userGuid)
72  {
73  return this.First(x => x.User.Guid == userGuid);
74  }
75 
76  public IUserProjectAssignment GetByUserName(string userName)
77  {
78  return this[userName];
79  }
80 
81  public IUserProjectAssignment this[IUser user]
82  {
83  get { return this[user.Name]; }
84  }
85 
86  public IProject Project
87  {
88  get { return _project; }
89  }
90 
91  public void Remove(IUser user)
92  {
93  UserProjectAssignment.Delete(Project, user);
94  }
95 
96  public ISession Session
97  {
98  get { return _project.Session; }
99  }
100 
101  public bool TryGetByUser(IUser user, out IUserProjectAssignment assignment)
102  {
103  return TryGetByUserGuid(user.Guid, out assignment);
104  }
105 
106  public bool TryGetByUserGuid(Guid userGuid, out IUserProjectAssignment assignment)
107  {
108  assignment = this.FirstOrDefault(projectAssignment => projectAssignment.User.Guid == userGuid);
109  return assignment != null;
110  }
111 
112  public bool TryGetByUserName(string userName, out IUserProjectAssignment assignment)
113  {
114  return TryGet(userName, out assignment);
115  }
116 
117  private List<IUserProjectAssignment> GetUsersOfProject()
118  {
119  try
120  {
121  const string GET_USERS = @"<PROJECT><USERS action=""list""/></PROJECT>";
122  var xmlDoc = Session.ExecuteRQLInProjectContext(GET_USERS, _project.Guid);
123  var xmlNodes = xmlDoc.GetElementsByTagName("USER");
124 
125  return (from XmlElement node in xmlNodes
126  select (IUserProjectAssignment) new UserProjectAssignment(new User(Session, node), _project))
127  .ToList();
128  } catch (Exception e)
129  {
130  throw new SmartAPIException(Session.ServerLogin,
131  string.Format("Could not load users of project {0} ", this), e);
132  }
133  }
134  }
135 }