Example usage for java.security Permission getClass

List of usage examples for java.security Permission getClass

Introduction

In this page you can find the example usage for java.security Permission getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static void remove(Acl policy, PolicyData pd) {
    Enumeration<AclEntry> entryEnum = policy.entries();

    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        Principal principal = entry.getPrincipal();

        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();

        if (principalClass == null || principalName == null)
            continue;
        if (principalClass.equals(pd.getPrincipalClass()) && principalName.equals(pd.getPrincipalName())) {
            Enumeration<Permission> permissEnum = entry.permissions();
            while (permissEnum.hasMoreElements()) {
                Permission permission = permissEnum.nextElement();

                String permissionClass = permission.getClass().getName();
                String permissionName = permission.getName();
                String permissionActions = permission.getActions();

                if (permissionClass == null || permissionName == null || permissionActions == null)
                    continue;
                if (permissionClass.equals(pd.getPermissionClass())
                        && permissionName.equals(pd.getPermissionName())
                        && permissionActions.equals(pd.getPermissionActions())) {
                    // 
                    AclEntry policyEntry = policy.getEntry(principal);
                    policyEntry.removePermission(permission);
                }//from w w  w .  j  av  a 2 s .com
            }
        }
    }
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

private static String permiss2PolicyString(Permission permission) {
    StringBuffer sb = new StringBuffer();
    sb.append("permission ").append(permission.getClass().getName()).append(" \"");
    if (permission instanceof UserPermission) {
        sb.append(((UserPermission) permission).getWiki());
    } else if (permission instanceof VWBPermission) {
        sb.append("*");
    } else if (permission instanceof PagePermission) {
        sb.append(((PagePermission) permission).getPage());
    } else if (permission instanceof AllPermission) {
        sb.append("*");
    } else {// w w  w . jav  a  2 s  .  c om
        sb.append(permission.getName());
    }
    sb.append("\"");
    String actions = permission.getActions();
    if (actions != null) {
        sb.append(" \"").append(actions).append("\"");
    }
    sb.append(";\n\r");
    return sb.toString();
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static List<PolicyData> acl2PolicyData(Acl acl) {
    List<PolicyData> pdl = new ArrayList<PolicyData>();

    Enumeration<AclEntry> entryEnum = acl.entries();
    while (entryEnum.hasMoreElements()) {
        AclEntry entry = entryEnum.nextElement();
        Principal principal = entry.getPrincipal();
        String principalClass = principal.getClass().getName();
        String principalName = principal.getName();
        String objectname = null;
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.Role")
                && principalName.equalsIgnoreCase("All")) {
            objectname = "";
        }//from  www.  j a  v  a  2  s. c o m
        if (principalClass.equals("cn.vlabs.duckling.vwb.service.auth.GroupPrincipal")) {
            objectname = "";
        }
        if (objectname == null) {
            continue;
        }
        Enumeration<Permission> permissEnum = entry.permissions();
        while (permissEnum.hasMoreElements()) {
            Permission permission = permissEnum.nextElement();

            String permissionClass = permission.getClass().getName();
            String permissionType = null;
            if (permissionClass.equals("cn.vlabs.duckling.vwb.service.auth.permissions.PagePermission")) {
                permissionType = "?";
            }
            if (permissionClass.equals("cn.vlabs.duckling.vwb.service.auth.permissions.VWBPermission")) {
                permissionType = "";
            }
            if (permissionType == null) {
                continue;
            }

            String permissionActions = permission.getActions();
            String operations = permissionActions;

            String permissionName = permission.getName();

            String resource = null;
            if (permission instanceof UserPermission) {
                resource = ((UserPermission) permission).getWiki();
            } else if (permission instanceof VWBPermission) {
                resource = "*";
            } else if (permission instanceof PagePermission) {
                resource = ((PagePermission) permission).getPage();
            } else if (permission instanceof AllPermission) {
                resource = "*";
            } else {
                resource = permission.getName();
            }

            PolicyData pd = new PolicyData();

            pd.setId(pdl.size());
            pd.setPrincipalClass(principalClass);
            pd.setPrincipalName(principalName);

            pd.setPermissionClass(permissionClass);
            pd.setPermissionActions(permissionActions);
            pd.setPermissionName(permissionName);

            pd.setPrincipal(objectname);
            pd.setPermission(permissionType);
            pd.setOperation(operations);
            pd.setResource(resource);

            pdl.add(pd);
        }
    }

    return pdl;
}

From source file:cn.vlabs.duckling.vwb.service.auth.policy.PolicyUtil.java

public static PolicyData add(String voGroup, Acl policy, String principalAlias, String permissionAlias,
        String operation, String resource) {

    Principal principal = null;/*  w  ww  .  j  a  va2 s  .  c  o  m*/
    if (principalAlias.equals("All")) {
        principal = new Role("All");
        principalAlias = "";
    } else if (principalAlias.equals("VO")) {
        principal = new GroupPrincipal(voGroup);
        principalAlias = "";
    } else {
        return null;
    }

    AclEntry policyEntry = policy.getEntry(principal);
    Permission permission = null;
    if (permissionAlias.equals("Page")) {
        permission = new PagePermission(resource, operation);
        permissionAlias = "?";
    } else if (permissionAlias.equals("VWB")) {
        permission = new VWBPermission(resource, operation);
        permissionAlias = "";
    } else {
        return null;
    }

    policyEntry.addPermission(permission);

    PolicyData pd = new PolicyData();
    pd.setPrincipalClass(principal.getClass().getName());
    pd.setPrincipalName(principal.getName());
    pd.setPrincipal(principalAlias);

    pd.setPermissionClass(permission.getClass().getName());
    pd.setPermissionName(permission.getName());
    pd.setPermissionActions(permission.getActions());
    pd.setPermission(permissionAlias);

    pd.setOperation(operation);
    pd.setResource(resource);

    return pd;
}

From source file:de.ingrid.usermanagement.jetspeed.IngridPermissionManager.java

/**
 * <p>/*  ww  w . j a  va 2  s. co m*/
 * Returns the {@link InternalPermission} from a Permission.
 * </p>
 * 
 * @param permission The permission.
 * @return The {@link InternalPermission}.
 */
InternalPermission getInternalPermission(Permission permission) {
    Criteria filter = new Criteria();
    filter.addEqualTo("classname", permission.getClass().getName());
    filter.addEqualTo("name", permission.getName());
    filter.addEqualTo("actions", permission.getActions());
    Query query = QueryFactory.newQuery(InternalPermissionImpl.class, filter);
    InternalPermission internalPermission = (InternalPermission) broker.getObjectByQuery(query);
    return internalPermission;
}

From source file:de.ingrid.usermanagement.jetspeed.IngridPermissionManager.java

/**
 * @see org.apache.jetspeed.security.PermissionManager#addPermission(java.security.Permission)
 *///w w w .  jav  a2  s .c o m
public void addPermission(Permission permission) throws SecurityException {
    ArgUtil.notNull(new Object[] { permission }, new String[] { "permission" },
            "addPermission(java.security.Permission)");

    InternalPermission internalPermission = new InternalPermissionImpl(permission.getClass().getName(),
            permission.getName(), permission.getActions());
    try {
        broker.beginTransaction();
        broker.store(internalPermission);
        broker.commitTransaction();
    } catch (Exception e) {
        KeyedMessage msg = SecurityException.UNEXPECTED.create("PermissionManager.addPermission", "store",
                e.getMessage());
        log.error(msg, e);
        broker.abortTransaction();
        throw new SecurityException(msg, e);
    }
}

From source file:de.ingrid.usermanagement.jetspeed.IngridPermissionManager.java

/**
 * <p>//from w  ww .  j  a  v a  2  s. c o m
 * Iterate through a collection of {@link InternalPermission}and build a
 * unique collection of {@link java.security.Permission}.
 * </p>
 * 
 * @param omPermissions The collection of {@link InternalPermission}.
 * @return The collection of {@link java.security.Permission}.
 */
private Permissions appendSecurityPermissions(Collection omPermissions, Permissions permissions) {
    Iterator internalPermissionsIter = omPermissions.iterator();
    while (internalPermissionsIter.hasNext()) {
        InternalPermission internalPermission = (InternalPermission) internalPermissionsIter.next();
        Permission permission = null;
        try {
            Class permissionClass = Class.forName(internalPermission.getClassname());
            Class[] parameterTypes = { String.class, String.class };
            Constructor permissionConstructor = permissionClass.getConstructor(parameterTypes);
            Object[] initArgs = { internalPermission.getName(), internalPermission.getActions() };
            permission = (Permission) permissionConstructor.newInstance(initArgs);
            if (!Collections.list(permissions.elements()).contains(permission)) {
                if (log.isDebugEnabled()) {
                    log.debug("Adding permimssion: [class, " + permission.getClass().getName() + "], "
                            + "[name, " + permission.getName() + "], " + "[actions, " + permission.getActions()
                            + "]");
                }
                permissions.add(permission);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return permissions;
}

From source file:com.ecyrd.jspwiki.auth.SecurityVerifier.java

/**
 * Prints a &lt;td&gt; HTML element with the results of a permission test.
 * @param perm the permission to format/*from   w w  w .j  a  va2s  . c o m*/
 * @param allowed whether the permission is allowed
 */
private final String printPermissionTest(Permission permission, Principal principal, int cols) {
    StringBuffer s = new StringBuffer();
    if (permission == null) {
        s.append("    <td colspan=\"" + cols + "\" align=\"center\" title=\"N/A\">");
        s.append("&nbsp;</td>\n");
    } else {
        boolean allowed = verifyStaticPermission(principal, permission);
        s.append("    <td colspan=\"" + cols + "\" align=\"center\" title=\"");
        s.append(allowed ? "ALLOW: " : "DENY: ");
        s.append(permission.getClass().getName());
        s.append(" &quot;");
        s.append(permission.getName());
        s.append("&quot;");
        if (permission.getName() != null) {
            s.append(",&quot;");
            s.append(permission.getActions());
            s.append("&quot;");
        }
        s.append(" ");
        s.append(principal.getClass().getName());
        s.append(" &quot;");
        s.append(principal.getName());
        s.append("&quot;");
        s.append("\"");
        s.append(allowed ? BG_GREEN + ">" : BG_RED + ">");
        s.append("&nbsp;</td>\n");
    }
    return s.toString();
}

From source file:de.ingrid.usermanagement.jetspeed.IngridPermissionManager.java

/**
 * @see org.apache.jetspeed.security.PermissionManager#revokePermission(java.security.Principal,
 *      java.security.Permission)/* w  w  w  .ja v a  2s  . c  om*/
 */
public void revokePermission(Principal principal, Permission permission) throws SecurityException {
    String fullPath = SecurityHelper.getPreferencesFullPath(principal);
    ArgUtil.notNull(new Object[] { fullPath, permission }, new String[] { "fullPath", "permission" },
            "revokePermission(java.security.Principal, java.security.Permission)");

    // Remove permissions on principal.
    InternalPrincipal internalPrincipal = getInternalPrincipal(fullPath);
    if (null != internalPrincipal) {
        Collection internalPermissions = internalPrincipal.getPermissions();
        if (null != internalPermissions) {
            boolean revokePermission = false;
            ArrayList newInternalPermissions = new ArrayList();
            Iterator internalPermissionsIter = internalPermissions.iterator();
            while (internalPermissionsIter.hasNext()) {
                InternalPermission internalPermission = (InternalPermission) internalPermissionsIter.next();
                if (!((internalPermission.getClassname().equals(permission.getClass().getName()))
                        && (internalPermission.getName().equals(permission.getName()))
                        && (internalPermission.getActions().equals(permission.getActions())))) {
                    newInternalPermissions.add(internalPermission);
                } else {
                    revokePermission = true;
                }
            }
            if (revokePermission) {
                try {
                    internalPrincipal.setModifiedDate(new Timestamp(System.currentTimeMillis()));
                    internalPrincipal.setPermissions(newInternalPermissions);

                    broker.beginTransaction();
                    broker.store(internalPrincipal);
                    broker.commitTransaction();
                } catch (Exception e) {
                    KeyedMessage msg = SecurityException.UNEXPECTED.create("PermissionManager.revokePermission",
                            "store", e.getMessage());
                    log.error(msg, e);
                    broker.abortTransaction();
                    throw new SecurityException(msg, e);
                }
            }
        }
    }
}

From source file:org.jboss.dashboard.security.PermissionDescriptor.java

public void setPermission(Permission perm) {
    permissionClass = null;//  ww w .ja v a2 s .co m
    permissionResource = null;
    permissionActions = null;

    if (perm != null) {
        permissionClass = perm.getClass().getName();
        permissionResource = perm.getName();
        permissionActions = perm.getActions();
    }
}