1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package net.sf.jguard.ext.authentication;
29
30 import java.util.HashSet;
31 import java.util.Set;
32 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
33 import net.sf.jguard.core.organization.Organization;
34 import net.sf.jguard.ext.authentication.manager.HibernateConverterUtils;
35 import net.sf.jguard.ext.organization.PersistedOrganization;
36 import net.sf.jguard.ext.util.SubjectUtils;
37
38
39
40
41
42 public class PersistedSubject {
43
44 private Set principals;
45 private Set publicCredentials;
46 private Set privateCredentials;
47 private Long id;
48 private PersistedOrganization persistedOrganization;
49 private String login = null;
50 private boolean active = true;
51 public static final String LOGIN ="login";
52 public static final String ACTIVE ="active";
53 public final static String PERSISTENCE_ID ="persistenceId";
54 public PersistedSubject(){
55
56 }
57
58 public PersistedSubject(javax.security.auth.Subject subject,PersistedOrganization organization){
59
60 String idToString = SubjectUtils.getCredentialValueAsString(subject, false, PERSISTENCE_ID);
61 if (idToString != null && !idToString.equals("")) {
62 id = new Long(idToString);
63 }
64
65 principals = HibernateConverterUtils.getPersistedPrincipals(subject.getPrincipals());
66 privateCredentials = subject.getPrivateCredentials(JGuardCredential.class);
67 privateCredentials.remove(new JGuardCredential(PERSISTENCE_ID, idToString));
68
69 login = SubjectUtils.getCredentialValueAsString(subject, false,LOGIN);
70 active = Boolean.valueOf(SubjectUtils.getCredentialValueAsString(subject, false,ACTIVE)).booleanValue();
71 privateCredentials.remove(new JGuardCredential(LOGIN,getLogin()));
72
73 publicCredentials = subject.getPublicCredentials(JGuardCredential.class);
74 persistedOrganization = organization;
75
76 }
77
78 public javax.security.auth.Subject toJavaxSecuritySubject(){
79 Set ppals = HibernateConverterUtils.getjavaSecurityPrincipals(principals);
80 if(id!=null && !id.toString().equals("0")){
81
82
83 JGuardCredential persistanceIdCredential = new JGuardCredential(PERSISTENCE_ID,id.toString());
84 privateCredentials.add(persistanceIdCredential);
85 }
86 Set clonedPrincipals = new HashSet(ppals);
87 clonedPrincipals.add(persistedOrganization.toOrganization());
88 HashSet privCredentials = new HashSet(privateCredentials);
89 privCredentials.add(new JGuardCredential(LOGIN,getLogin()));
90 privCredentials.add(new JGuardCredential(ACTIVE,Boolean.toString(active)));
91 javax.security.auth.Subject subject = new javax.security.auth.Subject(false,clonedPrincipals,publicCredentials,privCredentials);
92 return subject;
93 }
94
95 Set getPrincipals() {
96 return principals;
97 }
98
99 public void setPrincipals(Set principals) {
100 this.principals = principals;
101 }
102
103 public Set getPublicCredentials() {
104 return publicCredentials;
105 }
106
107 public void setPublicCredentials(Set publicCredentials) {
108 this.publicCredentials = publicCredentials;
109 }
110
111 public Set getPrivateCredentials() {
112 return privateCredentials;
113 }
114
115 public void setPrivateCredentials(Set privateCredentials) {
116 this.privateCredentials = privateCredentials;
117 }
118
119 public Long getId() {
120 return id;
121 }
122
123
124 private void setId(Long id) {
125 this.id = id;
126 }
127
128 public Organization getOrganization() {
129 return persistedOrganization;
130 }
131
132 public void setOrganization(PersistedOrganization organization) {
133 this.persistedOrganization = organization;
134 }
135
136 public int hashCode() {
137 int hash = 3;
138 hash = 79 * hash + (this.principals != null ? this.principals.hashCode() : 0);
139 hash = 79 * hash + (this.publicCredentials != null ? this.publicCredentials.hashCode() : 0);
140 hash = 79 * hash + (this.privateCredentials != null ? this.privateCredentials.hashCode() : 0);
141 hash = 79 * hash + (this.persistedOrganization != null ? this.persistedOrganization.hashCode() : 0);
142 hash = 79 * hash + (this.getLogin()!= null ? this.getLogin().hashCode() : 0);
143 return hash;
144 }
145
146 public boolean equals(Object other) {
147 if (this == other){
148 return true;
149 }
150 if ( !(other instanceof PersistedSubject) ){
151 return false;
152 }
153
154 final PersistedSubject psubject = (PersistedSubject)other;
155 if(principals.equals(psubject.getPrincipals())&&
156 privateCredentials.equals(psubject.getPrivateCredentials())&&
157 publicCredentials.equals(psubject.getPublicCredentials())&&
158 persistedOrganization.equals(psubject.getOrganization())){
159 return true;
160 }
161 return false;
162 }
163
164 public String getLogin() {
165 return login;
166 }
167
168 public void setLogin(String login) {
169 this.login = login;
170 }
171
172 public boolean isActive() {
173 return active;
174 }
175
176 public void setActive(boolean active) {
177 this.active = active;
178 }
179
180 }