Package com.adito.policyframework

Examples of com.adito.policyframework.Policy


    }

    private static void assertPolicies() throws Exception {
        List<Policy> policies = PolicyDatabaseFactory.getInstance().getPolicies();
        assertEquals("There should be only one policy", policies.size(), 1);
        Policy policy = policies.get(0);
        assertEquals("The policy should be called 'Everyone'", policy.getResourceName(), "Everyone");
        assertEquals("The policy id should be '0'", policy.getResourceId(), 0);
    }
View Full Code Here


    public ActionForward commit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        ActionForward fwd = super.commit(mapping, form, request, response);
        PolicyForm policyForm = (PolicyForm) form;
        Policy pol = (Policy) policyForm.getResource();
        UserDatabase udb = UserDatabaseManager.getInstance().getUserDatabase(getSessionInfo(request).getUser().getRealm());
        if (pol.getResourceId() != PolicyDatabaseFactory.getInstance().getEveryonePolicyIDForRealm(udb.getRealm())) {
            List wasAttached = PolicyDatabaseFactory.getInstance().getPrincipalsGrantedPolicy(pol, udb.getRealm()); // objects
            List nowAttached = policyForm.getSelectedAccountsList();
            for (Iterator i = wasAttached.iterator(); i.hasNext();) {
                Principal p = (Principal) i.next();
                try {
                    if (!nowAttached.contains(p.getPrincipalName())) {
                        CoreServlet.getServlet().fireCoreEvent(
                            new CoreEvent(this, CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL, null, null,
                                            CoreEvent.STATE_SUCCESSFUL).addAttribute(
                                CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, p instanceof User ? "user" : "group")
                                            .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName())
                                            .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));

                    }
                } catch (Exception e) {
                    CoreServlet.getServlet().fireCoreEvent(
                        new CoreEvent(this, CoreEventConstants.REVOKE_POLICY_FROM_PRINCIPAL, null, null, e).addAttribute(
                            CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, p instanceof User ? "user" : "group").addAttribute(
                            CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute(
                            CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
                    throw e;
                }
            }

            // TODO fire revoking events
            PolicyDatabaseFactory.getInstance().revokePolicyFromAllPrincipals(pol, udb.getRealm());
            for (Iterator i = nowAttached.iterator(); i.hasNext();) {
                Principal p = udb.getAccount((String) i.next());
                try {
                    PolicyDatabaseFactory.getInstance().grantPolicyToPrincipal(pol, p);
                    CoreServlet.getServlet().fireCoreEvent(
                        new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_SUCCESSFUL)
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE,
                                            "user").addAttribute(
                                            CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute(
                                            CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
                } catch (Exception e) {
                    CoreServlet.getServlet().fireCoreEvent(
                        new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, e).addAttribute(
                            CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "user").addAttribute(
                            CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName()).addAttribute(
                            CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
                    throw e;
                }
            }
            for (Iterator i = policyForm.getSelectedRolesList().iterator(); i.hasNext();) {
                Principal p = udb.getRole((String) i.next());
                try {
                    PolicyDatabaseFactory.getInstance().grantPolicyToPrincipal(pol, p);
                    CoreServlet.getServlet().fireCoreEvent(
                        new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_SUCCESSFUL)
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "group")
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName())
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
                } catch (Exception e) {
                    CoreServlet.getServlet().fireCoreEvent(
                        new CoreEvent(this, CoreEventConstants.GRANT_POLICY_TO_PRINCIPAL, null, null, CoreEvent.STATE_UNSUCCESSFUL)
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_TYPE, "group")
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_POLICY_NAME, pol.getResourceName())
                                        .addAttribute(CoreAttributeConstants.EVENT_ATTR_PRINCIPAL_ID, p.getPrincipalName()));
                    throw e;
                }
            }
        }
View Full Code Here

    /* (non-Javadoc)
     * @see com.adito.policyframework.actions.AbstractResourcesDispatchAction#remove(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    public ActionForward remove(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
                    throws Exception {
        Policy resource = getSelectedResource(form);
        super.remove(mapping, form, request, response);
        saveMessage(request, "policy.deleted.message", resource);
        return getRedirectWithMessages(mapping, request);
    }
View Full Code Here

     */
    @Test
    public void createPolicy() throws Exception {
        Realm realm = getUserService().getRealm(1);
        assertEquals("There should be only one policy", getPolicyService().getPolicies().size(), 1);
        Policy policy = getPolicyService().createPolicy("Policy A", "Policy A description", Policy.TYPE_NORMAL, realm.getRealmID());
        assertEquals("There should be only two policies", getPolicyService().getPolicies().size(), 2);
        getPolicyService().deletePolicy(policy.getResourceId());
        assertEquals("There should be only one policy", getPolicyService().getPolicies().size(), 1);
    }
View Full Code Here

     */
    @Test
    public void createPersonalPolicy() throws Exception {
        Realm realm = getUserService().getDefaultRealm();
        assertEquals("There should be only one policy (Everyone policy)", getPolicyService().getPoliciesExcludePersonal(realm), getPolicyService().getPolicies(realm));
        Policy policy = getPolicyService().createPolicy("Personal Policy A", "Personal Policy A description", Policy.TYPE_PERSONAL, realm.getRealmID());
        assertEquals("There should be two policies", 2, getPolicyService().getPolicies().size());
        assertEquals("There should be only one global policy", 1, getPolicyService().getPoliciesExcludePersonal(realm).size());
        getPolicyService().deletePolicy(policy.getResourceId());
        assertEquals("There should be only one policy (Everyone policy)", getPolicyService().getPoliciesExcludePersonal(realm), getPolicyService().getPolicies(realm));
    }
View Full Code Here

     */
    @Test
    public void updatePolicyName() throws Exception {
        String newPolicyName = "NewName";
        Realm realm = getUserService().getRealm(1);
        Policy policy = createPolicy(realm);
        policy.setResourceName(newPolicyName);
        getPolicyService().updatePolicy(policy);
        Policy updatedPolicy = getPolicyService().getPolicy(policy.getResourceId());
        assertEquals("The new policy name should be " + newPolicyName, newPolicyName, updatedPolicy.getResourceName());
        getPolicyService().deletePolicy(policy.getResourceId());
    }
View Full Code Here

     */
    @Test
    public void updatePolicyDescription() throws Exception {
        String newPolicyDescription = "NewDescription";
        Realm realm = getUserService().getRealm(1);
        Policy policy = createPolicy(realm);
        policy.setResourceDescription(newPolicyDescription);
        getPolicyService().updatePolicy(policy);
        Policy updatedPolicy = getPolicyService().getPolicy(policy.getResourceId());
        assertEquals("The new policy description should be " + newPolicyDescription, newPolicyDescription, updatedPolicy.getResourceDescription());
        getPolicyService().deletePolicy(policy.getResourceId());
    }
View Full Code Here

     * @throws Exception
     */
    @Test
    public void checkPolicyRetrieval() throws Exception {
        Realm realm = getUserService().getRealm(1);
        Policy policy = createPolicy(realm);
        Policy retrievedById = getPolicyService().getPolicy(policy.getResourceId());
        Policy retrievedByName = getPolicyService().getPolicyByName(policy.getResourceName(), realm.getResourceId());
        assertEquals("The policies should be the same.", retrievedById, retrievedByName);
        getPolicyService().deletePolicy(policy.getResourceId());
    }
View Full Code Here

     * @throws Exception
     */
    @Test
    public void attachDetachPolicyToUser() throws Exception {
        Realm realm = getUserService().getDefaultRealm();
        Policy policy = createPolicy(realm);
        int users = getUserService().getDefaultUserDatabase().listAllUsers(UserDatabase.WILDCARD_SEARCH, Integer.MAX_VALUE).length;
        User user = createAccount();
        assertEquals(
            getUserService().getDefaultUserDatabase().listAllUsers(UserDatabase.WILDCARD_SEARCH, Integer.MAX_VALUE).length,
            users + 1);
        getPolicyService().grantPolicyToPrincipal(policy, user);
        assertTrue("The policy should be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
        getPolicyService().revokePolicyFromPrincipal(policy, user);
        assertFalse("The policy should not be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
        getUserService().getDefaultUserDatabase().deleteAccount(user);
        assertEquals(
            getUserService().getDefaultUserDatabase().listAllUsers(UserDatabase.WILDCARD_SEARCH, Integer.MAX_VALUE).length, users);
        getPolicyService().deletePolicy(policy.getResourceId());
    }
View Full Code Here

     * @throws Exception
     */
    @Test
    public void attachDetachPolicyToRole() throws Exception {
        Realm realm = getUserService().getDefaultRealm();
        Policy policy = createPolicy(realm);
        User user = createAccount();
        Role role = createRole("Group1");
        user = updateAccountRoles(user, Collections.singleton(role));
        getPolicyService().grantPolicyToPrincipal(policy, role);
        assertTrue("The policy should be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
        getPolicyService().revokePolicyFromPrincipal(policy, role);
        assertFalse("The policy should not be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
        getUserService().getDefaultUserDatabase().deleteAccount(user);
        getPolicyService().deletePolicy(policy.getResourceId());
    }
View Full Code Here

TOP

Related Classes of com.adito.policyframework.Policy

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.