BaseEntityService.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2019 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. * >>
  17. *
  18. */
  19. package edp.davinci.service.impl;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Component;
  22. import edp.core.consts.Consts;
  23. import edp.core.exception.ServerException;
  24. import edp.core.exception.UnAuthorizedException;
  25. import edp.core.utils.BaseLock;
  26. import edp.core.utils.LockFactory;
  27. import edp.davinci.core.enums.CheckEntityEnum;
  28. import edp.davinci.core.enums.LockType;
  29. import edp.davinci.core.enums.UserPermissionEnum;
  30. import edp.davinci.dto.projectDto.ProjectPermission;
  31. import edp.davinci.model.User;
  32. import edp.davinci.service.ProjectService;
  33. import lombok.extern.slf4j.Slf4j;
  34. @Slf4j
  35. @Component
  36. public abstract class BaseEntityService {
  37. @Autowired
  38. ProjectService projectService;
  39. protected BaseLock getLock(CheckEntityEnum entity, String name, Long domainId) {
  40. return LockFactory.getLock(
  41. entity.getSource().toUpperCase() + Consts.AT_SYMBOL + name + Consts.AT_SYMBOL + domainId, 5,
  42. LockType.REDIS);
  43. }
  44. protected void releaseLock(BaseLock lock) {
  45. // workaround for very high concurrency
  46. // do nothing, wait for the service layer transaction to commit
  47. }
  48. protected void alertNameTaken(CheckEntityEnum entity, String name) throws ServerException {
  49. log.warn("The {} name({}) is already taken", entity.getSource(), name);
  50. // throw new ServerException("The " + entity.getSource() + " name is already taken");
  51. throw new ServerException("名称:[ " + name + " ] 已经存在!");
  52. }
  53. protected void alertUnAuthorized(CheckEntityEnum entity, User user, String operation) throws ServerException {
  54. log.warn("User({}) don't have permission to {} this {}", user.getId(), operation, entity.getSource());
  55. throw new UnAuthorizedException("当前用户没有权限: " + operation + " 此资源: " + entity.getSource());
  56. }
  57. protected ProjectPermission getProjectPermission(Long projectId, User user) {
  58. try {
  59. return projectService.getProjectPermission(projectService.getProjectDetail(projectId, user, false), user);
  60. } catch (Exception e) {
  61. return null;
  62. }
  63. }
  64. private short getEntityPermission(CheckEntityEnum entity, ProjectPermission projectPermission) {
  65. short permission = (short) -1;
  66. switch (entity) {
  67. case SOURCE:
  68. permission = projectPermission.getSourcePermission();
  69. break;
  70. case CRONJOB:
  71. permission = projectPermission.getSchedulePermission();
  72. break;
  73. case DISPLAY:
  74. case DISPLAYSLIDE:
  75. case DASHBOARDPORTAL:
  76. case DASHBOARD:
  77. permission = projectPermission.getVizPermission();
  78. break;
  79. case VIEW:
  80. permission = projectPermission.getViewPermission();
  81. break;
  82. case WIDGET:
  83. permission = projectPermission.getWidgetPermission();
  84. break;
  85. default:
  86. break;
  87. }
  88. return permission;
  89. }
  90. protected void checkDeletePermission(CheckEntityEnum entity, Long projectId, User user)
  91. throws UnAuthorizedException {
  92. ProjectPermission projectPermission = getProjectPermission(projectId, user);
  93. if (projectPermission == null) {
  94. alertUnAuthorized(entity, user, "delete");
  95. }
  96. if (getEntityPermission(entity, projectPermission) < UserPermissionEnum.DELETE.getPermission()) {
  97. alertUnAuthorized(entity, user, "delete");
  98. }
  99. }
  100. protected void checkWritePermission(CheckEntityEnum entity, Long projectId, User user, String operation)
  101. throws UnAuthorizedException {
  102. ProjectPermission projectPermission = getProjectPermission(projectId, user);
  103. if (projectPermission == null) {
  104. alertUnAuthorized(entity, user, operation);
  105. }
  106. if (getEntityPermission(entity, projectPermission) < UserPermissionEnum.WRITE.getPermission()) {
  107. alertUnAuthorized(entity, user, operation);
  108. }
  109. }
  110. protected void checkSharePermission(CheckEntityEnum entity, Long projectId, User user)
  111. throws UnAuthorizedException {
  112. ProjectPermission projectPermission = getProjectPermission(projectId, user);
  113. if (projectPermission == null) {
  114. alertUnAuthorized(entity, user, "share");
  115. }
  116. if (!projectPermission.getSharePermission()) {
  117. alertUnAuthorized(entity, user, "share");
  118. }
  119. }
  120. public boolean checkReadPermission(CheckEntityEnum entity, Long projectId, User user) {
  121. ProjectPermission projectPermission = getProjectPermission(projectId, user);
  122. if (projectPermission == null) {
  123. return false;
  124. }
  125. if (getEntityPermission(entity, projectPermission) < UserPermissionEnum.READ.getPermission()) {
  126. return false;
  127. }
  128. return true;
  129. }
  130. }