permission.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/usr/bin/env python
  2. # -*- coding=UTF-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2016 jianglin
  5. # File Name: permission.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2016-06-09 19:53:35 (CST)
  9. # Last Update:星期三 2016-6-15 17:50:37 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (request, abort, current_app, redirect, jsonify, url_for,
  14. flash, g)
  15. from flask_principal import Permission, RoleNeed, UserNeed, identity_loaded
  16. from flask_login import current_user, login_required
  17. from maple import app
  18. from collections import namedtuple
  19. from functools import partial, wraps
  20. TopicNeed = namedtuple('topic', ['method', 'value'])
  21. EditTopicNeed = partial(TopicNeed, 'edit')
  22. class EditTopicPermission(Permission):
  23. def __init__(self, topic_id):
  24. need = EditTopicNeed(topic_id)
  25. super(EditTopicPermission, self).__init__(need)
  26. class BasePermission(object):
  27. decorators = ()
  28. def __call__(self, func):
  29. if self.decorators:
  30. for dec in self.decorators:
  31. return dec(func)
  32. @wraps(func)
  33. def decorator(*args, **kwargs):
  34. meth = getattr(self, request.method.lower(), None)
  35. if meth is None and request.method == 'HEAD':
  36. meth = getattr(self, 'get', None)
  37. assert meth is not None, 'Unimplemented method %r' % request.method
  38. check = meth(*args, **kwargs)
  39. if check:
  40. return check
  41. else:
  42. pass
  43. return func(*args, **kwargs)
  44. return decorator
  45. class TopicPermission(BasePermission):
  46. @login_required
  47. def post(self):
  48. pass
  49. def get(self, uid):
  50. pass
  51. @login_required
  52. def put(self, uid):
  53. permission = EditTopicPermission(uid)
  54. if not permission.can():
  55. flash('你没有权限')
  56. return redirect(url_for('topic.topic', uid=uid))
  57. @login_required
  58. def delete(self):
  59. pass
  60. class ReplyPermission(BasePermission):
  61. decorators = [login_required]
  62. def post(self, uid):
  63. pass
  64. def put(self, uid):
  65. pass
  66. def delete(self, uid):
  67. pass
  68. class FollowPermission(BasePermission):
  69. decorators = [login_required]
  70. def get(self, type):
  71. pass
  72. def post(self, uid):
  73. pass
  74. def delete(self, uid):
  75. pass
  76. class CollectPermission(BasePermission):
  77. decorators = [login_required]
  78. def get(self, type):
  79. pass
  80. def post(self, uid):
  81. pass
  82. def put(self, uid):
  83. pass
  84. def delete(self, uid):
  85. pass
  86. class TagPermission(BasePermission):
  87. def get(self, tag):
  88. pass
  89. @login_required
  90. def post(self, tag):
  91. pass
  92. @login_required
  93. def put(self, tag):
  94. pass
  95. class LikePermission(BasePermission):
  96. def post(self):
  97. if not g.user.is_authenticated:
  98. return jsonify(judge=False, url=url_for('auth.login'))
  99. def delete(self):
  100. if not g.user.is_authenticated:
  101. return jsonify(judge=False, url=url_for('auth.login'))
  102. topic_permission = TopicPermission()
  103. reply_permission = ReplyPermission()
  104. follow_permission = FollowPermission()
  105. collect_permission = CollectPermission()
  106. tag_permission = TagPermission()
  107. like_permission = LikePermission()
  108. super_permission = Permission(RoleNeed('super'))
  109. @identity_loaded.connect_via(app)
  110. def on_identity_loaded(sender, identity):
  111. '''基础权限'''
  112. identity.user = current_user
  113. if hasattr(current_user, 'id'):
  114. identity.provides.add(UserNeed(current_user.id))
  115. if hasattr(current_user, 'roles'):
  116. for role in current_user.roles:
  117. identity.provides.add(RoleNeed(role.rolename))
  118. if hasattr(current_user, 'is_superuser'):
  119. if current_user.is_superuser:
  120. identity.provides.add(RoleNeed('super'))
  121. if hasattr(current_user, 'topics'):
  122. for topic in current_user.topics:
  123. identity.provides.add(EditTopicNeed(topic.id))