Skip to content

Commit

Permalink
[IMP] base: assign default groups to the user based on it's type
Browse files Browse the repository at this point in the history
Before this commit:
Though user selects User Type as Portal/Public while creating an user, it was changed into Internal User.

After this commit:
Whatever User Type was selected while creating the user will be kept on saving the record.

Solution:
defined onchange for 'User Type' field of res.users model which will fetch and set groups information from template defined for default/portal/public users

Issue-ID: 1865571
  • Loading branch information
tsh-odoo committed Jul 23, 2018
1 parent da15493 commit 1fbc472
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions odoo/addons/base/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ def on_change_login(self):
def onchange_parent_id(self):
return self.mapped('partner_id').onchange_parent_id()

@api.onchange('group_base_user')
def _onchange_group_base_user(self):
self.groups_id = self._get_groups_for_limited_user()

@api.multi
@api.constrains('company_id', 'company_ids')
def _check_company(self):
Expand All @@ -359,6 +363,18 @@ def _check_action_id(self):
if action_open_website and any(user.action_id.id == action_open_website.id for user in self):
raise ValidationError(_('The "App Switcher" action cannot be selected as home action.'))

def _get_groups_for_limited_user(self):
group_portal = self.env.ref('base.group_portal', False)
group_public = self.env.ref('base.group_public', False)
if self.group_base_user == (group_portal and group_portal.id) or False:
template_portal_user_id = self.env['ir.config_parameter'].get_param('base.template_portal_user_id')
user = self.browse(int(template_portal_user_id)) or self.env.ref('base.template_portal_user_id', False)
elif self.group_base_user == (group_public and group_public.id) or False:
user = self.env.ref('base.public_user', False)
else:
user = self.env.ref('base.default_user', False)
return (user or self.env['res.users']).groups_id

@api.multi
def toggle_active(self):
for user in self:
Expand Down Expand Up @@ -787,10 +803,14 @@ def _inverse_groups_id(self):
"""Update 'groups_id' according the group fields values in cache."""
computed_group_fields = [field for field in self._field_computed.keys() if field.compute == '_compute_groups_id']
all_groups = self.env['res.groups'].search([])
limited_access_groups = self.env.ref('base.group_portal') + self.env.ref('base.group_public')
for user in self:
# we need to read all values in cache before any prefetch
field_values = {field: user[field.name] for field in computed_group_fields if field.name in user._cache}
groups_id_vals = []
if user.group_base_user in limited_access_groups.ids:
groups_id_vals = self._get_groups_for_limited_user()
continue
for field, value in field_values.items():
# Checkbox group
if getattr(field, 'group_xml_id', None):
Expand Down

0 comments on commit 1fbc472

Please sign in to comment.