Search in sources :

Example 1 with PerunBl

use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.

the class Utils method generateAllGroupsToWriter.

/**
	 * Method generate all Groups 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 generateAllGroupsToWriter(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();
    //First get all vos
    List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
    //Then from all vos get all assigned groups and generate data about them to the writer
    for (Vo vo : vos) {
        List<Group> groups;
        groups = perun.getGroupsManagerBl().getGroups(perunSession, vo);
        for (Group group : groups) {
            String dn = "dn: ";
            String oc1 = "objectclass: top";
            String oc3 = "objectclass: perunGroup";
            String cn = "cn: ";
            String perunVoId = "perunVoId: ";
            String parentGroup = "perunParentGroup: ";
            String parentGroupId = "perunParentGroupId: ";
            String perunGroupId = "perunGroupId: ";
            String owner = "owner: ";
            String description = "description: ";
            String perunUniqueGroupName = "perunUniqueGroupName: ";
            List<Member> members;
            members = perun.getGroupsManagerBl().getGroupMembers(perunSession, group, Status.VALID);
            perunGroupId += String.valueOf(group.getId());
            perunVoId += String.valueOf(group.getVoId());
            dn += "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            cn += group.getName();
            perunUniqueGroupName += vo.getShortName() + ":" + group.getName();
            if (group.getDescription() != null)
                description += group.getDescription();
            if (group.getParentGroupId() != null) {
                parentGroupId += group.getParentGroupId();
                parentGroup += "perunGroupId=" + group.getParentGroupId() + ",perunVoId=" + group.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            }
            List<Member> admins = new ArrayList<>();
            writer.write(dn + '\n');
            writer.write(oc1 + '\n');
            writer.write(oc3 + '\n');
            writer.write(cn + '\n');
            writer.write(perunUniqueGroupName + '\n');
            writer.write(perunGroupId + '\n');
            writer.write(perunVoId + '\n');
            if (group.getDescription() != null)
                writer.write(description + '\n');
            if (group.getParentGroupId() != null) {
                writer.write(parentGroupId + '\n');
                writer.write(parentGroup + '\n');
            }
            //ADD Group Members
            for (Member m : members) {
                writer.write("uniqueMember: " + "perunUserId=" + m.getUserId() + ",ou=People,dc=perun,dc=cesnet,dc=cz");
                writer.write('\n');
            }
            //ADD resources which group is assigned to
            List<Resource> associatedResources;
            associatedResources = perun.getResourcesManagerBl().getAssignedResources(perunSession, group);
            for (Resource r : associatedResources) {
                writer.write("assignedToResourceId: " + r.getId());
                writer.write('\n');
            }
            //FOR NOW No groups has owner
            writer.write(owner + '\n');
            writer.write('\n');
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunSession(cz.metacentrum.perun.core.api.PerunSession) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) BufferedWriter(java.io.BufferedWriter) Vo(cz.metacentrum.perun.core.api.Vo) Member(cz.metacentrum.perun.core.api.Member)

Example 2 with PerunBl

use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.

the class Utils method generateAllResourcesToWriter.

/**
	 * Method generate all Resources 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 generateAllResourcesToWriter(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();
    //first get all Vos
    List<Vo> vos = perun.getVosManagerBl().getVos(perunSession);
    //Then from every Vo get all assigned resources and write their data to the writer
    for (Vo vo : vos) {
        List<Resource> resources;
        resources = perun.getResourcesManagerBl().getResources(perunSession, vo);
        for (Resource resource : resources) {
            //Read facility attribute entityID and write it for the resource if exists
            Facility facility = null;
            try {
                facility = perun.getFacilitiesManagerBl().getFacilityById(perunSession, resource.getFacilityId());
            } catch (FacilityNotExistsException ex) {
                throw new InternalErrorException("Can't found facility of this resource " + resource, ex);
            }
            Attribute entityIDAttr = null;
            try {
                entityIDAttr = perun.getAttributesManagerBl().getAttribute(perunSession, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":entityID");
            } catch (AttributeNotExistsException | WrongAttributeAssignmentException ex) {
                throw new InternalErrorException("Problem with loading entityID attribute of facility " + facility, ex);
            }
            String dn = "dn: ";
            String oc1 = "objectclass: top";
            String oc3 = "objectclass: perunResource";
            String cn = "cn: ";
            String perunVoId = "perunVoId: ";
            String perunFacilityId = "perunFacilityId: ";
            String perunResourceId = "perunResourceId: ";
            String description = "description: ";
            String entityID = "entityID: ";
            perunVoId += String.valueOf(resource.getVoId());
            perunFacilityId += String.valueOf(resource.getFacilityId());
            perunResourceId += String.valueOf(resource.getId());
            dn += "perunResourceId=" + resource.getId() + ",perunVoId=" + resource.getVoId() + ",dc=perun,dc=cesnet,dc=cz";
            cn += resource.getName();
            String descriptionValue = resource.getDescription();
            if (descriptionValue != null) {
                if (descriptionValue.matches("^[ ]*$"))
                    descriptionValue = null;
            }
            writer.write(dn + '\n');
            writer.write(oc1 + '\n');
            writer.write(oc3 + '\n');
            writer.write(cn + '\n');
            writer.write(perunResourceId + '\n');
            if (descriptionValue != null)
                writer.write(description + descriptionValue + '\n');
            writer.write(perunVoId + '\n');
            writer.write(perunFacilityId + '\n');
            if (entityIDAttr.getValue() != null)
                writer.write(entityID + (String) entityIDAttr.getValue() + '\n');
            //ADD resources which group is assigned to
            List<Group> associatedGroups = perun.getResourcesManagerBl().getAssignedGroups(perunSession, resource);
            for (Group g : associatedGroups) {
                writer.write("assignedGroupId: " + g.getId());
                writer.write('\n');
            }
            writer.write('\n');
        }
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) PerunSession(cz.metacentrum.perun.core.api.PerunSession) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) FacilityNotExistsException(cz.metacentrum.perun.core.api.exceptions.FacilityNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) BufferedWriter(java.io.BufferedWriter) Vo(cz.metacentrum.perun.core.api.Vo) Facility(cz.metacentrum.perun.core.api.Facility)

Example 3 with PerunBl

use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.

the class AuthzResolver method getAdminGroups.

/**
	 * Get all authorizedGroups for complementary object and role.
	 *
	 * @param sess perun session
	 * @param complementaryObjectId id of object for which we will get richUser administrators
	 * @param complementaryObjectName name of object for which we will get richUser administrators
	 * @param role expected role to filter authorizedGroups by (perunadmin | voadmin | groupadmin | self | facilityadmin | voobserver | topgroupcreator)
	 *
	 * @return list of authorizedGroups for complementary object and role
	 *
	 * @throws InternalErrorException
	 * @throws UserNotExistsException
	 * @throws PrivilegeException
	 * @throws GroupNotExistsException
	 * @throws VoNotExistsException
	 * @throws FacilityNotExistsException
	 * @throws RoleNotSupportedException
	 * @throws PerunBeanNotSupportedException
	 */
public static List<Group> getAdminGroups(PerunSession sess, int complementaryObjectId, String complementaryObjectName, Role role) throws InternalErrorException, UserNotExistsException, PrivilegeException, GroupNotExistsException, VoNotExistsException, FacilityNotExistsException, RoleNotSupportedException, PerunBeanNotSupportedException {
    Utils.checkPerunSession(sess);
    Utils.notNull(role, "role");
    Utils.notNull(complementaryObjectName, "complementaryObjectName");
    List<Group> authorizedGroups;
    //Try to get complementary Object
    if (complementaryObjectName.equals("Group")) {
        if (!role.equals(Role.GROUPADMIN))
            throw new RoleNotSupportedException("Not supported other role than group manager for object Group.");
        Group group = ((PerunBl) sess.getPerun()).getGroupsManagerBl().getGroupById(sess, complementaryObjectId);
        authorizedGroups = sess.getPerun().getGroupsManager().getAdminGroups(sess, group);
    } else if (complementaryObjectName.equals("Vo")) {
        Vo vo = ((PerunBl) sess.getPerun()).getVosManagerBl().getVoById(sess, complementaryObjectId);
        authorizedGroups = sess.getPerun().getVosManager().getAdminGroups(sess, vo, role);
    } else if (complementaryObjectName.equals("Facility")) {
        if (!role.equals(Role.FACILITYADMIN))
            throw new RoleNotSupportedException("Not supported other role than facility manager for object Facility.");
        Facility facility = ((PerunBl) sess.getPerun()).getFacilitiesManagerBl().getFacilityById(sess, complementaryObjectId);
        authorizedGroups = sess.getPerun().getFacilitiesManager().getAdminGroups(sess, facility);
    } else {
        throw new PerunBeanNotSupportedException("Only Vo, Group and Facility are supported complementary names.");
    }
    return authorizedGroups;
}
Also used : RoleNotSupportedException(cz.metacentrum.perun.core.api.exceptions.RoleNotSupportedException) PerunBeanNotSupportedException(cz.metacentrum.perun.core.api.exceptions.PerunBeanNotSupportedException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl)

Example 4 with PerunBl

use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.

the class AuthzResolver method getRichAdmins.

/**
	 * Get all richUser administrators for complementary object and role with specified attributes.
	 *
	 * If <b>onlyDirectAdmins</b> is <b>true</b>, return only direct users of the complementary object for role with specific attributes.
	 * If <b>allUserAttributes</b> is <b>true</b>, do not specify attributes through list and return them all in objects richUser. Ignoring list of specific attributes.
	 *
	 * @param sess perun session
	 * @param complementaryObjectId id of object for which we will get richUser administrators
	 * @param complementaryObjectName name of object for which we will get richUser administrators
	 * @param specificAttributes list of specified attributes which are needed in object richUser
	 * @param role expected role to filter managers by
	 * @param onlyDirectAdmins if true, get only direct user administrators (if false, get both direct and indirect)
	 * @param allUserAttributes if true, get all possible user attributes and ignore list of specificAttributes (if false, get only specific attributes)
	 *
	 * @return list of richUser administrators for complementary object and role with specified attributes.
	 *
	 * @throws InternalErrorException
	 * @throws PrivilegeException
	 * @throws GroupNotExistsException
	 * @throws VoNotExistsException
	 * @throws FacilityNotExistsException
	 * @throws RoleNotSupportedException
	 * @throws PerunBeanNotSupportedException
	 * @throws UserNotExistsException
	 */
public static List<RichUser> getRichAdmins(PerunSession sess, int complementaryObjectId, String complementaryObjectName, List<String> specificAttributes, Role role, boolean onlyDirectAdmins, boolean allUserAttributes) throws InternalErrorException, PrivilegeException, GroupNotExistsException, VoNotExistsException, FacilityNotExistsException, RoleNotSupportedException, PerunBeanNotSupportedException, UserNotExistsException {
    Utils.checkPerunSession(sess);
    Utils.notNull(role, "role");
    Utils.notNull(complementaryObjectName, "complementaryObjectName");
    if (!allUserAttributes)
        Utils.notNull(specificAttributes, "specificAttributes");
    List<RichUser> richUsers;
    //Try to get complementary Object
    if (complementaryObjectName.equals("Group")) {
        if (!role.equals(Role.GROUPADMIN))
            throw new RoleNotSupportedException("Not supported other role than group manager for object Group.");
        Group group = ((PerunBl) sess.getPerun()).getGroupsManagerBl().getGroupById(sess, complementaryObjectId);
        richUsers = sess.getPerun().getGroupsManager().getRichAdmins(sess, group, specificAttributes, allUserAttributes, onlyDirectAdmins);
    } else if (complementaryObjectName.equals("Vo")) {
        Vo vo = ((PerunBl) sess.getPerun()).getVosManagerBl().getVoById(sess, complementaryObjectId);
        richUsers = sess.getPerun().getVosManager().getRichAdmins(sess, vo, role, specificAttributes, allUserAttributes, onlyDirectAdmins);
    } else if (complementaryObjectName.equals("Facility")) {
        if (!role.equals(Role.FACILITYADMIN))
            throw new RoleNotSupportedException("Not supported other role than facility manager for object Facility.");
        Facility facility = ((PerunBl) sess.getPerun()).getFacilitiesManagerBl().getFacilityById(sess, complementaryObjectId);
        richUsers = sess.getPerun().getFacilitiesManager().getRichAdmins(sess, facility, specificAttributes, allUserAttributes, onlyDirectAdmins);
    } else {
        throw new PerunBeanNotSupportedException("Only Vo, Group and Facility are supported complementary names.");
    }
    return richUsers;
}
Also used : RoleNotSupportedException(cz.metacentrum.perun.core.api.exceptions.RoleNotSupportedException) PerunBeanNotSupportedException(cz.metacentrum.perun.core.api.exceptions.PerunBeanNotSupportedException) PerunBl(cz.metacentrum.perun.core.bl.PerunBl)

Example 5 with PerunBl

use of cz.metacentrum.perun.core.bl.PerunBl in project perun by CESNET.

the class GroupsManagerBlImpl method saveInformationAboutGroupSynchronization.

public void saveInformationAboutGroupSynchronization(PerunSession sess, Group group, boolean failedDueToException, String exceptionMessage) throws AttributeNotExistsException, InternalErrorException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException, WrongAttributeValueException {
    //get current timestamp of this synchronization
    Date currentTimestamp = new Date();
    String originalExceptionMessage = exceptionMessage;
    //If session is null, throw an exception
    if (sess == null) {
        throw new InternalErrorException("Session is null when trying to save information about synchronization. Group: " + group + ", timestamp: " + currentTimestamp + ",message: " + exceptionMessage);
    }
    //If group is null, throw an exception
    if (group == null) {
        throw new InternalErrorException("Object group is null when trying to save information about synchronization. Timestamp: " + currentTimestamp + ", message: " + exceptionMessage);
    }
    //if exceptionMessage is empty, use "Empty message" instead
    if (exceptionMessage != null && exceptionMessage.isEmpty()) {
        exceptionMessage = "Empty message.";
    //else trim the message on 1000 characters if not null
    } else if (exceptionMessage != null && exceptionMessage.length() > 1000) {
        exceptionMessage = exceptionMessage.substring(0, 1000) + " ... message is too long, other info is in perun log file. If needed, please ask perun administrators.";
    }
    //Set correct format of currentTimestamp
    String correctTimestampString = BeansUtils.getDateFormatter().format(currentTimestamp);
    //Get both attribute definition lastSynchroTimestamp and lastSynchroState
    //Get definitions and values, set values
    Attribute lastSynchronizationTimestamp = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":lastSynchronizationTimestamp"));
    Attribute lastSynchronizationState = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":lastSynchronizationState"));
    lastSynchronizationTimestamp.setValue(correctTimestampString);
    //if exception is null, set null to value => remove attribute instead of setting in method setAttributes
    lastSynchronizationState.setValue(exceptionMessage);
    //attributes to set
    List<Attribute> attrsToSet = new ArrayList<>();
    //Set lastSuccessSynchronizationTimestamp if this one is success
    if (exceptionMessage == null) {
        String attrName = AttributesManager.NS_GROUP_ATTR_DEF + ":lastSuccessSynchronizationTimestamp";
        try {
            Attribute lastSuccessSynchronizationTimestamp = new Attribute(((PerunBl) sess.getPerun()).getAttributesManagerBl().getAttributeDefinition(sess, attrName));
            lastSuccessSynchronizationTimestamp.setValue(correctTimestampString);
            attrsToSet.add(lastSuccessSynchronizationTimestamp);
        } catch (AttributeNotExistsException ex) {
            log.error("Can't save lastSuccessSynchronizationTimestamp, because there is missing attribute with name {}", attrName);
        }
    } else {
        //Log to auditer_log that synchronization failed or finished with some errors
        if (failedDueToException) {
            getPerunBl().getAuditer().log(sess, "{} synchronization failed because of {}.", group, originalExceptionMessage);
        } else {
            getPerunBl().getAuditer().log(sess, "{} synchronization finished with errors: {}.", group, originalExceptionMessage);
        }
    }
    //set lastSynchronizationState and lastSynchronizationTimestamp
    attrsToSet.add(lastSynchronizationState);
    attrsToSet.add(lastSynchronizationTimestamp);
    ((PerunBl) sess.getPerun()).getAttributesManagerBl().setAttributes(sess, group, attrsToSet);
}
Also used : ArrayList(java.util.ArrayList) PerunBl(cz.metacentrum.perun.core.bl.PerunBl) Date(java.util.Date)

Aggregations

PerunBl (cz.metacentrum.perun.core.bl.PerunBl)130 Attribute (cz.metacentrum.perun.core.api.Attribute)93 Before (org.junit.Before)65 PerunSessionImpl (cz.metacentrum.perun.core.impl.PerunSessionImpl)64 AttributesManagerBl (cz.metacentrum.perun.core.bl.AttributesManagerBl)48 User (cz.metacentrum.perun.core.api.User)41 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)37 ArrayList (java.util.ArrayList)22 Vo (cz.metacentrum.perun.core.api.Vo)21 Facility (cz.metacentrum.perun.core.api.Facility)19 UserExtSource (cz.metacentrum.perun.core.api.UserExtSource)19 ModulesUtilsBl (cz.metacentrum.perun.core.bl.ModulesUtilsBl)19 ExtSource (cz.metacentrum.perun.core.api.ExtSource)16 Member (cz.metacentrum.perun.core.api.Member)16 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)16 UserExtSourceExistsException (cz.metacentrum.perun.core.api.exceptions.UserExtSourceExistsException)16 UsersManagerBl (cz.metacentrum.perun.core.bl.UsersManagerBl)15 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)14 GroupsManagerBl (cz.metacentrum.perun.core.bl.GroupsManagerBl)13 Group (cz.metacentrum.perun.core.api.Group)12