/*
 * ModeShape (http://www.modeshape.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.modeshape.jboss.subsystem;

import java.util.List;
import org.jboss.as.controller.ReloadRequiredRemoveStepHandler;
import org.jboss.as.controller.SimpleResourceDefinition;
import org.jboss.as.controller.access.constraint.SensitivityClassification;
import org.jboss.as.controller.access.management.AccessConstraintDefinition;
import org.jboss.as.controller.access.management.SensitiveTargetAccessConstraintDefinition;
import org.jboss.as.controller.operations.common.GenericSubsystemDescribeHandler;
import org.jboss.as.controller.registry.ManagementResourceRegistration;

/**
 * @author <a href="mailto:tomaz.cerar@redhat.com">Tomaz Cerar</a>
 */
public class ModeShapeRootResource extends SimpleResourceDefinition {
    protected final static ModeShapeRootResource INSTANCE = new ModeShapeRootResource();

    /**
     * Set the default ModeShape RBAC access as follows:
     *  Addressing is not sensitive: any management user can address (i.e. read the configuration)
     *  Read is sensitive: only Auditor, Administrator, SuperUser can read attributes, resources etc.
     *  Write is sensitive: only Administrator and SuperUser can write
     */
    protected static final SensitivityClassification MODESHAPE_SECURITY =
            new SensitivityClassification(ModeShapeExtension.SUBSYSTEM_NAME, "modeshape-access-control", false, true, true);
    protected static final SensitiveTargetAccessConstraintDefinition MODESHAPE_SECURITY_DEF =
            new SensitiveTargetAccessConstraintDefinition(MODESHAPE_SECURITY);


    private ModeShapeRootResource() {
        super(ModeShapeExtension.SUBSYSTEM_PATH, ModeShapeExtension.getResourceDescriptionResolver(),
              AddModeShapeSubsystem.INSTANCE, ReloadRequiredRemoveStepHandler.INSTANCE);
    }

    @Override
    public void registerOperations( ManagementResourceRegistration resourceRegistration ) {
        super.registerOperations(resourceRegistration);
        resourceRegistration.registerOperationHandler(GenericSubsystemDescribeHandler.DEFINITION,
                                                      GenericSubsystemDescribeHandler.INSTANCE,
                                                      false);
    }

    @Override
    public List<AccessConstraintDefinition> getAccessConstraints() {
        return MODESHAPE_SECURITY_DEF.wrapAsList();
    }
}
