/*
Translates CMIS permission to a set of JCR privileges.

@param cmisPermission
@return
163331
public static String[] jcrPermissions( String cmisPermission ) {
        switch (cmisPermission) {
            case "cmis:read" :
                return new String[] {
                    Privilege.JCR_READ,
                    Privilege.JCR_READ_ACCESS_CONTROL
                };
            case "cmis:write" : 
                return new String[] {
                    Privilege.JCR_WRITE,
                    Privilege.JCR_READ_ACCESS_CONTROL,
                    Privilege.JCR_MODIFY_ACCESS_CONTROL,
                    Privilege.JCR_NODE_TYPE_MANAGEMENT,
                    Privilege.JCR_RETENTION_MANAGEMENT,
                    Privilege.JCR_VERSION_MANAGEMENT
                };
            case "cmis:all" :
                return new String[] {Privilege.JCR_ALL};
            default :
                    return null;
        }
    }
1206032
public String privilegesMapToYml(Map<String, Collection<Privilege>> privileges) {
        try {
            return mapper.writeValueAsString(privileges);
        } catch (Exception e) {
            log.error("Failed to create privileges YML file from map, error: {}", e.getMessage(), e);
        }
        return null;
    }
812892
@SuppressWarnings("unchecked")
    public Permission getPermission(final String name) {
        final Query query = pm.newQuery(Permission.class, "name == :name");
        final List<Permission> result = (List<Permission>) query.execute(name);
        return Collections.isEmpty(result) ? null : result.get(0);
    }
1048331
private String getPermissionString(CmsObject cms, CmsResource res, DialogType type) throws CmsException {

        if (type.equals(DialogType.User)) {
            cms.getPermissions(res.getRootPath(), m_principal.getName()).getPermissionString();
        } else if (type.equals(DialogType.Group)) {
            Iterator<CmsAccessControlEntry> itAces = cms.getAccessControlEntries(res.getRootPath(), false).iterator();
            while (itAces.hasNext()) {
                CmsAccessControlEntry ace = itAces.next();
                if (ace.getPrincipal().equals(m_principal.getId())) {
                    return ace.getPermissions().getPermissionString();
                }
            }
        }
        return "";
    }
766670
public com.google.protobuf.ByteString
      getPermissionBytes() {
    java.lang.Object ref = permission_;
    if (ref instanceof java.lang.String) {
      com.google.protobuf.ByteString b = 
          com.google.protobuf.ByteString.copyFromUtf8(
              (java.lang.String) ref);
      permission_ = b;
      return b;
    } else {
      return (com.google.protobuf.ByteString) ref;
    }
  }
103401
public String getPermissionString() {

        StringBuffer p = new StringBuffer("");

        if ((m_denied & CmsPermissionSet.PERMISSION_READ) > 0) {
            p.append("-r");
        } else if (requiresReadPermission()) {
            p.append("+r");
        }

        if ((m_denied & CmsPermissionSet.PERMISSION_WRITE) > 0) {
            p.append("-w");
        } else if (requiresWritePermission()) {
            p.append("+w");
        }

        if ((m_denied & CmsPermissionSet.PERMISSION_VIEW) > 0) {
            p.append("-v");
        } else if (requiresViewPermission()) {
            p.append("+v");
        }

        if ((m_denied & CmsPermissionSet.PERMISSION_CONTROL) > 0) {
            p.append("-c");
        } else if (requiresControlPermission()) {
            p.append("+c");
        }

        if ((m_denied & CmsPermissionSet.PERMISSION_DIRECT_PUBLISH) > 0) {
            p.append("-d");
        } else if (requiresDirectPublishPermission()) {
            p.append("+d");
        }

        return p.toString();
    }
1133393
public static List<String> getNativePermissions(CmsAccessControlEntry ace) {

        List<String> result = getNativePermissions(ace.getPermissions().getAllowedPermissions(), false);
        result.addAll(getNativePermissions(ace.getPermissions().getDeniedPermissions(), true));
        return result;
    }
743713
public CmsPermissionSet getPermissions(String resourceName) throws CmsException {

        return getPermissions(resourceName, m_context.getCurrentUser().getName());
    }
863794
@Override
    public void addPermissions(IPermission[] permissions) throws AuthorizationException {
        if (permissions.length > 0) {
            getPermissionStore().add(permissions);
            if (this.cachePermissions) {
                removeFromPermissionsCache(permissions);
            }
        }
    }
558157
public boolean exportUserHasReadPermission() {

        String vfsName = getParamResource();
        CmsObject cms = getCms();
        try {
            // static export must always be checked with the export users permissions,
            // not the current users permissions
            CmsObject exportCms = OpenCms.initCmsObject(OpenCms.getDefaultUsers().getUserExport());
            exportCms.getRequestContext().setSiteRoot(getCms().getRequestContext().getSiteRoot());
            // let's look up if the export user has the permission to read
            return exportCms.hasPermissions(
                cms.readResource(vfsName, CmsResourceFilter.IGNORE_EXPIRATION),
                CmsPermissionSet.ACCESS_READ);
        } catch (CmsException e) {
            // ignore this exception
        }
        return false;
    }
550007
@SuppressWarnings("unchecked")
    public List<Permission> getPermissions() {
        final Query query = pm.newQuery(Permission.class);
        query.setOrdering("name asc");
        return (List<Permission>) query.execute();
    }
399330
public static List<Entry> permissionStructure(Permission permission) {
        List<Entry> entryList = new LinkedList<>();
        entryList.add(namedObject(DnFactory.permission(permission)));
        entryList.add(namedDescriptiveObject(DnFactory.permissionAction(permission), permission.getAction()));
        entryList
                .add(namedDescriptiveObject(DnFactory.permissionComponent(permission), permission.getComponentName()));
        return entryList;
    }
370244
private CmsPermissionSet buildPermissionsForCurrentUser() throws CmsException {

        CmsResourceUtil resUtil = new CmsResourceUtil(
            getCms(),
            getCms().readResource(getParamResource(), CmsResourceFilter.ALL));
        return resUtil.getPermissionSet();
    }
592262
private Privilege[] includePrivilege( AccessControlManager acm,
                                          Privilege[] privileges,
                                          JcrPermission permission ) throws RepositoryException {
        ArrayList<Privilege> list = new ArrayList<>();

        for (Privilege privilege : privileges) {
            if (!privilege.getName().equalsIgnoreCase(permission.getName())) {
                list.add(privilege);
            }
        }

        list.add(acm.privilegeFromName(permission.getJcrName()));
        Privilege[] res = new Privilege[list.size()];
        list.toArray(res);
        return res;
    }
1121166
public Map<String, Permission> getPermissions(String tenant) {
        if (!permissions.containsKey(tenant)) {
            return new HashMap<>();
        }
        return permissions.get(tenant);
    }
75980
public Map<String, Map<String, List<String>>> revokeResourcePermission(String subjectid, String resourcePath) {
		if (StringUtils.isBlank(subjectid) || StringUtils.isBlank(resourcePath)) {
			return Collections.emptyMap();
		}
		resourcePath = Utils.urlEncode(resourcePath);
		return getEntity(invokeDelete(Utils.formatMessage("_permissions/{0}/{1}", subjectid, resourcePath),
				null), Map.class);
	}
832788
public synchronized Acl getAcl(CmsCmisCallContext context, String objectId, boolean onlyBasicPermissions) {

        try {

            CmsObject cms = m_repository.getCmsObject(context);
            CmsUUID structureId = new CmsUUID(objectId);
            CmsResource resource = cms.readResource(structureId);
            return collectAcl(cms, resource, onlyBasicPermissions);
        } catch (CmsException e) {
            handleCmsException(e);
            return null;
        }

    }