workflow_permission_service.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. from apps.workflow.models import WorkflowUserPermission
  2. from service.account.account_base_service import account_base_service_ins
  3. from service.base_service import BaseService
  4. from service.common.common_service import common_service_ins
  5. from service.common.constant_service import constant_service_ins
  6. class WorkflowPermissionService(BaseService):
  7. """
  8. 流程服务
  9. """
  10. def __init__(self):
  11. pass
  12. def get_workflow_id_list_by_permission(self, permission, user_type, user):
  13. """
  14. 获取操作权限
  15. :param permission:
  16. :param user_type:
  17. :param user: 支持多人,多部门
  18. :return:
  19. """
  20. if user_type not in ['app', 'user', 'department']:
  21. return False, 'user type is invalid'
  22. if not user:
  23. if user_type == 'app':
  24. return False, 'app_name is not provided'
  25. if user_type == 'user':
  26. return False, 'user is not provided'
  27. if user_type == 'department':
  28. return False, 'department is not provided'
  29. if user == 'loonflow':
  30. from apps.workflow.models import Workflow
  31. workflow_query_set = Workflow.objects.filter(is_deleted=0).all()
  32. workflow_id_list = []
  33. for workflow_obj in workflow_query_set:
  34. workflow_id_list.append(workflow_obj.id)
  35. return True, dict(workflow_id_list=workflow_id_list)
  36. result_queryset = WorkflowUserPermission.objects.filter(permission=permission, user_type=user_type,
  37. user__in=user.split(','), is_deleted=0).all()
  38. workflow_id_list = [result.workflow_id for result in result_queryset]
  39. workflow_id_list = list(set(workflow_id_list))
  40. return True, dict(workflow_id_list=workflow_id_list)
  41. def workflow_id_permission_check(self, workflow_id, permission, user_type, user):
  42. """
  43. 检查是否有某workflow_id的权限
  44. :param workflow_id:
  45. :param permission:
  46. :param user_type:
  47. :param user:
  48. :return:
  49. """
  50. if user_type == 'app' and user == 'loonflow':
  51. return True, ''
  52. workflow_query_set = WorkflowUserPermission.objects.filter(
  53. is_deleted=0, workflow_id=workflow_id, permission=permission, user_type=user_type, user=user).first()
  54. if workflow_query_set:
  55. return True, ''
  56. else:
  57. if permission == 'api':
  58. return False, 'app: {} has no api permission for workflow_id: {}'.format(user, workflow_id)
  59. if permission == 'admin':
  60. return False, 'user: {} has no admin permission for workflow_id:{}'.format(user, workflow_id)
  61. if permission == 'intervene':
  62. return False, 'user: {} has no intervene permission for workflow_id:{}'.format(user, workflow_id)
  63. if permission == 'view':
  64. if user_type == 'user':
  65. return False, 'user: {} has no view permission for workflow_id:{}'.format(user, workflow_id)
  66. if user_type == 'department':
  67. return False, 'department: {} has no view permission for workflow_id:{}'.format(user, workflow_id)
  68. return False, 'no permission'
  69. def get_record_list_by_app_list(self, app_list):
  70. """
  71. 批量获取应用的workflow权限
  72. :param app_list:
  73. :return:
  74. """
  75. permission_query_set = WorkflowUserPermission.objects.filter(
  76. is_deleted=0, permission='api', user_type='app', user__in=app_list).all()
  77. return True, dict(permission_query_set=permission_query_set)
  78. def update_app_permission(self, app_name, workflow_ids):
  79. """
  80. 更新应用的权限
  81. :param app_name:
  82. :param workflow_ids:
  83. :return:
  84. """
  85. if workflow_ids:
  86. workflow_id_list = [int(workflow_id) for workflow_id in workflow_ids.split(',')]
  87. else:
  88. workflow_id_list = []
  89. permission_query_set = WorkflowUserPermission.objects.filter(
  90. is_deleted=0, permission='api', user_type='app', user=app_name).all()
  91. exist_workflow_id_list = [permission_query.workflow_id for permission_query in permission_query_set]
  92. flag, need_add_workflow_list = common_service_ins.list_difference(workflow_id_list, exist_workflow_id_list)
  93. if flag is False:
  94. return False, need_add_workflow_list
  95. flag, need_del_workflow_list = common_service_ins.list_difference(exist_workflow_id_list, workflow_id_list)
  96. if flag is False:
  97. return False, need_del_workflow_list
  98. add_permission_query_list = []
  99. for workflow_id in need_add_workflow_list:
  100. add_permission_query_list.append(WorkflowUserPermission(permission='api', user_type='app', user=app_name, workflow_id=workflow_id))
  101. WorkflowUserPermission.objects.bulk_create(add_permission_query_list)
  102. WorkflowUserPermission.objects.filter(
  103. is_deleted=0, permission='api', user_type='app', user=app_name, workflow_id__in=need_del_workflow_list).update(is_deleted=1)
  104. return True, ''
  105. def del_app_permission(self, app_name, workflow_ids=None):
  106. """
  107. 删除应用权限
  108. :param app_name:
  109. :param workflow_ids:
  110. :return:
  111. """
  112. if workflow_ids == None:
  113. WorkflowUserPermission.objects.filter(
  114. is_deleted=0, permission='api', user_type='app', user=app_name).update(is_deleted=1)
  115. else:
  116. WorkflowUserPermission.objects.filter(
  117. is_deleted=0, permission='api', user_type='app', user=app_name, workflow_id__in=workflow_ids.split(',')).update(is_deleted=1)
  118. return True, ''
  119. def manage_workflow_permission_check(self, workflow_id, username, app_name):
  120. """
  121. 用户是否有管理工作流的权限
  122. :param workflow_id:
  123. :param username:
  124. :param app_name:
  125. :return:
  126. """
  127. # 判断应用是否有工作流的权限
  128. flag, msg = self.workflow_id_permission_check(workflow_id, 'api', 'app', app_name)
  129. if flag is False:
  130. return flag, msg
  131. # 工作流创建人有管理权限
  132. from service.workflow.workflow_base_service import workflow_base_service_ins
  133. flag, workflow_obj = workflow_base_service_ins.get_by_id(workflow_id)
  134. if workflow_obj.creator == username:
  135. return True, "creator has workflow's manage permission"
  136. # 超级管理员拥有所有工作流的管理权限
  137. flag, user_obj = account_base_service_ins.get_user_by_username(username)
  138. if flag is False:
  139. return flag, user_obj
  140. if user_obj.type_id == constant_service_ins.ACCOUNT_TYPE_SUPER_ADMIN:
  141. return True, "superuser has all workflow's manage permission"
  142. flag, msg = self.workflow_id_permission_check(workflow_id, 'admin', 'user', username)
  143. return flag, msg
  144. workflow_permission_service_ins = WorkflowPermissionService()