All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.as.console.client.shared.subsys.security.SecurityDomainsPresenter Maven / Gradle / Ivy

Go to download

Bundles the core AS7 console as a GWT module. Includes minor customizations to support extensions.

There is a newer version: 0.7.0.Final
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2011 Red Hat Inc. and/or its affiliates and other contributors
 * as indicated by the @author tags. All rights reserved.
 * See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This copyrighted material is made available to anyone wishing to use,
 * modify, copy, or redistribute it subject to the terms and conditions
 * of the GNU Lesser General Public License, v. 2.1.
 * This program is distributed in the hope that it will be useful, but WITHOUT A
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
 * You should have received a copy of the GNU Lesser General Public License,
 * v.2.1 along with this distribution; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA  02110-1301, USA.
 */
package org.jboss.as.console.client.shared.subsys.security;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Command;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.Place;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.PlaceRequest;
import com.gwtplatform.mvp.client.proxy.Proxy;
import org.jboss.as.console.client.Console;
import org.jboss.as.console.client.core.NameTokens;
import org.jboss.as.console.client.domain.model.SimpleCallback;
import org.jboss.as.console.client.shared.BeanFactory;
import org.jboss.as.console.client.shared.dispatch.DispatchAsync;
import org.jboss.as.console.client.shared.dispatch.impl.DMRAction;
import org.jboss.as.console.client.shared.dispatch.impl.DMRResponse;
import org.jboss.as.console.client.shared.dispatch.impl.SimpleDMRResponseHandler;
import org.jboss.as.console.client.shared.properties.PropertyRecord;
import org.jboss.as.console.client.shared.subsys.Baseadress;
import org.jboss.as.console.client.shared.subsys.RevealStrategy;
import org.jboss.as.console.client.shared.subsys.security.model.AbstractAuthData;
import org.jboss.as.console.client.shared.subsys.security.model.AuthenticationLoginModule;
import org.jboss.as.console.client.shared.subsys.security.model.AuthorizationPolicyProvider;
import org.jboss.as.console.client.shared.subsys.security.model.GenericSecurityDomainData;
import org.jboss.as.console.client.shared.subsys.security.model.MappingModule;
import org.jboss.as.console.client.shared.subsys.security.model.SecurityDomain;
import org.jboss.as.console.client.shared.viewframework.FrameworkView;
import org.jboss.as.console.client.widgets.forms.ApplicationMetaData;
import org.jboss.as.console.client.widgets.forms.EntityAdapter;
import org.jboss.dmr.client.ModelDescriptionConstants;
import org.jboss.dmr.client.ModelNode;
import org.jboss.dmr.client.Property;

import java.util.ArrayList;
import java.util.List;

/**
 * @author David Bosschaert
 * @author Heiko Braun
 */
public class SecurityDomainsPresenter
        extends Presenter
    {

    private static final String CLASSIC = "classic";
    private static final String SECURITY_DOMAIN = "security-domain";

    public static final String AUDIT_IDENTIFIER = "audit";
    public static final String AUTHENTICATION_IDENTIFIER = "authentication";
    public static final String AUTHORIZATION_IDENTIFIER = "authorization";
    public static final String MAPPING_IDENTIFIER = "mapping";

    public static final String SECURITY_SUBSYSTEM = "security";

    private final DispatchAsync dispatcher;
    private final BeanFactory factory;
    private final RevealStrategy revealStrategy;
    private final EntityAdapter entityAdapter;
    private PlaceManager placeManager;

    private String selectedDomain;

    public PlaceManager getPlaceManager() {
        return this.placeManager;
    }

    @ProxyCodeSplit
    @NameToken(NameTokens.SecurityDomainsPresenter)
    public interface MyProxy extends Proxy, Place {
    }

    public interface MyView extends View, FrameworkView {
        void setPresenter(SecurityDomainsPresenter presenter);
        void setAuthenticationLoginModules(String domainName, List modules, boolean resourceExists);
        void setAuthorizationPolicyProviders(String domainName, List providers, boolean resourceExists);
        void setMappingModules(String domainName, List modules, boolean resourceExists);
        void setAuditModules(String domainName, List modules, boolean resourceExists);

        void loadSecurityDomain(String domainName);

        @Deprecated
        void setAuthFlagValues(String type, List values);

        void setSelectedDomain(String selectedDomain);
    }

    @Inject
    public SecurityDomainsPresenter(EventBus eventBus, MyView view, MyProxy proxy,
                                    DispatchAsync dispatcher, BeanFactory factory, RevealStrategy revealStrategy,
                                    ApplicationMetaData appMetaData, PlaceManager placeManager) {
        super(eventBus, view, proxy);

        this.dispatcher = dispatcher;
        this.factory = factory;
        this.revealStrategy = revealStrategy;
        this.entityAdapter = new EntityAdapter(SecurityDomain.class, appMetaData);

        this.placeManager = placeManager;
    }

    @Override
    protected void onBind() {
        super.onBind();
        getView().setPresenter(this);
    }

    @Override
    protected void onReset() {
        super.onReset();
        getView().initialLoad();
        getView().setSelectedDomain(selectedDomain);
    }

    @Override
    protected void revealInParent() {
        revealStrategy.revealInParent(this);

        Console.schedule(new Command() {
            @Override
            public void execute() {
                loadAuthFlagValues();
            }
        });
    }

    @Override
    public void prepareFromRequest(PlaceRequest request) {
        super.prepareFromRequest(request);
        selectedDomain = request.getParameter("name", null);
    }

    private void loadAuthFlagValues() {
        loadAuthFlagValues(AUTHORIZATION_IDENTIFIER, "policy-modules");
        loadAuthFlagValues(AUTHENTICATION_IDENTIFIER, "login-modules");
    }

    @Deprecated
    private void loadAuthFlagValues(final String type, final String attrName) {
        ModelNode operation = createOperation(ModelDescriptionConstants.READ_RESOURCE_DESCRIPTION_OPERATION);
        operation.get(ModelDescriptionConstants.ADDRESS).add(SECURITY_DOMAIN, "*");
        operation.get(ModelDescriptionConstants.ADDRESS).add(type, CLASSIC);

        dispatcher.execute(new DMRAction(operation), new SimpleCallback() {
            @Override
            public void onSuccess(DMRResponse result) {
                ModelNode response = result.get();
                List res = response.get(ModelDescriptionConstants.RESULT).asList();
                if (res.size() == 0)
                    return;

                ModelNode attrDesc = res.get(0).get(ModelDescriptionConstants.RESULT,
                        ModelDescriptionConstants.ATTRIBUTES, attrName,
                        ModelDescriptionConstants.VALUE_TYPE, "flag");

                List values = new ArrayList();
                for (ModelNode option : attrDesc.get(ModelDescriptionConstants.ALLOWED).asList()) {
                    values.add(option.asString());
                }

                getView().setAuthFlagValues(type, values);
            }
        });
    }

    public void updateDomainSelection(final SecurityDomain domain) {
        // load sub-elements which are not automatically loaded by the framework
        ModelNode operation = createOperation(ModelDescriptionConstants.READ_RESOURCE_OPERATION);
        operation.get(ModelDescriptionConstants.ADDRESS).add(SECURITY_DOMAIN, domain.getName());
        operation.get(ModelDescriptionConstants.RECURSIVE).set(true);

        dispatcher.execute(new DMRAction(operation), new SimpleCallback() {
            @Override
            public void onSuccess(DMRResponse result) {
                ModelNode response = result.get();
                ModelNode model = response.get(ModelDescriptionConstants.RESULT);

                loadGeneric(model, domain, AUTHORIZATION_IDENTIFIER, "policy-modules", AuthorizationPolicyProvider.class,
                        new CustomLoadHandler() {
                            @Override
                            public void readFromModel(ModelNode n, AuthorizationPolicyProvider object) {
                                object.setFlag(n.get("flag").asString());
                            }

                            @Override
                            public void setInView(List modules, boolean resourceExists) {
                                getView().setAuthorizationPolicyProviders(domain.getName(), modules, resourceExists);
                            }
                        });

                loadGeneric(model, domain, AUTHENTICATION_IDENTIFIER, "login-modules", AuthenticationLoginModule.class,
                        new CustomLoadHandler() {
                            @Override
                            public void readFromModel(ModelNode n, AuthenticationLoginModule object) {
                                object.setFlag(n.get("flag").asString());
                            }

                            @Override
                            public void setInView(List modules, boolean resourceExists) {
                                getView().setAuthenticationLoginModules(domain.getName(), modules, resourceExists);
                            }
                        });

                loadGeneric(model, domain, MAPPING_IDENTIFIER, "mapping-modules", MappingModule.class,
                        new CustomLoadHandler() {
                            @Override
                            public void readFromModel(ModelNode n, MappingModule object) {
                                object.setType(n.get("type").asString());
                            }

                            @Override
                            public void setInView(List modules, boolean resourceExists) {
                                getView().setMappingModules(domain.getName(), modules, resourceExists);
                            }
                        });

                loadGeneric(model, domain, AUDIT_IDENTIFIER, "provider-modules", GenericSecurityDomainData.class,
                        new CustomLoadHandler() {
                            @Override
                            public void readFromModel(ModelNode n, GenericSecurityDomainData object) {
                            }

                            @Override
                            public void setInView(List modules, boolean resourceExists) {
                                getView().setAuditModules(domain.getName(), modules, resourceExists);
                            }
                        });
            }
        });
    }

    private  void loadGeneric(ModelNode model, SecurityDomain domain, String type, String attrName, Class cls,
                                                                   CustomLoadHandler customHandler) {
        List modules = new ArrayList();
        boolean resourceExists = false;
        if (model.hasDefined(type)) {
            ModelNode subModel = model.get(type, CLASSIC);
            resourceExists = subModel.hasDefined(attrName);

            if (resourceExists) {
                for (ModelNode node : subModel.get(attrName).asList()) {
                    T pm = factory.create(cls).as();

                    pm.setCode(node.get("code").asString());
                    customHandler.readFromModel(node, pm);

                    if (node.hasDefined("module-options")) {
                        List pl = node.require("module-options").asPropertyList();
                        pm.setProperties(entityAdapter.fromDMRPropertyList(pl));
                    }

                    modules.add(pm);
                }
            }
        }

        customHandler.setInView(modules, resourceExists);
    }

    private static class CustomAuthSaveFieldhandler

extends CustomSaveHandler

{ @Override public void setInModel(ModelNode n, P object) { n.get("flag").set(object.getFlag()); } } public void saveAuthorization(String domainName, List list, boolean resourceExists) { saveGeneric(domainName, list, AUTHORIZATION_IDENTIFIER, "policy-modules", resourceExists, new CustomAuthSaveFieldhandler()); } public void saveAuthentication(String domainName, List list, boolean resourceExists) { saveGeneric(domainName, list, AUTHENTICATION_IDENTIFIER, "login-modules", resourceExists, new CustomAuthSaveFieldhandler()); } public void saveMapping(String domainName, List list, boolean resourceExists) { saveGeneric(domainName, list, MAPPING_IDENTIFIER, "mapping-modules", resourceExists, new CustomSaveHandler() { @Override public void setInModel(ModelNode n, MappingModule object) { n.get("type").set(object.getType()); } }); } public void saveAudit(String domainName, List list, boolean resourceExists) { saveGeneric(domainName, list, AUDIT_IDENTIFIER, "provider-modules", resourceExists, new CustomSaveHandler()); } // TODO: https://issues.jboss.org/browse/AS7-2936 public void saveGeneric(final String domainName, List list, String type, String attrName, boolean resourceExists, CustomSaveHandler customHandler) { ModelNode operation = null; ModelNode valueList = new ModelNode(); valueList.setEmptyList(); for (T pm : list) { ModelNode n = new ModelNode(); n.get("code").set(pm.getCode()); if (customHandler != null) { customHandler.setInModel(n, pm); } List props = pm.getProperties(); if (props != null) n.get("module-options").set(entityAdapter.fromEntityPropertyList(props)); valueList.add(n); } if (resourceExists) { operation = createOperation(ModelDescriptionConstants.WRITE_ATTRIBUTE_OPERATION); operation.get(ModelDescriptionConstants.ADDRESS).add(SECURITY_DOMAIN, domainName); operation.get(ModelDescriptionConstants.ADDRESS).add(type, CLASSIC); operation.get(ModelDescriptionConstants.NAME).set(attrName); if(list.size()>0) operation.get("value").set(valueList); else operation.get("value").setEmptyList(); } else { operation = createOperation(ModelDescriptionConstants.ADD); operation.get(ModelDescriptionConstants.ADDRESS).add(SECURITY_DOMAIN, domainName); operation.get(ModelDescriptionConstants.ADDRESS).add(type, CLASSIC); if(list.size()>0) operation.get(attrName).set(valueList); else operation.get(attrName).setEmptyList(); } dispatcher.execute(new DMRAction(operation), new SimpleDMRResponseHandler(ModelDescriptionConstants.WRITE_ATTRIBUTE_OPERATION, attrName, domainName, new Command() { @Override public void execute() { getView().loadSecurityDomain(domainName); } })); } public void getDescription(String type, final DescriptionCallBack callback) { ModelNode operation = createOperation(ModelDescriptionConstants.READ_RESOURCE_DESCRIPTION_OPERATION); operation.get(ModelDescriptionConstants.ADDRESS).add(SECURITY_DOMAIN, "*"); operation.get(ModelDescriptionConstants.ADDRESS).add(type, CLASSIC); dispatcher.execute(new DMRAction(operation), new SimpleCallback() { @Override public void onSuccess(DMRResponse result) { ModelNode response = result.get(); List resList = response.get(ModelDescriptionConstants.RESULT).asList(); if (resList.size() == 0) return; callback.setDescription(resList.get(0).get(ModelDescriptionConstants.RESULT)); } }); } private ModelNode createOperation(String operator) { ModelNode operation = new ModelNode(); operation.get(ModelDescriptionConstants.OP).set(operator); operation.get(ModelDescriptionConstants.ADDRESS).set(Baseadress.get()); operation.get(ModelDescriptionConstants.ADDRESS).add(ModelDescriptionConstants.SUBSYSTEM, SECURITY_SUBSYSTEM); return operation; } private interface CustomLoadHandler

{ void readFromModel(ModelNode n, P object); void setInView(List

modules, boolean resourceExists); } private static class CustomSaveHandler

{ void setInModel(ModelNode n, P object) {} } public interface DescriptionCallBack { public void setDescription(ModelNode desc); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy