use of cz.metacentrum.perun.core.api.PerunSession in project perun by CESNET.
the class Utils method generateAllVosToWriter.
/**
* Method generate all Vos to the text for using in LDIF.
* Write all these information to writer in perunInitializer object.
*
* @param perunInitializer need to be loaded to get all needed dependencies
*
* @throws InternalErrorException if some problem with initializer or objects in perun-core
* @throws IOException if some problem with writer
*/
public static void generateAllVosToWriter(PerunInitializer perunInitializer) throws InternalErrorException, IOException {
//Load basic variables
if (perunInitializer == null)
throw new InternalErrorException("PerunInitializer must be loaded before using in generating methods!");
PerunSession perunSession = perunInitializer.getPerunSession();
PerunBl perun = perunInitializer.getPerunBl();
BufferedWriter writer = perunInitializer.getOutputWriter();
//Get list of all vos
List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
//For every vos get needed information and write them to the writer
for (Vo vo : vos) {
String dn = "dn: ";
String desc = "description: ";
String oc1 = "objectclass: top";
String oc2 = "objectclass: organization";
String oc3 = "objectclass: perunVO";
String o = "o: ";
String perunVoId = "perunVoId: ";
perunVoId += String.valueOf(vo.getId());
o += vo.getShortName();
desc += vo.getName();
dn += "perunVoId=" + vo.getId() + ",dc=perun,dc=cesnet,dc=cz";
writer.write(dn + '\n');
writer.write(oc1 + '\n');
writer.write(oc2 + '\n');
writer.write(oc3 + '\n');
writer.write(o + '\n');
writer.write(perunVoId + '\n');
writer.write(desc + '\n');
//Generate all members in member groups of this vo and add them here (only members with status Valid)
List<Member> validMembers = perun.getMembersManagerBl().getMembers(perunSession, vo, Status.VALID);
for (Member m : validMembers) {
writer.write("uniqueMember: perunUserId=" + m.getUserId() + ",ou=People,dc=perun,dc=cesnet,dc=cz" + '\n');
}
writer.write('\n');
}
}
use of cz.metacentrum.perun.core.api.PerunSession in project perun by CESNET.
the class Utils method generateAllUsersToWriter.
/**
* Method generate all Users to the text for using in LDIF.
* Write all these information to writer in perunInitializer object.
*
* @param perunInitializer need to be loaded to get all needed dependencies
*
* @throws InternalErrorException if some problem with initializer or objects in perun-core
* @throws IOException if some problem with writer
* @throws AttributeNotExistsException
* @throws WrongAttributeAssignmentException
*/
public static void generateAllUsersToWriter(PerunInitializer perunInitializer) throws IOException, InternalErrorException, AttributeNotExistsException, WrongAttributeAssignmentException {
//Load basic variables
if (perunInitializer == null)
throw new InternalErrorException("PerunInitializer must be loaded before using in generating methods!");
PerunSession perunSession = perunInitializer.getPerunSession();
PerunBl perun = perunInitializer.getPerunBl();
BufferedWriter writer = perunInitializer.getOutputWriter();
List<User> users = perun.getUsersManagerBl().getUsers(perunSession);
for (User user : users) {
String dn = "dn: ";
String entryStatus = "entryStatus: active";
String oc1 = "objectclass: top";
String oc2 = "objectclass: person";
String oc3 = "objectclass: organizationalPerson";
String oc4 = "objectclass: inetOrgPerson";
String oc5 = "objectclass: perunUser";
String oc6 = "objectclass: tenOperEntry";
String oc7 = "objectclass: inetUser";
String sn = "sn: ";
String cn = "cn: ";
String givenName = "givenName: ";
String perunUserId = "perunUserId: ";
String mail = "mail: ";
String preferredMail = "preferredMail: ";
String o = "o: ";
String isServiceUser = "isServiceUser: ";
String isSponsoredUser = "isSponsoredUser: ";
String userPassword = "userPassword: ";
List<String> membersOf = new ArrayList<>();
List<Member> members;
Set<String> membersOfPerunVo = new HashSet<>();
members = perun.getMembersManagerBl().getMembersByUser(perunSession, user);
for (Member member : members) {
if (member.getStatus().equals(Status.VALID)) {
membersOfPerunVo.add("memberOfPerunVo: " + member.getVoId());
List<Group> groups;
groups = perun.getGroupsManagerBl().getAllMemberGroups(perunSession, member);
for (Group group : groups) {
membersOf.add("memberOf: " + "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + ",dc=perun,dc=cesnet,dc=cz");
}
}
}
//Attribute attrMail = perun.getAttributesManagerBl().getAttribute(perunSession, u, AttributesManager.NS_USER_ATTR_DEF + ":mail");
Attribute attrPreferredMail = perun.getAttributesManagerBl().getAttribute(perunSession, user, AttributesManager.NS_USER_ATTR_DEF + ":preferredMail");
Attribute attrOrganization = perun.getAttributesManagerBl().getAttribute(perunSession, user, AttributesManager.NS_USER_ATTR_DEF + ":organization");
Attribute attrVirtCertDNs = perun.getAttributesManagerBl().getAttribute(perunSession, user, AttributesManager.NS_USER_ATTR_VIRT + ":userCertDNs");
Attribute attrLibraryIDs = perun.getAttributesManagerBl().getAttribute(perunSession, user, AttributesManager.NS_USER_ATTR_DEF + ":libraryIDs");
perunUserId += String.valueOf(user.getId());
dn += "perunUserId=" + user.getId() + ",ou=People,dc=perun,dc=cesnet,dc=cz";
String firstName = user.getFirstName();
String lastName = user.getLastName();
if (firstName == null)
firstName = "";
if (lastName == null || lastName.isEmpty())
lastName = "N/A";
sn += lastName;
cn += firstName + " " + lastName;
if (user.isServiceUser())
isServiceUser += "1";
else
isServiceUser += "0";
if (user.isSponsoredUser())
isSponsoredUser += "1";
else
isSponsoredUser += "0";
if (firstName.isEmpty())
givenName = null;
else
givenName += firstName;
if (attrPreferredMail == null || attrPreferredMail.getValue() == null)
mail = null;
else
mail += (String) attrPreferredMail.getValue();
if (attrPreferredMail == null || attrPreferredMail.getValue() == null)
preferredMail = null;
else
preferredMail += (String) attrPreferredMail.getValue();
if (attrOrganization == null || attrOrganization.getValue() == null)
o = null;
else
o += (String) attrOrganization.getValue();
Map<String, String> certDNs = null;
Set<String> certSubjectsWithPrefix = null;
Set<String> certSubjectsWithoutPrefix = new HashSet<>();
if (attrVirtCertDNs != null && attrVirtCertDNs.getValue() != null) {
certDNs = (Map) attrVirtCertDNs.getValue();
certSubjectsWithPrefix = certDNs.keySet();
for (String certSubject : certSubjectsWithPrefix) {
certSubjectsWithoutPrefix.add(certSubject.replaceFirst("^[0-9]+[:]", ""));
}
}
writer.write(dn + '\n');
writer.write(oc1 + '\n');
writer.write(oc2 + '\n');
writer.write(oc3 + '\n');
writer.write(oc4 + '\n');
writer.write(oc5 + '\n');
writer.write(oc6 + '\n');
writer.write(oc7 + '\n');
writer.write(entryStatus + '\n');
writer.write(sn + '\n');
writer.write(cn + '\n');
if (givenName != null)
writer.write(givenName + '\n');
writer.write(perunUserId + '\n');
writer.write(isServiceUser + '\n');
writer.write(isSponsoredUser + '\n');
if (mail != null)
writer.write(mail + '\n');
if (preferredMail != null)
writer.write(preferredMail + '\n');
if (o != null)
writer.write(o + '\n');
if (certSubjectsWithoutPrefix != null && !certSubjectsWithoutPrefix.isEmpty()) {
for (String s : certSubjectsWithoutPrefix) {
writer.write("userCertificateSubject: " + s + '\n');
}
}
List<String> libraryIDs = new ArrayList<>();
if (attrLibraryIDs.getValue() != null) {
libraryIDs = (ArrayList) attrLibraryIDs.getValue();
}
if (libraryIDs != null && !libraryIDs.isEmpty()) {
for (String id : libraryIDs) {
writer.write("libraryIDs: " + id + '\n');
}
}
//GET ALL USERS UIDs
List<String> similarUids = perun.getAttributesManagerBl().getAllSimilarAttributeNames(perunSession, AttributesManager.NS_USER_ATTR_DEF + ":uid-namespace:");
if (similarUids != null && !similarUids.isEmpty()) {
for (String s : similarUids) {
Attribute uidNamespace = perun.getAttributesManagerBl().getAttribute(perunSession, user, s);
if (uidNamespace != null && uidNamespace.getValue() != null) {
writer.write("uidNumber;x-ns-" + uidNamespace.getFriendlyNameParameter() + ": " + uidNamespace.getValue().toString() + '\n');
}
}
}
//GET ALL USERS LOGINs
List<String> similarLogins = perun.getAttributesManagerBl().getAllSimilarAttributeNames(perunSession, AttributesManager.NS_USER_ATTR_DEF + ":login-namespace:");
if (similarLogins != null && !similarLogins.isEmpty()) {
for (String s : similarLogins) {
Attribute loginNamespace = perun.getAttributesManagerBl().getAttribute(perunSession, user, s);
if (loginNamespace != null && loginNamespace.getValue() != null) {
writer.write("login;x-ns-" + loginNamespace.getFriendlyNameParameter() + ": " + loginNamespace.getValue().toString() + '\n');
if (loginNamespace.getFriendlyNameParameter().equals("einfra")) {
writer.write(userPassword + "{SASL}" + loginNamespace.getValue().toString() + '@' + loginNamespace.getFriendlyNameParameter().toUpperCase() + '\n');
}
}
}
}
//GET ALL USERS EXTlogins FOR EVERY EXTSOURCE WITH TYPE EQUALS IDP
List<UserExtSource> userExtSources = perun.getUsersManagerBl().getUserExtSources(perunSession, user);
List<String> extLogins = new ArrayList<>();
for (UserExtSource ues : userExtSources) {
if (ues != null && ues.getExtSource() != null) {
String type = ues.getExtSource().getType();
if (type != null) {
if (type.equals(ExtSourcesManager.EXTSOURCE_IDP)) {
String extLogin;
extLogin = ues.getLogin();
if (extLogin == null)
extLogin = "";
writer.write("eduPersonPrincipalNames: " + extLogin + '\n');
}
}
}
}
//ADD MEMBEROF ATTRIBUTE TO WRITER
for (String s : membersOf) {
writer.write(s + '\n');
}
//ADD MEMBEROFPERUNVO ATTRIBUTE TO WRITER
for (String s : membersOfPerunVo) {
writer.write(s + '\n');
}
writer.write('\n');
}
}
Aggregations