Coverage Report - org.kuali.student.security.spring.KSRiceDefaultUserDetailsService
 
Classes in this File Line Coverage Branch Coverage Complexity
KSRiceDefaultUserDetailsService
0%
0/42
0%
0/16
4.5
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.security.spring;
 17  
 
 18  
 import java.util.List;
 19  
 
 20  
 import org.kuali.rice.core.api.config.property.Config;
 21  
 import org.kuali.rice.core.api.config.property.ConfigContext;
 22  
 import org.kuali.rice.kim.api.identity.principal.Principal;
 23  
 import org.kuali.rice.kim.api.identity.IdentityService;
 24  
 import org.kuali.student.common.util.security.UserWithId;
 25  
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 26  
 import org.springframework.security.core.GrantedAuthority;
 27  
 import org.springframework.security.core.authority.AuthorityUtils;
 28  
 import org.springframework.security.core.userdetails.User;
 29  
 import org.springframework.security.core.userdetails.UserDetails;
 30  
 import org.springframework.security.core.userdetails.UserDetailsService;
 31  
 import org.springframework.security.core.userdetails.UsernameNotFoundException;
 32  
 
 33  
 /**
 34  
  * This is a description of what this class does. 
 35  
  * 
 36  
  * @author Kuali Student Team
 37  
  *
 38  
  */
 39  0
 public class KSRiceDefaultUserDetailsService implements UserDetailsService{
 40  
 
 41  0
     private UserWithId ksuser = null;
 42  0
     private String password = "";
 43  
    
 44  0
     private boolean enabled = true;
 45  0
     private boolean nonlocked = true;
 46  
             
 47  0
     private IdentityService identityService = null;
 48  
     
 49  
     // Spring Security requires roles to have a prefix of ROLE_ , 
 50  
     // look in org.springframework.security.vote.RoleVoter to change.
 51  0
     private List<GrantedAuthority> authorities = 
 52  
         AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_KS_ADMIN, ROLE_KS_USER");
 53  
     
 54  
     @Override
 55  
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 56  0
         if(username==null || username.equals("")){
 57  0
             throw new UsernameNotFoundException("Username cannot be null or empty");
 58  
         }
 59  
         
 60  0
         Config config = ConfigContext.getCurrentContextConfig();
 61  0
         String ksIgnoreRiceLogin = config.getProperty("ks.ignore.rice.login");
 62  
         
 63  
         // if property was not set in a config file then 
 64  
         // it will be null and it falls through to the identityService code.
 65  0
         if(Boolean.valueOf(ksIgnoreRiceLogin) == true){
 66  0
             return new User(username, password, enabled, true, true, nonlocked, authorities);
 67  
         }
 68  
         
 69  0
         Principal principal = null;
 70  0
         principal = identityService.getPrincipalByPrincipalName(username);
 71  
         
 72  
         String userId;
 73  0
         if (null != principal) {
 74  0
             username = principal.getPrincipalName();
 75  0
             userId = principal.getPrincipalId();
 76  
         } else {
 77  
         // When a UsernameNotFoundException is thrown, spring security will proceed to the next AuthenticationProvider on the list.
 78  
         // When Rice is running and username is not found in KIM, we want authentication to stop and allow the user to enter the correct username.
 79  
         // to do this we need to throw a AccountStatusException and not UsernameNotFoundException.
 80  0
             throw new KimUserNotFoundException("Invalid username or password");  
 81  
         }
 82  0
         ksuser = new UserWithId(username, password, enabled, true, true, nonlocked, authorities);
 83  0
         ksuser.setUserId(userId);
 84  0
         return ksuser;
 85  
     }
 86  
     
 87  
     public UserDetails loadUserByUsernameAndToken(String username, UsernamePasswordAuthenticationToken authentication) throws UsernameNotFoundException {
 88  0
         if(username==null || username.equals("")){
 89  0
             throw new UsernameNotFoundException("Username cannot be null or empty");
 90  
         }
 91  
         
 92  0
         Config config = ConfigContext.getCurrentContextConfig();
 93  0
         String ksIgnoreRiceLogin = config.getProperty("ks.ignore.rice.login");
 94  
         
 95  
         // if property was not set in a config file then 
 96  
         // it will be null and it falls through to the identityService code.
 97  0
         if(Boolean.valueOf(ksIgnoreRiceLogin) == true){
 98  0
             return null;
 99  
         }
 100  
         
 101  0
         password = (String)authentication.getCredentials();
 102  
         
 103  0
         Principal principal = null;
 104  
         
 105  0
         principal = identityService.getPrincipalByPrincipalNameAndPassword(username, password);
 106  
         String userId;
 107  0
         if (null != principal) {
 108  0
             username = principal.getPrincipalName();
 109  0
             userId = principal.getPrincipalId();
 110  
         } else {
 111  
         // When a UsernameNotFoundException is thrown, spring security will proceed to the next AuthenticationProvider on the list.
 112  
         // When Rice is running and username is not found in KIM, we want authentication to stop and allow the user to enter the correct username.
 113  
         // to do this we need to throw a AccountStatusException and not UsernameNotFoundException.
 114  
             //System.out.println("principal is null ");
 115  0
             throw new KimUserNotFoundException("Invalid username or password");  
 116  
         }
 117  0
         ksuser = new UserWithId(username, password, enabled, true, true, nonlocked, authorities);
 118  0
         ksuser.setUserId(userId);
 119  0
         return ksuser;
 120  
     }
 121  
     
 122  
     public void setAuthorities(String[] roles) {
 123  0
         this.authorities =  AuthorityUtils.createAuthorityList(roles);
 124  0
     }
 125  
 
 126  
     public void setIdentityService(IdentityService identityService) {
 127  0
         this.identityService = identityService;
 128  0
     }
 129  
 }