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.userdirectory.ldap;
23  
24  import org.springframework.security.core.GrantedAuthority;
25  import org.springframework.security.core.authority.AuthorityUtils;
26  import org.springframework.security.core.userdetails.UserDetails;
27  
28  import java.util.ArrayList;
29  import java.util.Collection;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.Objects;
33  
34  import javax.naming.Name;
35  
36  public class OpencastUserDetails implements UserDetails {
37  
38    private String dn;
39  
40    private String password;
41  
42    private String username;
43  
44    private String name;
45  
46    private String mail;
47  
48    private Collection<GrantedAuthority> authorities = AuthorityUtils.NO_AUTHORITIES;
49    private boolean accountNonExpired = true;
50    private boolean accountNonLocked = true;
51    private boolean credentialsNonExpired = true;
52    private boolean enabled = true;
53  
54    protected OpencastUserDetails() {
55    }
56  
57    @Override
58    public boolean equals(Object obj) {
59      if (obj instanceof OpencastUserDetails) {
60        return dn.equals(((OpencastUserDetails) obj).dn);
61      }
62      return false;
63    }
64  
65    @Override
66    public int hashCode() {
67      return dn.hashCode();
68    }
69  
70    public String toString() {
71      StringBuilder sb = new StringBuilder();
72      sb.append(super.toString()).append(": ");
73      sb.append("Dn: ").append(dn).append("; ");
74      sb.append("Username: ").append(this.username).append("; ");
75      sb.append("Password: [PROTECTED]; ");
76      sb.append("Name: ").append(this.name);
77      sb.append("Mail: ").append(this.mail);
78      sb.append("Enabled: ").append(this.enabled).append("; ");
79      sb.append("AccountNonExpired: ").append(this.accountNonExpired).append("; ");
80      sb.append("CredentialsNonExpired: ").append(this.credentialsNonExpired).append("; ");
81      sb.append("AccountNonLocked: ").append(this.accountNonLocked).append("; ");
82  
83      if (this.getAuthorities() != null) {
84        sb.append("Granted Authorities: ");
85        boolean first = true;
86  
87        for (Object authority : this.getAuthorities()) {
88          if (first) {
89            first = false;
90          } else {
91            sb.append(", ");
92          }
93  
94          sb.append(authority.toString());
95        }
96      } else {
97        sb.append("Not granted any authorities");
98      }
99  
100     return sb.toString();
101   }
102 
103   public String getDn() {
104     return dn;
105   }
106 
107   @Override
108   public Collection<? extends GrantedAuthority> getAuthorities() {
109     return authorities;
110   }
111 
112   @Override
113   public String getPassword() {
114     return password;
115   }
116 
117   @Override
118   public String getUsername() {
119     return username;
120   }
121 
122   public String getMail() {
123     return mail;
124   }
125 
126   public String getName() {
127     return name;
128   }
129 
130   @Override
131   public boolean isAccountNonExpired() {
132     return accountNonExpired;
133   }
134 
135   @Override
136   public boolean isAccountNonLocked() {
137     return accountNonLocked;
138   }
139 
140   @Override
141   public boolean isCredentialsNonExpired() {
142     return credentialsNonExpired;
143   }
144 
145   @Override
146   public boolean isEnabled() {
147     return enabled;
148   }
149 
150   public static class Essence {
151     protected OpencastUserDetails instance = createTarget();
152     private List<GrantedAuthority> mutableAuthorities = new ArrayList<>();
153 
154     public Essence() {
155     }
156 
157     protected OpencastUserDetails createTarget() {
158       return new OpencastUserDetails();
159     }
160 
161     /**
162      * Adds the authority to the list, unless it is already there, in which case it is ignored
163      */
164     public void addAuthority(GrantedAuthority a) {
165       if (!hasAuthority(a)) {
166         mutableAuthorities.add(a);
167       }
168     }
169 
170     private boolean hasAuthority(GrantedAuthority a) {
171       for (GrantedAuthority authority : mutableAuthorities) {
172         if (authority.equals(a)) {
173           return true;
174         }
175       }
176       return false;
177     }
178 
179     public OpencastUserDetails createUserDetails() {
180       Objects.requireNonNull(instance,"Essence can only be used to create a single instance");
181       Objects.requireNonNull(instance, "Essence can only be used to create a single instance");
182       Objects.requireNonNull(instance.username, "username must not be null");
183       Objects.requireNonNull(instance.getDn(), "Distinguished name must not be null");
184 
185       instance.authorities = Collections.unmodifiableList(mutableAuthorities);
186 
187       OpencastUserDetails newInstance = instance;
188 
189       instance = null;
190 
191       return newInstance;
192     }
193 
194     public Collection<GrantedAuthority> getGrantedAuthorities() {
195       return mutableAuthorities;
196     }
197 
198     public void setAccountNonExpired(boolean accountNonExpired) {
199       instance.accountNonExpired = accountNonExpired;
200     }
201 
202     public void setAccountNonLocked(boolean accountNonLocked) {
203       instance.accountNonLocked = accountNonLocked;
204     }
205 
206     public void setAuthorities(Collection<? extends GrantedAuthority> authorities) {
207       mutableAuthorities = new ArrayList<>();
208       mutableAuthorities.addAll(authorities);
209     }
210 
211     public void setCredentialsNonExpired(boolean credentialsNonExpired) {
212       instance.credentialsNonExpired = credentialsNonExpired;
213     }
214 
215     public void setDn(String dn) {
216       instance.dn = dn;
217     }
218 
219     public void setDn(Name dn) {
220       instance.dn = dn.toString();
221     }
222 
223     public void setEnabled(boolean enabled) {
224       instance.enabled = enabled;
225     }
226 
227     public void setPassword(String password) {
228       instance.password = password;
229     }
230 
231     public void setUsername(String username) {
232       instance.username = username;
233     }
234 
235     public void setName(String name) {
236       instance.name = name;
237     }
238 
239     public void setMail(String mail) {
240       instance.mail = mail;
241     }
242 
243   }
244 }