package com.thinkbiganalytics.metadata.modeshape.security.role;

import com.thinkbiganalytics.metadata.api.MetadataAccess;
import com.thinkbiganalytics.metadata.api.PostMetadataConfigAction;
import com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions;
import com.thinkbiganalytics.security.action.AllowedActions;
import com.thinkbiganalytics.security.action.AllowedEntityActionsProvider;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.annotation.Order;

import java.util.Optional;

import javax.inject.Inject;
import javax.jcr.Node;
import javax.jcr.security.Privilege;

/**
 * Ensures that the AllowedActions.Services prototype nodes are available under the Services noded
 */
@Order(PostMetadataConfigAction.LATE_ORDER)
public class EnsureServicesAccessControlAction implements PostMetadataConfigAction {


    @Inject
    private MetadataAccess metadata;


    @Inject
    private AllowedEntityActionsProvider allowedEntityActionsProvider;

    private static final Logger log = LoggerFactory.getLogger(EnsureServicesAccessControlAction.class);

    @Override
    public void run() {
        log.info("Ensuring the Services prototype access control permissions are members of the actual Services access control node");
        metadata.commit(() -> {
            //find services entity node
            Optional<AllowedActions> option = this.allowedEntityActionsProvider.getAllowedActions(AllowedActions.SERVICES);
            Node servicesNode = ((JcrAllowedActions) option.get()).getNode();
            JcrAllowedActions allowedAction = ((JcrAllowedActions) allowedEntityActionsProvider.getAvailableActions(AllowedActions.SERVICES).get());
            allowedAction.copy(servicesNode, MetadataAccess.ADMIN, Privilege.JCR_ALL);
        }, MetadataAccess.SERVICE);
    }


}

@@ -58,16 +58,676 @@
 .role;%0A%0A
+/*-%0A * #%25L%0A * kylo-metadata-modeshape%0A * %25%25%0A * Copyright (C) 2017 ThinkBig Analytics%0A * %25%25%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A * %0A *     http://www.apache.org/licenses/LICENSE-2.0%0A * %0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A * #L%25%0A */%0A%0A
 import c

add missing header