01: /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * 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: * Represents an <code>Authentication.getPrincipal()</code> as a <code>Sid</code>.<p>This is a basic implementation
25: * that simply uses the <code>String</code>-based principal for <code>Sid</code> comparison. More complex principal
26: * objects may wish to provide an alternative <code>Sid</code> implementation that uses some other identifier.</p>
27: *
28: * @author Ben Alex
29: * @version $Id: PrincipalSid.java 1754 2006-11-17 02:01:21Z benalex $
30: */
31: public class PrincipalSid implements Sid {
32: //~ Instance fields ================================================================================================
33:
34: private String principal;
35:
36: //~ Constructors ===================================================================================================
37:
38: public PrincipalSid(String principal) {
39: Assert.hasText(principal, "Principal required");
40: this .principal = principal;
41: }
42:
43: public PrincipalSid(Authentication authentication) {
44: Assert.notNull(authentication, "Authentication required");
45: Assert.notNull(authentication.getPrincipal(),
46: "Principal required");
47: this .principal = authentication.getPrincipal().toString();
48:
49: if (authentication.getPrincipal() instanceof UserDetails) {
50: this .principal = ((UserDetails) authentication
51: .getPrincipal()).getUsername();
52: }
53: }
54:
55: //~ Methods ========================================================================================================
56:
57: public boolean equals(Object object) {
58: if ((object == null) || !(object instanceof PrincipalSid)) {
59: return false;
60: }
61:
62: // Delegate to getPrincipal() to perform actual comparison (both should be identical)
63: return ((PrincipalSid) object).getPrincipal().equals(
64: this .getPrincipal());
65: }
66:
67: public String getPrincipal() {
68: return principal;
69: }
70:
71: public String toString() {
72: return "PrincipalSid[" + this .principal + "]";
73: }
74: }
|