auth.py 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import typing
  2. from pyramid.security import ALL_PERMISSIONS
  3. from pyramid.security import Allow
  4. from pyramid.security import Authenticated
  5. from tracim.lib.core.user import UserApi
  6. from tracim.models.auth import Group
  7. from tracim.lib.core.workspace import WorkspaceApi
  8. # INFO - G.M - 06-04-2018 - Auth for pyramid
  9. # based on this tutorial : https://docs.pylonsproject.org/projects/pyramid-cookbook/en/latest/auth/basic.html # nopep8
  10. def check_credentials(username, password, request) -> typing.Optional[dict]:
  11. permissions = None
  12. app_config = request.registry.settings['CFG']
  13. uapi = UserApi(None, session=request.dbsession, config=app_config)
  14. try:
  15. user = uapi.get_one_by_email(username)
  16. if user.validate_password(password):
  17. permissions = []
  18. for group in user.groups:
  19. permissions.append(group.group_name)
  20. # TODO - G.M - 06-04-2018 - Add workspace specific permission ?
  21. # TODO - G.M - 06-04-2018 - Better catch for exception of bad password, bad
  22. # user
  23. except:
  24. pass
  25. return permissions
  26. class Root:
  27. # root
  28. __acl__ = (
  29. (Allow, Group.TIM_ADMIN_GROUPNAME, ALL_PERMISSIONS),
  30. (Allow, Group.TIM_MANAGER_GROUPNAME, 'manager'),
  31. (Allow, Group.TIM_USER_GROUPNAME, 'user'),
  32. )