public Admin CreateSuperAdmin() { var adminId = RoleIds.SuperAdminId; var roleId = RoleIds.SuperAdminId; var superAdmin = "SuperAdmin"; var role = new Role { Id = roleId, Code = superAdmin, Name = superAdmin, CreatedDate = DateTime.UtcNow }; var admin = new Admin { Id = adminId, Username = superAdmin, FirstName = superAdmin, LastName = superAdmin, IsActive = true, Description = superAdmin, Role = role }; _securityRepository.Admins.AddOrUpdate(admin); _securityRepository.SaveChanges(); _authCommands.CreateRole(new CreateRole { RoleId = roleId, Permissions = _authRepository.Permissions.Select(p => p.Id).ToList() }); _authCommands.CreateActor(new CreateActor { ActorId = admin.Id, Username = admin.Username, Password = admin.Username }); _authCommands.AssignRoleToActor(new AssignRole { ActorId = adminId, RoleId = role.Id }); return(admin); }
public void Can_assign_role_to_actor() { var createActor = new CreateActor { ActorId = Guid.NewGuid(), Username = TestDataGenerator.GetRandomString(), Password = TestDataGenerator.GetRandomString() }; _authCommands.CreateActor(createActor); _authCommands.CreatePermission(new CreatePermission { Name = "Test", Module = "Test" }); var createRole = new CreateRole { RoleId = Guid.NewGuid(), Permissions = _authQueries.GetPermissions().Select(p => p.Id).ToList() }; _authCommands.CreateRole(createRole); _authCommands.AssignRoleToActor(new AssignRole { ActorId = createActor.ActorId, RoleId = createRole.RoleId }); var actor = _authRepository.Actors.Single(a => a.Id == createActor.ActorId); actor.Role.Should().NotBeNull(); actor.Role.Id.Should().Be(createRole.RoleId); }
public void Can_verify_permission_for_admin() { _authCommands.CreatePermission(new CreatePermission { Name = "Test", Module = "Test" }); var permissions = _authQueries.GetPermissions().Select(p => p.Id); var roleId = Guid.NewGuid(); _authCommands.CreateRole(new CreateRole { RoleId = roleId, Permissions = permissions.ToList() }); var actorId = Guid.NewGuid(); var createActorModel = new CreateActor { ActorId = actorId, Password = TestDataGenerator.GetRandomString(), Username = TestDataGenerator.GetRandomString() }; _authCommands.CreateActor(createActorModel); _authCommands.AssignRoleToActor(new AssignRole { ActorId = createActorModel.ActorId, RoleId = roleId }); var actorHasPermission = _authQueries.VerifyPermission(actorId, "Test", "Test"); var actorHasNoPermission = _authQueries.VerifyPermission(actorId, "Invalid", "Invalid"); actorHasPermission.Should().BeTrue(); actorHasNoPermission.Should().BeFalse(); }
public Admin CreateAdmin(AddAdminData data) { var validationResult = new AddAdminValidator(_repository).Validate(data); if (!validationResult.IsValid) { throw new RegoException(validationResult.Errors.First().ErrorMessage); } //todo: KB: not sure how input role id can be null. And if it is validation should trigger var role = _repository.Roles.SingleOrDefault(r => r.Id == (data.RoleId ?? new Guid("00000000-0000-0000-0000-000000000002"))); var admin = Mapper.DynamicMap <Admin>(data); using (var scope = CustomTransactionScope.GetTransactionScope()) { admin.Id = Guid.NewGuid(); admin.Role = role; admin.SetLicensees(data.AssignedLicensees); admin.SetAllowedBrands(data.AllowedBrands); admin.SetCurrencies(data.Currencies); if (data.AllowedBrands != null) { foreach (var allowedBrand in data.AllowedBrands) { admin.BrandFilterSelections.Add(new BrandFilterSelection { AdminId = admin.Id, BrandId = allowedBrand, Admin = admin }); } } _authCommands.CreateActor(new CreateActor { ActorId = admin.Id, Username = admin.Username, Password = data.Password }); _authCommands.AssignRoleToActor(new AssignRole { ActorId = admin.Id, RoleId = role.Id }); _repository.Admins.Add(admin); _repository.SaveChanges(); _eventBus.Publish(new AdminCreated(admin)); scope.Complete(); } return(admin); }