#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.Linq; using System.Text; using HeuristicLab.Services.Authentication; using System.ServiceModel.Security; using System.ServiceModel; using HeuristicLab.Core; using HeuristicLab.Collections; namespace HeuristicLab.Services.Authentication { public class AuthenticationClient { private static AuthenticationClient instance; public static AuthenticationClient Instance { get { if (instance == null) instance = new AuthenticationClient(); return instance; } } #region Properties private ItemCollection users; public ItemCollection Users { get { return users; } } private ItemCollection applications; public ItemCollection Applications { get { return applications; } } private ItemCollection roles; public ItemCollection Roles { get { return roles; } } #endregion private AuthenticationClient() { applications = new ItemCollection(); applications.ItemsRemoved += new CollectionItemsChangedEventHandler(applications_ItemsRemoved); users = new ItemCollection(); users.ItemsRemoved += new CollectionItemsChangedEventHandler(users_ItemsRemoved); roles = new ItemCollection(); roles.ItemsRemoved += new CollectionItemsChangedEventHandler(roles_ItemsRemoved); } #region Store public bool Store(AuthenticationItem item) { try { if (item.Id == Guid.Empty) { if (item is Role) item.Id = CallService(s => s.AddRole((Role)item)); else if (item is User) item.Id = CallService(s => s.AddUser((User)item)); else if (item is Application) item.Id = CallService(s => s.AddApplication((Application)item)); } else { if (item is Role) CallService(s => s.UpdateRole((Role)item)); else if (item is User) CallService(s => s.UpdateUser((User)item)); else if (item is Application) CallService(s => s.UpdateApplication((Application)item)); } return true; } catch (Exception ex) { //ErrorHandling.ShowErrorDialog("Store failed.", ex); return false; } } #endregion #region Refresh public void Refresh(Guid applicationId) { OnRefreshing(); users.Clear(); roles.Clear(); applications.Clear(); var call = new Func(delegate() { try { applications.AddRange(CallService(s => s.GetApplications()).OrderBy(x => x.Name).ToArray()); if (!applicationId.Equals(Guid.Empty)) { users.AddRange(CallService(s => s.GetUsersForApplication(applicationId)).OrderBy(x => x.Name).ToArray()); roles.AddRange(CallService(s => s.GetRolesForApplication(applicationId)).OrderBy(x => x.Name).ToArray()); } return null; } catch (Exception ex) { return ex; } }); call.BeginInvoke(delegate(IAsyncResult result) { Exception ex = call.EndInvoke(result); if (ex != null) { //ErrorHandling.ShowErrorDialog("Refresh failed.", ex); } OnRefreshed(); }, null); } #endregion #region Helpers private void CallService(Action call) { AuthenticationServiceClient client = new AuthenticationServiceClient(); client.ClientCredentials.UserName.UserName = "Alice"; client.ClientCredentials.UserName.Password = "YouWillNeverKnow"; client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; //AuthenticationServiceClient client = ClientFactory.Create(); try { call(client); } finally { try { client.Close(); } catch (Exception) { client.Abort(); } } } private T CallService(Func call) { AuthenticationServiceClient client = new AuthenticationServiceClient(); client.ClientCredentials.UserName.UserName = "Alice"; client.ClientCredentials.UserName.Password = "YouWillNeverKnow"; client.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; //AuthenticationServiceClient client = ClientFactory.Create(); try { return call(client); } finally { try { client.Close(); } catch (Exception) { client.Abort(); } } } #endregion #region Application methods public Application GetApplication(Guid applicationId) { try { return CallService(s => s.GetApplication(applicationId)); } catch (Exception ex) { //ErrorHandling.ShowErrorDialog("Refresh problem data failed.", ex); return null; } } public IEnumerable GetApplications() { try { return CallService>(s => s.GetApplications()); } catch (Exception ex) { // Todo Errorhandling return null; } } #endregion #region Role Methods public Role GetRole(Guid Id) { try { return CallService(s => s.GetRole(Id)); } catch (Exception ex) { // todo Errorhandling return null; } } public IEnumerable GetRolesForUser(Guid userId) { try { return CallService>(s => s.GetRolesForUser(userId)); } catch (Exception ex) { // Todo Errorhandling return null; } } #endregion #region User methods public User GetUser(Guid Id) { try { return CallService(s => s.GetUser(Id)); } catch (Exception ex) { // Todo Errorhandling return null; } } public IEnumerable GetUsers() { try { return CallService>(s => s.GetUsers()); } catch (Exception ex) { // Todo Errorhandling return null; } } public bool IsUserInRole(Guid userId, Guid roleId) { try { return CallService(s => s.IsUserInRole(userId, roleId)); } catch (Exception ex) { // Todo Errorhandling return false; } } public void AddUserToRole(Guid roleId, Guid userId) { try { CallService(s => s.AddUserToRole(roleId, userId)); } catch (Exception ex) { // Todo Errorhandling } } public void RemoveUserFromRole(Guid roleId, Guid userId) { try { CallService(s => s.RemoveUserFromRole(roleId, userId)); } catch (Exception ex) { // Todo Errorhandling } } public IEnumerable GetUsersInRole(Guid roleId) { try { return CallService>(s => s.GetUsersInRole(roleId)); } catch (Exception ex) { // Todo Errorhandling return null; } } public User ResetPassword(string applicationName, string userName, string password) { try { return CallService(s => s.ResetPassword(applicationName, userName, password)); } catch (Exception ex) { // Todo Errorhandling return null; } } #endregion #region Events public event EventHandler Refreshing; private void OnRefreshing() { EventHandler handler = Refreshing; if (handler != null) handler(this, EventArgs.Empty); } public event EventHandler Refreshed; private void OnRefreshed() { EventHandler handler = Refreshed; if (handler != null) handler(this, EventArgs.Empty); } void roles_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) { try { foreach (Role r in e.Items) CallService(s => s.DeleteRole(r.Id)); } catch (Exception ex) { // ErrorHandling.ShowErrorDialog("Delete failed.", ex); } } void users_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) { try { foreach (User u in e.Items) CallService(s => s.DeleteUser(u.Id)); } catch (Exception ex) { // ErrorHandling.ShowErrorDialog("Delete failed.", ex); } } void applications_ItemsRemoved(object sender, CollectionItemsChangedEventArgs e) { try { foreach (Application a in e.Items) CallService(s => s.DeleteApplication(a.Id)); } catch (Exception ex) { // ErrorHandling.ShowErrorDialog("Delete failed.", ex); } } #endregion } }