You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
3.0 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # This module copyright (C) 2013 Therp BV (<http://therp.nl>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. import logging
  22. from openerp.osv.orm import Model
  23. from openerp.osv import fields
  24. from openerp.tools.safe_eval import safe_eval
  25. from openerp import SUPERUSER_ID
  26. class res_groups(Model):
  27. _inherit = 'res.groups'
  28. _columns = {
  29. 'is_dynamic': fields.boolean('Dynamic'),
  30. 'dynamic_group_condition': fields.text(
  31. 'Condition', help='The condition to be met for a user to be a '
  32. 'member of this group. It is evaluated as python code at login '
  33. 'time, you get `user` passed as a browse record')
  34. }
  35. def eval_dynamic_group_condition(self, cr, uid, ids, context=None):
  36. result = True
  37. user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid,
  38. context=context)
  39. for this in self.browse(cr, uid, ids, context=context):
  40. result &= bool(
  41. safe_eval(
  42. this.dynamic_group_condition,
  43. {
  44. 'user': user,
  45. 'any': any,
  46. 'all': all,
  47. 'filter': filter,
  48. }))
  49. return result
  50. def _check_dynamic_group_condition(self, cr, uid, ids, context=None):
  51. try:
  52. for this in self.browse(cr, uid, ids, context=context):
  53. if this.is_dynamic:
  54. this.eval_dynamic_group_condition()
  55. except (NameError, SyntaxError, TypeError) as e:
  56. logging.info(e)
  57. return False
  58. return True
  59. _constraints = [
  60. (_check_dynamic_group_condition,
  61. 'The condition doesn\'t evaluate correctly!',
  62. ['dynamic_group_condition']),
  63. ]
  64. def action_evaluate(self, cr, uid, ids, context=None):
  65. user_obj = self.pool.get('res.users')
  66. for user in user_obj.browse(
  67. cr, uid,
  68. user_obj.search(cr, uid, [], context=context),
  69. context=context):
  70. user_obj.update_dynamic_groups(user.id, cr.dbname)