View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * JPA-annotated user reference object.
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    * No-arg constructor needed by JPA
131    */
132   public JpaUserReference() {
133   }
134 
135   /**
136    * Constructs a user with the specified username, name, email, login mechanism, last login date and organization.
137    *
138    * @param username
139    *          the username
140    * @param name
141    *          the name
142    * @param email
143    *          the email address
144    * @param loginMechanism
145    *          the login mechanism
146    * @param lastLogin
147    *          the last login date
148    * @param organization
149    *          the organization
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    * Constructs a user with the specified username, name, email, login mechanism, last login date, organization and
165    * roles.
166    *
167    * @param username
168    *          the username
169    * @param name
170    *          the name
171    * @param email
172    *          the email address
173    * @param loginMechanism
174    *          the login mechanism
175    * @param lastLogin
176    *          the last login date
177    * @param organization
178    *          the organization
179    * @param roles
180    *          the roles
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    * {@inheritDoc}
247    *
248    * @see java.lang.Object#equals(java.lang.Object)
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    * {@inheritDoc}
261    *
262    * @see java.lang.Object#hashCode()
263    */
264   @Override
265   public int hashCode() {
266     return EqualsUtil.hash(username, organization);
267   }
268 
269   /**
270    * {@inheritDoc}
271    *
272    * @see java.lang.Object#toString()
273    */
274   @Override
275   public String toString() {
276     return new StringBuilder(username).append(":").append(organization).toString();
277   }
278 
279 }