Commit 6ea880d3 authored by Liang Ding's avatar Liang Ding

🎨 注册/创建新用户校验

parent ce3f49a2
......@@ -17,6 +17,8 @@
*/
package org.b3log.solo.model;
import org.apache.commons.lang.StringUtils;
/**
* This class defines ext of user model relevant keys.
*
......@@ -89,6 +91,6 @@ public final class UserExt {
return true;
}
return name.contains("admin") || name.contains("Admin");
return StringUtils.containsIgnoreCase(name, "admin");
}
}
......@@ -180,7 +180,7 @@ public class UserConsole {
return;
}
// if a normal user or a visitor register a new user, treates the new user as a visitor
// if a normal user or a visitor register a new user, treats the new user as a visitor
// (visitorRole) who couldn't post article
requestJSONObject.put(User.USER_ROLE, Role.VISITOR_ROLE);
}
......
......@@ -25,29 +25,28 @@ import org.json.JSONObject;
* User repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.0.0.4, Nov 8, 2011
* @version 1.1.0.0, Sep 21, 2018
* @since 0.3.1
*/
public interface UserRepository extends Repository {
/**
* Gets a user by the specified email.
* Gets a user by the specified username.
*
* @param email the specified email
* @param userName the specified username
* @return user, returns {@code null} if not found
* @throws RepositoryException repository exception
*/
JSONObject getByEmail(final String email) throws RepositoryException;
JSONObject getByUserName(final String userName) throws RepositoryException;
/**
* Determine whether the specified email is administrator's.
* Gets a user by the specified email.
*
* @param email the specified email
* @return {@code true} if it is administrator's email, {@code false}
* otherwise
* @return user, returns {@code null} if not found
* @throws RepositoryException repository exception
*/
boolean isAdminEmail(final String email) throws RepositoryException;
JSONObject getByEmail(final String email) throws RepositoryException;
/**
* Gets the administrator user.
......
......@@ -28,11 +28,13 @@ import org.b3log.solo.repository.UserRepository;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
/**
* User repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.1.0.9, Aug 27, 2017
* @version 1.1.0.10, Sep 21, 2018
* @since 0.3.1
*/
@Repository
......@@ -87,6 +89,18 @@ public class UserRepositoryImpl extends AbstractRepository implements UserReposi
}
}
@Override
public JSONObject getByUserName(final String userName) throws RepositoryException {
final Query query = new Query().setPageCount(1).
setFilter(new PropertyFilter(User.USER_NAME, FilterOperator.EQUAL, userName));
final List<JSONObject> users = getList(query);
if (users.isEmpty()) {
return null;
}
return users.get(0);
}
@Override
public JSONObject getByEmail(final String email) throws RepositoryException {
JSONObject ret = userCache.getUserByEmail(email);
......@@ -128,14 +142,4 @@ public class UserRepositoryImpl extends AbstractRepository implements UserReposi
return ret;
}
@Override
public boolean isAdminEmail(final String email) throws RepositoryException {
final JSONObject user = getByEmail(email);
if (null == user) {
return false;
}
return Role.ADMIN_ROLE.equals(user.optString(User.USER_ROLE));
}
}
......@@ -271,7 +271,7 @@ public class UserMgmtService {
* @return generated user id
* @throws ServiceException service exception
*/
public String addUser(final JSONObject requestJSONObject) throws ServiceException {
public synchronized String addUser(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
......@@ -281,8 +281,7 @@ public class UserMgmtService {
throw new ServiceException(langPropsService.get("mailInvalidLabel"));
}
final JSONObject duplicatedUser = userRepository.getByEmail(userEmail);
JSONObject duplicatedUser = userRepository.getByEmail(userEmail);
if (null != duplicatedUser) {
if (transaction.isActive()) {
transaction.rollback();
......@@ -297,6 +296,14 @@ public class UserMgmtService {
if (UserExt.invalidUserName(userName)) {
throw new ServiceException(langPropsService.get("userNameInvalidLabel"));
}
duplicatedUser = userRepository.getByUserName(userName);
if (null != duplicatedUser) {
if (transaction.isActive()) {
transaction.rollback();
}
throw new ServiceException(langPropsService.get("duplicatedUserNameLabel"));
}
user.put(User.USER_NAME, userName);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
......@@ -326,7 +333,6 @@ public class UserMgmtService {
user.put(UserExt.USER_AVATAR, userAvatar);
userRepository.add(user);
transaction.commit();
return user.optString(Keys.OBJECT_ID);
......
......@@ -18,7 +18,7 @@
#
# Description: Solo language configurations(en_US).
# Version: 2.21.0.2, Aug 27, 2018
# Version: 2.22.0.0, Sep 21, 2018
# Author: Liang Ding
# Author: Liyuan Li
# Author: Dongxu Wang
......@@ -36,7 +36,6 @@ addItalicLabel=Add italic text
insertQuoteLabel=Insert a quote
addBulletedLabel=Add a bulleted list
addNumberedListLabel=Add a numbered list
addLinkLabel=Add a link
undoLabel=Undo
redoLabel=Redo
fullscreenLabel=Fullscreen
......@@ -189,7 +188,6 @@ archiveLabel=Archive
archive1Label=archive:
yearLabel=
monthLabel=
pageLabel=Page
navMgmtLabel=Navigation
navLabel=Navigation
openMethod1Label=Target:
......@@ -401,6 +399,7 @@ make sure the <em>Consumer Secret</em> you typed in and then try again.
duplicatedPermalinkLabel=Duplicated permalink!
invalidPermalinkFormatLabel=Invalid permalink format!
duplicatedEmailLabel=Duplicated email!
duplicatedUserNameLabel=Duplicated username!
refreshAndRetryLabel=Please refresh and try again!
editorLeaveLabel=Content is not null, Do you leave?
editorPostLabel=Content is not null, Do you clear?
......
......@@ -18,7 +18,7 @@
#
# Description: Solo default language configurations(zh_CN).
# Version: 2.21.0.2, Aug 27, 2018
# Version: 2.22.0.0, Sep 21, 2018
# Author: Liang Ding
# Author: Liyuan Li
# Author: Dongxu Wang
......@@ -36,7 +36,6 @@ addItalicLabel=\u6DFB\u52A0\u659C\u4F53
insertQuoteLabel=\u63D2\u5165\u5F15\u7528
addBulletedLabel=\u6DFB\u52A0\u65E0\u5E8F\u5217\u8868
addNumberedListLabel=\u6DFB\u52A0\u6709\u5E8F\u5217\u8868
addLink1Label=\u6DFB\u52A0\u94FE\u63A5
undoLabel=\u64A4\u9500
redoLabel=\u6062\u590D
helpLabel=\u5E2E\u52A9
......@@ -189,7 +188,6 @@ archiveLabel=\u5B58\u6863
archive1Label=\u5B58\u6863\uFF1A
yearLabel=\u5E74
monthLabel=\u6708
pageLabel=\u9875\u9762
navMgmtLabel=\u5BFC\u822A\u7BA1\u7406
navLabel=\u5BFC\u822A
openMethod1Label=\u9875\u9762\u6253\u5F00\u65B9\u5F0F\uFF1A
......@@ -399,6 +397,7 @@ noAuthorizationURLLabel=\u4ECE Google \u83B7\u53D6\u6388\u6743\u5730\u5740\u5931
duplicatedPermalinkLabel=\u94FE\u63A5\u91CD\u590D\uFF01
invalidPermalinkFormatLabel=\u975E\u6CD5\u7684\u94FE\u63A5\u683C\u5F0F\uFF01
duplicatedEmailLabel=\u90AE\u4EF6\u5730\u5740\u91CD\u590D\uFF01
duplicatedUserNameLabel=\u7528\u6237\u540D\u91CD\u590D\uFF01
refreshAndRetryLabel=\u8BF7\u5237\u65B0\u91CD\u8BD5\uFF01
editorLeaveLabel=\u7F16\u8F91\u5668\u4E2D\u8FD8\u6709\u5185\u5BB9\uFF0C\u662F\u5426\u79BB\u5F00\uFF1F
editorPostLabel=\u7F16\u8F91\u5668\u4E2D\u8FD8\u6709\u5185\u5BB9\uFF0C\u662F\u5426\u6E05\u7A7A\uFF1F
......
......@@ -80,9 +80,6 @@ public final class UserRepositoryImplTestCase extends AbstractTestCase {
userRepository.add(admin);
transaction.commit();
Assert.assertTrue(userRepository.isAdminEmail("test@gmail.com"));
Assert.assertFalse(userRepository.isAdminEmail("notFound@gmail.com"));
admin = userRepository.getAdmin();
Assert.assertNotNull(admin);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment