permissions.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. #*************************************************************************
  2. # Copyright © 2015 JiangLin. All rights reserved.
  3. # File Name: permissions.py
  4. # Author:JiangLin
  5. # Mail:xiyang0807@gmail.com
  6. # Created Time: 2015-12-12 20:28:00
  7. #*************************************************************************
  8. #!/usr/bin/env python
  9. # -*- coding=UTF-8 -*-
  10. from maple import app
  11. from flask_login import current_user
  12. from flask_principal import Permission, RoleNeed, UserNeed, identity_loaded
  13. from flask import abort, jsonify
  14. from functools import wraps
  15. from flask import g, redirect, flash, url_for
  16. from maple import redis_data
  17. from maple.group.models import Group
  18. from time import time
  19. from flask import request
  20. from collections import namedtuple
  21. from functools import partial
  22. Need = namedtuple('need', ['method', 'value'])
  23. EditQuestionNeed = partial(Need, 'id')
  24. PostNeed = partial(Need, 'post')
  25. GroupNeed = partial(Need, 'id')
  26. BoardNeed = partial(Need, 'id')
  27. UserNameNeed = partial(Need, 'name')
  28. ShowNeed = partial(Need, 'permission')
  29. class MyPermission(object):
  30. def __init__(self,required=None,name=None):
  31. self.required = required
  32. def __call__(self, func):
  33. @wraps(func)
  34. def decorator(*args, **kwargs):
  35. if not self.allow():
  36. return self.action()
  37. return func(*args, **kwargs)
  38. return decorator
  39. def allow(self):
  40. return False
  41. def action(self):
  42. abort(403)
  43. class QuePermission(MyPermission):
  44. def allow(self):
  45. if current_user.infor.score > 5:
  46. return True
  47. else:
  48. return False
  49. def action(self):
  50. flash('你的积分不足,不能发帖,如有问题请联系管理员')
  51. return redirect(url_for('user.index',user_url=current_user.name))
  52. class RepPermission(MyPermission):
  53. def allow(self):
  54. if current_user.infor.score > 1:
  55. return True
  56. else:
  57. return False
  58. def action(self):
  59. error = '你的积分不足,不能回复,如有问题请联系管理员'
  60. return jsonify(judge=False,error=error)
  61. class OwnPermission(MyPermission):
  62. def allow(self):
  63. if current_user.name == g.user_url:
  64. return True
  65. else:
  66. return False
  67. def action(self):
  68. return redirect(url_for('user.setting',user_url=current_user.name))
  69. class GuestPermission(MyPermission):
  70. def allow(self):
  71. if not g.user.is_authenticated:
  72. return True
  73. else:
  74. return False
  75. def action(self):
  76. flash('你已经登陆,不能重复登陆')
  77. return redirect(url_for('forums.forums'))
  78. class TimePermission(MyPermission):
  79. def allow(self):
  80. user = 'user:%s' % str(current_user.id)
  81. last_time = redis_data.hget(user, 'send_email_time')
  82. now_time = int(time()) + 28800
  83. if last_time is None:
  84. last_time = now_time
  85. return True
  86. else:
  87. last_time = int(last_time)
  88. if last_time < now_time - 3600:
  89. return True
  90. else:
  91. return False
  92. def action(self):
  93. error = u'你的验证链接还未过期,请尽快验证'
  94. return error
  95. que_permission = QuePermission()
  96. rep_permission = RepPermission()
  97. own_permission = OwnPermission()
  98. guest_permission = GuestPermission()
  99. time_permission = TimePermission()
  100. class QuestionPermission(Permission):
  101. def __init__(self, pid):
  102. need = EditQuestionNeed(int(pid))
  103. super(QuestionPermission, self).__init__(need)
  104. class PostPermission(Permission):
  105. def __init__(self):
  106. score = current_user.infor.score
  107. need = PostNeed(score)
  108. super(PostPermission, self).__init__(need)
  109. class GroupPermission(Permission):
  110. def __init__(self, uid):
  111. need = GroupNeed(int(uid))
  112. super(GroupPermission, self).__init__(need)
  113. class BoardPermission(Permission):
  114. def __init__(self, uid):
  115. need = BoardNeed(int(uid))
  116. super(BoardPermission, self).__init__(need)
  117. class OwnsPermission(Permission):
  118. def __init__(self, name):
  119. need = UserNameNeed(name)
  120. super(OwnsPermission, self).__init__(need)
  121. class ShowPermission(Permission):
  122. def __init__(self, data):
  123. need = ShowNeed(data)
  124. super(ShowPermission, self).__init__(need)
  125. super_permission = Permission(RoleNeed('super'))
  126. admin_permission = Permission(RoleNeed('admin')).union(super_permission)
  127. member_permission = Permission(RoleNeed('member')).union(admin_permission)
  128. banned_permission = Permission(RoleNeed('banned')).union(member_permission)
  129. confirm_permission = Permission(
  130. RoleNeed('confirm')).union(member_permission)
  131. # guest_permission = Permission(
  132. # RoleNeed('guest')).union(confirm_permission)
  133. show_own_permission = Permission(ShowNeed(3))
  134. show_login_permission = Permission(ShowNeed(2)).union(show_own_permission)
  135. show_all_permission = Permission(ShowNeed(1)).union(show_login_permission)
  136. @identity_loaded.connect_via(app)
  137. def on_identity_loaded(sender, identity):
  138. identity.user = current_user
  139. identity.group = Group.query.filter_by(id=34).first()
  140. if hasattr(current_user, 'id'):
  141. identity.provides.add(UserNeed(current_user.id))
  142. if hasattr(current_user, 'roles'):
  143. for role in current_user.roles:
  144. identity.provides.add(RoleNeed(role.name))
  145. if hasattr(current_user, 'is_superuser'):
  146. if current_user.is_superuser:
  147. identity.provides.add(RoleNeed('super'))
  148. # if hasattr(current_user, 'is_confirmed'):
  149. # if current_user.is_confirmed:
  150. # identity.provides.add(PostNeed(True))
  151. # if hasattr(current_user, 'questions'):
  152. # for question in current_user.questions:
  153. # identity.provides.add(EditQuestionNeed(int(question.id)))
  154. # if hasattr(current_user, 'infor'):
  155. # score = current_user.infor.score
  156. # if score > 5:
  157. # identity.provides.add(PostNeed(score))
  158. # elif score > 1:
  159. # identity.provides.add(PostNeed(score))
  160. # else:
  161. # pass
  162. # if hasattr(current_user, 'groups'):
  163. # for group in current_user.groups:
  164. # identity.provides.add(GroupNeed(int(group.id)))
  165. # if hasattr(Group, 'permission'):
  166. # identity.provides.add(ShowNeed(identity.group.permission))
  167. # print(identity)
  168. # identity.provides.add(ShowNeed(1))
  169. # identity.provides.add(ShowNeed(2))
  170. # identity.provides.add(ShowNeed(3))
  171. # print('%s\n'%identity)
  172. if hasattr(current_user, 'name'):
  173. identity.provides.add(UserNameNeed(current_user.name))
  174. # identity.allow_admin = admin_permission.allows(identity)
  175. # identity.allow_edit = editor_permission.allows(identity)
  176. # identity.allow_write = writer_permission.allows(identity)
  177. class OwnPermission(object):
  178. def required(self, role='super'):
  179. def permission(func):
  180. @wraps(func)
  181. def decorator(*args, **kwargs):
  182. if role == 'question':
  183. return self.question()
  184. elif role == 'replies':
  185. return self.rep()
  186. elif role == 'super':
  187. return self.superuser()
  188. elif role == 'own':
  189. return self.own()
  190. elif role == 'time':
  191. return self.time()
  192. else:
  193. abort(404)
  194. return func(*args, **kwargs)
  195. return decorator
  196. return permission
  197. def question(self):
  198. if not confirm_permission.can():
  199. flash('你尚未验证账户,请尽快验证')
  200. return redirect(url_for('user.index',user_url=current_user.name))
  201. if current_user.infor.score < 5:
  202. flash('你的积分不足,不能发帖,如有问题请联系管理员')
  203. return redirect(url_for('user.index',user_url=current_user.name))
  204. def super(self):
  205. if not super_permission.can():
  206. abort(404)
  207. def admin(self):
  208. if not admin_permission.can():
  209. abort(404)
  210. def member(self):
  211. if not member_permission.can():
  212. abort(404)
  213. def banned(self):
  214. if not banned_permission.can():
  215. abort(404)
  216. def confirm(self):
  217. if not confirm_permission.can():
  218. flash('你尚未验证账户,请尽快验证')
  219. return redirect(url_for('user.index',user_url=current_user.name))
  220. else:
  221. pass
  222. return redirect(url_for('user.index',user_url=current_user.name))
  223. # def question(self):
  224. # if not confirm_permission.can():
  225. # flash('你尚未验证账户,请尽快验证')
  226. # return redirect(url_for('user.index',user_url=current_user.name))
  227. # if current_user.infor.score < 5:
  228. # flash('你的积分不足,不能发帖,如有问题请联系管理员')
  229. # return redirect(url_for('user.index',user_url=current_user.name))
  230. def rep(self):
  231. if not rep_permission:
  232. flash('你尚未验证账户,请尽快验证')
  233. return redirect(url_for('user.index',user_url=current_user.name))
  234. else:
  235. pass
  236. def own(self,user):
  237. if current_user.name == user:
  238. pass
  239. else:
  240. abort(404)
  241. def time_permission(self):
  242. if request.method == "POST":
  243. user = 'user:%s' % str(current_user.id)
  244. last_time = redis_data.hget(user, 'send_email_time')
  245. now_time = int(time()) + 28800
  246. if not last_time:
  247. last_time = now_time
  248. else:
  249. last_time = int(last_time)
  250. if last_time > now_time - 3600:
  251. error = u'你的验证链接还未过期,请尽快验证'
  252. return error
  253. else:
  254. pass
  255. else:
  256. abort(404)
  257. allow = OwnPermission()
  258. # def allow_ip(user_ip):
  259. # def decorator(f):
  260. # @wraps(f)
  261. # def decorated_function(*args, **kwargs):
  262. # '''查询IP是否在黑名单中'''
  263. # visited_users = redis_data.smembers('blacklist')
  264. # if user_ip in visited_users:
  265. # abort(404)
  266. # else:
  267. # pass
  268. # return decorated_function
  269. # return decorator