Skip to content

Commit 4b2d843

Browse files
committed
refactoring with UserSource and UserType
1 parent 0b0e1ae commit 4b2d843

File tree

14 files changed

+90
-72
lines changed

14 files changed

+90
-72
lines changed

logicaldoc-core/src/main/java/com/logicaldoc/core/security/authentication/DefaultAuthenticator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.logicaldoc.core.security.Client;
1313
import com.logicaldoc.core.security.user.User;
1414
import com.logicaldoc.core.security.user.UserDAO;
15+
import com.logicaldoc.core.security.user.UserType;
1516
import com.logicaldoc.util.crypt.CryptUtil;
1617

1718
/**
@@ -78,7 +79,7 @@ public void validateUser(User user) throws AuthenticationException {
7879
throw new AccountNotFoundException(this);
7980

8081
// Check the type
81-
if (user.getType() != User.TYPE_DEFAULT && user.getType() != User.TYPE_READONLY)
82+
if (user.getType() != UserType.DEFAULT && user.getType() != UserType.READONLY)
8283
throw new AccountTypeNotAllowedException();
8384

8485
try {
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.logicaldoc.core.security.user;
22

33
public enum GroupType {
4-
DEFAULT,
5-
USER;
4+
DEFAULT, USER;
65
}

logicaldoc-core/src/main/java/com/logicaldoc/core/security/user/HibernateUserDAO.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ public void store(User user, UserHistory transaction) throws PasswordAlreadyUsed
207207

208208
validateUsernameUniquenes(user, newUser);
209209

210-
if (user.getType() == User.TYPE_SYSTEM)
211-
user.setType(User.TYPE_DEFAULT);
210+
if (user.getType() == UserType.SYSTEM)
211+
user.setType(UserType.DEFAULT);
212212

213213
enforceReadOnlyUserGroups(user);
214214

@@ -465,8 +465,7 @@ private void saveEnabledOrDisabledHistory(User user, UserHistory transaction, bo
465465
else {
466466
enabledOrDisabledHistory.setUser(user);
467467
}
468-
enabledOrDisabledHistory
469-
.setEvent(user.getEnabled() == 1 ? UserEvent.ENABLED : UserEvent.DISABLED);
468+
enabledOrDisabledHistory.setEvent(user.getEnabled() == 1 ? UserEvent.ENABLED : UserEvent.DISABLED);
470469
enabledOrDisabledHistory.setComment(null);
471470
saveUserHistory(user, enabledOrDisabledHistory);
472471
}
@@ -511,7 +510,7 @@ private void invokeListenersBefore(User user, UserHistory transaction, Map<Strin
511510
private boolean processPasswordChanged(User user) throws PersistenceException {
512511
// Do not implement password change logic in case of users imported from
513512
// another system
514-
if (user.getSource() != User.SOURCE_DEFAULT)
513+
if (user.getSource() != UserSource.DEFAULT)
515514
return false;
516515

517516
boolean passwordChanged;
@@ -560,7 +559,7 @@ private void saveDefaultDashlets(User user) throws PersistenceException {
560559
private boolean isPasswordExpired(User user) throws PersistenceException {
561560
// Never consider changed the password of a user imported from another
562561
// system
563-
if (user == null || user.getSource() != User.SOURCE_DEFAULT)
562+
if (user == null || user.getSource() != UserSource.DEFAULT)
564563
return false;
565564

566565
if (user.getPasswordExpired() == 1)
@@ -676,15 +675,15 @@ public boolean isInactive(String username) throws PersistenceException {
676675

677676
@Override
678677
public int count(Long tenantId) throws PersistenceException {
679-
String query = "select count(*) from ld_user where ld_type=" + User.TYPE_DEFAULT + " and ld_deleted=0 "
678+
String query = "select count(*) from ld_user where ld_type=" + UserType.DEFAULT.ordinal() + " and ld_deleted=0 "
680679
+ (tenantId != null ? " and ld_tenantid=" + tenantId : "");
681680
return queryForInt(query);
682681
}
683682

684683
@Override
685684
public int countGuests(Long tenantId) throws PersistenceException {
686-
String query = "select count(*) from ld_user where ld_type=" + User.TYPE_READONLY + " and ld_deleted=0 "
687-
+ (tenantId != null ? " and ld_tenantid=" + tenantId : "");
685+
String query = "select count(*) from ld_user where ld_type=" + UserType.READONLY.ordinal()
686+
+ " and ld_deleted=0 " + (tenantId != null ? " and ld_tenantid=" + tenantId : "");
688687
return queryForInt(query);
689688
}
690689

logicaldoc-core/src/main/java/com/logicaldoc/core/security/user/User.java

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import javax.persistence.Cacheable;
1212
import javax.persistence.Column;
1313
import javax.persistence.Entity;
14+
import javax.persistence.EnumType;
15+
import javax.persistence.Enumerated;
1416
import javax.persistence.Table;
1517
import javax.persistence.Transient;
1618

@@ -43,23 +45,19 @@ public class User extends PersistentObject implements Serializable {
4345

4446
private static final Logger log = LoggerFactory.getLogger(User.class);
4547

46-
public static final int TYPE_DEFAULT = 0;
47-
48-
public static final int TYPE_SYSTEM = 1;
49-
50-
public static final int TYPE_READONLY = 2;
51-
52-
public static final int SOURCE_DEFAULT = 0;
53-
54-
public static final int SOURCE_LDAP = 1;
55-
56-
public static final int SOURCE_SAML = 2;
57-
5848
public static final long USERID_ADMIN = 1;
5949

6050
public static final long USERID_SYSTEM = -1010;
6151

6252
private static final long serialVersionUID = 8093874904302301982L;
53+
54+
@Column(name = "ld_type", nullable = false)
55+
@Enumerated(EnumType.ORDINAL)
56+
private UserType type = UserType.DEFAULT;
57+
58+
@Column(name = "ld_source", nullable = false)
59+
@Enumerated(EnumType.ORDINAL)
60+
private UserSource source = UserSource.DEFAULT;
6361

6462
@Column(name = "ld_username", length = 255, nullable = false)
6563
private String username = "";
@@ -115,10 +113,7 @@ public class User extends PersistentObject implements Serializable {
115113

116114
@Column(name = "ld_telephone2", length = 255)
117115
private String telephone2 = "";
118-
119-
@Column(name = "ld_type", nullable = false)
120-
private int type = TYPE_DEFAULT;
121-
116+
122117
// Not persisted
123118
@Transient
124119
private Set<Group> groups = new HashSet<>();
@@ -150,9 +145,6 @@ public class User extends PersistentObject implements Serializable {
150145
@Transient
151146
private String repass;
152147

153-
@Column(name = "ld_source", nullable = false)
154-
private int source = 0;
155-
156148
@Column(name = "ld_quota", nullable = false)
157149
private long quota = -1;
158150

@@ -161,7 +153,7 @@ public class User extends PersistentObject implements Serializable {
161153

162154
@Column(name = "ld_defworkspace")
163155
private Long defaultWorkspace;
164-
156+
165157
@Column(name = "ld_ipwhitelist", length = 1000, nullable = true)
166158
private String ipWhiteList;
167159

@@ -173,37 +165,37 @@ public class User extends PersistentObject implements Serializable {
173165
*/
174166
@Column(name = "ld_certexpire")
175167
private Date certExpire;
176-
168+
177169
/**
178170
* The distinguished name of the certificate
179171
*/
180172
@Column(name = "ld_certdn", length = 1000)
181173
private String certDN;
182-
174+
183175
/**
184176
* The second factor authenticator to use
185177
*/
186178
@Column(name = "ld_secondfactor", length = 255)
187179
private String secondFactor;
188-
180+
189181
/**
190182
* A key used by the second factor authenticator
191183
*/
192184
@Column(name = "ld_key", length = 255)
193185
private String key;
194-
186+
195187
/**
196188
* Description of the grid that displays the list of documents
197189
*/
198190
@Column(name = "ld_docsgrid", nullable = true)
199191
private String docsGrid;
200-
192+
201193
/**
202194
* Description of the grid that shows the results of a search
203195
*/
204196
@Column(name = "ld_hitsgrid", nullable = true)
205197
private String hitsGrid;
206-
198+
207199
@Column(name = "ld_dateformat", length = 255)
208200
private String dateFormat;
209201

@@ -219,25 +211,25 @@ public class User extends PersistentObject implements Serializable {
219211
*/
220212
@Column(name = "ld_searchpref", length = 255)
221213
private String searchPref;
222-
214+
223215
/**
224216
* Last time this account has been activated
225217
*/
226218
@Column(name = "ld_lastenabled")
227219
private Date lastEnabled;
228-
220+
229221
/**
230222
* When this account expires
231223
*/
232224
@Column(name = "ld_expire")
233225
private Date expire;
234-
226+
235227
/**
236228
* If the system must forbid the login outside the working time
237229
*/
238230
@Column(name = "ld_enforcewrktime", nullable = false)
239231
private int enforceWorkingTime = 0;
240-
232+
241233
/**
242234
* Maximum number of inactivity days after which the account gets disabled.
243235
* Possible values are:
@@ -249,7 +241,7 @@ public class User extends PersistentObject implements Serializable {
249241
*/
250242
@Column(name = "ld_maxinactivity")
251243
private Integer maxInactivity;
252-
244+
253245
@Column(name = "ld_timezone")
254246
private String timeZone;
255247

@@ -258,13 +250,13 @@ public class User extends PersistentObject implements Serializable {
258250
*/
259251
@Column(name = "ld_avatar")
260252
private String avatar;
261-
253+
262254
/**
263255
* Last time the user successfully logged in
264256
*/
265257
@Column(name = "ld_lastlogin")
266258
private Date lastLogin = new Date();
267-
259+
268260
@Transient
269261
private String decodedPassword;
270262

@@ -311,13 +303,17 @@ public void setCompany(String company) {
311303
this.company = company;
312304
}
313305

314-
public int getType() {
306+
public UserType getType() {
315307
return type;
316308
}
317309

318-
public void setType(int type) {
310+
public void setType(UserType type) {
319311
this.type = type;
320312
}
313+
314+
public void setType(int type) {
315+
this.type = UserType.values()[type];
316+
}
321317

322318
public String getRepass() {
323319
return repass;
@@ -642,18 +638,24 @@ public void setPasswordExpires(int passwordExpires) {
642638
/**
643639
* The source from which the user has been created
644640
*
645-
* @see User#SOURCE_DEFAULT
646-
* @see User#SOURCE_LDAP
641+
* @see UserSource.DEFAULT
642+
* @see UserSource.LDAP
643+
* @see UserSource.SAML
647644
*
648645
* @return the source
649646
*/
650-
public int getSource() {
647+
648+
public UserSource getSource() {
651649
return source;
652650
}
653651

654-
public void setSource(int source) {
652+
public void setSource(UserSource source) {
655653
this.source = source;
656654
}
655+
656+
public void setSource(int source) {
657+
this.source = UserSource.values()[source];
658+
}
657659

658660
public long getQuota() {
659661
return quota;
@@ -785,7 +787,7 @@ public void setKey(String key) {
785787
}
786788

787789
public boolean isReadonly() {
788-
return type == TYPE_READONLY;
790+
return type == UserType.READONLY;
789791
}
790792

791793
public String getDocsGrid() {
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.logicaldoc.core.security.user;
2+
3+
public enum UserSource {
4+
5+
DEFAULT, LDAP, SAML;
6+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.logicaldoc.core.security.user;
2+
3+
public enum UserType {
4+
DEFAULT, SYSTEM, READONLY;
5+
}

logicaldoc-core/src/main/java/com/logicaldoc/core/util/UserUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.logicaldoc.core.security.TenantDAO;
2020
import com.logicaldoc.core.security.user.User;
2121
import com.logicaldoc.core.security.user.UserDAO;
22+
import com.logicaldoc.core.security.user.UserType;
2223
import com.logicaldoc.util.Context;
2324
import com.logicaldoc.util.config.ContextProperties;
2425
import com.logicaldoc.util.io.FileUtil;
@@ -150,7 +151,7 @@ public static void saveAvatar(User user, File avatarImageFile) {
150151
*/
151152
public static void generateDefaultAvatar(User user) {
152153
UserDAO userDao = Context.get(UserDAO.class);
153-
154+
154155
File tmpAvatarImage = null;
155156
try {
156157
userDao.initialize(user);
@@ -164,7 +165,7 @@ public static void generateDefaultAvatar(User user) {
164165
ImageIO.write(avatar, "png", tmpAvatarImage);
165166
user.setAvatar(ImageUtil.encodeImage(tmpAvatarImage));
166167

167-
if (user.getType() != User.TYPE_SYSTEM) {
168+
if (user.getType() != UserType.SYSTEM) {
168169
userDao.store(user);
169170
} else {
170171
Map<String, Object> params = new HashMap<>();

logicaldoc-core/src/test/java/com/logicaldoc/core/security/user/HibernateUserDAOTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ public void setUp() throws IOException, SQLException, PluginException {
4040

4141
// Retrieve the instance under test from spring context. Make sure that
4242
// it is an HibernateUserDAO
43-
dao = (UserDAO) context.getBean("UserDAO");
44-
45-
groupDao = (GroupDAO) context.getBean("GroupDAO");
43+
dao = Context.get(UserDAO.class);
44+
groupDao = Context.get(GroupDAO.class);
4645
}
4746

4847
@Test

logicaldoc-webapp/src/main/java/com/logicaldoc/web/data/UsersDataServlet.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.logicaldoc.core.security.user.GroupType;
2323
import com.logicaldoc.core.security.user.User;
2424
import com.logicaldoc.core.security.user.UserDAO;
25+
import com.logicaldoc.core.security.user.UserType;
2526
import com.logicaldoc.util.Context;
2627

2728
/**
@@ -59,7 +60,7 @@ private void printUsers(List<User> users, boolean required, boolean skipdisabled
5960
*/
6061
UserDAO userDao = Context.get(UserDAO.class);
6162
for (User user : users) {
62-
if (user.getType() == User.TYPE_SYSTEM || (skipdisabled && user.getEnabled() != 1))
63+
if (user.getType() == UserType.SYSTEM || (skipdisabled && user.getEnabled() != 1))
6364
continue;
6465

6566
userDao.initialize(user);

0 commit comments

Comments
 (0)