role.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. # -*- coding: utf-8 -*-
  2. """
  3. walle-web
  4. :copyright: © 2015-2017 walle-web.io
  5. :created time: 2018-11-04 22:08:28
  6. :author: wushuiyong@walle-web.io
  7. """
  8. from flask import current_app, session
  9. from flask_login import login_required, current_user
  10. from functools import wraps
  11. from walle.service.code import Code
  12. from walle.service.error import WalleError
  13. GUEST = 'GUEST'
  14. REPORT = 'REPORT'
  15. DEVELOPER = 'DEVELOPER'
  16. MASTER = 'MASTER'
  17. OWNER = 'OWNER'
  18. SUPER = 'SUPER'
  19. ACCESS_ROLE = {
  20. '10': GUEST,
  21. '20': REPORT,
  22. '30': DEVELOPER,
  23. '40': MASTER,
  24. '50': OWNER,
  25. '60': SUPER,
  26. }
  27. ROLE_ACCESS = {
  28. 'GUEST': '10',
  29. 'REPORT': '20',
  30. 'DEVELOPER': '30',
  31. 'MASTER': '40',
  32. 'OWNER': '50',
  33. 'SUPER': '60',
  34. }
  35. class Permission():
  36. app = None
  37. def __init__(self, app=None):
  38. if app:
  39. self.init_app(app)
  40. def init_app(self, app):
  41. self.app = app
  42. def gte_develop_or_uid(self, func):
  43. @wraps(func)
  44. @login_required
  45. def decorator(*args, **kwargs):
  46. current_app.logger.info('============== gte_develop_or_uid.decorator ======')
  47. if self.is_gte_develop_or_uid(current_user.id):
  48. current_app.logger.info('============== gte_develop_or_uid.if ======')
  49. return func(*args, **kwargs)
  50. raise WalleError(Code.not_allow)
  51. return decorator
  52. def is_gte_develop_or_uid(self, uid=None):
  53. if uid is None:
  54. uid = current_user.id
  55. if self.enable_uid(uid) or self.enable_role(DEVELOPER):
  56. return True
  57. return False
  58. @staticmethod
  59. def list_enable(self, list, access_level):
  60. current_role = OWNER
  61. access_level = {
  62. 'create': OWNER,
  63. 'update': MASTER,
  64. 'delete': MASTER,
  65. 'online': DEVELOPER,
  66. 'audit': MASTER,
  67. 'block': DEVELOPER,
  68. }
  69. # 1 uid == current_uid && access_level >= current_role
  70. # all true
  71. # uid, project_id, space_id
  72. return {
  73. 'enable_create': OWNER,
  74. 'enable_update': MASTER,
  75. 'enable_delete': MASTER,
  76. 'enable_online': DEVELOPER,
  77. 'enable_audit': MASTER,
  78. 'enable_block': DEVELOPER,
  79. }
  80. pass
  81. # @classmethod
  82. def enable_uid(self, uid):
  83. '''
  84. 当前登录用户 == 数据用户
  85. :param uid:
  86. :return:
  87. '''
  88. # TODO
  89. # current_app.logger.info(current_user.id)
  90. # current_app.logger.info(current_user.is_active())
  91. current_app.logger.info(dir(current_user))
  92. current_app.logger.info(uid)
  93. return current_user.id == uid
  94. # @classmethod
  95. def enable_role(self, role):
  96. '''
  97. 当前角色 >= 数据项角色
  98. :param role:
  99. :return:
  100. '''
  101. # TODO about project/task
  102. current_role = session['space_info']['role']
  103. return self.compare_role(current_role, role)
  104. # @classmethod
  105. def compare_role(self, role_high, role_low):
  106. if role_high not in ROLE_ACCESS or role_low not in ROLE_ACCESS:
  107. # TODO 也可以抛出
  108. return False
  109. return ROLE_ACCESS[role_high] > ROLE_ACCESS[role_low]