permission.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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-07-16 17:18:48 (CST)
  9. # Last Update:星期日 2016-7-24 12:57:59 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask import (url_for, jsonify, g, flash, redirect, request, abort)
  14. from flask_login import login_required
  15. from flask_principal import Permission
  16. from maple.permission.base import RestBase
  17. from maple.permission.permission import GetCollect, PostCollect
  18. class FollowPermission(RestBase):
  19. decorators = [login_required]
  20. def get(self, type):
  21. if type is not None:
  22. type_list = ['tag', 'topic', 'user', 'collect']
  23. if type not in type_list:
  24. return True
  25. def put(self, type):
  26. return True
  27. def post(self, type):
  28. type_list = ['tag', 'topic', 'user', 'collect']
  29. if type not in type_list:
  30. return True
  31. def delete(self, type):
  32. type_list = ['tag', 'topic', 'user', 'collect']
  33. if type not in type_list:
  34. return True
  35. class CollectPermission(RestBase):
  36. decorators = [login_required]
  37. def put(self, collectId):
  38. permission = Permission(GetCollect(collectId))
  39. if not permission.can():
  40. return True
  41. def delete(self, collectId):
  42. permission = Permission(GetCollect(collectId))
  43. if not permission.can():
  44. return True
  45. def callback(self):
  46. flash('你没有权限', 'warning')
  47. return redirect(url_for('forums.index'))
  48. class CollectDetailPermission(RestBase):
  49. decorators = [login_required]
  50. def get(self, collectId):
  51. if collectId is not None:
  52. permission = Permission(GetCollect(collectId))
  53. if not permission.can():
  54. return True
  55. def post(self):
  56. form = request.form.getlist('add-to-collect')
  57. for collectId in form:
  58. try:
  59. collectId = int(collectId)
  60. permission = Permission(PostCollect(collectId))
  61. if not permission.can():
  62. return True
  63. except ValueError:
  64. abort(403)
  65. def put(self, collectId):
  66. permission = Permission(GetCollect(collectId))
  67. if not permission.can():
  68. return True
  69. def delete(self, collectId):
  70. permission = Permission(GetCollect(collectId))
  71. if not permission.can():
  72. return True
  73. def callback(self):
  74. flash('你没有权限', 'warning')
  75. return redirect(url_for('mine.collect'))
  76. class LikePermission(RestBase):
  77. def post(self, replyId):
  78. if not g.user.is_authenticated:
  79. return jsonify(judge=False, url=url_for('auth.login'))
  80. def delete(self, replyId):
  81. if not g.user.is_authenticated:
  82. return jsonify(judge=False, url=url_for('auth.login'))
  83. follow_permission = FollowPermission()
  84. collect_permission = CollectPermission()
  85. like_permission = LikePermission()
  86. collect_detail_permission = CollectDetailPermission()