1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.opencastproject.security.impl.jpa;
22
23 import org.opencastproject.security.api.JaxbOrganization;
24 import org.opencastproject.security.api.JaxbRole;
25 import org.opencastproject.security.api.JaxbUser;
26 import org.opencastproject.security.api.Organization;
27 import org.opencastproject.security.api.Role;
28 import org.opencastproject.security.api.User;
29 import org.opencastproject.util.EqualsUtil;
30
31 import java.util.Date;
32 import java.util.HashSet;
33 import java.util.Objects;
34 import java.util.Set;
35
36 import javax.persistence.Access;
37 import javax.persistence.AccessType;
38 import javax.persistence.CascadeType;
39 import javax.persistence.Column;
40 import javax.persistence.Entity;
41 import javax.persistence.FetchType;
42 import javax.persistence.GeneratedValue;
43 import javax.persistence.Id;
44 import javax.persistence.JoinColumn;
45 import javax.persistence.JoinTable;
46 import javax.persistence.ManyToMany;
47 import javax.persistence.NamedQueries;
48 import javax.persistence.NamedQuery;
49 import javax.persistence.OneToOne;
50 import javax.persistence.Table;
51 import javax.persistence.Temporal;
52 import javax.persistence.TemporalType;
53 import javax.persistence.UniqueConstraint;
54
55
56
57
58 @Entity
59 @Access(AccessType.FIELD)
60 @Table(
61 name = "oc_user_ref",
62 uniqueConstraints = {
63 @UniqueConstraint(name = "UNQ_oc_user_ref", columnNames = { "username", "organization" }),
64 }
65 )
66 @NamedQueries({
67 @NamedQuery(
68 name = "UserReference.findByQuery",
69 query = "select u from JpaUserReference u where UPPER(u.username) like :query and u.organization.id = :org"
70 ),
71 @NamedQuery(
72 name = "UserReference.findByUsername",
73 query = "select u from JpaUserReference u where u.username=:u and u.organization.id = :org"
74 ),
75 @NamedQuery(
76 name = "UserReference.findAll",
77 query = "select u from JpaUserReference u where u.organization.id = :org"
78 ),
79 @NamedQuery(
80 name = "UserReference.findAllByUserNames",
81 query = "select u from JpaUserReference u where u.organization.id = :org and u.username in :names"
82 ),
83 @NamedQuery(
84 name = "UserReference.countAll",
85 query = "select COUNT(u) from JpaUserReference u where u.organization.id = :org"
86 ),
87 })
88 public class JpaUserReference {
89 @Id
90 @GeneratedValue
91 @Column(name = "id")
92 private Long id;
93
94 @Column(name = "username", length = 128)
95 protected String username;
96
97 @Column(name = "name")
98 protected String name;
99
100 @Column(name = "email")
101 protected String email;
102
103 @Column(name = "login_mechanism")
104 protected String loginMechanism;
105
106 @Column(name = "last_login")
107 @Temporal(TemporalType.TIMESTAMP)
108 protected Date lastLogin;
109
110 @OneToOne()
111 @JoinColumn(name = "organization")
112 protected JpaOrganization organization;
113
114 @ManyToMany(cascade = { CascadeType.MERGE }, fetch = FetchType.LAZY)
115 @JoinTable(name = "oc_user_ref_role", joinColumns = {
116 @JoinColumn(name = "user_id") }, inverseJoinColumns = {
117 @JoinColumn(name = "role_id") })
118 protected Set<JpaRole> roles;
119
120 public User toUser(final String providerName) {
121 Set<JaxbRole> roleSet = new HashSet<JaxbRole>();
122 for (JpaRole role : roles) {
123 roleSet.add(JaxbRole.fromRole(role));
124 }
125 return new JaxbUser(username, null, name, email, providerName, JaxbOrganization.fromOrganization(organization),
126 roleSet);
127 }
128
129
130
131
132 public JpaUserReference() {
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151 public JpaUserReference(String username, String name, String email, String loginMechanism, Date lastLogin,
152 JpaOrganization organization) {
153 super();
154 this.username = username;
155 this.name = name;
156 this.email = email;
157 this.loginMechanism = loginMechanism;
158 this.lastLogin = lastLogin;
159 this.organization = organization;
160 this.roles = new HashSet<JpaRole>();
161 }
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182 public JpaUserReference(String username, String name, String email, String loginMechanism, Date lastLogin,
183 JpaOrganization organization, Set<JpaRole> roles) {
184 this(username, name, email, loginMechanism, lastLogin, organization);
185 for (Role role : roles) {
186 if (!Objects.equals(organization.getId(), role.getOrganizationId())) {
187 throw new IllegalArgumentException("Role " + role + " is not from the same organization!");
188 }
189 }
190 this.roles = roles;
191 }
192
193 public void setUsername(String username) {
194 this.username = username;
195 }
196
197 public String getUsername() {
198 return username;
199 }
200
201 public void setName(String name) {
202 this.name = name;
203 }
204
205 public String getName() {
206 return name;
207 }
208
209 public void setEmail(String email) {
210 this.email = email;
211 }
212
213 public String getEmail() {
214 return email;
215 }
216
217 public void setLoginMechanism(String loginMechanism) {
218 this.loginMechanism = loginMechanism;
219 }
220
221 public String getLoginMechanism() {
222 return loginMechanism;
223 }
224
225 public void setLastLogin(Date lastLogin) {
226 this.lastLogin = lastLogin;
227 }
228
229 public Date getLastLogin() {
230 return lastLogin;
231 }
232
233 public Organization getOrganization() {
234 return organization;
235 }
236
237 public void setRoles(Set<JpaRole> roles) {
238 this.roles = roles;
239 }
240
241 public Set<Role> getRoles() {
242 return new HashSet<Role>(roles);
243 }
244
245
246
247
248
249
250 @Override
251 public boolean equals(Object obj) {
252 if (!(obj instanceof JpaUserReference)) {
253 return false;
254 }
255 JpaUserReference other = (JpaUserReference) obj;
256 return username.equals(other.getUsername()) && organization.equals(other.getOrganization());
257 }
258
259
260
261
262
263
264 @Override
265 public int hashCode() {
266 return EqualsUtil.hash(username, organization);
267 }
268
269
270
271
272
273
274 @Override
275 public String toString() {
276 return new StringBuilder(username).append(":").append(organization).toString();
277 }
278
279 }