Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UserManagement/HeuristicLab.Services.Authentication.TestClient2/UserManagement.cs @ 6150

Last change on this file since 6150 was 5257, checked in by mjesner, 14 years ago

#1196

File size: 12.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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
21using System;
22using System.Collections.Generic;
23using System.ComponentModel;
24using System.Data;
25using System.Drawing;
26using System.Linq;
27using System.Text;
28using System.Windows.Forms;
29
30namespace HeuristicLab.Services.Authentication {
31  public partial class UserManagement : Form {
32
33    private Application currentApplication;
34    private Application selectedApplication;
35    private User selectedUser;
36    private Role selectedRole;
37
38    private List<Application> applications;
39    private List<User> users;
40    private List<Role> roles;
41
42
43    public UserManagement() {
44      InitializeComponent();
45      Initialize();
46    }
47
48
49    private void Initialize() {
50      AuthenticationClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
51      cbxApplications.DataSource = AuthenticationClient.Instance.GetApplications();
52    }
53
54
55    void Instance_Refreshed(object sender, EventArgs e) {
56
57      if (InvokeRequired) {
58        Invoke(new EventHandler(Instance_Refreshed), sender, e);
59      } else {
60
61        applications = AuthenticationClient.Instance.Applications.ToList();
62        dgvApplications.DataSource = applications;
63
64        roles = AuthenticationClient.Instance.Roles.ToList();
65        dgvRoles.DataSource = roles;
66
67        users = AuthenticationClient.Instance.Users.ToList();
68        dgvUsers.DataSource = users;
69      }
70
71    }
72
73
74    private void RefreshApplications() {
75      if (currentApplication != null) {
76        AuthenticationClient.Instance.Refresh(currentApplication.Id);
77      }
78    }
79
80
81
82    private void RefreshUsersAndRoles() {
83
84
85      if (currentApplication != null) {
86        AuthenticationClient.Instance.Refresh(currentApplication.Id);
87      }
88    }
89
90
91
92    private void cbxApplications_SelectedIndexChanged(object sender, EventArgs e) {
93      this.currentApplication = (HeuristicLab.Services.Authentication.Application)cbxApplications.SelectedItem;
94      if (currentApplication != null) {
95        AuthenticationClient.Instance.Refresh(currentApplication.Id);
96      }
97    }
98
99
100
101
102    private void dgvApplications_SelectionChanged(object sender, EventArgs e) {
103      if (dgvApplications.SelectedRows.Count > 0) {
104        selectedApplication = (HeuristicLab.Services.Authentication.Application)dgvApplications.SelectedRows[0].DataBoundItem;
105
106        if (selectedApplication != null) {
107          tbApplicationName.Text = selectedApplication.Name;
108          tbApplicationDescription.Text = selectedApplication.Description;
109          selectedApplication.PropertyChanged += new PropertyChangedEventHandler(selectedApplication_PropertyChanged);
110        }
111      }
112    }
113
114
115
116    void selectedApplication_PropertyChanged(object sender, PropertyChangedEventArgs e) {
117      if (selectedApplication.Modified) {
118        tssApplicationStatus.Text = "modified";
119      } else {
120        tssApplicationStatus.Text = "";
121      }
122    }
123
124
125
126
127    private void dgvUsers_SelectionChanged(object sender, EventArgs e) {
128      if (dgvUsers.SelectedRows.Count > 0) {
129        selectedUser = (HeuristicLab.Services.Authentication.User)dgvUsers.SelectedRows[0].DataBoundItem;
130
131        if (selectedUser != null) {
132          tbUserComment.Text = selectedUser.Description;
133          tbUserEmail.Text = selectedUser.Email;
134          tbUserName.Text = selectedUser.Name;
135          selectedUser.PropertyChanged += new PropertyChangedEventHandler(selectedUser_PropertyChanged);
136          RefreshUserRolesList();
137        }
138        RefreshUserRolesList();
139      }
140    }
141
142    void selectedUser_PropertyChanged(object sender, PropertyChangedEventArgs e) {
143      if (selectedUser.Modified) {
144        tssStatusUser.Text = "modified";
145      } else {
146        tssStatusUser.Text = "";
147      }
148    }
149
150
151
152    private void dgvRoles_SelectionChanged(object sender, EventArgs e) {
153      if (dgvRoles.SelectedRows.Count > 0) {
154        selectedRole = (HeuristicLab.Services.Authentication.Role)dgvRoles.SelectedRows[0].DataBoundItem;
155
156        if (selectedRole != null) {
157          tbRoleName.Text = selectedRole.Name;
158          tbRoleDescription.Text = selectedRole.Description;
159          selectedRole.PropertyChanged += new PropertyChangedEventHandler(selectedRole_PropertyChanged);
160        }
161
162        RefreshRoleUsersList();
163      }
164    }
165
166    void selectedRole_PropertyChanged(object sender, PropertyChangedEventArgs e) {
167      if (selectedRole.Modified) {
168        tssStatusRole.Text = "modified";
169      } else {
170        tssStatusRole.Text = "";
171      }
172    }
173
174
175
176    private void RefreshUserRolesList() {
177      IEnumerable<Guid> guidList = AuthenticationClient.Instance.GetRolesForUser(selectedUser.Id);
178      List<Role> rolesForUser = new List<Role>();
179      if (guidList != null) {
180        foreach (Guid g in guidList) {
181          rolesForUser.Add(AuthenticationClient.Instance.GetRole(g));
182        }
183      }
184      dgvUserRoles.DataSource = rolesForUser;
185      dgvUserRolesAll.DataSource = AuthenticationClient.Instance.Roles;
186
187    }
188
189
190
191    private void RefreshRoleUsersList() {
192
193      IEnumerable<Guid> guidList = AuthenticationClient.Instance.GetUsersInRole(selectedRole.Id);
194
195      List<User> usersForRole = new List<User>();
196
197      if (guidList != null) {
198        foreach (Guid g in guidList) {
199          usersForRole.Add(AuthenticationClient.Instance.GetUser(g));
200        }
201      }
202
203      dgvRoleUsers.DataSource = usersForRole;
204      dgvRoleUsersAll.DataSource = AuthenticationClient.Instance.Users;
205    }
206
207
208    private void tbApplicationName_TextChanged(object sender, EventArgs e) {
209      if (selectedApplication != null) {
210        selectedApplication.Name = tbApplicationName.Text;
211      }
212    }
213
214    private void tbApplicationDescription_TextChanged(object sender, EventArgs e) {
215      if (selectedApplication != null) {
216        selectedApplication.Description = tbApplicationDescription.Text;
217      }
218    }
219
220    private void tbRoleName_TextChanged(object sender, EventArgs e) {
221      if (selectedRole != null) {
222        selectedRole.Name = tbRoleName.Text;
223      }
224    }
225
226    private void tbRoleDescription_TextChanged(object sender, EventArgs e) {
227      if (selectedRole != null) {
228        selectedRole.Description = tbRoleDescription.Text;
229      }
230    }
231
232    private void tbUserComment_TextChanged(object sender, EventArgs e) {
233      if (selectedUser != null) {
234        selectedUser.Description = tbUserComment.Text;
235      }
236    }
237
238    private void tbUserName_TextChanged(object sender, EventArgs e) {
239      if (selectedUser != null) {
240        selectedUser.Name = tbUserName.Text;
241      }
242    }
243
244    private void tbUserEmail_TextChanged(object sender, EventArgs e) {
245      if (selectedUser != null) {
246        selectedUser.Email = tbUserEmail.Text;
247      }
248    }
249
250
251    private void tspNewApplication_Click(object sender, EventArgs e) {
252      selectedApplication = new Application();
253      tbApplicationDescription.Text = "";
254      tbApplicationName.Text = "";
255    }
256
257    private void tspDeleteApplication_Click(object sender, EventArgs e) {
258      if (this.dgvApplications.SelectedRows.Count > 0) {
259        selectedApplication = (HeuristicLab.Services.Authentication.Application)this.dgvApplications.SelectedRows[0].DataBoundItem;
260        if (selectedApplication != null) {
261          AuthenticationClient.Instance.Applications.Remove(selectedApplication);
262          RefreshApplications();
263        }
264      }
265    }
266
267    private void tspSaveApplication_Click(object sender, EventArgs e) {
268      if (selectedApplication != null) {
269        selectedApplication.Store();
270        if (currentApplication != null) {
271          AuthenticationClient.Instance.Refresh(currentApplication.Id);
272        }
273      }
274
275    }
276
277    private void tsbNewUser_Click(object sender, EventArgs e) {
278      selectedUser = new User();
279      selectedUser.ApplicationId = currentApplication.Id;
280      selectedUser.CreateDate = DateTime.Now;
281      selectedUser.LastActivityDate = DateTime.Now;
282      selectedUser.LastLoginDate = DateTime.Now;
283      selectedUser.LastLockoutDate = DateTime.Now;
284      selectedUser.LastPasswordChangeDate = DateTime.Now;
285
286      tbUserComment.Text = "";
287      tbUserEmail.Text = "";
288      tbUserName.Text = "";
289    }
290
291    private void tsbDeleteUser_Click(object sender, EventArgs e) {
292      if (this.dgvUsers.SelectedRows.Count > 0) {
293        selectedUser = (HeuristicLab.Services.Authentication.User)this.dgvUsers.SelectedRows[0].DataBoundItem;
294        if (selectedUser != null) {
295          AuthenticationClient.Instance.Users.Remove(selectedUser);
296          RefreshUsersAndRoles();
297        }
298      }
299    }
300
301    private void tsbSaveUser_Click(object sender, EventArgs e) {
302      if (selectedUser != null) {
303        selectedUser.Store();
304        if (currentApplication != null) {
305          AuthenticationClient.Instance.Refresh(currentApplication.Id);
306        }
307      }
308    }
309
310    private void tsbRemoveRoleFromUser_Click(object sender, EventArgs e) {
311      if (this.dgvUserRoles.SelectedRows.Count > 0) {
312
313        HeuristicLab.Services.Authentication.Role role = (HeuristicLab.Services.Authentication.Role)this.dgvUserRoles.SelectedRows[0].DataBoundItem;
314        if (role != null) {
315          AuthenticationClient.Instance.RemoveUserFromRole(role.Id, selectedUser.Id);
316          RefreshUserRolesList();
317        }
318      }
319    }
320
321    private void tsbAddRoleToUser_Click(object sender, EventArgs e) {
322      if (this.dgvUserRolesAll.SelectedRows.Count > 0) {
323        HeuristicLab.Services.Authentication.Role role = (HeuristicLab.Services.Authentication.Role)this.dgvUserRolesAll.SelectedRows[0].DataBoundItem;
324        if (role != null) {
325          if (!AuthenticationClient.Instance.IsUserInRole(selectedUser.Id, role.Id)) {
326            AuthenticationClient.Instance.AddUserToRole(role.Id, selectedUser.Id);
327            RefreshUserRolesList();
328          }
329        }
330      }
331    }
332
333    private void tspNewRole_Click(object sender, EventArgs e) {
334      selectedRole = new Role();
335      selectedRole.ApplicationId = currentApplication.Id;
336      tbRoleDescription.Text = "";
337      tbRoleName.Text = "";
338    }
339
340
341    private void tsbDeleteRole_Click(object sender, EventArgs e) {
342      if (this.dgvRoles.SelectedRows.Count > 0) {
343        selectedRole = (HeuristicLab.Services.Authentication.Role)this.dgvRoles.SelectedRows[0].DataBoundItem;
344        if (selectedRole != null) {
345          AuthenticationClient.Instance.Roles.Remove(selectedRole);
346          RefreshUsersAndRoles();
347        }
348      }
349    }
350
351
352    private void tsbSaveRole_Click(object sender, EventArgs e) {
353      if (selectedRole != null) {
354        selectedRole.Store();
355        if (currentApplication != null) {
356          AuthenticationClient.Instance.Refresh(currentApplication.Id);
357        }
358      }
359    }
360
361    private void tsbRemoveUserFromRole_Click(object sender, EventArgs e) {
362      if (this.dgvRoleUsers.SelectedRows.Count > 0) {
363
364        HeuristicLab.Services.Authentication.User user = (HeuristicLab.Services.Authentication.User)this.dgvRoleUsers.SelectedRows[0].DataBoundItem;
365        if (user != null) {
366          AuthenticationClient.Instance.RemoveUserFromRole(selectedRole.Id, user.Id);
367          RefreshRoleUsersList();
368        }
369      }
370    }
371
372    private void tsbAddUserToRole_Click(object sender, EventArgs e) {
373      if (this.dgvRoleUsersAll.SelectedRows.Count > 0) {
374        HeuristicLab.Services.Authentication.User user = (HeuristicLab.Services.Authentication.User)this.dgvRoleUsersAll.SelectedRows[0].DataBoundItem;
375        if (user != null) {
376          if (!AuthenticationClient.Instance.IsUserInRole(user.Id, selectedRole.Id)) {
377            AuthenticationClient.Instance.AddUserToRole(selectedRole.Id, user.Id);
378            RefreshRoleUsersList();
379          }
380        }
381      }
382    }
383
384
385    private void btnResetPassword_Click(object sender, EventArgs e) {
386
387      string applicationName = currentApplication.Name;
388      string userName = selectedUser.Name;
389      string password = tbPassword.Text;
390
391      AuthenticationClient.Instance.ResetPassword(applicationName, userName, password);
392    }
393  }
394}
Note: See TracBrowser for help on using the repository browser.