Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Clients.Access/3.3/UserInformation.cs @ 13401

Last change on this file since 13401 was 8076, checked in by ascheibe, 12 years ago

#1648 fetch user and client information when the optimizer starts

File size: 2.9 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.ServiceModel.Security;
24using System.Threading.Tasks;
25
26namespace HeuristicLab.Clients.Access {
27  public sealed class UserInformation {
28    public const string AdministratorRoleName = "AccessService Administrator";
29
30    private static UserInformation instance;
31    public static UserInformation Instance {
32      get {
33        InitializeUserInformation();
34        return instance;
35      }
36    }
37
38    private string userName;
39    public string UserName {
40      get { return userName; }
41    }
42
43    private bool userExists = false;
44    public bool UserExists {
45      get { return userExists; }
46    }
47
48    private LightweightUser user;
49    public LightweightUser User {
50      get { return user; }
51    }
52
53    private bool errorOccured;
54    public bool ErrorOccured {
55      get { return errorOccured; }
56    }
57
58    private Exception occuredException;
59    public Exception OccuredException {
60      get { return occuredException; }
61    }
62
63    private UserInformation() {
64      if (instance == null) {
65        FetchUserInformationFromServer();
66      }
67    }
68
69    private void FetchUserInformationFromServer() {
70      userName = HeuristicLab.Clients.Common.Properties.Settings.Default.UserName;
71
72      try {
73        AccessClient.CallAccessService(x => user = x.Login());
74        errorOccured = false;
75        userExists = true;
76        occuredException = null;
77      }
78      catch (MessageSecurityException e) {
79        //wrong username or password
80        errorOccured = false;
81        userExists = false;
82        occuredException = e;
83      }
84      catch (Exception e) {
85        errorOccured = true;
86        userExists = false;
87        occuredException = e;
88      }
89    }
90
91    public void Refresh() {
92      FetchUserInformationFromServer();
93    }
94
95    private static void InitializeUserInformation() {
96      if (instance == null) instance = new UserInformation();
97    }
98
99    public static void InitializeAsync() {
100      Task.Factory.StartNew(InitializeUserInformation);
101    }
102  }
103}
Note: See TracBrowser for help on using the repository browser.