use of org.b3log.latke.repository.RepositoryException in project solo by b3log.
the class UserMgmtService method updateUser.
/**
* Updates a user by the specified request json object.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "oId": "",
* "userName": "",
* "userEmail": "",
* "userPassword": "", // Unhashed
* "userRole": "", // optional
* "userURL": "", // optional
* }
* </pre>
*
* @throws ServiceException service exception
*/
public void updateUser(final JSONObject requestJSONObject) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final String oldUserId = requestJSONObject.optString(Keys.OBJECT_ID);
final JSONObject oldUser = userRepository.get(oldUserId);
if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel"));
}
final String userNewEmail = requestJSONObject.optString(User.USER_EMAIL).toLowerCase().trim();
// Check email is whether duplicated
final JSONObject mayBeAnother = userRepository.getByEmail(userNewEmail);
if (null != mayBeAnother && !mayBeAnother.optString(Keys.OBJECT_ID).equals(oldUserId)) {
// Exists someone else has the save email as requested
throw new ServiceException(langPropsService.get("duplicatedEmailLabel"));
}
// Update
final String userName = requestJSONObject.optString(User.USER_NAME);
final String userPassword = requestJSONObject.optString(User.USER_PASSWORD);
oldUser.put(User.USER_EMAIL, userNewEmail);
oldUser.put(User.USER_NAME, userName);
final boolean mybeHashed = HASHED_PASSWORD_LENGTH == userPassword.length();
final String newHashedPassword = MD5.hash(userPassword);
final String oldHashedPassword = oldUser.optString(User.USER_PASSWORD);
if (!"demo.b3log.org".equals(Latkes.getServerHost())) {
// Skips the Solo Online Demo (http://demo.b3log.org)
if (!mybeHashed || (!oldHashedPassword.equals(userPassword) && !oldHashedPassword.equals(newHashedPassword))) {
oldUser.put(User.USER_PASSWORD, newHashedPassword);
}
}
final String userRole = requestJSONObject.optString(User.USER_ROLE);
if (!Strings.isEmptyOrNull(userRole)) {
oldUser.put(User.USER_ROLE, userRole);
}
final String userURL = requestJSONObject.optString(User.USER_URL);
if (!Strings.isEmptyOrNull(userURL)) {
oldUser.put(User.USER_URL, userURL);
}
final String userAvatar = requestJSONObject.optString(UserExt.USER_AVATAR);
if (!StringUtils.equals(userAvatar, oldUser.optString(UserExt.USER_AVATAR))) {
oldUser.put(UserExt.USER_AVATAR, userAvatar);
}
userRepository.update(oldUserId, oldUser);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates a user failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.RepositoryException in project solo by b3log.
the class UserMgmtService method changeRole.
/**
* Swithches the user role between "defaultRole" and "visitorRole" by the specified user id.
*
* @param userId the specified user id
* @throws ServiceException exception
* @see User
*/
public void changeRole(final String userId) throws ServiceException {
final Transaction transaction = userRepository.beginTransaction();
try {
final JSONObject oldUser = userRepository.get(userId);
if (null == oldUser) {
throw new ServiceException(langPropsService.get("updateFailLabel"));
}
final String role = oldUser.optString(User.USER_ROLE);
if (Role.VISITOR_ROLE.equals(role)) {
oldUser.put(User.USER_ROLE, Role.DEFAULT_ROLE);
} else if (Role.DEFAULT_ROLE.equals(role)) {
oldUser.put(User.USER_ROLE, Role.VISITOR_ROLE);
}
userRepository.update(userId, oldUser);
transaction.commit();
} catch (final RepositoryException e) {
if (transaction.isActive()) {
transaction.rollback();
}
LOGGER.log(Level.ERROR, "Updates a user failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.repository.RepositoryException in project solo by b3log.
the class UserQueryService method getUsers.
/**
* Gets users by the specified request json object.
*
* @param requestJSONObject the specified request json object, for example,
* <pre>
* {
* "paginationCurrentPageNum": 1,
* "paginationPageSize": 20,
* "paginationWindowSize": 10,
* }, see {@link Pagination} for more details
* </pre>
* @return for example,
* <pre>
* {
* "pagination": {
* "paginationPageCount": 100,
* "paginationPageNums": [1, 2, 3, 4, 5]
* },
* "users": [{
* "oId": "",
* "userName": "",
* "userEmail": "",
* "userPassword": "",
* "roleName": ""
* }, ....]
* }
* </pre>
* @throws ServiceException service exception
* @see Pagination
*/
public JSONObject getUsers(final JSONObject requestJSONObject) throws ServiceException {
final JSONObject ret = new JSONObject();
final int currentPageNum = requestJSONObject.optInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
final int pageSize = requestJSONObject.optInt(Pagination.PAGINATION_PAGE_SIZE);
final int windowSize = requestJSONObject.optInt(Pagination.PAGINATION_WINDOW_SIZE);
final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize);
JSONObject result = null;
try {
result = userRepository.get(query);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets users failed", e);
throw new ServiceException(e);
}
final int pageCount = result.optJSONObject(Pagination.PAGINATION).optInt(Pagination.PAGINATION_PAGE_COUNT);
final JSONObject pagination = new JSONObject();
ret.put(Pagination.PAGINATION, pagination);
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
final JSONArray users = result.optJSONArray(Keys.RESULTS);
ret.put(User.USERS, users);
return ret;
}
use of org.b3log.latke.repository.RepositoryException in project solo by b3log.
the class UserQueryService method getCurrentUser.
/**
* Gets the current user.
*
* @param request the specified request
* @return the current user, {@code null} if not found
*/
public JSONObject getCurrentUser(final HttpServletRequest request) {
final GeneralUser currentUser = userService.getCurrentUser(request);
if (null == currentUser) {
return null;
}
final String email = currentUser.getEmail();
try {
return userRepository.getByEmail(email);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets current user by request failed, returns null", e);
return null;
}
}
use of org.b3log.latke.repository.RepositoryException in project solo by b3log.
the class UserQueryService method getUser.
/**
* Gets a user by the specified user id.
*
* @param userId the specified user id
* @return for example,
* <pre>
* {
* "user": {
* "oId": "",
* "userName": "",
* "userEmail": "",
* "userPassword": ""
* }
* }
* </pre>, returns {@code null} if not found
* @throws ServiceException service exception
*/
public JSONObject getUser(final String userId) throws ServiceException {
final JSONObject ret = new JSONObject();
JSONObject user = null;
try {
user = userRepository.get(userId);
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets a user failed", e);
throw new ServiceException(e);
}
if (null == user) {
return null;
}
ret.put(User.USER, user);
return ret;
}
Aggregations