/// <summary> /// initialize by name /// </summary> /// <param name="login">user name [email]</param> /// <param name="auth"></param> public void Init(string login, IAuthenticationService auth) { if (!string.IsNullOrEmpty(login)) { User = auth.GetUser(login); } }
public void CreateUser(User user) { try { _unitOfWork.UserRepository.Insert(user); _unitOfWork.Save(); } catch (Exception ex) { _logger.Error("Error in AuthentificationService.cs CreateUser(User user)" + ex.Message); throw; } }
public void DeleteUser(User user) { try { if (user == null) { throw new ArgumentNullException("user"); } _unitOfWork.UserRepository.Delete(user); _unitOfWork.Save(); } catch (Exception ex) { _logger.Error("Error in AuthentificationService.cs DeleteUser(User user)" + ex.Message); throw; } }
public bool UpdateUser(User user, IEnumerable<int> roles) { try { if (user == null) { _logger.Error("UpdateUser(User user, IEnumerable<int> roles) user is null "); throw new ArgumentNullException("user"); } User u = _unitOfWork.UserRepository.GetById(user.Id); user.UserRoles = new List<Role>(); foreach (var r in roles) user.UserRoles.Add(_unitOfWork.RoleRepository.GetById(r)); _unitOfWork.UserRepository.AttachStubs(user.UserRoles.ToArray()); foreach (var role in _unitOfWork.RoleRepository.Get().Where(role => role.Users.Any(x => x.Id == user.Id))) { role.Users.Remove(role.Users.First(x => x.Id == user.Id)); } _unitOfWork.Save(); foreach (var prop in u.GetType().GetProperties()) { prop.SetValue(u, user.GetType().GetProperty(prop.Name).GetValue(user)); } _unitOfWork.UserRepository.Update(u); _unitOfWork.Save(); return true; } catch (Exception ex) { _logger.Error("Error in AuthentificationService.cs UpdateUser(User user, IEnumerable<int> roles))" + ex.Message); throw; } }
public bool InRoles(string roles, User user) { try { if (user == null) { throw new NullReferenceException("user"); } if (string.IsNullOrWhiteSpace(roles)) return false; var rolesArray = roles.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries); return rolesArray.Select(role => _unitOfWork.UserRepository.GetById(user.Id).UserRoles. Any(p => String.Compare(p.Name, role, StringComparison.OrdinalIgnoreCase) == 0)). Any(hasRole => hasRole); } catch (Exception ex) { _logger.Error("Error in AuthentificationService.cs InRoles(string roles, User user))" + ex.Message); throw; } }