View Javadoc
1   package org.oxerr.commons.user.domain;
2   
3   import java.util.Arrays;
4   import java.util.HashSet;
5   import java.util.Set;
6   
7   import javax.persistence.Access;
8   import javax.persistence.AccessType;
9   import javax.persistence.Column;
10  import javax.persistence.Entity;
11  import javax.persistence.FetchType;
12  import javax.persistence.ForeignKey;
13  import javax.persistence.Index;
14  import javax.persistence.JoinColumn;
15  import javax.persistence.JoinTable;
16  import javax.persistence.ManyToMany;
17  import javax.persistence.Table;
18  import javax.persistence.Transient;
19  import javax.persistence.UniqueConstraint;
20  import javax.xml.bind.annotation.XmlTransient;
21  
22  @Entity
23  @Access(AccessType.PROPERTY)
24  @Table(
25  	name = "\"user\"",
26  	uniqueConstraints = {
27  		@UniqueConstraint(
28  			name = "uk_user_username",
29  			columnNames = "username"
30  		),
31  	}
32  )
33  public class User extends BaseEntity {
34  
35  	private static final long serialVersionUID = 2019062401L;
36  
37  	private String username;
38  	private String password;
39  
40  	private String nickname;
41  
42  	private Set<Role> roles;
43  
44  	private boolean enabled;
45  
46  	public User() {
47  	}
48  
49  	public User(
50  		String username,
51  		String password,
52  		String nickname,
53  		Set<Role> roles,
54  		boolean enabled
55  	) {
56  		this.username = username;
57  		this.password = password;
58  		this.nickname = nickname;
59  		this.roles = roles;
60  		this.enabled = enabled;
61  	}
62  
63  	@Column(name = "username")
64  	public String getUsername() {
65  		return username;
66  	}
67  
68  	public void setUsername(String username) {
69  		this.username = username;
70  	}
71  
72  	@XmlTransient
73  	@Column(name = "password")
74  	public String getPassword() {
75  		return password;
76  	}
77  
78  	public void setPassword(String password) {
79  		this.password = password;
80  	}
81  
82  	@Column(name = "nickname")
83  	public String getNickname() {
84  		return nickname;
85  	}
86  
87  	public void setNickname(String nickname) {
88  		this.nickname = nickname;
89  	}
90  
91  	@XmlTransient
92  	@ManyToMany(fetch = FetchType.EAGER)
93  	@JoinTable(
94  		name = "\"authorities\"",
95  		joinColumns = @JoinColumn(name = "user_id"),
96  		inverseJoinColumns = @JoinColumn(name = "role_id"),
97  		foreignKey = @ForeignKey(name = "fk_authorities_user"),
98  		inverseForeignKey = @ForeignKey(name = "fk_authorities_role"),
99  		indexes = {
100 			@Index(
101 				name = "idx_authorities_user",
102 				columnList = "user_id"
103 			),
104 			@Index(
105 				name = "idx_authorities_role",
106 				columnList = "role_id"
107 			),
108 		}
109 	)
110 	public Set<Role> getRoles() {
111 		return roles;
112 	}
113 
114 	public void setRoles(Set<Role> roles) {
115 		this.roles = roles;
116 	}
117 
118 	@XmlTransient
119 	@Transient
120 	private Set<Role> getOrCreateRoles() {
121 		Set<Role> exists = this.getRoles();
122 		if (exists == null) {
123 			exists = new HashSet<>();
124 			this.setRoles(exists);
125 		}
126 		return exists;
127 	}
128 
129 	@XmlTransient
130 	@Transient
131 	public void addRoles(Set<Role> roles) {
132 		if (roles == null || roles.isEmpty()) {
133 			return;
134 		}
135 
136 		final Set<Role> exists = this.getOrCreateRoles();
137 
138 		try {
139 			exists.addAll(roles);
140 		} catch (UnsupportedOperationException e) {
141 			final Set<Role> ret = new HashSet<>();
142 			ret.addAll(exists);
143 			ret.addAll(roles);
144 			this.setRoles(ret);
145 		}
146 	}
147 
148 	@XmlTransient
149 	@Transient
150 	public void addRole(Role role) {
151 		if (role == null) {
152 			return;
153 		}
154 
155 		final Set<Role> exists = this.getOrCreateRoles();
156 
157 		try {
158 			exists.add(role);
159 		} catch (UnsupportedOperationException e) {
160 			final Set<Role> ret = new HashSet<>();
161 			ret.addAll(exists);
162 			ret.add(role);
163 			this.setRoles(ret);
164 		}
165 	}
166 
167 	/**
168 	 * Returns if this user has any of the specified authorities.
169 	 *
170 	 * @param authorities requires at least one of the authorities (i.e.
171 	 * "ROLE_USER","ROLE_ADMIN" would mean either "ROLE_USER" or "ROLE_ADMIN"
172 	 * is required).
173 	 * @return true if any of the authorities is present, otherwise false.
174 	 */
175 	@XmlTransient
176 	@Transient
177 	public boolean hasAnyAuthority(String... authorities) {
178 		return getRoles().stream().map(role -> role.getName())
179 			.anyMatch(Arrays.asList(authorities)::contains);
180 	}
181 
182 	@Column(nullable = false)
183 	public boolean isEnabled() {
184 		return enabled;
185 	}
186 
187 	public void setEnabled(boolean enabled) {
188 		this.enabled = enabled;
189 	}
190 
191 }