UserServiceImpl.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package com.uas.kanban.service.impl;
  2. import java.util.List;
  3. import java.util.Objects;
  4. import org.mongodb.morphia.query.Query;
  5. import org.mongodb.morphia.query.UpdateOperations;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. import com.uas.kanban.annotation.NotEmpty;
  9. import com.uas.kanban.base.BaseService;
  10. import com.uas.kanban.dao.ResourcePointDao;
  11. import com.uas.kanban.dao.UserDao;
  12. import com.uas.kanban.exception.OperationException;
  13. import com.uas.kanban.model.ResourcePoint;
  14. import com.uas.kanban.model.User;
  15. import com.uas.kanban.model.User.Role;
  16. import com.uas.kanban.service.UserService;
  17. import com.uas.kanban.support.SystemSession;
  18. import com.uas.kanban.util.CollectionUtils;
  19. /**
  20. * 用户
  21. *
  22. * @author sunyj
  23. * @since 2017年9月2日 下午8:47:20
  24. */
  25. @Service
  26. public class UserServiceImpl extends BaseService<User> implements UserService {
  27. @Autowired
  28. private UserDao userDao;
  29. @Autowired
  30. private ResourcePointDao resourcePointDao;
  31. @Override
  32. public User save(@NotEmpty("json") String json) {
  33. User user = userDao.parse(json);
  34. if (exist(user.getName())) {
  35. throw new IllegalStateException("用户已存在");
  36. }
  37. if (user.getRole() == null) {
  38. user.setRole(Role.Default);
  39. }
  40. checkValid(user);
  41. return userDao.save(user);
  42. }
  43. @Override
  44. public User savePart(String json) {
  45. return save(json);
  46. }
  47. @Override
  48. public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
  49. User user = userDao.parse(json);
  50. String code = user.codeNotEmpty();
  51. User rPoint = userDao.findOne(code);
  52. if (rPoint == null) {
  53. throw new IllegalStateException("用户不存在");
  54. }
  55. if (rPoint.getRole() == Role.Admin) {
  56. if (!Objects.equals(code, SystemSession.checkUser().getCode())) {
  57. throw new OperationException("不允许修改其他管理员");
  58. }
  59. }
  60. if (Objects.equals(user, rPoint)) {
  61. throw new IllegalStateException("未发现任何变更");
  62. }
  63. String name = user.getName();
  64. if (!Objects.equals(name, rPoint.getName()) && exist(name)) {
  65. throw new IllegalStateException("用户已存在");
  66. }
  67. checkValid(user);
  68. return userDao.update(user);
  69. }
  70. @Override
  71. public int updatePart(String json) throws IllegalArgumentException, OperationException {
  72. return update(json);
  73. }
  74. /**
  75. * 对名称和密码长度进行校验
  76. *
  77. * @param user
  78. */
  79. private void checkValid(@NotEmpty("user") User user) {
  80. String name = user.getName();
  81. String password = user.getPassword();
  82. if (name != null && name.trim().length() < 3) {
  83. throw new IllegalArgumentException("名称过短");
  84. }
  85. if (password != null && password.trim().length() < 3) {
  86. throw new IllegalArgumentException("密码过短");
  87. }
  88. checkResourcePointCodes(user.getResourcePointCodes());
  89. }
  90. /**
  91. * 检查资源点是否存在
  92. *
  93. * @param resourcePointCodes
  94. * 资源点的 code
  95. * @throws IllegalArgumentException
  96. * 资源点不存在
  97. */
  98. private void checkResourcePointCodes(List<String> resourcePointCodes) throws IllegalArgumentException {
  99. if (CollectionUtils.isEmpty(resourcePointCodes)) {
  100. return;
  101. }
  102. for (String resourcePointCode : resourcePointCodes) {
  103. ResourcePoint resourcePoint = resourcePointDao.findOne(resourcePointCode);
  104. if (resourcePoint == null) {
  105. throw new IllegalArgumentException("资源点不存在:" + resourcePointCode);
  106. }
  107. }
  108. }
  109. @Override
  110. public User login(@NotEmpty("name") String name, @NotEmpty("password") String password) {
  111. Query<User> query = userDao.createQuery();
  112. query.field("name").equal(name);
  113. query.field("password").equal(password);
  114. long count = query.count();
  115. if (count == 0) {
  116. throw new IllegalStateException("名称不存在或密码错误");
  117. }
  118. if (count > 1) {
  119. throw new IllegalStateException("用户重复");
  120. }
  121. User user = query.get();
  122. return user;
  123. }
  124. @Override
  125. public boolean resetPassword(@NotEmpty("password") String password, @NotEmpty("newPassword") String newPassword) {
  126. User rPoint = SystemSession.checkUser();
  127. String code = rPoint.getCode();
  128. User user = userDao.findOne(code);
  129. if (user == null) {
  130. throw new IllegalStateException("用户不存在:" + rPoint);
  131. }
  132. if (!Objects.equals(password, user.getPassword())) {
  133. throw new IllegalStateException("旧密码错误");
  134. }
  135. if (Objects.equals(password, newPassword)) {
  136. throw new IllegalStateException("新密码与旧密码相同");
  137. }
  138. UpdateOperations<User> operations = userDao.createUpdateOperations();
  139. operations.set("password", newPassword);
  140. userDao.update(code, operations);
  141. return true;
  142. }
  143. /**
  144. * 用户是否已存在
  145. *
  146. * @param name
  147. * 名称
  148. * @return 是否存在
  149. */
  150. public boolean exist(@NotEmpty("name") String name) {
  151. Query<User> query = userDao.createQuery();
  152. query.field("name").equal(name);
  153. return query.count() > 0;
  154. }
  155. @Override
  156. public int deleteByCodes(@NotEmpty("codes") List<String> codes) {
  157. Query<User> query = userDao.createQuery();
  158. query.field("code").in(codes);
  159. return userDao.delete(query);
  160. }
  161. }