using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Services.Authentication; using System.ServiceModel.Security; using System.ServiceModel; namespace HeuristicLab.Services.Authentication.ServiceClients { public class AuthenticationClient { private static AuthenticationClient instance; public static AuthenticationClient Instance { get { if (instance == null) instance = new AuthenticationClient(); return instance; } } #region Properties private IEnumerable users; public IEnumerable Users { get { return users; } } private IEnumerable applications; public IEnumerable Applications { get { return applications; } } private IEnumerable roles; public IEnumerable Roles { get { return roles; } } #endregion private AuthenticationClient() { applications = GetApplications(); users = CallService(s => s.GetAllUsers()).OrderBy(x => x.Name); roles = CallService(s => s.GetAllRoles()).OrderBy(x => x.Name); } #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() { var call = new Func(delegate() { Guid applicationId = Guid.Empty; // to be set! try { users = CallService(s => s.GetUsers(applicationId)).OrderBy(x => x.Name); roles = CallService(s => s.GetRoles(applicationId)).OrderBy(x => x.Name); // ... 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); } }, null); } #endregion 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(); } } } #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 Guid AddApplication(Application application) { try { return CallService(s => s.AddApplication(application)); } catch (Exception ex) { // Todo Errorhandling return Guid.Empty; } } public bool DeleteApplication(Guid id) { try { return CallService(s => s.DeleteApplication(id)); } catch (Exception ex) { // Todo Errorhandling return false; } } public IEnumerable GetApplications() { try { return CallService>(s => s.GetApplications()); } catch (Exception ex) { // Todo Errorhandling return null; } } public bool UpdateApplication(Application application) { try { return CallService(s => s.UpdateApplication(application)); } catch (Exception ex) { // Todo Errorhandling return false; } } #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 GetAllRoles() { try { return CallService>(s => s.GetAllRoles()); } catch (Exception ex) { // Todo Errorhandling return null; } } public IEnumerable GetRoles(Guid applicationId) { try { return CallService>(s => s.GetRoles(applicationId)); } catch (Exception ex) { // Todo Errorhandling return null; } } public Guid AddRole(Role role) { try { return CallService(s => s.AddRole(role)); } catch (Exception ex) { // Todo Errorhandling return Guid.Empty; } } public bool UpdateRole(Role role) { try { return CallService(s => s.UpdateRole(role)); } catch (Exception ex) { // Todo Errorhandling return false; } } public bool DeleteRole(Guid Id) { try { return CallService(s => s.DeleteRole(Id)); } catch (Exception ex) { // Todo Errorhandling return false; } } #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 GetAllUsers() { try { return CallService>(s => s.GetAllUsers()); } catch (Exception ex) { // Todo Errorhandling return null; } } public IEnumerable GetUsers(Guid applicationId) { try { return CallService>(s => s.GetUsers(applicationId)); } catch (Exception ex) { // Todo Errorhandling return null; } } public Guid AddUser(User user) { try { return CallService(s => s.AddUser(user)); } catch (Exception ex) { // Todo Errorhandling return Guid.Empty; } } public bool UpdateUser(User user) { try { return CallService(s => s.UpdateUser(user)); } catch (Exception ex) { // Todo Errorhandling return false; } } public bool AddUserToRole(Guid roleId, Guid userId) { try { return CallService(s => s.AddUserToRole(roleId, userId); } catch (Exception ex) { // Todo Errorhandling return false; } } public IEnumerable GetUsersInRole(Guid roleId) { try { return CallService>(s => s.GetUsersInRole(roleId)); } catch (Exception ex) { // Todo Errorhandling return null; } } #endregion //public static void Main() { // AuthenticationServiceClient auth = new AuthenticationServiceClient(); // auth.ClientCredentials.UserName.UserName = "Alice"; // auth.ClientCredentials.UserName.Password = "YouWillKnow"; // auth.ClientCredentials.ServiceCertificate.Authentication.CertificateValidationMode = X509CertificateValidationMode.None; // Application[] apps = auth.GetApplications(); // foreach (Application app in apps) { // Console.WriteLine(app.Name); // } // Console.ReadLine(); //} } }