/************************************************************************* * * * EJBCA Community: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package org.ejbca.ui.cli.roles; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import org.apache.commons.lang.StringUtils; import org.apache.log4j.Logger; import org.cesecore.authorization.user.matchvalues.X500PrincipalAccessMatchValue; import org.cesecore.util.EjbRemoteHelper; import org.ejbca.core.ejb.authentication.cli.CliUserAccessMatchValue; import org.ejbca.core.ejb.authorization.AuthorizationSystemSessionRemote; import org.ejbca.ui.cli.infrastructure.command.EjbcaCliUserCommandBase; /** * Base for Roles commands, contains common functions for Roles operations * @version $Id: BaseRolesCommand.java 29175 2018-06-08 13:38:23Z jeklund $ */ public abstract class BaseRolesCommand extends EjbcaCliUserCommandBase { private static final Logger log = Logger.getLogger(BaseRolesCommand.class); private Map resourceNameToResourceMap = null; private Map resourceToResourceNameMap = null; private static Set commandAliases = new HashSet(); static { commandAliases.add(new String[] { "admins" }); try { Class.forName(X500PrincipalAccessMatchValue.class.getName()); Class.forName(CliUserAccessMatchValue.class.getName()); } catch (ClassNotFoundException e) { log.error("Failure during match value initialization", e); } } @Override public String[] getCommandPath() { return new String[] { "roles" }; } @Override public Set getCommandPathAliases() { return commandAliases; } @Override protected abstract Logger getLogger(); /** @return a Map for authorized resources (cached in this remote JVM) */ public Map getResourceNameToResourceMap() { if (resourceNameToResourceMap==null) { final Map authorizedResourcesMap = EjbRemoteHelper.INSTANCE.getRemoteSession(AuthorizationSystemSessionRemote.class). getAllResources(getAuthenticationToken(), false); resourceNameToResourceMap = new HashMap<>(); for (final Entry entry: authorizedResourcesMap.entrySet()) { resourceNameToResourceMap.put(entry.getValue(), entry.getKey()); } } return resourceNameToResourceMap; } /** @return a Map for authorized resources (cached in this remote JVM) */ public Map getResourceToResourceNameMap() { if (resourceToResourceNameMap==null) { resourceToResourceNameMap = EjbRemoteHelper.INSTANCE.getRemoteSession(AuthorizationSystemSessionRemote.class). getAllResources(getAuthenticationToken(), false); } return resourceToResourceNameMap; } /** @return the full role name with namespace prefixed in brackets. */ protected String getFullRoleName(final String namespace, final String roleName) { return (StringUtils.isEmpty(namespace) ? "" : "["+namespace+"] ") + "'" + roleName + "'"; } }