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  
22  package org.opencastproject.security.api;
23  
24  import org.opencastproject.util.EqualsUtil;
25  
26  import org.apache.commons.lang3.StringUtils;
27  
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.HashSet;
32  import java.util.Objects;
33  import java.util.Set;
34  
35  import javax.xml.bind.annotation.XmlAccessType;
36  import javax.xml.bind.annotation.XmlAccessorType;
37  import javax.xml.bind.annotation.XmlElement;
38  import javax.xml.bind.annotation.XmlElementWrapper;
39  import javax.xml.bind.annotation.XmlRootElement;
40  import javax.xml.bind.annotation.XmlTransient;
41  import javax.xml.bind.annotation.XmlType;
42  
43  /**
44   * A simple user model.
45   */
46  @XmlAccessorType(XmlAccessType.FIELD)
47  @XmlType(name = "user", namespace = "http://org.opencastproject.security", propOrder = { "userName", "name", "email",
48          "provider", "isManageable", "roles", "organization" })
49  @XmlRootElement(name = "user", namespace = "http://org.opencastproject.security")
50  public final class JaxbUser implements User {
51  
52    /** The user name */
53    @XmlElement(name = "username")
54    protected String userName;
55  
56    /** The user's name */
57    @XmlElement(name = "name")
58    protected String name;
59  
60    /** The user's email address */
61    @XmlElement(name = "email")
62    protected String email;
63  
64    /** The user's provider */
65    @XmlElement(name = "provider")
66    protected String provider;
67  
68    @XmlElement(name = "manageable")
69    protected boolean isManageable = false;
70  
71    /** The roles */
72    @XmlElement(name = "role")
73    @XmlElementWrapper(name = "roles")
74    protected Set<JaxbRole> roles;
75  
76    /** The optional password. Note that this will never be serialized to xml */
77    @XmlTransient
78    protected String password;
79  
80    /** The user's home organization identifier */
81    @XmlElement(name = "organization")
82    protected JaxbOrganization organization;
83  
84    /**
85     * No-arg constructor needed by JAXB
86     */
87    public JaxbUser() {
88    }
89  
90    /**
91     * Constructs a user which is a member of the given organization that has the specified roles and no password set.
92     *
93     * @param userName
94     *          the username
95     * @param provider
96     *          the provider
97     * @param organization
98     *          the organization
99     * @param roles
100    *          the set of roles for this user
101    * @throws IllegalArgumentException
102    *           if <code>userName</code> or <code>organization</code> is <code>null</code>
103    */
104   public JaxbUser(String userName, String provider, JaxbOrganization organization, JaxbRole... roles)
105           throws IllegalArgumentException {
106     this(userName, provider, organization, new HashSet<JaxbRole>(Arrays.asList(roles)));
107   }
108 
109   /**
110    * Constructs a user which is a member of the given organization that has the specified roles.
111    *
112    * @param userName
113    *          the username
114    * @param password
115    *          the password
116    * @param provider
117    *          the provider
118    * @param organization
119    *          the organization
120    * @param roles
121    *          the set of roles for this user
122    * @throws IllegalArgumentException
123    *           if <code>userName</code> or <code>organization</code> is <code>null</code>
124    */
125   public JaxbUser(String userName, String password, String provider, JaxbOrganization organization, JaxbRole... roles)
126           throws IllegalArgumentException {
127     this(userName, password, provider, organization, new HashSet<JaxbRole>(Arrays.asList(roles)));
128   }
129 
130   /**
131    * Constructs a user which is a member of the given organization that has the specified roles.
132    *
133    * @param userName
134    *          the username
135    * @param password
136    *          the password
137    * @param provider
138    *          the provider
139    * @param organization
140    *          the organization
141    * @param roles
142    *          the set of roles for this user
143    * @throws IllegalArgumentException
144    *           if <code>userName</code> or <code>organization</code> is <code>null</code>
145    */
146   public JaxbUser(String userName, String password, String provider, JaxbOrganization organization, Set<JaxbRole> roles)
147           throws IllegalArgumentException {
148     this(userName, password, null, null, provider, organization, roles);
149   }
150 
151   /**
152    * Constructs a user which is a member of the given organization that has the specified roles and no password set.
153    *
154    * @param userName
155    *          the username
156    * @param provider
157    *          the provider
158    * @param organization
159    *          the organization
160    * @param roles
161    *          the set of roles for this user
162    * @throws IllegalArgumentException
163    *           if <code>userName</code> or <code>organization</code> is <code>null</code>
164    */
165   public JaxbUser(String userName, String provider, JaxbOrganization organization, Set<JaxbRole> roles)
166           throws IllegalArgumentException {
167     this(userName, null, provider, organization, roles);
168   }
169 
170   /**
171    * Constructs a user which is a member of the given organization that has the specified roles.
172    *
173    * @param userName
174    *          the username
175    * @param password
176    *          the password
177    * @param name
178    *          the name
179    * @param email
180    *          the email
181    * @param provider
182    *          the provider
183    * @param organization
184    *          the organization
185    * @param roles
186    *          the set of roles for this user
187    * @throws IllegalArgumentException
188    *           if <code>userName</code> or <code>organization</code> is <code>null</code>
189    */
190   public JaxbUser(String userName, String password, String name, String email, String provider,
191           JaxbOrganization organization, Set<JaxbRole> roles) throws IllegalArgumentException {
192     if (StringUtils.isBlank(userName))
193       throw new IllegalArgumentException("Username must be set");
194     if (organization == null)
195       throw new IllegalArgumentException("Organization must be set");
196     this.userName = userName;
197     this.password = password;
198     this.name = name;
199     this.email = email;
200     this.provider = provider;
201     this.organization = organization;
202     if (roles == null) {
203       this.roles = new HashSet<>();
204     } else {
205       for (Role role : roles) {
206         if (!Objects.equals(organization.getId(), role.getOrganizationId())) {
207           throw new IllegalArgumentException("Role " + role + " is not from the same organization!");
208         }
209       }
210       this.roles = roles;
211     }
212   }
213 
214   /**
215    * Creates a JAXB user from a regular user object.
216    *
217    * @param user
218    *          the user
219    * @return the JAXB user
220    */
221   public static JaxbUser fromUser(User user) {
222     if (user instanceof JaxbUser)
223       return (JaxbUser) user;
224     return fromUser(user, Collections.<JaxbRole> emptySet());
225   }
226 
227   /**
228    * Creates a JAXB user from a regular user object with an additional set of roles.
229    *
230    * @param user
231    *          the user
232    * @return the JAXB user
233    */
234   public static JaxbUser fromUser(User user, Collection<? extends Role> extraRoles) {
235     Set<JaxbRole> roles = new HashSet<JaxbRole>();
236     for (Role role : user.getRoles()) {
237       roles.add(JaxbRole.fromRole(role));
238     }
239     for (Role role : extraRoles) {
240       roles.add(JaxbRole.fromRole(role));
241     }
242 
243     JaxbUser jaxbUser = new JaxbUser(user.getUsername(), user.getPassword(), user.getName(), user.getEmail(),
244             user.getProvider(), JaxbOrganization.fromOrganization(user.getOrganization()), roles);
245     jaxbUser.setManageable(user.isManageable());
246     return jaxbUser;
247   }
248 
249   /**
250    * @see org.opencastproject.security.api.User#getUsername()
251    */
252   @Override
253   public String getUsername() {
254     return userName;
255   }
256 
257   /**
258    * @see org.opencastproject.security.api.User#getPassword()
259    */
260   @Override
261   public String getPassword() {
262     return password;
263   }
264 
265   /**
266    * @see org.opencastproject.security.api.User#getOrganization()
267    */
268   @Override
269   public Organization getOrganization() {
270     return organization;
271   }
272 
273   /**
274    * @see org.opencastproject.security.api.User#getRoles()
275    */
276   @Override
277   public Set<Role> getRoles() {
278     return new HashSet<>(roles);
279   }
280 
281   /**
282    * @see org.opencastproject.security.api.User#hasRole(java.lang.String)
283    */
284   @Override
285   public boolean hasRole(String roleName) {
286     for (Role role : roles) {
287       if (role.getName().equals(roleName))
288         return true;
289     }
290     return false;
291   }
292 
293   /**
294    * {@inheritDoc}
295    *
296    * @see java.lang.Object#equals(java.lang.Object)
297    */
298   @Override
299   public boolean equals(Object obj) {
300     if (!(obj instanceof User))
301       return false;
302     User other = (User) obj;
303     return userName.equals(other.getUsername()) && organization.equals(other.getOrganization());
304   }
305 
306   /**
307    * {@inheritDoc}
308    *
309    * @see java.lang.Object#hashCode()
310    */
311   @Override
312   public int hashCode() {
313     return EqualsUtil.hash(userName, organization, provider);
314   }
315 
316   /**
317    * {@inheritDoc}
318    *
319    * @see java.lang.Object#toString()
320    */
321   @Override
322   public String toString() {
323     return String.format("%s:%s:%s", userName, organization, provider);
324   }
325 
326   @Override
327   public String getName() {
328     return name;
329   }
330 
331   @Override
332   public String getEmail() {
333     return email;
334   }
335 
336   @Override
337   public String getProvider() {
338     return provider;
339   }
340 
341   public void setProvider(String provider) {
342     this.provider = provider;
343   }
344 
345   @Override
346   public boolean isManageable() {
347     return isManageable;
348   }
349 
350   public void setManageable(boolean isManageable) {
351     this.isManageable = isManageable;
352   }
353 
354 }