app.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # **************************************************************************
  4. # Copyright © 2017 jianglin
  5. # File Name: app.py
  6. # Author: jianglin
  7. # Email: xiyang0807@gmail.com
  8. # Created: 2017-03-28 16:11:07 (CST)
  9. # Last Update:星期二 2017-9-19 12:49:24 (CST)
  10. # By:
  11. # Description:
  12. # **************************************************************************
  13. from flask_principal import RoleNeed, UserNeed, identity_loaded
  14. from flask_login import current_user
  15. from .permission import TopicNeed, ReplyNeed, CollectNeed
  16. def init_app(app):
  17. @identity_loaded.connect_via(app)
  18. def on_identity_loaded(sender, identity):
  19. '''基础权限'''
  20. identity.user = current_user
  21. if hasattr(current_user, 'id'):
  22. identity.provides.add(UserNeed(current_user.id))
  23. if hasattr(current_user, 'is_superuser'):
  24. if current_user.is_superuser:
  25. identity.provides.add(RoleNeed('super'))
  26. if hasattr(current_user, 'is_confirmed'):
  27. if current_user.is_confirmed:
  28. identity.provides.add(RoleNeed('confirmed'))
  29. if hasattr(current_user, 'is_authenticated'):
  30. if current_user.is_authenticated:
  31. identity.provides.add(RoleNeed('auth'))
  32. else:
  33. identity.provides.add(RoleNeed('guest'))
  34. if hasattr(current_user, 'topics'):
  35. for topic in current_user.topics:
  36. identity.provides.add(TopicNeed(topic.id))
  37. if hasattr(current_user, 'replies'):
  38. for reply in current_user.replies:
  39. identity.provides.add(ReplyNeed(reply.id))
  40. if hasattr(current_user, 'collects'):
  41. for collect in current_user.collects:
  42. identity.provides.add(CollectNeed(collect.id))