1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2016 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 |
|
---|
22 | using System;
|
---|
23 | using System.ServiceModel.Security;
|
---|
24 | using System.Threading.Tasks;
|
---|
25 |
|
---|
26 | namespace 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 | } catch (MessageSecurityException e) {
|
---|
78 | //wrong username or password
|
---|
79 | errorOccured = false;
|
---|
80 | userExists = false;
|
---|
81 | occuredException = e;
|
---|
82 | } catch (Exception e) {
|
---|
83 | errorOccured = true;
|
---|
84 | userExists = false;
|
---|
85 | occuredException = e;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | public void Refresh() {
|
---|
90 | FetchUserInformationFromServer();
|
---|
91 | }
|
---|
92 |
|
---|
93 | private static void InitializeUserInformation() {
|
---|
94 | if (instance == null) instance = new UserInformation();
|
---|
95 | }
|
---|
96 |
|
---|
97 | public static void InitializeAsync() {
|
---|
98 | Task.Factory.StartNew(InitializeUserInformation);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|