|
|
@@ -57,20 +57,22 @@ public class UserServiceImpl extends BaseService<User> implements UserService {
|
|
|
public int update(@NotEmpty("json") String json) throws IllegalArgumentException, OperationException {
|
|
|
User user = userDao.parse(json);
|
|
|
String code = user.codeNotEmpty();
|
|
|
- User rPoint = userDao.findOne(code);
|
|
|
- if (rPoint == null) {
|
|
|
+ User oldUser = userDao.findOne(code);
|
|
|
+ if (oldUser == null) {
|
|
|
throw new IllegalStateException("用户不存在");
|
|
|
}
|
|
|
- if (rPoint.getRole() == Role.Admin) {
|
|
|
+ if (oldUser.getRole() == Role.Admin) {
|
|
|
if (!Objects.equals(code, SystemSession.checkUser().getCode())) {
|
|
|
- throw new OperationException("不允许修改其他管理员");
|
|
|
+ throw new OperationException("不允许更改其他管理员:" + oldUser.getName());
|
|
|
+ } else if (user.getRole() != Role.Admin && countAdmin() <= 1) {
|
|
|
+ throw new OperationException("只有一个管理员,不可降低权限");
|
|
|
}
|
|
|
}
|
|
|
- if (Objects.equals(user, rPoint)) {
|
|
|
+ if (Objects.equals(user, oldUser)) {
|
|
|
throw new IllegalStateException("未发现任何变更");
|
|
|
}
|
|
|
String name = user.getName();
|
|
|
- if (!Objects.equals(name, rPoint.getName()) && exist(name)) {
|
|
|
+ if (!Objects.equals(name, oldUser.getName()) && exist(name)) {
|
|
|
throw new IllegalStateException("用户已存在");
|
|
|
}
|
|
|
checkValid(user);
|
|
|
@@ -168,4 +170,53 @@ public class UserServiceImpl extends BaseService<User> implements UserService {
|
|
|
return query.count() > 0;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public int deleteOne(@NotEmpty("code") String code) throws OperationException {
|
|
|
+ checkAdmin(code);
|
|
|
+ return super.deleteOne(code);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public int delete(@NotEmpty("codes") List<String> codes) throws OperationException {
|
|
|
+ for (String code : codes) {
|
|
|
+ checkAdmin(code);
|
|
|
+ }
|
|
|
+ return super.delete(codes);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查对管理员的操作
|
|
|
+ *
|
|
|
+ * @param code
|
|
|
+ * 用户 code
|
|
|
+ * @throws OperationException
|
|
|
+ * 更改其他管理员
|
|
|
+ */
|
|
|
+ private void checkAdmin(@NotEmpty("code") String code) throws OperationException {
|
|
|
+ User user = userDao.findOne(code);
|
|
|
+ if (user == null) {
|
|
|
+ throw new IllegalStateException("用户不存在");
|
|
|
+ }
|
|
|
+ if (user.getRole() == Role.Admin) {
|
|
|
+ if (!Objects.equals(code, SystemSession.checkUser().getCode())) {
|
|
|
+ throw new OperationException("不允许删除其他管理员:" + user.getName());
|
|
|
+ } else {
|
|
|
+ if (countAdmin() <= 1) {
|
|
|
+ throw new OperationException("不可删除最后一个管理员");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 统计管理员的数量
|
|
|
+ *
|
|
|
+ * @return 统计的数量
|
|
|
+ */
|
|
|
+ private long countAdmin() {
|
|
|
+ Query<User> query = userDao.createQuery();
|
|
|
+ query.field("role").equal(Role.Admin);
|
|
|
+ return query.count();
|
|
|
+ }
|
|
|
+
|
|
|
}
|