<commit_before>/*
 * Copyright 2006-2008 The Kuali Foundation
 * 
 * Licensed under the Educational Community 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.osedu.org/licenses/ECL-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.kuali.kra.irb.service.impl;

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

import org.kuali.kra.bo.Person;
import org.kuali.kra.bo.RolePersons;
import org.kuali.kra.infrastructure.Constants;
import org.kuali.kra.infrastructure.RoleConstants;
import org.kuali.kra.irb.bo.Protocol;
import org.kuali.kra.irb.service.ProtocolAuthorizationService;
import org.kuali.kra.kim.pojo.QualifiedRole;
import org.kuali.kra.kim.service.PersonService;
import org.kuali.kra.kim.service.QualifiedRoleService;
import org.kuali.kra.service.UnitAuthorizationService;

/**
 * The Protocol Authorization Service Implementation.
 *
 * @author Kuali Research Administration Team (<EMAIL>)
 */
public class ProtocolAuthorizationServiceImpl implements ProtocolAuthorizationService {
    private static final org.apache.commons.logging.Log LOG = org.apache.commons.logging.LogFactory.getLog(ProtocolAuthorizationServiceImpl.class);
    
    private static final String PROTOCOL_KEY = "kra.protocol";
    
    private UnitAuthorizationService unitAuthorizationService;
    private PersonService kimPersonService;
    private QualifiedRoleService kimQualifiedRoleService;
    private org.kuali.kra.service.PersonService personService;

    /**
     * Set the Unit Authorization Service.  Injected by Spring.
     * @param unitAuthorizationService the Unit Authorization Service
     */
    public void setUnitAuthorizationService(UnitAuthorizationService unitAuthorizationService) {
        this.unitAuthorizationService = unitAuthorizationService;
    }
    
    /**
     * Set the KRA Person Service.  Injected by Spring.
     * @param personService the KRA Person Service
     */
    public void setPersonService(org.kuali.kra.service.PersonService personService) {
        this.personService = personService;
    }
    
    /**
     * Set the KIM Person Service.  Injected by Spring.
     * @param personService the KIM Person Service
     */
    public void setKimPersonService(PersonService personService) {
        this.kimPersonService = personService;
    }
    
    /**
     * Set the KIM Qualified Role Service.  Injected by Spring.
     * @param qualifiedRoleService the KIM Qualified Role Service
     */
    public void setKimQualifiedRoleService(QualifiedRoleService qualifiedRoleService) {
        this.kimQualifiedRoleService = qualifiedRoleService;
    }
    
    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#getUserNames(org.kuali.kra.protocoldevelopment.document.ProtocolDocument, java.lang.String)
     */
    public List<String> getUserNames(Protocol protocol, String roleName) {
        Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
        qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
        return kimQualifiedRoleService.getPersonUsernames(roleName, qualifiedRoleAttributes);
    }

    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#addRole(java.lang.String, java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument)
     */
    public void addRole(String username, String roleName, Protocol protocol) {
        Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
        qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
        kimPersonService.addQualifiedRole(username, roleName, qualifiedRoleAttributes);
    }

    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#removeRole(java.lang.String, java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument)
     */
    public void removeRole(String username, String roleName, Protocol protocol) {
        Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
        qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
        kimPersonService.removeQualifiedRole(username, roleName, qualifiedRoleAttributes);
    }
    
    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#hasPermission(java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument, java.lang.String)
     */
    public boolean hasPermission(String username, Protocol protocol, String permissionName) {
        boolean userHasPermission = false;
        if (isValidPerson(username)) {
            Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
            qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
            userHasPermission = kimPersonService.hasQualifiedPermission(username, Constants.KRA_NAMESPACE, permissionName, qualifiedRoleAttributes);
            if (!userHasPermission) {
                Person person = personService.getPersonByName(username);
                if (person != null) {
                    String unitNumber = person.getHomeUnit();
                    userHasPermission = unitAuthorizationService.hasPermission(username, unitNumber, permissionName);
                }
            }
        }
        return userHasPermission;
    }
   
    private boolean isValidPerson(String username) {
        return personService.isActiveByName(username);
    }

    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#hasRole(java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument, java.lang.String)
     */
    public boolean hasRole(String username, Protocol protocol, String roleName) {
        boolean userHasPermission = false;
        if (isValidPerson(username)) {
            Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
            qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
            return kimPersonService.hasQualifiedRole(username, roleName, qualifiedRoleAttributes);
        }
        return false;
    }
    <commit_msg>KCIRB-39 : Add IRB Authorization (minor changes from code review)<commit_after>/*
 * Copyright 2006-2008 The Kuali Foundation
 * 
 * Licensed under the Educational Community 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.osedu.org/licenses/ECL-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.kuali.kra.irb.service.impl;

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

import org.kuali.kra.bo.Person;
import org.kuali.kra.bo.RolePersons;
import org.kuali.kra.infrastructure.Constants;
import org.kuali.kra.infrastructure.RoleConstants;
import org.kuali.kra.irb.bo.Protocol;
import org.kuali.kra.irb.service.ProtocolAuthorizationService;
import org.kuali.kra.kim.pojo.QualifiedRole;
import org.kuali.kra.kim.service.PersonService;
import org.kuali.kra.kim.service.QualifiedRoleService;
import org.kuali.kra.service.UnitAuthorizationService;

/**
 * The Protocol Authorization Service Implementation.
 *
 * @author Kuali Research Administration Team (<EMAIL>)
 */
public class ProtocolAuthorizationServiceImpl implements ProtocolAuthorizationService {
    
    private static final String PROTOCOL_KEY = "kra.protocol";
    
    private UnitAuthorizationService unitAuthorizationService;
    private PersonService kimPersonService;
    private QualifiedRoleService kimQualifiedRoleService;
    private org.kuali.kra.service.PersonService personService;

    /**
     * Set the Unit Authorization Service.  Injected by Spring.
     * @param unitAuthorizationService the Unit Authorization Service
     */
    public void setUnitAuthorizationService(UnitAuthorizationService unitAuthorizationService) {
        this.unitAuthorizationService = unitAuthorizationService;
    }
    
    /**
     * Set the KRA Person Service.  Injected by Spring.
     * @param personService the KRA Person Service
     */
    public void setPersonService(org.kuali.kra.service.PersonService personService) {
        this.personService = personService;
    }
    
    /**
     * Set the KIM Person Service.  Injected by Spring.
     * @param personService the KIM Person Service
     */
    public void setKimPersonService(PersonService personService) {
        this.kimPersonService = personService;
    }
    
    /**
     * Set the KIM Qualified Role Service.  Injected by Spring.
     * @param qualifiedRoleService the KIM Qualified Role Service
     */
    public void setKimQualifiedRoleService(QualifiedRoleService qualifiedRoleService) {
        this.kimQualifiedRoleService = qualifiedRoleService;
    }
    
    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#getUserNames(org.kuali.kra.protocoldevelopment.document.ProtocolDocument, java.lang.String)
     */
    public List<String> getUserNames(Protocol protocol, String roleName) {
        Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
        qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
        return kimQualifiedRoleService.getPersonUsernames(roleName, qualifiedRoleAttributes);
    }

    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#addRole(java.lang.String, java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument)
     */
    public void addRole(String username, String roleName, Protocol protocol) {
        Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
        qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
        kimPersonService.addQualifiedRole(username, roleName, qualifiedRoleAttributes);
    }

    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#removeRole(java.lang.String, java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument)
     */
    public void removeRole(String username, String roleName, Protocol protocol) {
        Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
        qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
        kimPersonService.removeQualifiedRole(username, roleName, qualifiedRoleAttributes);
    }
    
    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#hasPermission(java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument, java.lang.String)
     */
    public boolean hasPermission(String username, Protocol protocol, String permissionName) {
        boolean userHasPermission = false;
        if (isValidPerson(username)) {
            Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
            qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
            userHasPermission = kimPersonService.hasQualifiedPermission(username, Constants.KRA_NAMESPACE, permissionName, qualifiedRoleAttributes);
            if (!userHasPermission) {
                Person person = personService.getPersonByName(username);
                if (person != null) {
                    String unitNumber = person.getHomeUnit();
                    userHasPermission = unitAuthorizationService.hasPermission(username, unitNumber, permissionName);
                }
            }
        }
        return userHasPermission;
    }
   
    private boolean isValidPerson(String username) {
        return personService.isActiveByName(username);
    }

    /**
     * @see org.kuali.kra.protocoldevelopment.service.ProtocolAuthorizationService#hasRole(java.lang.String, org.kuali.kra.protocoldevelopment.document.ProtocolDocument, java.lang.String)
     */
    public boolean hasRole(String username, Protocol protocol, String roleName) {
        if (isValidPerson(username)) {
            Map<String, String> qualifiedRoleAttributes = new HashMap<String, String>();
            qualifiedRoleAttributes.put(PROTOCOL_KEY, protocol.getProtocolNumber());
            return kimPersonService.hasQualifiedRole(username, roleName, qualifiedRoleAttributes);
        }
        return false;
    }
    
{'fraction_non_alphanumeric': 0.085554961423879, 'fraction_numerical': 0.0019860973187686196, 'mean_word_length': 3.774617067833698, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '20887', 'n_tokens_mistral': 3455, 'n_tokens_neox': 3431, 'n_words': 773}