/shabti/templates/auth_repozewho/+package+/lib/auth/__init__.py_tmpl

https://bitbucket.org/gawel/shabti · Unknown · 77 lines · 68 code · 9 blank · 0 comment · 0 complexity · 22a8afc579d31771b8fb9592fcf823d0 MD5 · raw file

  1. from repoze.what.plugins.quickstart import setup_sql_auth
  2. from repoze.who.plugins.friendlyform import FriendlyFormPlugin
  3. from {{package}}.model import User, Group, Permission, Session
  4. from pylons import request
  5. import logging
  6. log = logging.getLogger(__name__)
  7. # Bind values to the variables that will be provided to
  8. # :func:`setup_sql_auth`. This is a good example of the
  9. # difference between the ``.ini`` config file approach
  10. # and this one, i.e. of effecting the configuration within
  11. # the friendly confines of an executable Python source file.
  12. # Otherwise, the model entity classes would have to be
  13. # expressed as strings, to be later turned into real
  14. # references to the object's class.
  15. # The "translations" dictionary maps the repoze identity
  16. # model User Group, Permission and password validation
  17. # function to the field names and function that are actually
  18. # used in the standard Shabti elixir identity model.
  19. user_name = 'username'
  20. user_class = User
  21. group_class = Group
  22. permission_class = Permission
  23. dbsession = Session
  24. translations={'user_name': 'username',
  25. 'users': 'users',
  26. 'group_name': 'name',
  27. 'groups': 'groups',
  28. 'permission_name': 'name',
  29. 'permissions': 'permissions',
  30. 'validate_password': 'validate_password' }
  31. loginform = FriendlyFormPlugin(
  32. '/login/index', # arg - login_form_url
  33. login_handler_path = '/login_handler', # arg - login_handler_path
  34. logout_handler_path = '/logout_handler', # arg - logout_handler_path
  35. rememberer_name = 'cookie', # arg - rememberer_name
  36. post_login_url = '/login/welcome_back', # The URL/path to the post-login
  37. # page, if any.
  38. post_logout_url = '/login/see_you_later', # The URL/path to the post-logout
  39. # page, if any.
  40. )
  41. # Bind values to the variables that will be provided to
  42. # :class:`FriendlyRedirectingFormPlugin` and create an
  43. # instance of same.
  44. # ATM, an actual (configured) instance is required, ya
  45. # can't just supply the class name.
  46. def add_auth(app, skip_authentication):
  47. """Add authentication and authorization middleware to the ``app``."""
  48. return setup_sql_auth(app,
  49. user_class,
  50. group_class,
  51. permission_class,
  52. dbsession,
  53. form_plugin=loginform,
  54. form_identifies=True,
  55. cookie_secret='secretsquirrel',
  56. cookie_name='authtkt',
  57. login_url='/login/index',
  58. post_login_url='/login/welcome_back',
  59. post_logout_url='/login/see_you_later',
  60. login_counter_name='__tries',
  61. translations=translations,
  62. skip_authentication=skip_authentication)
  63. def get_user():
  64. """Return the current user's database object."""
  65. if 'repoze.who.identity' in request.environ:
  66. return request.environ.get('repoze.who.identity')['user']
  67. else:
  68. return None