# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    This module copyright (C) 2013 Therp BV (<http://therp.nl>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from openerp.models import Model
from openerp.modules.registry import RegistryManager
from openerp import SUPERUSER_ID


class res_users(Model):
    _inherit = 'res.users'

    def _login(self, db, login, password):
        uid = super(res_users, self)._login(db, login, password)

        if uid:
            self.update_dynamic_groups(uid, db)

        return uid

    def update_dynamic_groups(self, uid, db):
        cr = self.pool._db.cursor(serialized=False)
        user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid)
        groups_obj = self.pool.get('res.groups')
        user.write(
            {
                'groups_id': [
                    (4, dynamic_group.id)
                    if dynamic_group.eval_dynamic_group_condition(uid=uid)
                    else (3, dynamic_group.id)
                    for dynamic_group in groups_obj.browse(
                        cr, SUPERUSER_ID,
                        groups_obj.search(cr, SUPERUSER_ID,
                                          [('is_dynamic', '=', True)]))
                ],
            })
        cr.commit()
        cr.close()

@@ -1015,61 +1015,8 @@
 del%0A
-from openerp.modules.registry import RegistryManager%0A
 from
@@ -1398,20 +1398,26 @@
         
-user
+groups_obj
  = self.
@@ -1434,114 +1434,198 @@
 res.
-users').browse(cr, SUPERUSER_ID, uid)%0A        groups_obj = self.pool.get('res.groups')%0A        user.write(
+groups')%0A        try:%0A            dynamic_groups = groups_obj.browse(%0A                cr, SUPERUSER_ID, groups_obj.search(%0A                    cr, SUPERUSER_ID, %5B('is_dynamic', '=', True)%5D))
 %0A   
@@ -1637,9 +1637,19 @@
     
-%7B
+cr.execute(
 %0A   
@@ -1666,26 +1666,70 @@
    '
-groups_id': %5B%0A    
+delete from res_groups_users_rel where uid=%25s and gid in %25s',%0A
     
@@ -1745,11 +1745,19 @@
    (
-4, 
+uid, tuple(
 dyna
@@ -1761,33 +1761,82 @@
 ynamic_group
+s
 .id
+s))
 )%0A    
+        for dynamic_group in dynamic_groups:%0A
             
@@ -1893,16 +1893,17 @@
 uid=uid)
+:
 %0A       
@@ -1919,94 +1919,97 @@
     
-else (3, dynamic_group.id)%0A                    for dynamic_group in groups_obj.browse(
+cr.execute(%0A                        'insert into res_groups_users_rel (uid, gid) values '
 %0A   
@@ -2029,32 +2029,26 @@
         
-cr, SUPERUSER_ID
+'(%25s, %25s)'
 ,%0A      
@@ -2069,170 +2069,143 @@
     
-groups_obj.search(cr, SUPERUSER_ID,%0A                                          %5B('is_dynamic', '=', True)%5D))%0A                %5D,%0A            %7D)%0A        cr.commit()%0A
+(uid, dynamic_group.id))%0A            self.invalidate_cache(cr, uid, %5B'groups_id'%5D, %5Buid%5D)%0A            cr.commit()%0A        finally:%0A    
     

update groups with sql