base_copy_user_access.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # -*- coding: utf-8 -*-
  2. # © 2016 OpenSynergy Indonesia
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. from openerp import models, fields, api
  5. from lxml import etree
  6. class WizardBaseCopyUserAccess(models.TransientModel):
  7. _name = 'base.copy_user_access'
  8. _description = 'Wizard Copy User Access'
  9. user_id = fields.Many2one(
  10. string='User',
  11. comodel_name='res.users',
  12. required=True
  13. )
  14. @api.model
  15. def fields_view_get(
  16. self, view_id=None, view_type='form', toolbar=False, submenu=False
  17. ):
  18. res = super(WizardBaseCopyUserAccess, self).fields_view_get(
  19. view_id=view_id, view_type=view_type,
  20. toolbar=toolbar, submenu=submenu)
  21. doc = etree.XML(res['arch'])
  22. for node in doc.xpath("//field[@name='user_id']"):
  23. active_ids = self._context.get('active_ids')
  24. domain = "[('id', 'not in', " + str(active_ids) + ")]"
  25. node.set('domain', domain)
  26. res['arch'] = etree.tostring(doc)
  27. return res
  28. @api.multi
  29. def copy_access_right(self):
  30. res = []
  31. self.ensure_one()
  32. obj_user = self.env['res.users']
  33. context = self._context
  34. record_id = context['active_ids']
  35. user = obj_user.browse(self.user_id.id)
  36. for group in user.groups_id:
  37. res.append(group.id)
  38. for data in record_id:
  39. user_id = obj_user.browse(data)
  40. vals = {
  41. 'groups_id': [(6, 0, res)],
  42. }
  43. user_id.write(vals)
  44. return {'type': 'ir.actions.act_window_close'}