using System; using System.Collections.Generic; using System.Linq; using System.Text; using HeuristicLab.Security.Contracts.Interfaces; using HeuristicLab.Security.Contracts.BusinessObjects; using HeuristicLab.Security.DataAccess; using HeuristicLab.PluginInfrastructure; using HeuristicLab.DataAccess.Interfaces; using System.ServiceModel; namespace HeuristicLab.Security.Core { public class SecurityManager : ISecurityManager { private static ISessionFactory factory = ServiceLocator.GetSessionFactory(); private ISession session; /// /// Add new user. /// /// /// public User AddNewUser(User user) { try { session = factory.GetSessionForCurrentThread(); IUserAdapter userAdapter = session.GetDataAdapter(); if (user != null) userAdapter.Update(user); return user; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Update user. /// /// /// public User UpdateUser(User user) { try { session = factory.GetSessionForCurrentThread(); IUserAdapter userAdapter = session.GetDataAdapter(); if (user != null) userAdapter.Update(user); return user; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Remove user. /// /// /// public bool RemoveUser(Guid userId) { try { session = factory.GetSessionForCurrentThread(); IUserAdapter userAdapter = session.GetDataAdapter(); User user = userAdapter.GetById(userId); if (user != null) return userAdapter.Delete(user); return false; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Gets all Users. /// /// public ICollection GetAllUsers() { try { session = factory.GetSessionForCurrentThread(); IUserAdapter userAdapter = session.GetDataAdapter(); return userAdapter.GetAll(); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Gets user by name. /// /// /// public User GetUserByName(string name) { try { session = factory.GetSessionForCurrentThread(); IUserAdapter userAdapter = session.GetDataAdapter(); return userAdapter.GetByName(name); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Add new user group. /// /// /// public UserGroup AddNewUserGroup(UserGroup userGroup) { try { session = factory.GetSessionForCurrentThread(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); if (userGroup != null) userGroupAdapter.Update(userGroup); return userGroup; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Update user group. /// /// /// public UserGroup UpdateUserGroup(UserGroup userGroup) { try { session = factory.GetSessionForCurrentThread(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); if (userGroup != null) userGroupAdapter.Update(userGroup); return userGroup; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Remove user group. /// /// /// public bool RemoveUserGroup(Guid userGroupId) { try { session = factory.GetSessionForCurrentThread(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); UserGroup userGroup = userGroupAdapter.GetById(userGroupId); if (userGroup != null) return userGroupAdapter.Delete(userGroup); return false; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Gets all UserGroups. /// /// public ICollection GetAllUserGroups() { try { session = factory.GetSessionForCurrentThread(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); return userGroupAdapter.GetAll(); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Gets UserGroup by name. /// /// /// public UserGroup GetUserGroupByName(string name) { try { session = factory.GetSessionForCurrentThread(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); return userGroupAdapter.GetByName(name); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Updates a PermissionOwner. /// /// /// public PermissionOwner UpdatePermissionOwner(PermissionOwner permissionOwner) { try { session = factory.GetSessionForCurrentThread(); IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter(); if (permissionOwner != null) permOwnerAdapter.Update(permissionOwner); return permissionOwner; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Add permission owner to group. /// /// /// /// public bool AddPermissionOwnerToGroup(Guid userGroupId, Guid permissionOwnerId) { try { session = factory.GetSessionForCurrentThread(); ITransaction transaction = session.BeginTransaction(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); UserGroup userGroup = userGroupAdapter.GetById(userGroupId); IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter(); PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId); if ((userGroup != null) && (permissionOwner != null)) { userGroup.Members.Add(permissionOwner); userGroupAdapter.Update(userGroup); transaction.Commit(); return true; } return false; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Remove permission owner from group. /// /// /// /// public bool RemovePermissionOwnerFromGroup(Guid userGroupId, Guid permissionOwnerId) { try { session = factory.GetSessionForCurrentThread(); ITransaction transaction = session.BeginTransaction(); IUserGroupAdapter userGroupAdapter = session.GetDataAdapter(); UserGroup userGroup = userGroupAdapter.GetById(userGroupId); IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter(); PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId); if ((userGroup != null) && (permissionOwner != null)) { userGroup.Members.Add(permissionOwner); userGroupAdapter.Delete(userGroup); transaction.Commit(); return true; } return false; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Grant permission. /// /// /// /// /// public bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) { try { session = factory.GetSessionForCurrentThread(); IPermissionAdapter permissionAdapter = session.GetDataAdapter(); return permissionAdapter.grantPermission(permissionOwnerId, permissionId, entityId); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Gets Permission by ID. /// /// /// public Permission GetPermissionById(Guid permissionId) { try { session = factory.GetSessionForCurrentThread(); IPermissionAdapter permissionAdapter = session.GetDataAdapter(); return permissionAdapter.GetById(permissionId); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } /// /// Revoke permission. /// /// /// /// /// public bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) { try { session = factory.GetSessionForCurrentThread(); IPermissionAdapter permissionAdapter = session.GetDataAdapter(); return permissionAdapter.revokePermission(permissionOwnerId, permissionId, entityId); } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } public Permission AddPermission(Permission permission) { try { session = factory.GetSessionForCurrentThread(); IPermissionAdapter permissionAdapter = session.GetDataAdapter(); if (permission != null) { permissionAdapter.Update(permission); return permission; } else return null; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } public bool RemovePermission(Guid permissionId) { try { session = factory.GetSessionForCurrentThread(); IPermissionAdapter permissionAdapter = session.GetDataAdapter(); Permission permission = permissionAdapter.GetById(permissionId); if (permission != null) return permissionAdapter.Delete(permission); else return false; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } public Permission UpdatePermission(Permission permission) { try { session = factory.GetSessionForCurrentThread(); IPermissionAdapter permissionAdapter = session.GetDataAdapter(); if(permission != null) { permissionAdapter.Update(permission); return permission; } else return null; } catch (Exception ex) { throw new FaultException("Server: " + ex.Message); } finally { if (session != null) session.EndSession(); } } } }