Skip to content

Commit

Permalink
🎨 匿名机制 someone 改进 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Jun 23, 2020
1 parent 0a931ff commit 14aae6f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 30 deletions.
5 changes: 0 additions & 5 deletions src/main/java/org/b3log/symphony/model/UserExt.java
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,6 @@ public final class UserExt {
*/
public static final String ANONYMOUS_USER_NAME = "someone";

/**
* Anonymous user id.
*/
public static final String ANONYMOUS_USER_ID = "0";

//// Status constants
/**
* User status - valid.
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/org/b3log/symphony/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.b3log.latke.repository.annotation.Repository;
import org.b3log.symphony.cache.UserCache;
import org.b3log.symphony.model.Role;
import org.b3log.symphony.model.UserExt;
import org.json.JSONObject;

import java.util.List;
Expand All @@ -32,7 +33,7 @@
* User repository.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 2.1.2.3, Jun 6, 2019
* @version 2.2.0.0, Jun 23, 2020
* @since 0.2.0
*/
@Repository
Expand Down Expand Up @@ -134,4 +135,14 @@ public List<JSONObject> getAdmins() throws RepositoryException {

return ret;
}

/**
* Gets the anonymous user.
*
* @return anonymous user
* @throws RepositoryException repository exception
*/
public JSONObject getAnonymousUser() throws RepositoryException {
return getByName(UserExt.ANONYMOUS_USER_NAME);
}
}
18 changes: 15 additions & 3 deletions src/main/java/org/b3log/symphony/service/InitMgmtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* Initialization management service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.2.2.3, Jul 11, 2019
* @version 1.2.2.4, Jun 23, 2020
* @since 1.8.0
*/
@Service
Expand Down Expand Up @@ -638,13 +638,25 @@ public void initSym() {
comBot.put(User.USER_EMAIL, UserExt.COM_BOT_EMAIL);
comBot.put(User.USER_NAME, UserExt.COM_BOT_NAME);
comBot.put(User.USER_PASSWORD, DigestUtils.md5Hex(String.valueOf(new Random().nextInt())));
comBot.put(UserExt.USER_LANGUAGE, "en_US");
comBot.put(UserExt.USER_LANGUAGE, DEFAULT_LANG);
comBot.put(UserExt.USER_GUIDE_STEP, UserExt.USER_GUIDE_STEP_FIN);
comBot.put(User.USER_ROLE, Role.ROLE_ID_C_DEFAULT);
comBot.put(UserExt.USER_STATUS, UserExt.USER_STATUS_C_VALID);
userMgmtService.addUser(comBot);

LOGGER.info("Initialized admin user");
// Init community anonymous user placeholder
final JSONObject someone = new JSONObject();
someone.put(User.USER_EMAIL, UserExt.ANONYMOUS_USER_NAME + UserExt.USER_BUILTIN_EMAIL_SUFFIX);
someone.put(User.USER_NAME, UserExt.ANONYMOUS_USER_NAME);
someone.put(UserExt.USER_NICKNAME, UserExt.ANONYMOUS_USER_NAME);
someone.put(User.USER_PASSWORD, DigestUtils.md5Hex(String.valueOf(new Random().nextInt())));
someone.put(UserExt.USER_LANGUAGE, DEFAULT_LANG);
someone.put(UserExt.USER_GUIDE_STEP, UserExt.USER_GUIDE_STEP_FIN);
someone.put(User.USER_ROLE, Role.ROLE_ID_C_DEFAULT);
someone.put(UserExt.USER_STATUS, UserExt.USER_STATUS_C_VALID);
userMgmtService.addUser(someone);

LOGGER.info("Initialized system users");

// Add tags
String tagTitle = Symphonys.SYS_ANNOUNCE_TAG;
Expand Down
25 changes: 10 additions & 15 deletions src/main/java/org/b3log/symphony/service/TagQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* Tag query service.
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @version 1.9.0.6, May 12, 2019
* @version 1.9.0.7, Jun 23, 2020
* @since 0.2.0
*/
@Service
Expand Down Expand Up @@ -421,7 +421,6 @@ public List<JSONObject> getTags(final int fetchSize) {
* @return tag creator, for example, <pre>
* {
* "tagCreatorThumbnailURL": "",
* "tagCreatorThumbnailUpdateTime": 0,
* "tagCreatorName": ""
* }
* </pre>, returns {@code null} if not found
Expand All @@ -437,24 +436,20 @@ public JSONObject getCreator(final String tagId) {
setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters)).
addSort(Keys.OBJECT_ID, SortDirection.ASCENDING);
try {
final JSONObject ret = new JSONObject();
JSONObject ret;
final JSONObject creatorTagRelation = userTagRepository.getFirst(query);
if (null == creatorTagRelation) {
LOGGER.log(Level.WARN, "Can't find tag [id=" + tagId + "]'s creator, uses anonymous user instead");
ret.put(Tag.TAG_T_CREATOR_NAME, UserExt.ANONYMOUS_USER_NAME);
return ret;
}

final String creatorId = creatorTagRelation.optString(User.USER + '_' + Keys.OBJECT_ID);
if (UserExt.ANONYMOUS_USER_ID.equals(creatorId)) {
ret.put(Tag.TAG_T_CREATOR_NAME, UserExt.ANONYMOUS_USER_NAME);
return ret;
LOGGER.log(Level.WARN, "Can't find tag [id=" + tagId + "]'s creator, uses admin user instead");
final List<JSONObject> admins = userRepository.getAdmins();
ret = admins.get(0);
} else {
final String creatorId = creatorTagRelation.optString(User.USER + '_' + Keys.OBJECT_ID);
ret = userRepository.get(creatorId);
}

final JSONObject creator = userRepository.get(creatorId);
final String thumbnailURL = avatarQueryService.getAvatarURLByUser(creator, "48");
final String thumbnailURL = avatarQueryService.getAvatarURLByUser(ret, "48");
ret.put(Tag.TAG_T_CREATOR_THUMBNAIL_URL, thumbnailURL);
ret.put(Tag.TAG_T_CREATOR_NAME, creator.optString(User.USER_NAME));
ret.put(Tag.TAG_T_CREATOR_NAME, ret.optString(User.USER_NAME));
return ret;
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets tag creator failed [tagId=" + tagId + "]", e);
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/b3log/symphony/service/UserQueryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
*
* @author <a href="http://88250.b3log.org">Liang Ding</a>
* @author <a href="https://hacpai.com/member/ZephyrJung">Zephyr</a>
* @version 1.8.7.2, Nov 6, 2018
* @version 1.9.0.0, Jun 23, 2020
* @since 0.2.0
*/
@Service
Expand Down Expand Up @@ -384,13 +384,17 @@ public List<JSONObject> getAdmins() {
}

/**
* Gets the super administrator.
* Gets the anonymous user.
*
* @return super administrator
* @throws ServiceException service exception
* @return anonymous user
*/
public JSONObject getSA() throws ServiceException {
return getAdmins().get(0);
public JSONObject getAnonymousUser() {
try {
return userRepository.getAnonymousUser();
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets the anonymous user failed", e);
return null;
}
}

/**
Expand Down

0 comments on commit 14aae6f

Please sign in to comment.