#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HeuristicLab.Services.Authentication {
public partial class UserManagement : Form {
private Application currentApplication;
private Application selectedApplication;
private User selectedUser;
private Role selectedRole;
private List applications;
private List users;
private List roles;
public UserManagement() {
InitializeComponent();
Initialize();
}
private void Initialize() {
AuthenticationClient.Instance.Refreshed += new EventHandler(Instance_Refreshed);
cbxApplications.DataSource = AuthenticationClient.Instance.GetApplications();
}
void Instance_Refreshed(object sender, EventArgs e) {
if (InvokeRequired) {
Invoke(new EventHandler(Instance_Refreshed), sender, e);
} else {
applications = AuthenticationClient.Instance.Applications.ToList();
dgvApplications.DataSource = applications;
roles = AuthenticationClient.Instance.Roles.ToList();
dgvRoles.DataSource = roles;
users = AuthenticationClient.Instance.Users.ToList();
dgvUsers.DataSource = users;
}
}
private void RefreshApplications() {
if (currentApplication != null) {
AuthenticationClient.Instance.Refresh(currentApplication.Id);
}
}
private void RefreshUsersAndRoles() {
if (currentApplication != null) {
AuthenticationClient.Instance.Refresh(currentApplication.Id);
}
}
private void cbxApplications_SelectedIndexChanged(object sender, EventArgs e) {
this.currentApplication = (HeuristicLab.Services.Authentication.Application)cbxApplications.SelectedItem;
if (currentApplication != null) {
AuthenticationClient.Instance.Refresh(currentApplication.Id);
}
}
private void dgvApplications_SelectionChanged(object sender, EventArgs e) {
if (dgvApplications.SelectedRows.Count > 0) {
selectedApplication = (HeuristicLab.Services.Authentication.Application)dgvApplications.SelectedRows[0].DataBoundItem;
if (selectedApplication != null) {
tbApplicationName.Text = selectedApplication.Name;
tbApplicationDescription.Text = selectedApplication.Description;
selectedApplication.PropertyChanged += new PropertyChangedEventHandler(selectedApplication_PropertyChanged);
}
}
}
void selectedApplication_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (selectedApplication.Modified) {
tssApplicationStatus.Text = "modified";
} else {
tssApplicationStatus.Text = "";
}
}
private void dgvUsers_SelectionChanged(object sender, EventArgs e) {
if (dgvUsers.SelectedRows.Count > 0) {
selectedUser = (HeuristicLab.Services.Authentication.User)dgvUsers.SelectedRows[0].DataBoundItem;
if (selectedUser != null) {
tbUserComment.Text = selectedUser.Description;
tbUserEmail.Text = selectedUser.Email;
tbUserName.Text = selectedUser.Name;
selectedUser.PropertyChanged += new PropertyChangedEventHandler(selectedUser_PropertyChanged);
RefreshUserRolesList();
}
RefreshUserRolesList();
}
}
void selectedUser_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (selectedUser.Modified) {
tssStatusUser.Text = "modified";
} else {
tssStatusUser.Text = "";
}
}
private void dgvRoles_SelectionChanged(object sender, EventArgs e) {
if (dgvRoles.SelectedRows.Count > 0) {
selectedRole = (HeuristicLab.Services.Authentication.Role)dgvRoles.SelectedRows[0].DataBoundItem;
if (selectedRole != null) {
tbRoleName.Text = selectedRole.Name;
tbRoleDescription.Text = selectedRole.Description;
selectedRole.PropertyChanged += new PropertyChangedEventHandler(selectedRole_PropertyChanged);
}
RefreshRoleUsersList();
}
}
void selectedRole_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (selectedRole.Modified) {
tssStatusRole.Text = "modified";
} else {
tssStatusRole.Text = "";
}
}
private void RefreshUserRolesList() {
IEnumerable guidList = AuthenticationClient.Instance.GetRolesForUser(selectedUser.Id);
List rolesForUser = new List();
if (guidList != null) {
foreach (Guid g in guidList) {
rolesForUser.Add(AuthenticationClient.Instance.GetRole(g));
}
}
dgvUserRoles.DataSource = rolesForUser;
dgvUserRolesAll.DataSource = AuthenticationClient.Instance.Roles;
}
private void RefreshRoleUsersList() {
IEnumerable guidList = AuthenticationClient.Instance.GetUsersInRole(selectedRole.Id);
List usersForRole = new List();
if (guidList != null) {
foreach (Guid g in guidList) {
usersForRole.Add(AuthenticationClient.Instance.GetUser(g));
}
}
dgvRoleUsers.DataSource = usersForRole;
dgvRoleUsersAll.DataSource = AuthenticationClient.Instance.Users;
}
private void tbApplicationName_TextChanged(object sender, EventArgs e) {
if (selectedApplication != null) {
selectedApplication.Name = tbApplicationName.Text;
}
}
private void tbApplicationDescription_TextChanged(object sender, EventArgs e) {
if (selectedApplication != null) {
selectedApplication.Description = tbApplicationDescription.Text;
}
}
private void tbRoleName_TextChanged(object sender, EventArgs e) {
if (selectedRole != null) {
selectedRole.Name = tbRoleName.Text;
}
}
private void tbRoleDescription_TextChanged(object sender, EventArgs e) {
if (selectedRole != null) {
selectedRole.Description = tbRoleDescription.Text;
}
}
private void tbUserComment_TextChanged(object sender, EventArgs e) {
if (selectedUser != null) {
selectedUser.Description = tbUserComment.Text;
}
}
private void tbUserName_TextChanged(object sender, EventArgs e) {
if (selectedUser != null) {
selectedUser.Name = tbUserName.Text;
}
}
private void tbUserEmail_TextChanged(object sender, EventArgs e) {
if (selectedUser != null) {
selectedUser.Email = tbUserEmail.Text;
}
}
private void tspNewApplication_Click(object sender, EventArgs e) {
selectedApplication = new Application();
tbApplicationDescription.Text = "";
tbApplicationName.Text = "";
}
private void tspDeleteApplication_Click(object sender, EventArgs e) {
if (this.dgvApplications.SelectedRows.Count > 0) {
selectedApplication = (HeuristicLab.Services.Authentication.Application)this.dgvApplications.SelectedRows[0].DataBoundItem;
if (selectedApplication != null) {
AuthenticationClient.Instance.Applications.Remove(selectedApplication);
RefreshApplications();
}
}
}
private void tspSaveApplication_Click(object sender, EventArgs e) {
if (selectedApplication != null) {
selectedApplication.Store();
if (currentApplication != null) {
AuthenticationClient.Instance.Refresh(currentApplication.Id);
}
}
}
private void tsbNewUser_Click(object sender, EventArgs e) {
selectedUser = new User();
selectedUser.ApplicationId = currentApplication.Id;
selectedUser.CreateDate = DateTime.Now;
selectedUser.LastActivityDate = DateTime.Now;
selectedUser.LastLoginDate = DateTime.Now;
selectedUser.LastLockoutDate = DateTime.Now;
selectedUser.LastPasswordChangeDate = DateTime.Now;
tbUserComment.Text = "";
tbUserEmail.Text = "";
tbUserName.Text = "";
}
private void tsbDeleteUser_Click(object sender, EventArgs e) {
if (this.dgvUsers.SelectedRows.Count > 0) {
selectedUser = (HeuristicLab.Services.Authentication.User)this.dgvUsers.SelectedRows[0].DataBoundItem;
if (selectedUser != null) {
AuthenticationClient.Instance.Users.Remove(selectedUser);
RefreshUsersAndRoles();
}
}
}
private void tsbSaveUser_Click(object sender, EventArgs e) {
if (selectedUser != null) {
selectedUser.Store();
if (currentApplication != null) {
AuthenticationClient.Instance.Refresh(currentApplication.Id);
}
}
}
private void tsbRemoveRoleFromUser_Click(object sender, EventArgs e) {
if (this.dgvUserRoles.SelectedRows.Count > 0) {
HeuristicLab.Services.Authentication.Role role = (HeuristicLab.Services.Authentication.Role)this.dgvUserRoles.SelectedRows[0].DataBoundItem;
if (role != null) {
AuthenticationClient.Instance.RemoveUserFromRole(role.Id, selectedUser.Id);
RefreshUserRolesList();
}
}
}
private void tsbAddRoleToUser_Click(object sender, EventArgs e) {
if (this.dgvUserRolesAll.SelectedRows.Count > 0) {
HeuristicLab.Services.Authentication.Role role = (HeuristicLab.Services.Authentication.Role)this.dgvUserRolesAll.SelectedRows[0].DataBoundItem;
if (role != null) {
if (!AuthenticationClient.Instance.IsUserInRole(selectedUser.Id, role.Id)) {
AuthenticationClient.Instance.AddUserToRole(role.Id, selectedUser.Id);
RefreshUserRolesList();
}
}
}
}
private void tspNewRole_Click(object sender, EventArgs e) {
selectedRole = new Role();
selectedRole.ApplicationId = currentApplication.Id;
tbRoleDescription.Text = "";
tbRoleName.Text = "";
}
private void tsbDeleteRole_Click(object sender, EventArgs e) {
if (this.dgvRoles.SelectedRows.Count > 0) {
selectedRole = (HeuristicLab.Services.Authentication.Role)this.dgvRoles.SelectedRows[0].DataBoundItem;
if (selectedRole != null) {
AuthenticationClient.Instance.Roles.Remove(selectedRole);
RefreshUsersAndRoles();
}
}
}
private void tsbSaveRole_Click(object sender, EventArgs e) {
if (selectedRole != null) {
selectedRole.Store();
if (currentApplication != null) {
AuthenticationClient.Instance.Refresh(currentApplication.Id);
}
}
}
private void tsbRemoveUserFromRole_Click(object sender, EventArgs e) {
if (this.dgvRoleUsers.SelectedRows.Count > 0) {
HeuristicLab.Services.Authentication.User user = (HeuristicLab.Services.Authentication.User)this.dgvRoleUsers.SelectedRows[0].DataBoundItem;
if (user != null) {
AuthenticationClient.Instance.RemoveUserFromRole(selectedRole.Id, user.Id);
RefreshRoleUsersList();
}
}
}
private void tsbAddUserToRole_Click(object sender, EventArgs e) {
if (this.dgvRoleUsersAll.SelectedRows.Count > 0) {
HeuristicLab.Services.Authentication.User user = (HeuristicLab.Services.Authentication.User)this.dgvRoleUsersAll.SelectedRows[0].DataBoundItem;
if (user != null) {
if (!AuthenticationClient.Instance.IsUserInRole(user.Id, selectedRole.Id)) {
AuthenticationClient.Instance.AddUserToRole(selectedRole.Id, user.Id);
RefreshRoleUsersList();
}
}
}
}
private void btnResetPassword_Click(object sender, EventArgs e) {
string applicationName = currentApplication.Name;
string userName = selectedUser.Name;
string password = tbPassword.Text;
AuthenticationClient.Instance.ResetPassword(applicationName, userName, password);
}
}
}