Search in sources :

Example 1 with Account

use of net.nemerosa.ontrack.model.security.Account in project ontrack by nemerosa.

the class BuildFilterServiceImpl method getBuildFilters.

@Override
public Collection<BuildFilterResource<?>> getBuildFilters(ID branchId) {
    Branch branch = structureService.getBranch(branchId);
    // Are we logged?
    Account account = securityService.getCurrentAccount();
    if (account != null) {
        // Gets the filters for this account and the branch
        return buildFilterRepository.findForBranch(OptionalInt.of(account.id()), branchId.getValue()).stream().map(t -> loadBuildFilterResource(branch, t)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    } else // Not logged, no filter
    {
        // Gets the filters for the branch
        return buildFilterRepository.findForBranch(OptionalInt.empty(), branchId.get()).stream().map(t -> loadBuildFilterResource(branch, t)).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    }
}
Also used : Ack(net.nemerosa.ontrack.model.Ack) BuildFilterNotFoundException(net.nemerosa.ontrack.model.exceptions.BuildFilterNotFoundException) BranchFilterMgt(net.nemerosa.ontrack.model.security.BranchFilterMgt) StandardBuildFilterData(net.nemerosa.ontrack.model.structure.StandardBuildFilterData) StructureService(net.nemerosa.ontrack.model.structure.StructureService) BuildFilterRepository(net.nemerosa.ontrack.repository.BuildFilterRepository) Collection(java.util.Collection) Account(net.nemerosa.ontrack.model.security.Account) Autowired(org.springframework.beans.factory.annotation.Autowired) TBuildFilter(net.nemerosa.ontrack.repository.TBuildFilter) BuildFilterNotLoggedException(net.nemerosa.ontrack.model.exceptions.BuildFilterNotLoggedException) OptionalInt(java.util.OptionalInt) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) SecurityService(net.nemerosa.ontrack.model.security.SecurityService) Service(org.springframework.stereotype.Service) LocalDate(java.time.LocalDate) Map(java.util.Map) Optional(java.util.Optional) JsonNode(com.fasterxml.jackson.databind.JsonNode) net.nemerosa.ontrack.model.buildfilter(net.nemerosa.ontrack.model.buildfilter) ID(net.nemerosa.ontrack.model.structure.ID) Branch(net.nemerosa.ontrack.model.structure.Branch) Transactional(org.springframework.transaction.annotation.Transactional) Account(net.nemerosa.ontrack.model.security.Account) Optional(java.util.Optional) Branch(net.nemerosa.ontrack.model.structure.Branch)

Example 2 with Account

use of net.nemerosa.ontrack.model.security.Account in project ontrack by nemerosa.

the class BuildFilterServiceImpl method saveFilter.

@Override
public Ack saveFilter(ID branchId, boolean shared, String name, String type, JsonNode parameters) {
    // Checks the account
    if (shared) {
        Account account = securityService.getCurrentAccount();
        // Gets the branch
        Branch branch = structureService.getBranch(branchId);
        // Checks access rights
        securityService.checkProjectFunction(branch, BranchFilterMgt.class);
        // Deletes any previous filter
        int currentAccountId = account.id();
        buildFilterRepository.findByBranchAndName(currentAccountId, branchId.get(), name).ifPresent((filter) -> buildFilterRepository.delete(currentAccountId, branchId.get(), name, true));
        // No account to be used
        return doSaveFilter(OptionalInt.empty(), branchId, name, type, parameters);
    } else {
        Account account = securityService.getCurrentAccount();
        if (account == null) {
            return Ack.NOK;
        } else {
            // Saves it for this account
            return doSaveFilter(OptionalInt.of(account.id()), branchId, name, type, parameters);
        }
    }
}
Also used : Account(net.nemerosa.ontrack.model.security.Account) Branch(net.nemerosa.ontrack.model.structure.Branch)

Example 3 with Account

use of net.nemerosa.ontrack.model.security.Account in project ontrack by nemerosa.

the class SecurityServiceIT method read_only_on_all_projects.

@Test
public void read_only_on_all_projects() throws Exception {
    withNoGrantViewToAll(() -> {
        // Creates two projects
        Project p1 = doCreateProject();
        Project p2 = doCreateProject();
        // Creates an account authorised to access all projects
        Account account = doCreateAccountWithGlobalRole("READ_ONLY");
        return asAccount(account).call(() -> {
            // With this account, gets the list of projects
            List<Project> list = structureService.getProjectList();
            // Checks we only have the two projects (among all others)
            assertTrue(list.size() >= 2);
            assertTrue(list.stream().anyMatch(project -> StringUtils.equals(p1.getName(), project.getName())));
            assertTrue(list.stream().anyMatch(project -> StringUtils.equals(p2.getName(), project.getName())));
            // Access to the projects
            assertTrue(structureService.findProjectByName(p1.getName()).isPresent());
            assertNotNull(structureService.getProject(p1.getId()));
            assertTrue(structureService.findProjectByName(p2.getName()).isPresent());
            assertNotNull(structureService.getProject(p2.getId()));
            // OK
            return true;
        });
    });
}
Also used : ProjectCreation(net.nemerosa.ontrack.model.security.ProjectCreation) Account(net.nemerosa.ontrack.model.security.Account) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) AccessDeniedException(org.springframework.security.access.AccessDeniedException) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) SecurityService(net.nemerosa.ontrack.model.security.SecurityService) List(java.util.List) AbstractServiceTestSupport(net.nemerosa.ontrack.it.AbstractServiceTestSupport) Authentication(org.springframework.security.core.Authentication) SecurityContextHolder(org.springframework.security.core.context.SecurityContextHolder) Assert(org.junit.Assert) Project(net.nemerosa.ontrack.model.structure.Project) Project(net.nemerosa.ontrack.model.structure.Project) Account(net.nemerosa.ontrack.model.security.Account) Test(org.junit.Test)

Example 4 with Account

use of net.nemerosa.ontrack.model.security.Account in project ontrack by nemerosa.

the class SecurityServiceIT method read_only_on_one_project.

@Test
public void read_only_on_one_project() throws Exception {
    withNoGrantViewToAll(() -> {
        // Creates two projects
        Project p1 = doCreateProject();
        Project p2 = doCreateProject();
        // Creates an account authorised to access only one project
        Account account = doCreateAccountWithProjectRole(p2, "READ_ONLY");
        return asAccount(account).call(() -> {
            // With this account, gets the list of projects
            List<Project> list = structureService.getProjectList();
            // Checks we only have one project
            assertEquals(1, list.size());
            assertEquals(p2.getName(), list.get(0).getName());
            // Access to the authorised project
            assertTrue(structureService.findProjectByName(p2.getName()).isPresent());
            assertNotNull(structureService.getProject(p2.getId()));
            // No access to the other project
            assertFalse(structureService.findProjectByName(p1.getName()).isPresent());
            try {
                structureService.getProject(p1.getId());
                fail("Project is not authorised");
            } catch (AccessDeniedException ignored) {
                assertTrue("Project cannot be found", true);
            }
            // OK
            return true;
        });
    });
}
Also used : Project(net.nemerosa.ontrack.model.structure.Project) Account(net.nemerosa.ontrack.model.security.Account) AccessDeniedException(org.springframework.security.access.AccessDeniedException) Test(org.junit.Test)

Example 5 with Account

use of net.nemerosa.ontrack.model.security.Account in project ontrack by nemerosa.

the class SecurityServiceIT method getCurrentAccount_none.

@Test
public void getCurrentAccount_none() throws Exception {
    Account account = securityService.getCurrentAccount();
    assertNull(account);
}
Also used : Account(net.nemerosa.ontrack.model.security.Account) Test(org.junit.Test)

Aggregations

Account (net.nemerosa.ontrack.model.security.Account)14 Test (org.junit.Test)10 Branch (net.nemerosa.ontrack.model.structure.Branch)7 Ack (net.nemerosa.ontrack.model.Ack)6 BuildFilterResource (net.nemerosa.ontrack.model.buildfilter.BuildFilterResource)4 Function (java.util.function.Function)2 BranchFilterMgt (net.nemerosa.ontrack.model.security.BranchFilterMgt)2 ProjectView (net.nemerosa.ontrack.model.security.ProjectView)2 SecurityService (net.nemerosa.ontrack.model.security.SecurityService)2 Project (net.nemerosa.ontrack.model.structure.Project)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 AccessDeniedException (org.springframework.security.access.AccessDeniedException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 LocalDate (java.time.LocalDate)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 OptionalInt (java.util.OptionalInt)1