use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException 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');
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException in project perun by CESNET.
the class GroupsManagerEntry method getMemberGroupsByAttribute.
public List<Group> getMemberGroupsByAttribute(PerunSession sess, Member member, Attribute attribute) throws WrongAttributeAssignmentException, PrivilegeException, InternalErrorException, VoNotExistsException, MemberNotExistsException, AttributeNotExistsException {
Utils.checkPerunSession(sess);
getPerunBl().getMembersManagerBl().checkMemberExists(sess, member);
getPerunBl().getAttributesManagerBl().checkAttributeExists(sess, new AttributeDefinition(attribute));
Vo vo = getPerunBl().getMembersManagerBl().getMemberVo(sess, member);
//Only group attributes are allowed
if (!this.getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManagerEntry.NS_GROUP_ATTR)) {
throw new WrongAttributeAssignmentException(attribute);
}
// Authorization
if (!AuthzResolver.isAuthorized(sess, Role.VOADMIN, vo) && !AuthzResolver.isAuthorized(sess, Role.VOOBSERVER, vo) && !AuthzResolver.isAuthorized(sess, Role.GROUPADMIN, vo) && !AuthzResolver.isAuthorized(sess, Role.SELF, member)) {
throw new PrivilegeException(sess, "getMemberGroupsByAttribute for " + member);
}
List<Group> groups = this.groupsManagerBl.getMemberGroupsByAttribute(sess, member, attribute);
//If actor has no right to read attribute for group, throw exception
for (Group group : groups) {
if (!AuthzResolver.isAuthorizedForAttribute(sess, ActionType.READ, attribute, group, null)) {
throw new PrivilegeException(sess, "Actor hasn't right to read attribute for a group.");
}
}
return groups;
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException in project perun by CESNET.
the class urn_perun_member_resource_attribute_def_virt_isBanned method resolveVirtualAttributeValueChange.
@Override
public List<String> resolveVirtualAttributeValueChange(PerunSessionImpl perunSession, String message) throws InternalErrorException, WrongReferenceAttributeValueException, AttributeNotExistsException, WrongAttributeAssignmentException {
List<String> resolvingMessages = new ArrayList<>();
if (message == null)
return resolvingMessages;
Matcher banModificationMatcher = banModification.matcher(message);
List<Pair<Resource, Member>> listOfAffectedObjects = new ArrayList<>();
String operationType = "";
if (banModificationMatcher.find()) {
try {
String banType = banModificationMatcher.group(1);
operationType = banModificationMatcher.group(2);
int firstHolderId = Integer.valueOf(banModificationMatcher.group(3));
int secondHolderId = Integer.valueOf(banModificationMatcher.group(4));
if (operationType.equals(OPERATION_UPDATED)) {
operationType = OPERATION_SET;
} else if (!operationType.equals(OPERATION_SET) && !operationType.equals(OPERATION_REMOVED)) {
throw new InternalErrorException("Type of operation '" + operationType + "' is unknown by module.");
}
if (banType.equals(BanOnResource.class.getSimpleName())) {
Member member = perunSession.getPerunBl().getMembersManagerBl().getMemberById(perunSession, firstHolderId);
Resource resource = perunSession.getPerunBl().getResourcesManagerBl().getResourceById(perunSession, secondHolderId);
listOfAffectedObjects.add(new Pair(resource, member));
} else if (banType.equals(BanOnFacility.class.getSimpleName())) {
User user = perunSession.getPerunBl().getUsersManagerBl().getUserById(perunSession, firstHolderId);
Facility facility = perunSession.getPerunBl().getFacilitiesManagerBl().getFacilityById(perunSession, secondHolderId);
listOfAffectedObjects = getAffectedMemberResourceObjects(perunSession, user, facility);
} else {
throw new InternalErrorException("Type of ban '" + banType + "' is unkown by module.");
}
} catch (Exception e) {
log.error("Can't resolve virtual attribute value change for " + this.getClass().getSimpleName() + " module because of exception.", e);
//return empty array, do not throw exception because it can create problems
return new ArrayList<>();
}
}
for (Pair<Resource, Member> affectedObjects : listOfAffectedObjects) {
try {
Attribute attrVirtMemberResourceIsBanned = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, affectedObjects.getLeft(), affectedObjects.getRight(), AttributesManager.NS_MEMBER_RESOURCE_ATTR_VIRT + ":isBanned");
resolvingMessages.add(attrVirtMemberResourceIsBanned.serializeToString() + " " + operationType + " for " + affectedObjects.getLeft().serializeToString() + " and " + affectedObjects.getRight().serializeToString());
} catch (AttributeNotExistsException ex) {
//This means that attribute isBanned not exists at all so we can skip this process
log.info("Virtual attribute {} not exists.", this.getClass().getSimpleName());
break;
}
}
return resolvingMessages;
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_userCertDNs method changedAttributeHook.
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
Attribute userPreferredCertDN = null;
try {
userPreferredCertDN = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":userPreferredCertDN");
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
String preferredCertDNValue = null;
if (userPreferredCertDN.getValue() != null)
preferredCertDNValue = (String) userPreferredCertDN.getValue();
Map<String, String> certDNs = null;
if (attribute.getValue() != null)
certDNs = (Map<String, String>) attribute.getValue();
if (certDNs == null || certDNs.isEmpty()) {
try {
session.getPerunBl().getAttributesManagerBl().removeAttribute(session, user, userPreferredCertDN);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
}
} else {
Set<String> certDNsKeys = certDNs.keySet();
String newPossibleCertDN = null;
for (String key : certDNsKeys) {
if (key != null && !key.isEmpty()) {
newPossibleCertDN = key;
break;
}
}
if (preferredCertDNValue == null) {
userPreferredCertDN.setValue(newPossibleCertDN);
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredCertDN);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
}
} else {
if (!certDNsKeys.contains(preferredCertDNValue)) {
userPreferredCertDN.setValue(newPossibleCertDN);
try {
session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPreferredCertDN);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException ex) {
throw new InternalErrorException(ex);
}
}
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException in project perun by CESNET.
the class urn_perun_user_attribute_def_def_userPreferredCertDN method changedAttributeHook.
//TODO what dependencies of this attribute???
@Override
public void changedAttributeHook(PerunSessionImpl session, User user, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
if (attribute.getValue() == null) {
Attribute userCertDNs = null;
try {
userCertDNs = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, AttributesManager.NS_USER_ATTR_DEF + ":userCertDNs");
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
Map<String, String> certDNsValue = null;
if (userCertDNs.getValue() != null) {
certDNsValue = (Map<String, String>) userCertDNs.getValue();
}
if (certDNsValue != null && !certDNsValue.isEmpty()) {
throw new WrongReferenceAttributeValueException(attribute, "Can't remove preferredCert if there is any existing certDNs for the user.");
}
}
}
Aggregations