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();
}
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;
}
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());
}
Aggregations