#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.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.FirstOrDefault(x => x.UserId == id); var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault(); return Convert.ToDto(user, membership); } } public IEnumerable GetUsers() { using (UserManagementDataContext db = new UserManagementDataContext()) { var users = db.aspnet_Users.OrderBy(x => x.UserId).ToList().Zip(db.aspnet_Memberships.OrderBy(x => x.UserId), (x, y) => Convert.ToDto(x, y)); return users; } } public IEnumerable GetUsersForApplication(Guid applicationId) { using (UserManagementDataContext db = new UserManagementDataContext()) { var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).OrderBy(x => x.UserId).ToList().Zip(db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).OrderBy(x => x.UserId), (x, y) => Convert.ToDto(x, y)).ToArray(); return users; } } public Guid AddUser(User user) { if (user != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_User eUser; aspnet_Membership eMembership; user.Id = Guid.NewGuid(); Convert.ToEntity(user, out eUser, out eMembership); db.aspnet_Users.InsertOnSubmit(eUser); db.aspnet_Memberships.InsertOnSubmit(eMembership); db.SubmitChanges(); return user.Id; } } return Guid.Empty; } public void 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(); db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); db.aspnet_Memberships.DeleteOnSubmit(membership); db.aspnet_Users.DeleteOnSubmit(user); db.SubmitChanges(); } } public void UpdateUser(User user) { 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); db.SubmitChanges(); } } public void AddUserToRole(Guid roleId, Guid userId) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_UsersInRole r = new aspnet_UsersInRole(); r.RoleId = roleId; r.UserId = userId; db.aspnet_UsersInRoles.InsertOnSubmit(r); db.SubmitChanges(); } } public IEnumerable GetUsersInRole(Guid roleId) { using (UserManagementDataContext db = new UserManagementDataContext()) { List userList = new List(); var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList(); foreach (aspnet_UsersInRole u in users) { userList.Add(GetUser(u.UserId).Id); } return userList; } } public bool IsUserInRole(Guid userId, Guid roleId) { using (UserManagementDataContext db = new UserManagementDataContext()) { bool isInRole = false; 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 void RemoveUserFromRole(Guid roleId, Guid userId) { using (UserManagementDataContext db = new UserManagementDataContext()) { var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault(); db.aspnet_UsersInRoles.DeleteOnSubmit(role); db.SubmitChanges(); } } // TODO !!! public User ResetPassword(string applicationName, string userName, string password) { string salt = ""; string answer = ""; using (UserManagementDataContext db = new UserManagementDataContext()) { db.aspnet_Membership_ResetPassword(applicationName, userName, password, null, null, salt, null, null, answer); return null; } } #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.ToDto(role); } } public IEnumerable GetRoles() { using (UserManagementDataContext db = new UserManagementDataContext()) { List roleList = new List(); var roles = db.aspnet_Roles.ToList(); foreach (aspnet_Role role in roles) { roleList.Add(Convert.ToDto(role)); } return roleList; } } public IEnumerable GetRolesForApplication(Guid applicationId) { using (UserManagementDataContext db = new UserManagementDataContext()) { List roleList = new List(); var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList(); foreach (aspnet_Role role in roles) { roleList.Add(Convert.ToDto(role)); } return roleList; } } public Guid AddRole(Role role) { if (role != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_Role eRole = new aspnet_Role(); role.Id = Guid.NewGuid(); Convert.ToEntity(role, eRole); db.aspnet_Roles.InsertOnSubmit(eRole); db.SubmitChanges(); return role.Id; } } return Guid.Empty; } public void DeleteRole(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault(); var userinroles = db.aspnet_UsersInRoles.Where(x => x.RoleId == id).ToList(); db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles); db.aspnet_Roles.DeleteOnSubmit(role); db.SubmitChanges(); } } public IEnumerable GetRolesForUser(Guid userId) { using (UserManagementDataContext db = new UserManagementDataContext()) { List roleList = new List(); var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList(); foreach (aspnet_UsersInRole r in roles) { roleList.Add(GetRole(r.RoleId).Id); } return roleList; } } public void UpdateRole(Role role) { using (UserManagementDataContext db = new UserManagementDataContext()) { var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault(); Convert.ToEntity(role, eRole); db.SubmitChanges(); } } #endregion #region Application public Guid AddApplication(Application application) { if (application != null) { using (UserManagementDataContext db = new UserManagementDataContext()) { aspnet_Application eApplication = new aspnet_Application(); application.Id = Guid.NewGuid(); Convert.ToEntity(application, eApplication); db.aspnet_Applications.InsertOnSubmit(eApplication); db.SubmitChanges(); return application.Id; } } return Guid.Empty; } public void UpdateApplication(Application application) { using (UserManagementDataContext db = new UserManagementDataContext()) { var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault(); Convert.ToEntity(application, eApplication); db.SubmitChanges(); } } public void 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(); 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(); } } public Application GetApplication(Guid id) { using (UserManagementDataContext db = new UserManagementDataContext()) { var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault(); return Convert.ToDto(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.ToDto(app)); } } return applicationList; } #endregion } }