001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.users;
019
020import com.fasterxml.jackson.annotation.JsonView;
021import io.stallion.dataAccess.AlternativeKey;
022import io.stallion.dataAccess.ModelBase;
023import io.stallion.dataAccess.UniqueKey;
024import io.stallion.dataAccess.db.DefaultSort;
025import io.stallion.dataAccess.db.ExtraKeyDefinitions;
026import io.stallion.dataAccess.file.ModelWithFilePath;
027import io.stallion.utils.GeneralUtils;
028import io.stallion.utils.json.RestrictedViews;
029
030import javax.persistence.Column;
031import javax.persistence.Table;
032import java.util.ArrayList;
033import java.util.List;
034
035import static io.stallion.utils.Literals.empty;
036
037
038@Table(name="stallion_users")
039@DefaultSort(field="email", direction = "ASC")
040public class User extends ModelBase implements IUser, ModelWithFilePath {
041    private String username = "";
042    private String displayName = "";
043    private String givenName = "";
044    private String familyName = "";
045    private String email = "";
046    private Role role = Role.ANON;
047    private Long orgId = 0L;
048    private List<IGroup> groups = new ArrayList<IGroup>();
049    private String secret = "";
050    private String encryptionSecret = "";
051    private String timeZoneId = "";
052    private String bcryptedPassword;
053    private String filePath = "";
054    private boolean predefined = false;
055    private Boolean approved = false;
056    private boolean emailVerified = false;
057    private String resetToken = "";
058    private Long aliasForId = 0L;
059    private boolean disabled = false;
060    private boolean optedOut = false;
061    private boolean totallyOptedOut = false;
062    private String honorific = "";
063    private Long createdAt = 0L;
064
065
066    @Override
067    @Column
068    @UniqueKey
069    @JsonView(RestrictedViews.Member.class)
070    public String getUsername() {
071        return username;
072    }
073
074    @Override
075    public <U extends IUser> U setUsername(String username) {
076        this.username = username;
077        return (U)this;
078    }
079
080    @Override
081    @Column
082    @JsonView(RestrictedViews.Public.class)
083    public String getDisplayName() {
084        return displayName;
085    }
086
087    /*
088    public void setDisplayName(String displayName) {
089        this.displayName = displayName;
090    }
091    */
092
093    @Override
094    public <U extends IUser> U setDisplayName(String displayName) {
095        this.displayName = displayName;
096        return (U)this;
097    }
098
099
100    @Column
101    @JsonView(RestrictedViews.Member.class)
102    public String getGivenName() {
103        return givenName;
104    }
105
106    public <U extends IUser> U setGivenName(String givenName) {
107        this.givenName = givenName;
108        return (U)this;
109    }
110
111    @Column
112    @JsonView(RestrictedViews.Member.class)
113    public String getFamilyName() {
114        return familyName;
115    }
116
117    public <U extends IUser> U setFamilyName(String familyName) {
118        this.familyName = familyName;
119        return (U)this;
120    }
121
122    @Override
123    @Column
124    @UniqueKey
125    @JsonView(RestrictedViews.Member.class)
126    public String getEmail() {
127        return email;
128    }
129
130    @Override
131    public User setEmail(String email) {
132        this.email = email;
133        return this;
134    }
135
136    @Override
137    @Column
138    @JsonView(RestrictedViews.Owner.class)
139    public Role getRole() {
140        return role;
141    }
142
143    @Override
144    public User setRole(Role role) {
145        this.role = role;
146        return this;
147    }
148
149
150    public User setRoleFromString(String role) {
151        this.role = Enum.valueOf(Role.class, role);
152        return this;
153    }
154
155    @Override
156    @Column
157    @AlternativeKey
158    @JsonView(RestrictedViews.Member.class)
159    public Long getOrgId() {
160        return orgId;
161    }
162
163    @Override
164    public User setOrgId(Long orgId) {
165        this.orgId = orgId;
166        return this;
167    }
168
169    @Override
170    public List<IGroup> getGroups() {
171        return groups;
172    }
173
174    @Override
175    public <U extends IUser> U setGroups(List<IGroup> groups) {
176        this.groups = groups;
177        return (U)this;
178    }
179
180    @Override
181    @Column
182    @JsonView(RestrictedViews.Internal.class)
183    public String getSecret() {
184        return secret;
185    }
186
187    @Override
188    public <U extends IUser> U  setSecret(String secret) {
189        this.secret = secret;
190        return (U)this;
191    }
192
193    @Override
194    @JsonView(RestrictedViews.Member.class)
195    @Column
196    public String getTimeZoneId() {
197        return timeZoneId;
198    }
199
200    @Override
201    public <U extends IUser> U setTimeZoneId(String timeZoneId) {
202        this.timeZoneId = timeZoneId;
203        return (U)this;
204    }
205
206    @Override
207    @JsonView(RestrictedViews.Internal.class)
208    @Column
209    public String getBcryptedPassword() {
210        return bcryptedPassword;
211    }
212
213    @Override
214    public <U extends IUser> U setBcryptedPassword(String bcryptedPassword) {
215        this.bcryptedPassword = bcryptedPassword;
216        return (U)this;
217    }
218
219    @Override
220    public String generateFilePath() {
221        String name = getFamilyName() + "-" + getGivenName();
222        if (empty(getFamilyName()) && empty(getGivenName())) {
223            name = email;
224        }
225        return GeneralUtils.slugify(name + "---" + getId().toString()) + ".json";
226    }
227
228    @Override
229    @JsonView(RestrictedViews.Internal.class)
230    public String getFilePath() {
231        return filePath;
232    }
233
234    @Override
235    public void setFilePath(String filePath) {
236        this.filePath = filePath;
237    }
238
239    @JsonView(RestrictedViews.Internal.class)
240    @Column
241    public String getEncryptionSecret() {
242        return encryptionSecret;
243    }
244
245    public <U extends IUser> U setEncryptionSecret(String encryptionSecret) {
246        this.encryptionSecret = encryptionSecret;
247        return (U)this;
248    }
249
250
251
252    @Override
253    public boolean isPredefined() {
254        return predefined;
255    }
256
257    @Override
258    public User setPredefined(boolean predefined) {
259        this.predefined = predefined;
260        return this;
261    }
262
263    @Column
264    @JsonView(RestrictedViews.Member.class)
265    @Override
266    public Boolean getApproved() {
267        return approved;
268    }
269
270    public <U extends IUser> U setApproved(Boolean approved) {
271        this.approved = approved;
272        return (U)this;
273    }
274
275    @Column
276    @JsonView(RestrictedViews.Member.class)
277    public boolean getEmailVerified() {
278        return emailVerified;
279    }
280
281
282    public <U extends IUser> U setEmailVerified(boolean emailVerified) {
283        this.emailVerified = emailVerified;
284        return (U)this;
285    }
286
287    @Override
288    @Column
289    @JsonView(RestrictedViews.Internal.class)
290    public String getResetToken() {
291        return resetToken;
292    }
293
294    public <U extends IUser> U setResetToken(String resetToken) {
295        this.resetToken = resetToken;
296        return (U)this;
297    }
298
299    @Column
300    @AlternativeKey
301    @JsonView(RestrictedViews.Internal.class)
302    public Long getAliasForId() {
303        return aliasForId;
304    }
305
306
307    @Override
308    public <U extends IUser> U setAliasForId(Long aliasForId) {
309        this.aliasForId = aliasForId;
310        return (U)this;
311    }
312
313    @Column
314    @Override
315    @JsonView(RestrictedViews.Member.class)
316    public String getHonorific() {
317        return honorific;
318    }
319
320    public User setHonorific(String honorific) {
321        this.honorific = honorific;
322        return this;
323    }
324
325    @Column
326    @JsonView(RestrictedViews.Owner.class)
327    public boolean isTotallyOptedOut() {
328        return totallyOptedOut;
329    }
330
331    public User setTotallyOptedOut(boolean totallyOptedOut) {
332        this.totallyOptedOut = totallyOptedOut;
333        return this;
334    }
335
336    @Column
337    @Override
338    @JsonView(RestrictedViews.Owner.class)
339    public boolean isOptedOut() {
340        return optedOut;
341    }
342
343    public User setOptedOut(boolean optedOut) {
344        this.optedOut = optedOut;
345        return this;
346    }
347
348    @Column
349    @JsonView(RestrictedViews.Owner.class)
350    public boolean isDisabled() {
351        return disabled;
352    }
353
354    public User setDisabled(boolean disabled) {
355        this.disabled = disabled;
356        return this;
357    }
358
359    @Override
360    @Column
361    @JsonView(RestrictedViews.Owner.class)
362    public Long getCreatedAt() {
363        return createdAt;
364    }
365
366    public User setCreatedAt(Long createdAt) {
367        this.createdAt = createdAt;
368        return this;
369    }
370}
371
372