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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.ServiceModel.Security;
|
---|
25 | using HeuristicLab.Clients.Common.Properties;
|
---|
26 |
|
---|
27 | namespace 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 bool isInitialized = false;
|
---|
75 |
|
---|
76 | public static void Initialize() {
|
---|
77 | if (!Instance.isInitialized) {
|
---|
78 | FetchUserInformationFromServerAsync();
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | private UserInformation() {
|
---|
83 | if (!Instance.isInitialized) {
|
---|
84 | //this blocks, so there should be anywhere in the Optimizer startup process
|
---|
85 | //a call to FetchUserInformationFromServerAsync which is non-blocking
|
---|
86 | FetchUserInformationFromServer();
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 |
|
---|
91 | private void FetchUserInformationFromServer() {
|
---|
92 | userName = Settings.Default.UserName;
|
---|
93 |
|
---|
94 | try {
|
---|
95 | AccessClient.CallRunCreationService(x => user = x.Login());
|
---|
96 | AccessClient.CallRunCreationService(x => groups = x.GetGroupsOfCurrentUser());
|
---|
97 | AccessClient.CallRunCreationService(x => roles = x.GetRolesOfCurrentUser());
|
---|
98 | }
|
---|
99 | catch (MessageSecurityException e) {
|
---|
100 | //wrong username or password
|
---|
101 | errorOccured = false;
|
---|
102 | userExists = false;
|
---|
103 | isInitialized = true;
|
---|
104 | occuredException = e;
|
---|
105 | return;
|
---|
106 | }
|
---|
107 | catch (Exception e) {
|
---|
108 | errorOccured = true;
|
---|
109 | userExists = false;
|
---|
110 | isInitialized = false;
|
---|
111 | occuredException = e;
|
---|
112 | return;
|
---|
113 | }
|
---|
114 |
|
---|
115 | errorOccured = false;
|
---|
116 | userExists = true;
|
---|
117 | isInitialized = true;
|
---|
118 | occuredException = null;
|
---|
119 | }
|
---|
120 |
|
---|
121 | private static void FetchUserInformationFromServerAsync() {
|
---|
122 | throw new NotImplementedException();
|
---|
123 | }
|
---|
124 |
|
---|
125 | public void Refresh() {
|
---|
126 | FetchUserInformationFromServer();
|
---|
127 | }
|
---|
128 | }
|
---|
129 | }
|
---|