<commit_before>/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * 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.slc.sli.api.init;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slc.sli.api.security.roles.Role;
import org.slc.sli.api.security.roles.RoleBuilder;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralCriteria;
import org.slc.sli.domain.NeutralQuery;
import org.slc.sli.domain.Repository;
import org.slc.sli.domain.enums.Right;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

/**
 * A simple initializing bean to initialize our Mongo instance with default roles.
 * 
 * IMPORTANT: If you add new SLI Administrative roles, make sure you set the admin flag to true.
 * Failure to do so can introduce a large security hole.
 * 
 * @author rlatta
 */
@Component
public class RoleInitializer {
    public static final String EDUCATOR = "Educator";
    public static final String AGGREGATE_VIEWER = "Aggregate Viewer";
    public static final String IT_ADMINISTRATOR = "IT Administrator";
    public static final String LEADER = "Leader";
    public static final String ROLES = "customRole";
    
    public static final String LEA_ADMINISTRATOR = "LEA Administrator";
    public static final String SEA_ADMINISTRATOR = "SEA Administrator";
    public static final String APP_DEVELOPER = "Application Developer";
    public static final String SLC_OPERATOR = "SLC Operator";
    public static final String REALM_ADMINISTRATOR = "Realm Administrator";
    public static final String INGESTION_USER = "Ingestion User";
    public static final String SANDBOX_SLC_OPERATOR = "Sandbox SLC Operator";
    public static final String SANDBOX_ADMINISTRATOR = "Sandbox Administrator";
    
    @Autowired
    @Qualifier("validationRepo")
    private Repository<Entity> repository;
    
    public void dropAndBuildRoles(String tenantId, String realmId) {
        dropRoles(tenantId, realmId);
        buildRoles(realmId);
    }
    
    private void dropRoles(String tenantId, String realmId) {
        if (tenantId != null) {
            if (realmId != null) {
                NeutralQuery query = new NeutralQuery(0);
                query.addCriteria(new NeutralCriteria("metaData.tenantId", "=", tenantId, false));
                query.addCriteria(new NeutralCriteria("realmId", "=", realmId));
                
                Entity entity = repository.findOne(ROLES, query);
                if (entity != null) {
                    repository.delete(ROLES, entity.getEntityId());
                    info("Successfully dropped roles from realm: {}", new Object[] { realmId });
                } else {
                    info("No roles exist to drop for realm: {}", new Object[] { realmId });
                }
            } else {
                warn("Null realm id --> not dropping roles.");
            }
        } else {
            warn("Null tenant id --> not dropping roles.");
        }
    }
    
    public int buildRoles(String realmId) {
        if (realmId != null) {
            info("Building roles for realm: {}", new Object[] { realmId });
            Map<String, Object> rolesBody = new HashMap<String, Object>();
            List<Map<String, Object>> groups = getDefaultRoles();
            rolesBody.put("realmId", realmId);
            rolesBody.put("roles", groups);
            rolesBody.put("customRights", new ArrayList<String>());
            return groups.size();
        } else {
            warn("Null realm id --> not building roles.");
        }
        return 0;
    }
    
    public List<Map<String, Object>> getDefaultRoles() {
        List<Map<String, Object>> groups = new ArrayList<Map<String, Object>>();
        groups.add(buildRoleGroup(buildAggregate()));
        groups.add(buildRoleGroup(buildLeader()));
        groups.add(buildRoleGroup(buildIT()));
        groups.add(buildRoleGroup(buildEducator()));
        return groups;
    }
    
    private Map<String, Object> buildRoleGroup(Role role) {
        Map<String, Object> group = new HashMap<String, Object>();
        group.put("groupTitle", role.getName());
        group.put("names", Arrays.asList(role.getName()));
        group.put("rights", iterableToList(role.getRights()));
        return group;
    }
    
    private List<GrantedAuthority> iterableToList(Set<GrantedAuthority> original) {
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        for (GrantedAuthority authority : original) {
            list.add(authority);<commit_msg>fix for role initialization to correctly create default roles in customRole collection<commit_after>/*
 * Copyright 2012 Shared Learning Collaborative, LLC
 *
 * 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.slc.sli.api.init;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slc.sli.api.security.roles.Role;
import org.slc.sli.api.security.roles.RoleBuilder;
import org.slc.sli.domain.Entity;
import org.slc.sli.domain.NeutralCriteria;
import org.slc.sli.domain.NeutralQuery;
import org.slc.sli.domain.Repository;
import org.slc.sli.domain.enums.Right;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

/**
 * A simple initializing bean to initialize our Mongo instance with default roles.
 * 
 * IMPORTANT: If you add new SLI Administrative roles, make sure you set the admin flag to true.
 * Failure to do so can introduce a large security hole.
 * 
 * @author rlatta
 */
@Component
public class RoleInitializer {
    public static final String EDUCATOR = "Educator";
    public static final String AGGREGATE_VIEWER = "Aggregate Viewer";
    public static final String IT_ADMINISTRATOR = "IT Administrator";
    public static final String LEADER = "Leader";
    public static final String ROLES = "customRole";
    
    public static final String LEA_ADMINISTRATOR = "LEA Administrator";
    public static final String SEA_ADMINISTRATOR = "SEA Administrator";
    public static final String APP_DEVELOPER = "Application Developer";
    public static final String SLC_OPERATOR = "SLC Operator";
    public static final String REALM_ADMINISTRATOR = "Realm Administrator";
    public static final String INGESTION_USER = "Ingestion User";
    public static final String SANDBOX_SLC_OPERATOR = "Sandbox SLC Operator";
    public static final String SANDBOX_ADMINISTRATOR = "Sandbox Administrator";
    
    @Autowired
    @Qualifier("validationRepo")
    private Repository<Entity> repository;
    
    public void dropAndBuildRoles(String tenantId, String realmId) {
        dropRoles(tenantId, realmId);
        buildRoles(realmId);
    }
    
    private void dropRoles(String tenantId, String realmId) {
        if (tenantId != null) {
            if (realmId != null) {
                NeutralQuery query = new NeutralQuery(0);
                query.addCriteria(new NeutralCriteria("metaData.tenantId", "=", tenantId, false));
                query.addCriteria(new NeutralCriteria("realmId", "=", realmId));
                
                Entity entity = repository.findOne(ROLES, query);
                if (entity != null) {
                    repository.delete(ROLES, entity.getEntityId());
                    info("Successfully dropped roles from realm: {}", new Object[] { realmId });
                } else {
                    info("No roles exist to drop for realm: {}", new Object[] { realmId });
                }
            } else {
                warn("Null realm id --> not dropping roles.");
            }
        } else {
            warn("Null tenant id --> not dropping roles.");
        }
    }
    
    public int buildRoles(String realmId) {
        if (realmId != null) {
            info("Building roles for realm: {}", new Object[] { realmId });
            Map<String, Object> rolesBody = new HashMap<String, Object>();
            List<Map<String, Object>> groups = getDefaultRoles();
            rolesBody.put("realmId", realmId);
            rolesBody.put("roles", groups);
            rolesBody.put("customRights", new ArrayList<String>());
            repository.create(ROLES, rolesBody);
            return groups.size();
        } else {
            warn("Null realm id --> not building roles.");
        }
        return 0;
    }
    
    public List<Map<String, Object>> getDefaultRoles() {
        List<Map<String, Object>> groups = new ArrayList<Map<String, Object>>();
        groups.add(buildRoleGroup(buildAggregate()));
        groups.add(buildRoleGroup(buildLeader()));
        groups.add(buildRoleGroup(buildIT()));
        groups.add(buildRoleGroup(buildEducator()));
        return groups;
    }
    
    private Map<String, Object> buildRoleGroup(Role role) {
        Map<String, Object> group = new HashMap<String, Object>();
        group.put("groupTitle", role.getName());
        group.put("names", Arrays.asList(role.getName()));
        group.put("rights", iterableToList(role.getRights()));
        return group;
    }
    
    private List<GrantedAuthority> iterableToList(Set<GrantedAuthority> original) {
        List<GrantedAuthority> list = new ArrayList<GrantedAuthority>();
        for (GrantedAuthority authority : original) {
            list.add(authority);
{'fraction_non_alphanumeric': 0.10360527782939974, 'fraction_numerical': 0.001858390633711206, 'mean_word_length': 2.9730527870062753, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3521830', 'n_tokens_mistral': 3003, 'n_tokens_neox': 2927, 'n_words': 860}