using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using HeuristicLab.Services.Authentication.DataTransfer; using HeuristicLab.Services.Authentication.DataAccess; using System.Data.Linq; using System.Diagnostics; namespace HeuristicLab.Services.Authentication { [ServiceBehavior(IncludeExceptionDetailInFaults = true)] public class AuthenticationService : IAuthenticationService { #region User public DataTransfer.User GetUser(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault(); var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); return Convert.ToDataTransfer(user, membership); } } public IEnumerable GetAllUsers() { List userList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { var users = db.aspnet_Users.ToList(); foreach (aspnet_User user in users) { var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault(); userList.Add(Convert.ToDataTransfer(user, membership)); } } return userList; } public IEnumerable GetUsers(Guid applicationId) { List userList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList(); foreach (aspnet_User user in users) { var membership = db.aspnet_Memberships.Where(x => x.UserId == user.UserId).FirstOrDefault(); userList.Add(Convert.ToDataTransfer(user, membership)); } } return userList; } public Guid AddUser(User user) { if (user != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_User eUser = new aspnet_User(); aspnet_Membership eMembership = new aspnet_Membership(); Convert.ToEntity(user, eUser, eMembership); try { db.aspnet_Users.InsertOnSubmit(eUser); db.aspnet_Memberships.InsertOnSubmit(eMembership); db.SubmitChanges(); } catch (Exception ex) { return Guid.Empty; } return user.Id; } } return Guid.Empty; } public bool DeleteUser(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault(); var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == id).ToList(); try { db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); db.aspnet_Memberships.DeleteOnSubmit(membership); db.aspnet_Users.DeleteOnSubmit(user); db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } return false; } public bool UpdateUser(User user) { if (user != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { var eUser = db.aspnet_Users.Where(x => x.UserId == user.Id).FirstOrDefault(); var eMembership = db.aspnet_Memberships.Where(x => x.UserId == user.Id).FirstOrDefault(); Convert.ToEntity(user, eUser, eMembership); try { db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } } return false; } public bool AddUserToRole(Guid roleId, Guid userId) { using (UserManagementDataContext db = new UserManagementDataContext()) { try { aspnet_UsersInRole r = new aspnet_UsersInRole(); r.RoleId = roleId; r.UserId = userId; db.aspnet_UsersInRoles.InsertOnSubmit(r); db.SubmitChanges(); return true; } catch (Exception ex) { Debug.WriteLine(ex.InnerException); return false; } } } public IEnumerable GetUsersInRole(Guid roleId) { List userList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { try { var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList(); foreach (aspnet_UsersInRole u in users) { userList.Add(GetUser(u.UserId).Id); } } catch (Exception ex) { Debug.WriteLine(ex.InnerException); return new List(); } } return userList; } /* public IEnumerable GetUsersInRole(Guid roleId) { List userList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { try { var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList(); foreach (aspnet_UsersInRole u in users) { userList.Add(GetUser(u.UserId)); } } catch (Exception ex) { Debug.WriteLine(ex.InnerException); return new List(); } } return userList; } */ public bool IsUserInRole(Guid userId, Guid roleId) { bool isInRole = false; using (UserManagementDataContext db = new UserManagementDataContext()) { var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).ToList(); foreach (aspnet_UsersInRole u in users) { isInRole = true; } } return isInRole; } public bool RemoveUserFromRole(Guid roleId, Guid userId) { using (UserManagementDataContext db = new UserManagementDataContext()) { try { var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault(); db.aspnet_UsersInRoles.DeleteOnSubmit(role); db.SubmitChanges(); return true; } catch (Exception ex) { Debug.WriteLine(ex.InnerException); return false; } } } #endregion #region Role public Role GetRole(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); return Convert.ToDataTransfer(role); } } public IEnumerable GetAllRoles() { List roleList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { var roles = db.aspnet_Roles.ToList(); foreach (aspnet_Role role in roles) { roleList.Add(Convert.ToDataTransfer(role)); } } return roleList; } public IEnumerable GetRoles(Guid applicationId) { List roleList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList(); foreach (aspnet_Role role in roles) { roleList.Add(Convert.ToDataTransfer(role)); } } return roleList; } public Guid AddRole(Role role) { if (role != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_Role eRole = new aspnet_Role(); Convert.ToEntity(role, eRole); try { db.aspnet_Roles.InsertOnSubmit(eRole); db.SubmitChanges(); } catch (Exception ex) { return Guid.Empty; } return role.Id; } } return Guid.Empty; } public bool DeleteRole(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); try { db.aspnet_Roles.DeleteOnSubmit(role); db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } } public IEnumerable GetRolesForUser(Guid userId) { List roleList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { try { var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList(); foreach (aspnet_UsersInRole r in roles) { roleList.Add(GetRole(r.RoleId).Id); } } catch (Exception ex) { Debug.WriteLine(ex.InnerException); return new List(); } } return roleList; } /* public IEnumerable GetRolesForUser(Guid userId) { List roleList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { try { var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList(); foreach (aspnet_UsersInRole r in roles) { roleList.Add(GetRole(r.RoleId)); } } catch (Exception ex) { Debug.WriteLine(ex.InnerException); return new List(); } } return roleList; } */ public bool UpdateRole(Role role) { if (role != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault(); Convert.ToEntity(role, eRole); try { db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } } return false; } #endregion #region Application public Guid AddApplication(Application application) { if (application != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_Application eApplication = new aspnet_Application(); Convert.ToEntity(application, eApplication); try { db.aspnet_Applications.InsertOnSubmit(eApplication); db.SubmitChanges(); } catch (Exception ex) { return Guid.Empty; } return application.Id; } } return Guid.Empty; } public bool UpdateApplication(Application application) { if (application != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault(); Convert.ToEntity(application, eApplication); try { db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } } return false; } public bool DeleteApplication(Guid applicationId) { using (UserManagementDataContext db = new UserManagementDataContext()) { var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList(); var memberships = db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).ToList(); var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList(); var application = db.aspnet_Applications.Where(x => x.ApplicationId == applicationId).FirstOrDefault(); try { foreach (aspnet_User u in users) { var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == u.UserId).ToList(); db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); } db.aspnet_Memberships.DeleteAllOnSubmit(memberships); db.aspnet_Users.DeleteAllOnSubmit(users); db.aspnet_Roles.DeleteAllOnSubmit(roles); db.aspnet_Applications.DeleteOnSubmit(application); db.SubmitChanges(); } catch (Exception ex) { return false; } return true; } return false; } public Application GetApplication(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault(); return Convert.ToDataTransfer(application); } } public IEnumerable GetApplications() { List applicationList = new List(); using (UserManagementDataContext db = new UserManagementDataContext()) { var apps = db.aspnet_Applications.ToList(); foreach (aspnet_Application app in apps) { applicationList.Add(Convert.ToDataTransfer(app)); } } return applicationList; } #endregion } }