1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 # Copyright (C) 2010-2014 OpenERP s.a. (<http://openerp.com>).
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as
10 # published by the Free Software Foundation, either version 3 of the
11 # License, or (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ##############################################################################
24 from functools import partial
25 from itertools import repeat
27 from lxml import etree
28 from lxml.builder import E
31 from openerp import SUPERUSER_ID, models
32 from openerp import tools
33 import openerp.exceptions
34 from openerp.osv import fields, osv, expression
35 from openerp.tools.translate import _
36 from openerp.http import request
38 _logger = logging.getLogger(__name__)
40 #----------------------------------------------------------
41 # Basic res.groups and res.users
42 #----------------------------------------------------------
44 class res_groups(osv.osv):
46 _description = "Access Groups"
47 _rec_name = 'full_name'
50 def _get_full_name(self, cr, uid, ids, field, arg, context=None):
52 for g in self.browse(cr, uid, ids, context):
54 res[g.id] = '%s / %s' % (g.category_id.name, g.name)
59 def _search_group(self, cr, uid, obj, name, args, context=None):
63 if isinstance(operand, bool):
64 domains = [[('name', operator, operand)], [('category_id.name', operator, operand)]]
65 if operator in expression.NEGATIVE_TERM_OPERATORS == (not operand):
66 return expression.AND(domains)
68 return expression.OR(domains)
69 if isinstance(operand, basestring):
74 values = filter(bool, group.split('/'))
75 group_name = values.pop().strip()
76 category_name = values and '/'.join(values).strip() or group_name
77 group_domain = [('name', operator, lst and [group_name] or group_name)]
78 category_domain = [('category_id.name', operator, lst and [category_name] or category_name)]
79 if operator in expression.NEGATIVE_TERM_OPERATORS and not values:
80 category_domain = expression.OR([category_domain, [('category_id', '=', False)]])
81 if (operator in expression.NEGATIVE_TERM_OPERATORS) == (not values):
82 sub_where = expression.AND([group_domain, category_domain])
84 sub_where = expression.OR([group_domain, category_domain])
85 if operator in expression.NEGATIVE_TERM_OPERATORS:
86 where = expression.AND([where, sub_where])
88 where = expression.OR([where, sub_where])
92 'name': fields.char('Name', required=True, translate=True),
93 'users': fields.many2many('res.users', 'res_groups_users_rel', 'gid', 'uid', 'Users'),
94 'model_access': fields.one2many('ir.model.access', 'group_id', 'Access Controls', copy=True),
95 'rule_groups': fields.many2many('ir.rule', 'rule_group_rel',
96 'group_id', 'rule_group_id', 'Rules', domain=[('global', '=', False)]),
97 'menu_access': fields.many2many('ir.ui.menu', 'ir_ui_menu_group_rel', 'gid', 'menu_id', 'Access Menu'),
98 'view_access': fields.many2many('ir.ui.view', 'ir_ui_view_group_rel', 'group_id', 'view_id', 'Views'),
99 'comment' : fields.text('Comment', size=250, translate=True),
100 'category_id': fields.many2one('ir.module.category', 'Application', select=True),
101 'full_name': fields.function(_get_full_name, type='char', string='Group Name', fnct_search=_search_group),
105 ('name_uniq', 'unique (category_id, name)', 'The name of the group must be unique within an application!')
108 def search(self, cr, uid, args, offset=0, limit=None, order=None, context=None, count=False):
109 # add explicit ordering if search is sorted on full_name
110 if order and order.startswith('full_name'):
111 ids = super(res_groups, self).search(cr, uid, args, context=context)
112 gs = self.browse(cr, uid, ids, context)
113 gs.sort(key=lambda g: g.full_name, reverse=order.endswith('DESC'))
114 gs = gs[offset:offset+limit] if limit else gs[offset:]
116 return super(res_groups, self).search(cr, uid, args, offset, limit, order, context, count)
118 def copy(self, cr, uid, id, default=None, context=None):
119 group_name = self.read(cr, uid, [id], ['name'])[0]['name']
120 default.update({'name': _('%s (copy)')%group_name})
121 return super(res_groups, self).copy(cr, uid, id, default, context)
123 def write(self, cr, uid, ids, vals, context=None):
125 if vals['name'].startswith('-'):
126 raise osv.except_osv(_('Error'),
127 _('The name of the group can not start with "-"'))
128 res = super(res_groups, self).write(cr, uid, ids, vals, context=context)
129 self.pool['ir.model.access'].call_cache_clearing_methods(cr)
130 self.pool['res.users'].has_group.clear_cache(self.pool['res.users'])
133 class res_users(osv.osv):
134 """ User class. A res.users record models an OpenERP user and is different
137 res.users class now inherits from res.partner. The partner model is
138 used to store the data related to the partner: lang, name, address,
139 avatar, ... The user model is now dedicated to technical data.
144 'res.partner': 'partner_id',
147 _description = 'Users'
149 def _set_new_password(self, cr, uid, id, name, value, args, context=None):
151 # Do not update the password if no value is provided, ignore silently.
152 # For example web client submits False values for all empty fields.
155 # To change their own password users must use the client-specific change password wizard,
156 # so that the new password is immediately used for further RPC requests, otherwise the user
157 # will face unexpected 'Access Denied' exceptions.
158 raise osv.except_osv(_('Operation Canceled'), _('Please use the change password wizard (in User Preferences or User menu) to change your own password.'))
159 self.write(cr, uid, id, {'password': value})
161 def _get_password(self, cr, uid, ids, arg, karg, context=None):
162 return dict.fromkeys(ids, '')
165 'id': fields.integer('ID'),
166 'login_date': fields.datetime('Latest connection', select=1, copy=False),
167 'partner_id': fields.many2one('res.partner', required=True,
168 string='Related Partner', ondelete='restrict',
169 help='Partner-related data of the user', auto_join=True),
170 'login': fields.char('Login', size=64, required=True,
171 help="Used to log into the system"),
172 'password': fields.char('Password', size=64, invisible=True, copy=False,
173 help="Keep empty if you don't want the user to be able to connect on the system."),
174 'new_password': fields.function(_get_password, type='char', size=64,
175 fnct_inv=_set_new_password, string='Set Password',
176 help="Specify a value only when creating a user or if you're "\
177 "changing the user's password, otherwise leave empty. After "\
178 "a change of password, the user has to login again."),
179 'signature': fields.html('Signature'),
180 'active': fields.boolean('Active'),
181 'action_id': fields.many2one('ir.actions.actions', 'Home Action', help="If specified, this action will be opened at log on for this user, in addition to the standard menu."),
182 'groups_id': fields.many2many('res.groups', 'res_groups_users_rel', 'uid', 'gid', 'Groups'),
183 # Special behavior for this field: res.company.search() will only return the companies
184 # available to the current user (should be the user's companies?), when the user_preference
186 'company_id': fields.many2one('res.company', 'Company', required=True,
187 help='The company this user is currently working for.', context={'user_preference': True}),
188 'company_ids':fields.many2many('res.company','res_company_users_rel','user_id','cid','Companies'),
191 # overridden inherited fields to bypass access rights, in case you have
192 # access to the user but not its corresponding partner
193 name = openerp.fields.Char(related='partner_id.name', inherited=True)
194 email = openerp.fields.Char(related='partner_id.email', inherited=True)
196 def on_change_login(self, cr, uid, ids, login, context=None):
197 if login and tools.single_email_re.match(login):
198 return {'value': {'email': login}}
201 def onchange_state(self, cr, uid, ids, state_id, context=None):
202 partner_ids = [user.partner_id.id for user in self.browse(cr, uid, ids, context=context)]
203 return self.pool.get('res.partner').onchange_state(cr, uid, partner_ids, state_id, context=context)
205 def onchange_type(self, cr, uid, ids, is_company, context=None):
206 """ Wrapper on the user.partner onchange_type, because some calls to the
207 partner form view applied to the user may trigger the
208 partner.onchange_type method, but applied to the user object.
210 partner_ids = [user.partner_id.id for user in self.browse(cr, uid, ids, context=context)]
211 return self.pool['res.partner'].onchange_type(cr, uid, partner_ids, is_company, context=context)
213 def onchange_address(self, cr, uid, ids, use_parent_address, parent_id, context=None):
214 """ Wrapper on the user.partner onchange_address, because some calls to the
215 partner form view applied to the user may trigger the
216 partner.onchange_type method, but applied to the user object.
218 partner_ids = [user.partner_id.id for user in self.browse(cr, uid, ids, context=context)]
219 return self.pool['res.partner'].onchange_address(cr, uid, partner_ids, use_parent_address, parent_id, context=context)
221 def _check_company(self, cr, uid, ids, context=None):
222 return all(((this.company_id in this.company_ids) or not this.company_ids) for this in self.browse(cr, uid, ids, context))
225 (_check_company, 'The chosen company is not in the allowed companies for this user', ['company_id', 'company_ids']),
229 ('login_key', 'UNIQUE (login)', 'You can not have two users with the same login !')
232 def _get_company(self,cr, uid, context=None, uid2=False):
235 # Use read() to compute default company, and pass load=_classic_write to
236 # avoid useless name_get() calls. This will avoid prefetching fields
237 # while computing default values for new db columns, as the
238 # db backend may not be fully initialized yet.
239 user_data = self.pool['res.users'].read(cr, uid, uid2, ['company_id'],
240 context=context, load='_classic_write')
241 comp_id = user_data['company_id']
242 return comp_id or False
244 def _get_companies(self, cr, uid, context=None):
245 c = self._get_company(cr, uid, context)
250 def _get_group(self,cr, uid, context=None):
251 dataobj = self.pool.get('ir.model.data')
254 dummy,group_id = dataobj.get_object_reference(cr, SUPERUSER_ID, 'base', 'group_user')
255 result.append(group_id)
256 dummy,group_id = dataobj.get_object_reference(cr, SUPERUSER_ID, 'base', 'group_partner_manager')
257 result.append(group_id)
259 # If these groups does not exists anymore
263 def _get_default_image(self, cr, uid, context=None):
264 return self.pool['res.partner']._get_default_image(cr, uid, False, colorize=True, context=context)
270 'company_id': _get_company,
271 'company_ids': _get_companies,
272 'groups_id': _get_group,
273 'image': _get_default_image,
276 # User can write on a few of his own fields (but not his groups for example)
277 SELF_WRITEABLE_FIELDS = ['password', 'signature', 'action_id', 'company_id', 'email', 'name', 'image', 'image_medium', 'image_small', 'lang', 'tz']
278 # User can read a few of his own fields
279 SELF_READABLE_FIELDS = ['signature', 'company_id', 'login', 'email', 'name', 'image', 'image_medium', 'image_small', 'lang', 'tz', 'tz_offset', 'groups_id', 'partner_id', '__last_update']
281 def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
282 def override_password(o):
283 if 'password' in o and ('id' not in o or o['id'] != uid):
284 o['password'] = '********'
287 if fields and (ids == [uid] or ids == uid):
289 if not (key in self.SELF_READABLE_FIELDS or key.startswith('context_')):
292 # safe fields only, so we read as super-user to bypass access rights
295 result = super(res_users, self).read(cr, uid, ids, fields=fields, context=context, load=load)
296 canwrite = self.pool['ir.model.access'].check(cr, uid, 'res.users', 'write', False)
298 if isinstance(ids, (int, long)):
299 result = override_password(result)
301 result = map(override_password, result)
305 def create(self, cr, uid, vals, context=None):
306 user_id = super(res_users, self).create(cr, uid, vals, context=context)
307 user = self.browse(cr, uid, user_id, context=context)
308 if user.partner_id.company_id:
309 user.partner_id.write({'company_id': user.company_id.id})
312 def write(self, cr, uid, ids, values, context=None):
313 if not hasattr(ids, '__iter__'):
316 for key in values.keys():
317 if not (key in self.SELF_WRITEABLE_FIELDS or key.startswith('context_')):
320 if 'company_id' in values:
321 user = self.browse(cr, SUPERUSER_ID, uid, context=context)
322 if not (values['company_id'] in user.company_ids.ids):
323 del values['company_id']
324 uid = 1 # safe fields only, so we write as super-user to bypass access rights
326 res = super(res_users, self).write(cr, uid, ids, values, context=context)
327 if 'company_id' in values:
328 for user in self.browse(cr, uid, ids, context=context):
329 # if partner is global we keep it that way
330 if user.partner_id.company_id and user.partner_id.company_id.id != values['company_id']:
331 user.partner_id.write({'company_id': user.company_id.id})
332 # clear caches linked to the users
333 self.pool['ir.model.access'].call_cache_clearing_methods(cr)
334 clear = partial(self.pool['ir.rule'].clear_cache, cr)
337 if db in self._uid_cache:
339 if id in self._uid_cache[db]:
340 del self._uid_cache[db][id]
341 self.context_get.clear_cache(self)
342 self.has_group.clear_cache(self)
345 def unlink(self, cr, uid, ids, context=None):
347 raise osv.except_osv(_('Can not remove root user!'), _('You can not remove the admin user as it is used internally for resources created by Odoo (updates, module installation, ...)'))
349 if db in self._uid_cache:
351 if id in self._uid_cache[db]:
352 del self._uid_cache[db][id]
353 return super(res_users, self).unlink(cr, uid, ids, context=context)
355 def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
361 if name and operator in ['=', 'ilike']:
362 ids = self.search(cr, user, [('login','=',name)]+ args, limit=limit, context=context)
364 ids = self.search(cr, user, [('name',operator,name)]+ args, limit=limit, context=context)
365 return self.name_get(cr, user, ids, context=context)
367 def copy(self, cr, uid, id, default=None, context=None):
368 user2copy = self.read(cr, uid, [id], ['login','name'])[0]
369 default = dict(default or {})
370 if ('name' not in default) and ('partner_id' not in default):
371 default['name'] = _("%s (copy)") % user2copy['name']
372 if 'login' not in default:
373 default['login'] = _("%s (copy)") % user2copy['login']
374 return super(res_users, self).copy(cr, uid, id, default, context)
376 @tools.ormcache(skiparg=2)
377 def context_get(self, cr, uid, context=None):
378 user = self.browse(cr, SUPERUSER_ID, uid, context)
380 for k in self._fields:
381 if k.startswith('context_'):
383 elif k in ['lang', 'tz']:
388 res = getattr(user, k) or False
389 if isinstance(res, models.BaseModel):
391 result[context_key] = res or False
394 def action_get(self, cr, uid, context=None):
395 dataobj = self.pool['ir.model.data']
396 data_id = dataobj._get_id(cr, SUPERUSER_ID, 'base', 'action_res_users_my')
397 return dataobj.browse(cr, uid, data_id, context=context).res_id
399 def check_super(self, passwd):
400 if passwd == tools.config['admin_passwd']:
403 raise openerp.exceptions.AccessDenied()
405 def check_credentials(self, cr, uid, password):
406 """ Override this method to plug additional authentication methods"""
407 res = self.search(cr, SUPERUSER_ID, [('id','=',uid),('password','=',password)])
409 raise openerp.exceptions.AccessDenied()
411 def _login(self, db, login, password):
415 cr = self.pool.cursor()
417 # autocommit: our single update request will be performed atomically.
418 # (In this way, there is no opportunity to have two transactions
419 # interleaving their cr.execute()..cr.commit() calls and have one
420 # of them rolled back due to a concurrent access.)
422 # check if user exists
423 res = self.search(cr, SUPERUSER_ID, [('login','=',login)])
427 self.check_credentials(cr, user_id, password)
428 # We effectively unconditionally write the res_users line.
429 # Even w/ autocommit there's a chance the user row will be locked,
430 # in which case we can't delay the login just for the purpose of
431 # update the last login date - hence we use FOR UPDATE NOWAIT to
432 # try to get the lock - fail-fast
433 # Failing to acquire the lock on the res_users row probably means
434 # another request is holding it. No big deal, we don't want to
435 # prevent/delay login in that case. It will also have been logged
436 # as a SQL error, if anyone cares.
438 # NO KEY introduced in PostgreSQL 9.3 http://www.postgresql.org/docs/9.3/static/release-9-3.html#AEN115299
439 update_clause = 'NO KEY UPDATE' if cr._cnx.server_version >= 90300 else 'UPDATE'
440 cr.execute("SELECT id FROM res_users WHERE id=%%s FOR %s NOWAIT" % update_clause, (user_id,), log_exceptions=False)
441 cr.execute("UPDATE res_users SET login_date = now() AT TIME ZONE 'UTC' WHERE id=%s", (user_id,))
442 self.invalidate_cache(cr, user_id, ['login_date'], [user_id])
444 _logger.debug("Failed to update last_login for db:%s login:%s", db, login, exc_info=True)
445 except openerp.exceptions.AccessDenied:
446 _logger.info("Login failed for db:%s login:%s", db, login)
453 def authenticate(self, db, login, password, user_agent_env):
454 """Verifies and returns the user ID corresponding to the given
455 ``login`` and ``password`` combination, or False if there was
458 :param str db: the database on which user is trying to authenticate
459 :param str login: username
460 :param str password: user password
461 :param dict user_agent_env: environment dictionary describing any
462 relevant environment attributes
464 uid = self._login(db, login, password)
465 if uid == openerp.SUPERUSER_ID:
466 # Successfully logged in as admin!
467 # Attempt to guess the web base url...
468 if user_agent_env and user_agent_env.get('base_location'):
469 cr = self.pool.cursor()
471 base = user_agent_env['base_location']
472 ICP = self.pool['ir.config_parameter']
473 if not ICP.get_param(cr, uid, 'web.base.url.freeze'):
474 ICP.set_param(cr, uid, 'web.base.url', base)
477 _logger.exception("Failed to update web.base.url configuration parameter")
482 def check(self, db, uid, passwd):
483 """Verifies that the given (uid, password) is authorized for the database ``db`` and
484 raise an exception if it is not."""
486 # empty passwords disallowed for obvious security reasons
487 raise openerp.exceptions.AccessDenied()
488 if self._uid_cache.get(db, {}).get(uid) == passwd:
490 cr = self.pool.cursor()
492 self.check_credentials(cr, uid, passwd)
493 if self._uid_cache.has_key(db):
494 self._uid_cache[db][uid] = passwd
496 self._uid_cache[db] = {uid:passwd}
500 def change_password(self, cr, uid, old_passwd, new_passwd, context=None):
501 """Change current user password. Old password must be provided explicitly
502 to prevent hijacking an existing user session, or for cases where the cleartext
503 password is not used to authenticate requests.
506 :raise: openerp.exceptions.AccessDenied when old password is wrong
507 :raise: except_osv when new password is not set or empty
509 self.check(cr.dbname, uid, old_passwd)
511 return self.write(cr, uid, uid, {'password': new_passwd})
512 raise osv.except_osv(_('Warning!'), _("Setting empty passwords is not allowed for security reasons!"))
514 def preference_save(self, cr, uid, ids, context=None):
516 'type': 'ir.actions.client',
517 'tag': 'reload_context',
520 def preference_change_password(self, cr, uid, ids, context=None):
522 'type': 'ir.actions.client',
523 'tag': 'change_password',
527 @tools.ormcache(skiparg=2)
528 def has_group(self, cr, uid, group_ext_id):
529 """Checks whether user belongs to given group.
531 :param str group_ext_id: external ID (XML ID) of the group.
532 Must be provided in fully-qualified form (``module.ext_id``), as there
533 is no implicit module to use..
534 :return: True if the current user is a member of the group with the
535 given external ID (XML ID), else False.
537 assert group_ext_id and '.' in group_ext_id, "External ID must be fully qualified"
538 module, ext_id = group_ext_id.split('.')
539 cr.execute("""SELECT 1 FROM res_groups_users_rel WHERE uid=%s AND gid IN
540 (SELECT res_id FROM ir_model_data WHERE module=%s AND name=%s)""",
541 (uid, module, ext_id))
542 return bool(cr.fetchone())
544 #----------------------------------------------------------
547 # Extension of res.groups and res.users with a relation for "implied"
548 # or "inherited" groups. Once a user belongs to a group, it
549 # automatically belongs to the implied groups (transitively).
550 #----------------------------------------------------------
553 """ A cset (constrained set) is a set of elements that may be constrained to
554 be a subset of other csets. Elements added to a cset are automatically
555 added to its supersets. Cycles in the subset constraints are supported.
557 def __init__(self, xs):
558 self.supersets = set()
559 self.elements = set(xs)
560 def subsetof(self, other):
561 if other is not self:
562 self.supersets.add(other)
563 other.update(self.elements)
564 def update(self, xs):
565 xs = set(xs) - self.elements
566 if xs: # xs will eventually be empty in case of a cycle
567 self.elements.update(xs)
568 for s in self.supersets:
571 return iter(self.elements)
573 concat = itertools.chain.from_iterable
575 class groups_implied(osv.osv):
576 _inherit = 'res.groups'
578 def _get_trans_implied(self, cr, uid, ids, field, arg, context=None):
579 "computes the transitive closure of relation implied_ids"
580 memo = {} # use a memo for performance and cycle avoidance
583 memo[g] = cset(g.implied_ids)
584 for h in g.implied_ids:
585 computed_set(h).subsetof(memo[g])
589 for g in self.browse(cr, SUPERUSER_ID, ids, context):
590 res[g.id] = map(int, computed_set(g))
594 'implied_ids': fields.many2many('res.groups', 'res_groups_implied_rel', 'gid', 'hid',
595 string='Inherits', help='Users of this group automatically inherit those groups'),
596 'trans_implied_ids': fields.function(_get_trans_implied,
597 type='many2many', relation='res.groups', string='Transitively inherits'),
600 def create(self, cr, uid, values, context=None):
601 users = values.pop('users', None)
602 gid = super(groups_implied, self).create(cr, uid, values, context)
604 # delegate addition of users to add implied groups
605 self.write(cr, uid, [gid], {'users': users}, context)
608 def write(self, cr, uid, ids, values, context=None):
609 res = super(groups_implied, self).write(cr, uid, ids, values, context)
610 if values.get('users') or values.get('implied_ids'):
611 # add all implied groups (to all users of each group)
612 for g in self.browse(cr, uid, ids, context=context):
613 gids = map(int, g.trans_implied_ids)
614 vals = {'users': [(4, u.id) for u in g.users]}
615 super(groups_implied, self).write(cr, uid, gids, vals, context)
618 class users_implied(osv.osv):
619 _inherit = 'res.users'
621 def create(self, cr, uid, values, context=None):
622 groups = values.pop('groups_id', None)
623 user_id = super(users_implied, self).create(cr, uid, values, context)
625 # delegate addition of groups to add implied groups
626 self.write(cr, uid, [user_id], {'groups_id': groups}, context)
627 self.pool['ir.ui.view'].clear_cache()
630 def write(self, cr, uid, ids, values, context=None):
631 if not isinstance(ids,list):
633 res = super(users_implied, self).write(cr, uid, ids, values, context)
634 if values.get('groups_id'):
635 # add implied groups for all users
636 for user in self.browse(cr, uid, ids):
637 gs = set(concat(g.trans_implied_ids for g in user.groups_id))
638 vals = {'groups_id': [(4, g.id) for g in gs]}
639 super(users_implied, self).write(cr, uid, [user.id], vals, context)
640 self.pool['ir.ui.view'].clear_cache()
643 #----------------------------------------------------------
644 # Vitrual checkbox and selection for res.user form view
646 # Extension of res.groups and res.users for the special groups view in the users
647 # form. This extension presents groups with selection and boolean widgets:
648 # - Groups are shown by application, with boolean and/or selection fields.
649 # Selection fields typically defines a role "Name" for the given application.
650 # - Uncategorized groups are presented as boolean fields and grouped in a
653 # The user form view is modified by an inherited view (base.user_groups_view);
654 # the inherited view replaces the field 'groups_id' by a set of reified group
655 # fields (boolean or selection fields). The arch of that view is regenerated
656 # each time groups are changed.
658 # Naming conventions for reified groups fields:
659 # - boolean field 'in_group_ID' is True iff
660 # ID is in 'groups_id'
661 # - selection field 'sel_groups_ID1_..._IDk' is ID iff
662 # ID is in 'groups_id' and ID is maximal in the set {ID1, ..., IDk}
663 #----------------------------------------------------------
665 def name_boolean_group(id):
666 return 'in_group_' + str(id)
668 def name_selection_groups(ids):
669 return 'sel_groups_' + '_'.join(map(str, ids))
671 def is_boolean_group(name):
672 return name.startswith('in_group_')
674 def is_selection_groups(name):
675 return name.startswith('sel_groups_')
677 def is_reified_group(name):
678 return is_boolean_group(name) or is_selection_groups(name)
680 def get_boolean_group(name):
683 def get_selection_groups(name):
684 return map(int, name[11:].split('_'))
686 def partition(f, xs):
687 "return a pair equivalent to (filter(f, xs), filter(lambda x: not f(x), xs))"
690 (yes if f(x) else nos).append(x)
693 def parse_m2m(commands):
694 "return a list of ids corresponding to a many2many value"
696 for command in commands:
697 if isinstance(command, (tuple, list)):
698 if command[0] in (1, 4):
699 ids.append(command[2])
700 elif command[0] == 5:
702 elif command[0] == 6:
703 ids = list(command[2])
709 class groups_view(osv.osv):
710 _inherit = 'res.groups'
712 def create(self, cr, uid, values, context=None):
713 res = super(groups_view, self).create(cr, uid, values, context)
714 self.update_user_groups_view(cr, uid, context)
717 def write(self, cr, uid, ids, values, context=None):
718 res = super(groups_view, self).write(cr, uid, ids, values, context)
719 self.update_user_groups_view(cr, uid, context)
722 def unlink(self, cr, uid, ids, context=None):
723 res = super(groups_view, self).unlink(cr, uid, ids, context)
724 self.update_user_groups_view(cr, uid, context)
727 def update_user_groups_view(self, cr, uid, context=None):
728 # the view with id 'base.user_groups_view' inherits the user form view,
729 # and introduces the reified group fields
730 # we have to try-catch this, because at first init the view does not exist
731 # but we are already creating some basic groups
732 view = self.pool['ir.model.data'].xmlid_to_object(cr, SUPERUSER_ID, 'base.user_groups_view', context=context)
733 if view and view.exists() and view._name == 'ir.ui.view':
735 xml1.append(E.separator(string=_('Application'), colspan="4"))
736 for app, kind, gs in self.get_groups_by_application(cr, uid, context):
737 # hide groups in category 'Hidden' (except to group_no_one)
738 attrs = {'groups': 'base.group_no_one'} if app and app.xml_id == 'base.module_category_hidden' else {}
739 if kind == 'selection':
740 # application name with a selection field
741 field_name = name_selection_groups(map(int, gs))
742 xml1.append(E.field(name=field_name, **attrs))
743 xml1.append(E.newline())
745 # application separator with boolean fields
746 app_name = app and app.name or _('Other')
747 xml2.append(E.separator(string=app_name, colspan="4", **attrs))
749 field_name = name_boolean_group(g.id)
750 xml2.append(E.field(name=field_name, **attrs))
752 xml = E.field(*(xml1 + xml2), name="groups_id", position="replace")
753 xml.addprevious(etree.Comment("GENERATED AUTOMATICALLY BY GROUPS"))
754 xml_content = etree.tostring(xml, pretty_print=True, xml_declaration=True, encoding="utf-8")
755 view.write({'arch': xml_content})
758 def get_application_groups(self, cr, uid, domain=None, context=None):
759 return self.search(cr, uid, domain or [])
761 def get_groups_by_application(self, cr, uid, context=None):
762 """ return all groups classified by application (module category), as a list of pairs:
763 [(app, kind, [group, ...]), ...],
764 where app and group are browse records, and kind is either 'boolean' or 'selection'.
765 Applications are given in sequence order. If kind is 'selection', the groups are
766 given in reverse implication order.
770 # determine sequence order: a group should appear after its implied groups
771 order = dict.fromkeys(gs, 0)
773 for h in gs.intersection(g.trans_implied_ids):
775 # check whether order is total, i.e., sequence orders are distinct
776 if len(set(order.itervalues())) == len(gs):
777 return sorted(gs, key=lambda g: order[g])
780 # classify all groups by application
781 gids = self.get_application_groups(cr, uid, context=context)
782 by_app, others = {}, []
783 for g in self.browse(cr, uid, gids, context):
785 by_app.setdefault(g.category_id, []).append(g)
790 apps = sorted(by_app.iterkeys(), key=lambda a: a.sequence or 0)
792 gs = linearized(by_app[app])
794 res.append((app, 'selection', gs))
796 res.append((app, 'boolean', by_app[app]))
798 res.append((False, 'boolean', others))
801 class users_view(osv.osv):
802 _inherit = 'res.users'
804 def create(self, cr, uid, values, context=None):
805 values = self._remove_reified_groups(values)
806 return super(users_view, self).create(cr, uid, values, context)
808 def write(self, cr, uid, ids, values, context=None):
809 values = self._remove_reified_groups(values)
810 return super(users_view, self).write(cr, uid, ids, values, context)
812 def _remove_reified_groups(self, values):
813 """ return `values` without reified group fields """
817 for key, val in values.iteritems():
818 if is_boolean_group(key):
819 (add if val else rem).append(get_boolean_group(key))
820 elif is_selection_groups(key):
821 rem += get_selection_groups(key)
827 if 'groups_id' not in values and (add or rem):
828 # remove group ids in `rem` and add group ids in `add`
829 values1['groups_id'] = zip(repeat(3), rem) + zip(repeat(4), add)
833 def default_get(self, cr, uid, fields, context=None):
834 group_fields, fields = partition(is_reified_group, fields)
835 fields1 = (fields + ['groups_id']) if group_fields else fields
836 values = super(users_view, self).default_get(cr, uid, fields1, context)
837 self._add_reified_groups(group_fields, values)
839 # add "default_groups_ref" inside the context to set default value for group_id with xml values
840 if 'groups_id' in fields and isinstance(context.get("default_groups_ref"), list):
842 ir_model_data = self.pool.get('ir.model.data')
843 for group_xml_id in context["default_groups_ref"]:
844 group_split = group_xml_id.split('.')
845 if len(group_split) != 2:
846 raise osv.except_osv(_('Invalid context value'), _('Invalid context default_groups_ref value (model.name_id) : "%s"') % group_xml_id)
848 temp, group_id = ir_model_data.get_object_reference(cr, uid, group_split[0], group_split[1])
852 values['groups_id'] = groups
855 def read(self, cr, uid, ids, fields=None, context=None, load='_classic_read'):
856 # determine whether reified groups fields are required, and which ones
857 fields1 = fields or self.fields_get(cr, uid, context=context).keys()
858 group_fields, other_fields = partition(is_reified_group, fields1)
860 # read regular fields (other_fields); add 'groups_id' if necessary
861 drop_groups_id = False
862 if group_fields and fields:
863 if 'groups_id' not in other_fields:
864 other_fields.append('groups_id')
865 drop_groups_id = True
867 other_fields = fields
869 res = super(users_view, self).read(cr, uid, ids, other_fields, context=context, load=load)
871 # post-process result to add reified group fields
873 for values in (res if isinstance(res, list) else [res]):
874 self._add_reified_groups(group_fields, values)
876 values.pop('groups_id', None)
879 def _add_reified_groups(self, fields, values):
880 """ add the given reified group fields into `values` """
881 gids = set(parse_m2m(values.get('groups_id') or []))
883 if is_boolean_group(f):
884 values[f] = get_boolean_group(f) in gids
885 elif is_selection_groups(f):
886 selected = [gid for gid in get_selection_groups(f) if gid in gids]
887 values[f] = selected and selected[-1] or False
889 def fields_get(self, cr, uid, allfields=None, context=None, write_access=True):
890 res = super(users_view, self).fields_get(cr, uid, allfields, context, write_access)
891 # add reified groups fields
892 for app, kind, gs in self.pool['res.groups'].get_groups_by_application(cr, uid, context):
893 if kind == 'selection':
894 # selection group field
895 tips = ['%s: %s' % (g.name, g.comment) for g in gs if g.comment]
896 res[name_selection_groups(map(int, gs))] = {
898 'string': app and app.name or _('Other'),
899 'selection': [(False, '')] + [(g.id, g.name) for g in gs],
900 'help': '\n'.join(tips),
905 # boolean group fields
907 res[name_boolean_group(g.id)] = {
916 #----------------------------------------------------------
917 # change password wizard
918 #----------------------------------------------------------
920 class change_password_wizard(osv.TransientModel):
922 A wizard to manage the change of users' passwords
925 _name = "change.password.wizard"
926 _description = "Change Password Wizard"
928 'user_ids': fields.one2many('change.password.user', 'wizard_id', string='Users'),
931 def _default_user_ids(self, cr, uid, context=None):
934 user_model = self.pool['res.users']
935 user_ids = context.get('active_model') == 'res.users' and context.get('active_ids') or []
937 (0, 0, {'user_id': user.id, 'user_login': user.login})
938 for user in user_model.browse(cr, uid, user_ids, context=context)
942 'user_ids': _default_user_ids,
945 def change_password_button(self, cr, uid, ids, context=None):
946 wizard = self.browse(cr, uid, ids, context=context)[0]
947 need_reload = any(uid == user.user_id.id for user in wizard.user_ids)
949 line_ids = [user.id for user in wizard.user_ids]
950 self.pool.get('change.password.user').change_password_button(cr, uid, line_ids, context=context)
954 'type': 'ir.actions.client',
958 return {'type': 'ir.actions.act_window_close'}
960 class change_password_user(osv.TransientModel):
962 A model to configure users in the change password wizard
965 _name = 'change.password.user'
966 _description = 'Change Password Wizard User'
968 'wizard_id': fields.many2one('change.password.wizard', string='Wizard', required=True),
969 'user_id': fields.many2one('res.users', string='User', required=True),
970 'user_login': fields.char('User Login', readonly=True),
971 'new_passwd': fields.char('New Password'),
977 def change_password_button(self, cr, uid, ids, context=None):
978 for line in self.browse(cr, uid, ids, context=context):
979 line.user_id.write({'password': line.new_passwd})
980 # don't keep temporary passwords in the database longer than necessary
981 self.write(cr, uid, ids, {'new_passwd': False}, context=context)
984 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: