SmartAPI
Open Source .NET RQL library for RedDot CMS / OpenText WSM Management Server
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
IUsers.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.Utils;
21 using erminas.SmartAPI.Utils.CachedCollections;
22 
23 namespace erminas.SmartAPI.CMS.ServerManagement
24 {
25  public interface IUsers : IIndexedRDList<String, IUser>
26  {
27  IUser Create(string name, string password);
28 
32  IUser Current { get; }
33  }
34 
35  internal class Users : NameIndexedRDList<IUser>, IUsers
36  {
37  private readonly Session _session;
38  private Lazy<IUser> _user;
39 
40  internal Users(Session session, Caching caching) : base(caching)
41  {
42  _session = session;
43  RetrieveFunc = GetUsers;
44  _user = new Lazy<IUser>(GetCurrentUser);
45  }
46 
47  public IUser Create(string name, string password)
48  {
49  const string CREATE_USER =
50  @"<ADMINISTRATION><USER action=""addnew"" name=""{0}"" pw=""{1}"" lcid=""{2}"" userlanguage=""{3}""></USER></ADMINISTRATION>";
51  _session.ExecuteRQL(CREATE_USER.SecureRQLFormat(name, password, _session.StandardLocale,
52  _session.StandardLocale.LanguageAbbreviation));
53  Refresh();
54  return this[name];
55  }
56 
57  public IUser Current
58  {
59  get { return _user.Value; }
60  internal set { _user = new Lazy<IUser>(() => value); }
61  }
62 
63  private IUser GetCurrentUser()
64  {
65  var userElement = _session.GetUserSessionInfoElement();
66 
67  return new User(_session, userElement.GetGuid()) {Name = userElement.GetName()};
68  }
69 
70  private List<IUser> GetUsers()
71  {
72  const string LIST_USERS = @"<ADMINISTRATION><USERS action=""list""/></ADMINISTRATION>";
73  var userListDoc = _session.ExecuteRQL(LIST_USERS);
74 
75  return (from XmlElement curUserElement in userListDoc.GetElementsByTagName("USER")
76  select (IUser) new User(_session, curUserElement)).ToList();
77  }
78  }
79 }