Permalink
Browse files

[FIX] base: creation of portal user

When creating a portal user from the User & Companies menu in the
Settings.

Before this commit, the created user had both groups' portal user and
internal user, which generated an error in the display of the user
accesses and rights. This also occurred if we manually add a portal
user to the internal user group.

Now, when creating a portal user, the user only has this group. Also,
if we manually add a portal user to the internal user group, an error
is raised to inform that only one user type is allowed.

OPW-1929367
  • Loading branch information...
jpp-odoo committed Feb 8, 2019
1 parent a46138c commit 34248cf5713ad8335fa987114d6801f4b5acbdcb
Showing with 54 additions and 0 deletions.
  1. +7 −0 odoo/addons/base/i18n/base.pot
  2. +47 −0 odoo/addons/base/models/res_users.py
msgid "The type of the report that will be rendered, each one having its own rendering method. HTML means the report will be opened directly in your browser PDF means the report will be rendered using Wkhtmltopdf and downloaded by the user."
msgstr ""

#. module: base
#: code:addons/base/models/res_users.py:117
#: code:addons/base/models/res_users.py:404
#, python-format
msgid "The user cannot be in more than one user types."
msgstr ""

#. module: base
#: model:ir.model.fields,help:base.field_ir_filters__user_id
msgid "The user this filter is private to. When left empty the filter is public and available to all users."
@@ -107,6 +107,15 @@ class Groups(models.Model):
('name_uniq', 'unique (category_id, name)', 'The name of the group must be unique within an application!')
]

@api.multi
@api.constrains('users')
def _check_one_user_type(self):
'''Contraints the user to be in one, and only one user type'''
for group in self:
for user in group.users:
if len(user.groups_id.filtered(lambda x : x.category_id.xml_id == 'base.module_category_user_type')) > 1:
raise ValidationError(_('The user cannot be in more than one user types.'))

@api.depends('category_id.name', 'name')
def _compute_full_name(self):
# Important: value must be stored in environment of group, not group1!
@@ -386,6 +395,14 @@ 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.'))

@api.multi
@api.constrains('groups_id')
def _check_one_user_type(self):
'''Contraints the user to be in one, and only one user type'''
for user in self:
if len(user.groups_id.filtered(lambda x : x.category_id.xml_id == 'base.module_category_user_type')) > 1:
raise ValidationError(_('The user cannot be in more than one user types.'))

@api.multi
def toggle_active(self):
for user in self:
@@ -1127,6 +1144,27 @@ def _remove_reified_groups(self, values):
else:
values1[key] = val

portal = self.env.ref('base.group_portal',raise_if_not_found=False)
public = self.env.ref('base.group_public',raise_if_not_found=False)
if add:
#If we choose Portal or Public as user type, only this group is added.
if portal and portal.id in add:
add = [portal.id]
elif public and public.id in add:
add = [public.id]

#If there are an existing user with two types, and we change another group.
#We leave only internal, to avoid raise an exception.
for user in self:
userTypeGrp = user.groups_id.filtered(lambda x : x.category_id.xml_id == 'base.module_category_user_type')
if len(userTypeGrp) > 1:
internal = user.env.ref('base.group_user',raise_if_not_found=False)
if internal in userTypeGrp:
if portal and portal not in rem:
rem.append(portal.id)
if public and public not in rem:
rem.append(public.id)

if 'groups_id' not in values and (add or rem):
# remove group ids in `rem` and add group ids in `add`
values1['groups_id'] = list(itertools.chain(
@@ -1178,6 +1216,15 @@ def _add_reified_groups(self, fields, values):
elif is_selection_groups(f):
selected = [gid for gid in get_selection_groups(f) if gid in gids]
values[f] = selected and selected[-1] or False
#When the user is in at least two groups of category == 'User types' (Internal User or Portal or Public)
#The selected value must be the group with the most access, ie : Internal User > Portal > Public
#This is made to show the correct user type
if len(selected) > 1 :
selectedGrps = self.env['res.groups'].browse(selected)
if selectedGrps[0].category_id.xml_id == 'base.module_category_user_type':
valInternal = next((grp.id for grp in selectedGrps if grp == self.env.ref('base.group_user',raise_if_not_found=False)), False)
valPortal = next((grp.id for grp in selectedGrps if grp == self.env.ref('base.group_portal',raise_if_not_found=False)), False)
values[f] = valInternal if valInternal else valPortal

@api.model
def fields_get(self, allfields=None, attributes=None):

0 comments on commit 34248cf

Please sign in to comment.