source: branches/ClientUserManagement/HeuristicLab.Clients.Access/3.3/UserInformation.cs @ 7534

Last change on this file since 7534 was 7534, checked in by ascheibe, 8 years ago

#1648 worked on user and client information singletons

File size: 3.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.ServiceModel.Security;
25using HeuristicLab.Clients.Common.Properties;
26
27namespace HeuristicLab.Clients.Access {
28  public sealed class UserInformation {
29    private static UserInformation instance;
30    public static UserInformation Instance {
31      get {
32        if (instance == null) instance = new UserInformation();
33        return instance;
34      }
35    }
36
37    private string userName;
38    public string UserName {
39      get { return userName; }
40    }
41
42    private bool userExists = false;
43    public bool UserExists {
44      get { return userExists; }
45    }
46
47    private LightweightUser user;
48    public LightweightUser User {
49      get { return user; }
50    }
51
52    //the groups a user is in
53    private List<UserGroup> groups;
54    public List<UserGroup> Groups {
55      get { return groups; }
56    }
57
58    //the roles a user has
59    private List<Role> roles;
60    public List<Role> Roles {
61      get { return roles; }
62    }
63
64    private bool errorOccured;
65    public bool ErrorOccured {
66      get { return errorOccured; }
67    }
68
69    private Exception occuredException;
70    public Exception OccuredException {
71      get { return occuredException; }
72    }
73
74    private UserInformation() {
75      //this blocks, so there should be anywhere in the Optimizer startup process
76      //a call to FetchUserInformationFromServerAsync which is non-blocking
77      FetchUserInformationFromServer();
78    }
79
80
81    private void FetchUserInformationFromServer() {
82      userName = Settings.Default.UserName;
83
84      try {
85        AccessClient.CallRunCreationService(x => user = x.Login());
86        AccessClient.CallRunCreationService(x => groups = x.GetGroupsOfCurrentUser());
87        AccessClient.CallRunCreationService(x => roles = x.GetRolesOfCurrentUser());
88        errorOccured = false;
89        userExists = true;
90        occuredException = null;
91      }
92      catch (MessageSecurityException e) {
93        //wrong username or password
94        errorOccured = false;
95        userExists = false;
96        occuredException = e;
97      }
98      catch (Exception e) {
99        errorOccured = true;
100        userExists = false;
101        occuredException = e;
102      }
103    }
104
105    public void Refresh() {
106      FetchUserInformationFromServer();
107    }
108  }
109}
Note: See TracBrowser for help on using the repository browser.