private void Update(Entities.User entity, User user, string password = null) { entity.FirstName = user.FirstName; entity.LastName = user.LastName; entity.CompanyId = user.CompanyId.Value; entity.Email = user.Email; entity.CompanyRole = user.CompanyRole; entity.Roles.RemoveAll(ur => user.Roles.All(r => r.BuildingId.Value != ur.BuildingId && r.UserBuildingRole != ur.UserBuildingRole && user.CompanyId.Value != ur.CompanyId)); entity.Roles.AddRange(user.Roles .Where(r => entity.Roles.All(ur => r.BuildingId.Value != ur.BuildingId && r.UserBuildingRole != ur.UserBuildingRole && user.CompanyId.Value != ur.CompanyId)) .Select(r => new UserRole { BuildingId = r.BuildingId.Value, CompanyId = user.CompanyId.Value, UserBuildingRole = r.UserBuildingRole, UserId = entity.Id })); if (password != null) { entity.Password = _bubelSoftUserPassword.Hash(new UserLogInInfo { Email = entity.Email, Password = password }); } }
private static string GeneratePasswordHash(User user, IBubelSoftUserPassword bubelSoftUserPassword) { var userLogInInfo = new UserLogInInfo { Email = user.Email, Password = "******" }; return(bubelSoftUserPassword.Hash(userLogInInfo)); }
private static User Create(Entities.User entity) { var user = new User(new UserId(entity.Id), entity.FirstName, entity.LastName, entity.CompanyRole, entity.Email); if (entity.CompanyId != null) { user.From(new CompanyId(entity.CompanyId.Value)); } entity.Roles.ForEach(r => user.AddRole(new BuildingId(r.BuildingId), r.UserBuildingRole)); return(user); }
private UserLogInInfo StubLogInRepository(User user, bool goodPassword) { const string passwordHash = "passwordHash"; _userRepository.GetForLogIn(user.Email).Returns((user, passwordHash)); var userLogInInfo = new UserLogInInfo { Email = user.Email, Password = passwordHash }; _bubelSoftPassword.Verify(Arg.Any <UserLogInInfo>(), Arg.Any <string>()).Returns(goodPassword); return(userLogInInfo); }
public void Get_Forbid_UserHasNoAccess() { var buildingId = new BuildingId(12); var companyId = new CompanyId(16); var building = new Domain.Models.Building(buildingId, "name", new Company(companyId, "cName", "cNumber")); _buildingRepository.Get(buildingId).Returns(building); var user = new User(new UserId(2), "", "", UserCompanyRole.UserAdmin, ""); user.From(companyId); _userSession.User.Returns(user); var response = _buildingsController.Get(buildingId.Value); Assert.That(response, Is.TypeOf <ForbidResult>()); }
public void GetCompanies_Ok_BuildingExistsUserHasAccess() { var buildingId = new BuildingId(12); var companyId = new CompanyId(16); var building = new Domain.Models.Building(buildingId, "name", new Company(companyId, "cName", "cNumber")); _buildingRepository.Get(buildingId).Returns(building); var user = new User(new UserId(2), "", "", UserCompanyRole.UserAdmin, ""); user.From(companyId); user.AddRole(buildingId, UserBuildingRole.Supervisor); _userSession.User.Returns(user); var response = _buildingsController.GetCompanies(buildingId.Value); Assert.That(response, Is.TypeOf <OkObjectResult>()); }
public UserId Save(User user, string password = null) { var entity = user.IsNew ? new Entities.User() : Get(user.Id.Value); Update(entity, user, password); if (entity.Id == 0) { _context.Add(entity); } _context.SaveChanges(); if (user.Id.Value == 0) { user.SetId(new UserId(entity.Id)); } return(new UserId(entity.Id)); }
public static void Initialize(MainContext context, IBubelSoftUserPassword bubelSoftUserPassword) { SetUpRepositories(context, bubelSoftUserPassword); var companies = new[] { new Company("Company 1", "123-456-789"), new Company("Company 2", "123-456-789") }; foreach (var company in companies) { _companyRepository.Save(company); } var buildings = new[] { new Building("Building 1", companies[0], new [] { companies[1] }), new Building("Building 2", companies[1], new List <Company>()) }; foreach (var building in buildings) { _buildingRepository.Save(building); } var maciek = new User("MacBub", "Bubel", UserCompanyRole.Admin, "*****@*****.**"); maciek.From(companies[0].Id); maciek.AddRole(buildings[0].Id, UserBuildingRole.Admin); maciek.AddRole(buildings[0].Id, UserBuildingRole.Reporter); _userRepository.Save(maciek, "qwe"); var kamil = new User("KamBub", "Bubel", UserCompanyRole.Admin, "*****@*****.**"); kamil.From(companies[1].Id); kamil.AddRole(buildings[1].Id, UserBuildingRole.Admin); kamil.AddRole(buildings[0].Id, UserBuildingRole.Admin); _userRepository.Save(kamil, "qwe"); }