Search in sources :

Example 1 with ActionType

use of cz.metacentrum.perun.core.api.ActionType in project perun by CESNET.

the class AttributesManagerImpl method setAttributeRight.

@Override
public void setAttributeRight(PerunSession sess, AttributeRights rights) throws InternalErrorException {
    try {
        // get action types of the attribute and role from the database
        List<ActionType> dbActionTypes = jdbc.query("select action_types.action_type as action_type from attributes_authz join action_types " + "on attributes_authz.action_type_id=action_types.id where attr_id=? and " + "role_id=(select id from roles where name=?)", new RowMapper<ActionType>() {

            @Override
            public ActionType mapRow(ResultSet rs, int rowNum) throws SQLException {
                return ActionType.valueOf(rs.getString("action_type").toUpperCase());
            }
        }, rights.getAttributeId(), rights.getRole().getRoleName());
        // inserting
        List<ActionType> actionTypesToInsert = new ArrayList<ActionType>();
        actionTypesToInsert.addAll(rights.getRights());
        actionTypesToInsert.removeAll(dbActionTypes);
        for (ActionType actionType : actionTypesToInsert) {
            jdbc.update("insert into attributes_authz (attr_id, role_id, action_type_id) values " + "(?, (select id from roles where name=?), (select id from action_types where action_type=?))", rights.getAttributeId(), rights.getRole().getRoleName(), actionType.getActionType());
        }
        // deleting
        List<ActionType> actionTypesToDelete = new ArrayList<ActionType>();
        actionTypesToDelete.addAll(dbActionTypes);
        actionTypesToDelete.removeAll(rights.getRights());
        for (ActionType actionType : actionTypesToDelete) {
            if (0 == jdbc.update("delete from attributes_authz where attr_id=? and role_id=(select id from roles where name=?) and " + "action_type_id=(select id from action_types where action_type=?)", rights.getAttributeId(), rights.getRole().getRoleName(), actionType.getActionType())) {
                throw new ConsistencyErrorException("Trying to delete non existing row : AttributeRight={ attributeId=" + Integer.toString(rights.getAttributeId()) + " role=" + rights.getRole().getRoleName() + " actionType=" + actionType.getActionType());
            }
        }
    } catch (RuntimeException e) {
        throw new InternalErrorException(e);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) ActionType(cz.metacentrum.perun.core.api.ActionType) ConsistencyErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException) InternalErrorRuntimeException(cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 2 with ActionType

use of cz.metacentrum.perun.core.api.ActionType in project perun by CESNET.

the class AttributesManagerEntryIntegrationTest method getAttributeRights.

@Test
public void getAttributeRights() throws Exception {
    System.out.println(CLASS_NAME + "getAttributeRights");
    // setting rights
    List<ActionType> listOfActions = new ArrayList<ActionType>();
    listOfActions.add(ActionType.WRITE);
    listOfActions.add(ActionType.READ);
    List<AttributeRights> rights = new ArrayList<AttributeRights>();
    rights.add(new AttributeRights(1, Role.VOADMIN, listOfActions));
    rights.add(new AttributeRights(1, Role.SELF, new ArrayList<ActionType>()));
    perun.getAttributesManager().setAttributeRights(sess, rights);
    // getting rights
    rights.clear();
    rights = perun.getAttributesManager().getAttributeRights(sess, 1);
    assertTrue("list of rights should have 4 items for each role", rights.size() == 4);
    for (AttributeRights attributeRights : rights) {
        if (attributeRights.getRole().equals(Role.VOADMIN)) {
            assertTrue("our attribute 1 should have right READ for VOADMIN", attributeRights.getRights().contains(ActionType.READ));
            assertTrue("our attribute 1 should have right WRITE for VOADMIN", attributeRights.getRights().contains(ActionType.WRITE));
        }
        if (attributeRights.getRole().equals(Role.SELF)) {
            assertTrue("our attribute 1 should not have rights for SELF", attributeRights.getRights().isEmpty());
        }
    }
}
Also used : ActionType(cz.metacentrum.perun.core.api.ActionType) AttributeRights(cz.metacentrum.perun.core.api.AttributeRights) ArrayList(java.util.ArrayList) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 3 with ActionType

use of cz.metacentrum.perun.core.api.ActionType in project perun by CESNET.

the class AttributesManagerEntryIntegrationTest method setAttributeRights.

@Test
public void setAttributeRights() throws Exception {
    System.out.println(CLASS_NAME + "setAttributeRights");
    List<ActionType> listOfActions = new ArrayList<ActionType>();
    listOfActions.add(ActionType.WRITE);
    listOfActions.add(ActionType.READ);
    List<AttributeRights> rights = new ArrayList<AttributeRights>();
    rights.add(new AttributeRights(1, Role.VOADMIN, listOfActions));
    listOfActions.clear();
    listOfActions.add(ActionType.READ);
    rights.add(new AttributeRights(1, Role.SELF, listOfActions));
    perun.getAttributesManager().setAttributeRights(sess, rights);
    listOfActions.clear();
    rights.clear();
    listOfActions.add(ActionType.WRITE);
    rights.add(new AttributeRights(1, Role.VOADMIN, new ArrayList<ActionType>()));
    rights.add(new AttributeRights(1, Role.SELF, listOfActions));
    perun.getAttributesManager().setAttributeRights(sess, rights);
    rights.clear();
    rights = perun.getAttributesManager().getAttributeRights(sess, 1);
    for (AttributeRights attributeRights : rights) {
        if (attributeRights.getRole().equals(Role.SELF)) {
            assertTrue("our attribute 1 should not have right READ for VOADMIN", !(attributeRights.getRights().contains(ActionType.READ)));
            assertTrue("our attribute 1 should have right WRITE for VOADMIN", attributeRights.getRights().contains(ActionType.WRITE));
        }
        if (attributeRights.getRole().equals(Role.VOADMIN)) {
            assertTrue("our attribute 1 should not have rights for VOADMIN", attributeRights.getRights().isEmpty());
        }
    }
}
Also used : ActionType(cz.metacentrum.perun.core.api.ActionType) AttributeRights(cz.metacentrum.perun.core.api.AttributeRights) ArrayList(java.util.ArrayList) AbstractPerunIntegrationTest(cz.metacentrum.perun.core.AbstractPerunIntegrationTest) Test(org.junit.Test)

Example 4 with ActionType

use of cz.metacentrum.perun.core.api.ActionType in project perun by CESNET.

the class AttributesManagerBlImpl method setAttributeRights.

@Override
public void setAttributeRights(PerunSession sess, List<AttributeRights> rights) throws InternalErrorException {
    for (AttributeRights right : rights) {
        getAttributesManagerImpl().setAttributeRight(sess, right);
        getPerunBl().getAuditer().log(sess, "Attribute right set : {}", right);
        //If these rights are for VoAdmin, do the same for VoObserver but only for READ privilegies
        if (right.getRole().equals(Role.VOADMIN)) {
            List<ActionType> onlyReadActionType = new ArrayList<ActionType>();
            if (right.getRights().contains(ActionType.READ))
                onlyReadActionType.add(ActionType.READ);
            right.setRights(onlyReadActionType);
            right.setRole(Role.VOOBSERVER);
            //Rights are now set for VoObserver with read privilegies on the same attribute like VoAdmin
            getAttributesManagerImpl().setAttributeRight(sess, right);
            getPerunBl().getAuditer().log(sess, "Attribute right set : {}", right);
        }
    }
}
Also used : ActionType(cz.metacentrum.perun.core.api.ActionType) AttributeRights(cz.metacentrum.perun.core.api.AttributeRights) ArrayList(java.util.ArrayList)

Aggregations

ActionType (cz.metacentrum.perun.core.api.ActionType)4 AttributeRights (cz.metacentrum.perun.core.api.AttributeRights)3 ArrayList (java.util.ArrayList)3 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)2 Test (org.junit.Test)2 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)1 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)1 ConsistencyErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.ConsistencyErrorRuntimeException)1 InternalErrorRuntimeException (cz.metacentrum.perun.core.api.exceptions.rt.InternalErrorRuntimeException)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1