Changeset 5257 for branches/UserManagement/HeuristicLab.Services.Authentication.TestClient2/UserManagement.cs
- Timestamp:
- 01/08/11 14:39:29 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/UserManagement/HeuristicLab.Services.Authentication.TestClient2/UserManagement.cs
r4979 r5257 1 using System; 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 21 using System; 2 22 using System.Collections.Generic; 3 23 using System.ComponentModel; … … 8 28 using System.Windows.Forms; 9 29 10 namespace HeuristicLab.Services.Authentication.TestClient2 11 { 12 public partial class UserManagement : Form 13 { 14 15 private AuthenticationService auth; 16 private DataTransfer.Application currentApplication; 17 18 private DataTransfer.Application selectedApplication; 19 private DataTransfer.User selectedUser; 20 private DataTransfer.Role selectedRole; 21 22 public UserManagement() 23 { 24 InitializeComponent(); 25 Initialize(); 26 } 27 28 private void Initialize() 29 { 30 auth = new AuthenticationService(); 31 bsApplications.DataSource = auth.GetApplications(); 32 bsApplicationsCombo.DataSource = auth.GetApplications(); 33 RefreshLists(); 34 } 35 36 private void RefreshLists() 37 { 38 if (currentApplication != null) 39 { 40 bsRoles.DataSource = auth.GetRoles(currentApplication.Id); 41 bsUsers.DataSource = auth.GetUsers(currentApplication.Id); 42 } 43 } 44 45 private void cbxApplications_SelectedIndexChanged(object sender, EventArgs e) 46 { 47 this.currentApplication = (HeuristicLab.Services.Authentication.DataTransfer.Application)cbxApplications.SelectedItem; 48 RefreshLists(); 49 } 50 51 private void btnNewApplication_Click(object sender, EventArgs e) 52 { 53 // new 54 //DataTransfer.Application = new DataTransfer.Application(); 55 selectedApplication = (DataTransfer.Application)this.bsApplications.AddNew(); 56 57 } 58 59 private void btnDeleteApplication_Click(object sender, EventArgs e) 60 { 61 if (this.dgvApplications.SelectedRows.Count > 0) 62 { 63 selectedApplication= ( DataTransfer.Application)this.dgvApplications.SelectedRows[0].DataBoundItem; 64 if (selectedApplication != null) 65 { 66 auth.DeleteApplication(selectedApplication.Id); 67 RefreshLists(); 68 } 69 } 70 } 71 72 private void btnUpdateApplication_Click(object sender, EventArgs e) 73 { 74 75 if (selectedApplication != null) 76 { 77 if (selectedApplication.Id.Equals(Guid.Empty)) 78 { 79 // insert 80 selectedApplication.Id = Guid.NewGuid(); 81 auth.AddApplication(selectedApplication); 82 } 83 else 84 { 85 // update 86 auth.UpdateApplication(selectedApplication); 87 } 88 } 89 90 91 } 92 93 private void dgvApplications_SelectionChanged(object sender, EventArgs e) 94 { 95 if (dgvApplications.SelectedRows.Count > 0) 96 { 97 selectedApplication = (DataTransfer.Application)dgvApplications.SelectedRows[0].DataBoundItem; 98 } 99 } 100 101 private void dgvUsers_SelectionChanged(object sender, EventArgs e) 102 { 103 if (dgvUsers.SelectedRows.Count > 0) 104 { 105 selectedUser = (DataTransfer.User)dgvUsers.SelectedRows[0].DataBoundItem; 106 RefreshUserRolesList(); 107 } 108 } 109 110 private void dgvRoles_SelectionChanged(object sender, EventArgs e) 111 { 112 if (dgvRoles.SelectedRows.Count > 0) 113 { 114 selectedRole = (DataTransfer.Role)dgvRoles.SelectedRows[0].DataBoundItem; 115 RefreshRoleUsersList(); 116 } 117 } 118 119 private void btnUpdateRole_Click(object sender, EventArgs e) 120 { 121 if (selectedRole != null) 122 { 123 if (selectedRole.Id.Equals(Guid.Empty)) 124 { 125 // insert 126 selectedRole.Id = Guid.NewGuid(); 127 selectedRole.ApplicationId = currentApplication.Id; 128 129 auth.AddRole(selectedRole); 130 } 131 else 132 { 133 // update 134 auth.UpdateRole(selectedRole); 135 } 136 } 137 } 138 139 private void btnNewRole_Click(object sender, EventArgs e) 140 { 141 selectedRole = (DataTransfer.Role)this.bsRoles.AddNew(); 142 } 143 144 private void btnDeleteRole_Click(object sender, EventArgs e) 145 { 146 if (this.dgvRoles.SelectedRows.Count > 0) 147 { 148 selectedRole = (DataTransfer.Role)this.dgvRoles.SelectedRows[0].DataBoundItem; 149 if (selectedRole != null) 150 { 151 auth.DeleteRole(selectedRole.Id); 152 RefreshLists(); 153 } 154 } 155 } 156 157 private void btnNewUser_Click(object sender, EventArgs e) 158 { 159 selectedUser = (DataTransfer.User)this.bsUsers.AddNew(); 160 } 161 162 private void btnDeleteUser_Click(object sender, EventArgs e) 163 { 164 if (this.dgvUsers.SelectedRows.Count > 0) 165 { 166 selectedUser = (DataTransfer.User)this.dgvUsers.SelectedRows[0].DataBoundItem; 167 if (selectedUser != null) 168 { 169 auth.DeleteUser(selectedUser.Id); 170 RefreshLists(); 171 } 172 } 173 } 174 175 private void btnUpdateUser_Click(object sender, EventArgs e) 176 { 177 if (selectedUser != null) 178 { 179 if (selectedUser.Id.Equals(Guid.Empty)) 180 { 181 // insert 182 selectedUser.Id = Guid.NewGuid(); 183 selectedUser.ApplicationId = currentApplication.Id; 184 185 auth.AddUser(selectedUser); 186 } 187 else 188 { 189 // update 190 auth.UpdateUser(selectedUser); 191 } 192 } 193 } 194 195 private void btnRemoveRoleFromUser_Click(object sender, EventArgs e) 196 { 197 if (this.dgvUserRoles.SelectedRows.Count > 0) 198 { 199 200 DataTransfer.Role role = (DataTransfer.Role)this.dgvUserRoles.SelectedRows[0].DataBoundItem; 201 if (role != null) 202 { 203 auth.RemoveUserFromRole(role.Id, selectedUser.Id); 204 RefreshUserRolesList(); 205 } 206 } 207 } 208 209 private void btnAddRoleToUser_Click(object sender, EventArgs e) 210 { 211 if (this.dgvUserRolesAll.SelectedRows.Count > 0) 212 { 213 DataTransfer.Role role = (DataTransfer.Role)this.dgvUserRolesAll.SelectedRows[0].DataBoundItem; 214 if (role != null) 215 { 216 auth.AddUserToRole( role.Id,selectedUser.Id); 217 RefreshUserRolesList(); 218 } 219 } 220 } 221 222 private void RefreshUserRolesList() 223 { 224 bsUserRoles.DataSource = auth.GetRolesForUser(selectedUser.Id); 225 } 226 227 private void RefreshRoleUsersList() 228 { 229 bsRoleUsers.DataSource = auth.GetUsersInRole(selectedRole.Id); 230 } 231 232 private void btnRemoveUserFromRole_Click(object sender, EventArgs e) 233 { 234 if (this.dgvRoleUsers.SelectedRows.Count > 0) 235 { 236 237 DataTransfer.User user = (DataTransfer.User)this.dgvRoleUsers.SelectedRows[0].DataBoundItem; 238 if (user != null) 239 { 240 auth.RemoveUserFromRole(selectedRole.Id, user.Id); 241 RefreshRoleUsersList(); 242 } 243 } 244 } 245 246 private void btnAddUserToRole_Click(object sender, EventArgs e) 247 { 248 if (this.dgvRoleUsersAll.SelectedRows.Count > 0) 249 { 250 DataTransfer.User user = (DataTransfer.User)this.dgvRoleUsersAll.SelectedRows[0].DataBoundItem; 251 if (user != null) 252 { 253 auth.AddUserToRole(selectedRole.Id, user.Id); 254 RefreshRoleUsersList(); 255 } 256 } 257 } 258 259 260 } 30 namespace 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 } 261 394 }
Note: See TracChangeset
for help on using the changeset viewer.