Coverage Report - org.kuali.student.security.spring.KSDefaultUserDetailsService
 
Classes in this File Line Coverage Branch Coverage Complexity
KSDefaultUserDetailsService
0%
0/25
0%
0/6
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 org.kuali.rice.kim.bo.entity.dto.KimPrincipalInfo;
 19  
 import org.kuali.rice.kim.service.IdentityService;
 20  
 import org.kuali.student.common.util.security.UserWithId;
 21  
 import org.springframework.security.GrantedAuthority;
 22  
 import org.springframework.security.userdetails.User;
 23  
 import org.springframework.security.userdetails.UserDetails;
 24  
 import org.springframework.security.userdetails.UserDetailsService;
 25  
 import org.springframework.security.userdetails.UsernameNotFoundException;
 26  
 import org.springframework.security.util.AuthorityUtils;
 27  
 import org.kuali.student.common.util.security.UserWithId;
 28  
 
 29  
 
 30  
 /**
 31  
  * This is a description of what this class does - Rich don't forget to fill this in. 
 32  
  * 
 33  
  * @author Kuali Student Team
 34  
  *
 35  
  */
 36  0
 public class KSDefaultUserDetailsService implements UserDetailsService{
 37  
 
 38  0
     private UserWithId ksuser = null;
 39  0
     private String password = "";
 40  
    
 41  0
     private boolean enabled = true;
 42  0
     private boolean nonlocked = true;
 43  0
     private IdentityService identityService = null; // This is added so we can get the correct principal ID
 44  
     
 45  
     
 46  
     // Spring Security requires roles to have a prefix of ROLE_ , 
 47  
     // look in org.springframework.security.vote.RoleVoter to change.
 48  0
     private GrantedAuthority[] authorities = 
 49  
         AuthorityUtils.commaSeparatedStringToAuthorityArray("ROLE_KS_ADMIN, ROLE_KS_USER");
 50  
     
 51  
     public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
 52  
         
 53  0
         if(username==null || username.equals("")){
 54  0
             throw new UsernameNotFoundException("Username cannot be null or empty");
 55  
         }
 56  0
         password = username;
 57  
         
 58  
         //ksuser = new User(username, password, enabled, true, true, nonlocked, authorities);
 59  
         
 60  0
         KimPrincipalInfo kimPrincipalInfo = null;
 61  
         try {
 62  0
         kimPrincipalInfo = identityService.getPrincipalByPrincipalName(username);
 63  0
         }catch(Exception e)
 64  
         {
 65  0
                 System.out.println("This is the error" + e.getMessage());
 66  0
                                 return new User(username, password, enabled, true, true, nonlocked, authorities);
 67  0
         }
 68  
         
 69  
         
 70  
         String userId;
 71  0
         if (null != kimPrincipalInfo) {
 72  0
             username = kimPrincipalInfo.getPrincipalName();
 73  0
             userId = kimPrincipalInfo.getPrincipalId();
 74  
         } else {
 75  
         // When a UsernameNotFoundException is thrown, spring security will proceed to the next AuthenticationProvider on the list.
 76  
         // 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.
 77  
         // to do this we need to throw a AccountStatusException and not UsernameNotFoundException.
 78  
             //System.out.println("kimPrincipalInfo is null ");
 79  0
             throw new KimUserNotFoundException("Invalid username or password");  
 80  
         }
 81  0
         ksuser = new UserWithId(username, password, enabled, true, true, nonlocked, authorities);
 82  0
         ksuser.setUserId(userId);
 83  
         
 84  
         
 85  
         
 86  
         
 87  0
         return ksuser;
 88  
     }
 89  
     
 90  
     public void setAuthorities(String[] roles) {
 91  0
         this.authorities =  AuthorityUtils.stringArrayToAuthorityArray(roles);
 92  0
     }
 93  
 }