| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| SecurityUtils | 
 | 
 | 3.0;3 | 
| 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.common.util.security; | |
| 17 | ||
| 18 |  import org.springframework.security.Authentication; | |
| 19 |  import org.springframework.security.context.SecurityContextHolder; | |
| 20 |  import org.springframework.security.userdetails.UserDetails; | |
| 21 | ||
| 22 | 0 |  public class SecurityUtils { | 
| 23 | ||
| 24 |          /**  | |
| 25 |           * This can be used to get the current user's principal id from security context | |
| 26 |           *  | |
| 27 |           * @return principal id | |
| 28 |           */ | |
| 29 | public static String getCurrentPrincipalId() { | |
| 30 | 0 |          String principalID=null; | 
| 31 | ||
| 32 | 0 |          Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | 
| 33 | ||
| 34 | 0 |                  if(auth!=null){ | 
| 35 | 0 |                  Object obj = auth.getPrincipal(); | 
| 36 | 0 |                  if(obj instanceof UserWithId){ | 
| 37 |                          //This is actually the user's Principal Id | |
| 38 | 0 |                          principalID = ((UserWithId)obj).getUserId(); | 
| 39 | } | |
| 40 | } | |
| 41 | 0 |                  return principalID; | 
| 42 | } | |
| 43 | ||
| 44 |          /** | |
| 45 |           * This can be used to get the current user's principal name from security context | |
| 46 |           *  | |
| 47 |           * @return principal name | |
| 48 |           */ | |
| 49 | public static String getCurrentPrincipalName(){ | |
| 50 | 0 |                  String username = "unknown"; | 
| 51 | ||
| 52 | 0 |                  Authentication auth = SecurityContextHolder.getContext().getAuthentication(); | 
| 53 | ||
| 54 | 0 |                  if (auth != null) { | 
| 55 | 0 |                          Object obj = auth.getPrincipal(); | 
| 56 | 0 |                      if (obj instanceof UserDetails) { | 
| 57 | 0 |                              username = ((UserDetails)obj).getUsername(); | 
| 58 |                      } else { | |
| 59 | 0 |                          username = obj.toString(); | 
| 60 | } | |
| 61 | } | |
| 62 | ||
| 63 | 0 |              return username; | 
| 64 | } | |
| 65 | ||
| 66 | } |