Coverage Report - org.acegisecurity.acls.sid.PrincipalSid
 
Classes in this File Line Coverage Branch Coverage Complexity
PrincipalSid
88% 
100% 
1.6
 
 1  
 /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
 2  
  *
 3  
  * Licensed under the Apache License, Version 2.0 (the "License");
 4  
  * you may not use this file except in compliance with the License.
 5  
  * You may obtain a copy of the License at
 6  
  *
 7  
  *     http://www.apache.org/licenses/LICENSE-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing, software
 10  
  * distributed under the License is distributed on an "AS IS" BASIS,
 11  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
  * See the License for the specific language governing permissions and
 13  
  * limitations under the License.
 14  
  */
 15  
 package org.acegisecurity.acls.sid;
 16  
 
 17  
 import org.acegisecurity.Authentication;
 18  
 
 19  
 import org.acegisecurity.userdetails.UserDetails;
 20  
 
 21  
 import org.springframework.util.Assert;
 22  
 
 23  
 
 24  
 /**
 25  
  * Represents an <code>Authentication.getPrincipal()</code> as a <code>Sid</code>.<p>This is a basic implementation
 26  
  * that simply uses the <code>String</code>-based principal for <code>Sid</code> comparison. More complex principal
 27  
  * objects may wish to provide an alternative <code>Sid</code> implementation that uses some other identifier.</p>
 28  
  *
 29  
  * @author Ben Alex
 30  
  * @version $Id: PrincipalSid.java 1754 2006-11-17 02:01:21Z benalex $
 31  
  */
 32  
 public class PrincipalSid implements Sid {
 33  
     //~ Instance fields ================================================================================================
 34  
 
 35  
     private String principal;
 36  
 
 37  
     //~ Constructors ===================================================================================================
 38  
 
 39  19
     public PrincipalSid(String principal) {
 40  19
         Assert.hasText(principal, "Principal required");
 41  19
         this.principal = principal;
 42  19
     }
 43  
 
 44  34
     public PrincipalSid(Authentication authentication) {
 45  34
         Assert.notNull(authentication, "Authentication required");
 46  34
         Assert.notNull(authentication.getPrincipal(), "Principal required");
 47  34
         this.principal = authentication.getPrincipal().toString();
 48  
 
 49  34
         if (authentication.getPrincipal() instanceof UserDetails) {
 50  0
             this.principal = ((UserDetails) authentication.getPrincipal()).getUsername();
 51  
         }
 52  34
     }
 53  
 
 54  
     //~ Methods ========================================================================================================
 55  
 
 56  
     public boolean equals(Object object) {
 57  22
         if ((object == null) || !(object instanceof PrincipalSid)) {
 58  0
             return false;
 59  
         }
 60  
 
 61  
         // Delegate to getPrincipal() to perform actual comparison (both should be identical) 
 62  22
         return ((PrincipalSid) object).getPrincipal().equals(this.getPrincipal());
 63  
     }
 64  
 
 65  
     public String getPrincipal() {
 66  63
         return principal;
 67  
     }
 68  
 
 69  
     public String toString() {
 70  3
         return "PrincipalSid[" + this.principal + "]";
 71  
     }
 72  
 }