auth.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # -*- coding: utf-8 -*-
  2. """Predicates for authorizations"""
  3. from tg.predicates import Predicate
  4. from pod.model import DBSession as session
  5. from pod.model.auth import Permission, User
  6. import logging as l
  7. DIRTY_canReadOrCanWriteSqlQuery = """
  8. SELECT
  9. pgn.node_id
  10. FROM
  11. pod_group_node AS pgn
  12. JOIN pod_nodes AS pn ON pn.node_id = pgn.node_id AND pn.is_shared = 't'
  13. JOIN pod_user_group AS pug ON pug.group_id = pgn.group_id
  14. JOIN pod_user AS pu ON pug.user_id = pu.user_id
  15. WHERE
  16. rights > :excluded_right_low_level
  17. AND email_address = :email
  18. AND pgn.node_id = :node_id
  19. UNION
  20. SELECT
  21. pnn.node_id
  22. FROM
  23. pod_nodes AS pnn,
  24. pod_user AS puu
  25. WHERE
  26. pnn.node_id = :node_id
  27. AND pnn.owner_id = puu.user_id
  28. AND puu.email_address = :email
  29. """
  30. class can_read(Predicate):
  31. message = ""
  32. def __init__(self, **kwargs):
  33. pass
  34. def evaluate(self, environ, credentials):
  35. if 'node_id' in environ['webob.adhoc_attrs']['validation']['values']:
  36. node_id = environ['webob.adhoc_attrs']['validation']['values']['node_id']
  37. if node_id!=0:
  38. has_right = session.execute(
  39. DIRTY_canReadOrCanWriteSqlQuery,
  40. {"email":credentials["repoze.who.userid"], "node_id":node_id, "excluded_right_low_level": 0}
  41. )
  42. if has_right.rowcount == 0 :
  43. l.info("User {} don't have read right on node {}".format(credentials["repoze.who.userid"], node_id))
  44. self.unmet()
  45. class can_write(Predicate):
  46. message = ""
  47. def __init__(self, **kwargs):
  48. pass
  49. def evaluate(self, environ, credentials):
  50. node_id = environ['webob.adhoc_attrs']['validation']['values']['node_id']
  51. if node_id!=0:
  52. has_right = session.execute(
  53. DIRTY_canReadOrCanWriteSqlQuery,
  54. {"email":credentials["repoze.who.userid"], "node_id":node_id, "excluded_right_low_level": 1}
  55. )
  56. if has_right.rowcount == 0 :
  57. self.unmet()