Search in sources :

Example 1 with CompanyOrg

use of org.ligoj.app.iam.CompanyOrg in project ligoj-api by ligoj.

the class AbstractOrgTest method setUpEntities.

@BeforeEach
public void setUpEntities() throws IOException {
    // Prepare the standard data
    persistEntities("csv", new Class[] { DelegateOrg.class }, StandardCharsets.UTF_8.name());
    persistEntities("csv", new Class[] { Node.class, Parameter.class, Project.class, Subscription.class, ParameterValue.class }, StandardCharsets.UTF_8.name());
    // Add the IAM data
    csvForJpa.cleanup(CacheCompany.class, CacheUser.class, CacheGroup.class, CacheMembership.class, CacheProjectGroup.class);
    final Map<String, CompanyOrg> companies = csvForJpa.insert("csv", CacheCompany.class, StandardCharsets.UTF_8.name()).stream().map(c -> new CompanyOrg(c.getDescription(), c.getName())).collect(Collectors.toMap(CompanyOrg::getId, Function.identity()));
    final Map<String, UserOrg> users = csvForJpa.insert("csv", CacheUser.class, StandardCharsets.UTF_8.name()).stream().map(c -> {
        final UserOrg user = new UserOrg();
        user.setId(c.getId());
        user.setDn("uid=" + c.getId() + "," + companies.get(c.getCompany().getId()).getDn());
        user.setCompany(c.getCompany().getId());
        user.setFirstName(c.getFirstName());
        user.setLastName(c.getLastName());
        user.setMails(Arrays.asList(Optional.ofNullable(c.getMails()).orElse("").split(",")));
        return user;
    }).collect(Collectors.toMap(UserOrg::getId, Function.identity()));
    final Map<String, GroupOrg> groups = csvForJpa.insert("csv", CacheGroup.class, StandardCharsets.UTF_8.name()).stream().map(c -> new GroupOrg(c.getDescription(), c.getName(), new HashSet<>())).collect(Collectors.toMap(GroupOrg::getId, Function.identity()));
    CacheMembership cacheMembership = csvForJpa.insert("csv", CacheMembership.class, StandardCharsets.UTF_8.name()).get(0);
    csvForJpa.insert("csv", CacheProjectGroup.class, StandardCharsets.UTF_8.name());
    // Coverage required here only there because of JPA bean
    Assertions.assertNotNull(cacheMembership.getGroup());
    Assertions.assertNotNull(cacheMembership.getUser());
    Assertions.assertNull(cacheMembership.getSubGroup());
    cacheMembership.setSubGroup(null);
    // Plug-in the IAMProvider to the database
    final IamConfiguration configuration = new IamConfiguration();
    final EmptyUserRepository userRepository = new EmptyUserRepository() {

        @Override
        public Map<String, UserOrg> findAll() {
            return users;
        }

        @Override
        public UserOrg findById(final String login) {
            return findAll().get(login);
        }

        @Override
        public UserOrg findOneBy(final String attribute, final String value) {
            return findAllBy(attribute, value).stream().findFirst().orElse(null);
        }
    };
    configuration.setUserRepository(userRepository);
    configuration.setCompanyRepository(new EmptyCompanyRepository() {

        @Override
        public Map<String, CompanyOrg> findAll() {
            return companies;
        }

        @Override
        public CompanyOrg findById(final String user, final String id) {
            // Check the container exists and return the in memory object.
            return Optional.ofNullable(cacheCompanyRepository.findById(user, Normalizer.normalize(id))).map(CacheContainer::getId).map(this::findById).orElse(null);
        }
    });
    configuration.setGroupRepository(new EmptyGroupRepository() {

        @Override
        public Map<String, GroupOrg> findAll() {
            return groups;
        }

        @Override
        public GroupOrg findById(final String user, final String id) {
            // Check the container exists and return the in memory object.
            return Optional.ofNullable(cacheGroupRepository.findById(user, Normalizer.normalize(id))).map(CacheContainer::getId).map(this::findById).orElse(null);
        }
    });
    userRepository.setCompanyRepository(configuration.getCompanyRepository());
    iamProvider = new EmptyIamProvider() {

        @Override
        public IamConfiguration getConfiguration() {
            return configuration;
        }
    };
    em.flush();
    em.clear();
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Parameter(org.ligoj.app.model.Parameter) Arrays(java.util.Arrays) EmptyIamProvider(org.ligoj.app.iam.empty.EmptyIamProvider) Autowired(org.springframework.beans.factory.annotation.Autowired) Function(java.util.function.Function) CacheUser(org.ligoj.app.iam.model.CacheUser) HashSet(java.util.HashSet) GroupOrg(org.ligoj.app.iam.GroupOrg) Map(java.util.Map) CacheCompany(org.ligoj.app.iam.model.CacheCompany) Subscription(org.ligoj.app.model.Subscription) Normalizer(org.ligoj.app.api.Normalizer) ParameterValue(org.ligoj.app.model.ParameterValue) CacheMembership(org.ligoj.app.iam.model.CacheMembership) Node(org.ligoj.app.model.Node) AbstractAppTest(org.ligoj.app.AbstractAppTest) CacheCompanyRepository(org.ligoj.app.iam.dao.CacheCompanyRepository) IOException(java.io.IOException) CacheProjectGroup(org.ligoj.app.model.CacheProjectGroup) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) DelegateOrg(org.ligoj.app.iam.model.DelegateOrg) IamConfiguration(org.ligoj.app.iam.IamConfiguration) EmptyUserRepository(org.ligoj.app.iam.empty.EmptyUserRepository) EmptyCompanyRepository(org.ligoj.app.iam.empty.EmptyCompanyRepository) EmptyGroupRepository(org.ligoj.app.iam.empty.EmptyGroupRepository) CompanyOrg(org.ligoj.app.iam.CompanyOrg) CacheGroup(org.ligoj.app.iam.model.CacheGroup) CacheContainer(org.ligoj.app.iam.model.CacheContainer) CacheGroupRepository(org.ligoj.app.iam.dao.CacheGroupRepository) Assertions(org.junit.jupiter.api.Assertions) Optional(java.util.Optional) UserOrg(org.ligoj.app.iam.UserOrg) Project(org.ligoj.app.model.Project) CacheGroup(org.ligoj.app.iam.model.CacheGroup) UserOrg(org.ligoj.app.iam.UserOrg) CacheCompany(org.ligoj.app.iam.model.CacheCompany) CompanyOrg(org.ligoj.app.iam.CompanyOrg) EmptyUserRepository(org.ligoj.app.iam.empty.EmptyUserRepository) EmptyCompanyRepository(org.ligoj.app.iam.empty.EmptyCompanyRepository) GroupOrg(org.ligoj.app.iam.GroupOrg) CacheMembership(org.ligoj.app.iam.model.CacheMembership) EmptyGroupRepository(org.ligoj.app.iam.empty.EmptyGroupRepository) IamConfiguration(org.ligoj.app.iam.IamConfiguration) Map(java.util.Map) CacheUser(org.ligoj.app.iam.model.CacheUser) EmptyIamProvider(org.ligoj.app.iam.empty.EmptyIamProvider) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with CompanyOrg

use of org.ligoj.app.iam.CompanyOrg in project ligoj-api by ligoj.

the class DelegateOrgResource method validateSaveOrUpdate.

/**
 * Validate the user changes regarding the current user's right. The associated DN and the real CN will stored in
 * database.<br>
 * Rules, order is important :
 * <ul>
 * <li>Related company must be managed by the current user, directly or via a another parent delegate tree/company,
 * or act as if the company does not exist.</li>
 * <li>Related group must be managed by the current user, directly or via a another parent delegate group/tree, or
 * act as if the group does not exist.</li>
 * <li>Related tree must be managed by the current user, directly or via a another parent delegate tree.</li>
 * <li>'write' flag cannot be <code>true</code> without already owning an applicable delegate with this flag.</li>
 * <li>'admin' flag cannot be <code>true</code> without already owning an applicable delegate with this flag.</li>
 * </ul>
 * Attention, DN is case sensitive.
 *
 * @return the created/update {@link DelegateOrg}
 */
private DelegateOrg validateSaveOrUpdate(final DelegateOrgEditionVo importEntry) {
    final Map<String, CompanyOrg> allCompanies = getCompany().findAll();
    final Map<String, GroupOrg> allGroups = getGroup().findAll();
    // Save the delegate with normalized name
    final DelegateOrg entity = toEntity(importEntry);
    // Get all delegates of current user
    String dn = "n/a";
    if (importEntry.getType() == DelegateType.COMPANY) {
        dn = validateCompany(importEntry, allCompanies, dn);
    } else if (importEntry.getType() == DelegateType.GROUP) {
        dn = validateGroup(importEntry, allGroups, dn);
    } else {
        // Tree, CN <- DN
        dn = validateTree(importEntry);
        // Name is ignored for this type in the internal format
        entity.setName("-");
    }
    // INTO the corresponding DN
    if (repository.findByMatchingDnForAdmin(securityHelper.getLogin(), dn, importEntry.getType()).isEmpty()) {
        throw new ForbiddenException();
    }
    // FROM the corresponding DN
    if (importEntry.getId() != null) {
        // Check the related DN
        validateWriteAccess(importEntry.getId());
    }
    // DN is already normalized
    entity.setDn(dn);
    repository.saveAndFlush(entity);
    return entity;
}
Also used : ForbiddenException(javax.ws.rs.ForbiddenException) DelegateOrg(org.ligoj.app.iam.model.DelegateOrg) CompanyOrg(org.ligoj.app.iam.CompanyOrg) GroupOrg(org.ligoj.app.iam.GroupOrg)

Example 3 with CompanyOrg

use of org.ligoj.app.iam.CompanyOrg in project ligoj-api by ligoj.

the class EmptyCompanyRepositoryTest method create.

@Test
public void create() {
    final CompanyOrg companyLdap = new EmptyCompanyRepository().create("Cn=Some", "Name");
    Assertions.assertEquals("Cn=Some", companyLdap.getDn());
    Assertions.assertEquals("Name", companyLdap.getName());
    Assertions.assertEquals("name", companyLdap.getId());
}
Also used : CompanyOrg(org.ligoj.app.iam.CompanyOrg) EmptyCompanyRepository(org.ligoj.app.iam.empty.EmptyCompanyRepository) Test(org.junit.jupiter.api.Test)

Aggregations

CompanyOrg (org.ligoj.app.iam.CompanyOrg)3 GroupOrg (org.ligoj.app.iam.GroupOrg)2 EmptyCompanyRepository (org.ligoj.app.iam.empty.EmptyCompanyRepository)2 DelegateOrg (org.ligoj.app.iam.model.DelegateOrg)2 IOException (java.io.IOException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 ForbiddenException (javax.ws.rs.ForbiddenException)1 Assertions (org.junit.jupiter.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1 AbstractAppTest (org.ligoj.app.AbstractAppTest)1 Normalizer (org.ligoj.app.api.Normalizer)1 IamConfiguration (org.ligoj.app.iam.IamConfiguration)1 UserOrg (org.ligoj.app.iam.UserOrg)1