Skip to content

Commit

Permalink
[IMP] portal_backend: portal backend group
Browse files Browse the repository at this point in the history
  • Loading branch information
bruno-zanotti committed May 2, 2023
1 parent 1c5c8c8 commit b1430d3
Show file tree
Hide file tree
Showing 12 changed files with 196 additions and 32 deletions.
33 changes: 32 additions & 1 deletion portal_backend/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,38 @@
Portal Backend
==============

This module is for generated an environment to be able to use portal users in backend.
This module creates a new user type "Portal Backend" that grants access to the Odoo backend. However, users with this group won't be able to use any app by default. To enable app access for Portal Backend users, it is necessary an specific module "portal_app" that defines the app permissions for this user group.

To create the necessary groups and categories for "portal_app", you can use the following template:

App category inheriting from portal advanced:

.. code-block:: xml
<record model="ir.module.category" id="category_portal_app">
<field name="name">Portal app</field>
<field name="parent_id" ref="portal_backend.category_portal_advanced"/>
</record>
Group to give access to Portal Backend users to use that app:

.. code-block:: xml
<record id="group_portal_backend_app" model="res.groups">
<field name="name">Portal app</field>
<field name="category_id" ref="category_portal_app"/>
</record>
You can also create a set of groups that inherit (or not) from each other, but the category always have to be "category_portal_app", because the views are different for each type of user. Here's an example:

.. code-block:: xml
<record id="group_portal_backend_app_2" model="res.groups">
<field name="name">Portal app 2</field>
<field name="category_id" ref="category_portal_app"/>
<field name="implied_ids" eval="[Command.link(ref('group_portal_backend_app'))]"/>
</record>
Installation
============
Expand Down
9 changes: 5 additions & 4 deletions portal_backend/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@
'author': 'ADHOC SA',
'website': 'www.adhoc.com.ar',
'license': 'AGPL-3',
'images': [
],
'depends': [
'portal',
],
'data': [
'security/res_groups.xml',
'security/ir.model.access.csv',
'views/base_menus.xml',
'views/portal_templates.xml',
'security/ir_rules.xml',
'security/ir.model.access.csv',
],
'demo': [
'demo/res_users_demo.xml',
],
'installable': True,
'auto_install': False,
Expand Down
29 changes: 29 additions & 0 deletions portal_backend/demo/res_users_demo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0"?>
<odoo>

<!-- Portal Backend: partner and user -->
<record model="res.partner" id="partner_portal_advanced">
<field name="name">John Portal Advanced</field>
<field name="company_name">YourCompany</field>
<field name="street">41 Lakes Street</field>
<field name="city">Rosario</field>
<field name="zip">2000</field>
<field name='country_id' ref='base.us'/>
<field name='state_id' ref='base.state_ar_s'/>
<field name="phone">+54 555-555-5555</field>
<field name="email">portaladvanced@yourcompany.example.com</field>
<field name="tz">Europe/Brussels</field>
<field name="image_1920" type="base64" file="portal_backend/static/img/advanced_partner-image.png"/>
</record>

<record id="user_portal_advanced" model="res.users" context="{'no_reset_password': True}">
<field name="partner_id" ref="partner_portal_advanced"/>
<field name="login">portal+</field>
<field name="password">portal+</field>
<field name="company_id" ref="base.main_company"/>
<field name="company_ids" eval="[Command.link(ref('base.main_company'))]"/>
<field name="signature"><![CDATA[<span>-- <br/>Mr Advanced portal</span>]]></field>
<field name="groups_id" eval="[Command.set([ref('base.group_portal'), ref('portal_backend.group_portal_backend')])]"/>
</record>

</odoo>
2 changes: 1 addition & 1 deletion portal_backend/models/ir_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def session_info(self):
user = request.env.user

session_info = super().session_info()
if self.env.user.has_group('base.group_portal'):
if self.env.user.has_group('portal_backend.group_portal_backend'):
# the following is only useful in the context of a webclient bootstrapping
# but is still included in some other calls (e.g. '/web/session/authenticate')
# to avoid access errors and unnecessary information, it is only included for users
Expand Down
12 changes: 6 additions & 6 deletions portal_backend/models/mail_activity_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ class MailActivityMixin(models.AbstractModel):
_inherit = 'mail.activity.mixin'

activity_ids = fields.One2many(
groups="base.group_user, base.group_portal",
groups="base.group_user, portal_backend.group_portal_backend",
)
activity_state = fields.Selection(
groups="base.group_user, base.group_portal",
groups="base.group_user, portal_backend.group_portal_backend",
)
activity_user_id = fields.Many2one(
groups="base.group_user, base.group_portal",
groups="base.group_user, portal_backend.group_portal_backend",
)
activity_type_id = fields.Many2one(
groups="base.group_user, base.group_portal",
groups="base.group_user, portal_backend.group_portal_backend",
)
activity_date_deadline = fields.Date(
groups="base.group_user, base.group_portal",
groups="base.group_user, portal_backend.group_portal_backend",
)
activity_summary = fields.Char(
groups="base.group_user, base.group_portal",
groups="base.group_user, portal_backend.group_portal_backend",
)
2 changes: 1 addition & 1 deletion portal_backend/models/mail_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ class MailThread(models.AbstractModel):
_inherit = 'mail.thread'

message_attachment_count = fields.Integer(
groups="base.group_user, base.group_portal")
groups="base.group_user, portal_backend.group_portal_backend")
93 changes: 89 additions & 4 deletions portal_backend/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
# directory
##############################################################################
from odoo import models, api
from odoo.addons.base.models import res_users

from odoo.addons.base.models.res_users import name_selection_groups
from odoo.addons.base.models.ir_model import MODULE_UNINSTALL_FLAG
from lxml import etree
from lxml.builder import E
import json


class ResUsers(models.Model):
Expand All @@ -17,13 +20,95 @@ class ResUsers(models.Model):
# Only apply this change if the user is portal.
# """
# import pdb;pdb.set_trace()
# if self.env.user.has_group('base.group_portal') and self.env['ir.module.module'].sudo().search(
# if self.env.user.has_group('portal_backend.group_portal_backend') and self.env['ir.module.module'].sudo().search(
# [('name', '=', 'note')]).state == 'installed':
# self = self.sudo()
# return super().systray_get_activities()

def _is_internal(self):
self.ensure_one()
if self.has_group('base.group_portal') and self.env.context.get('portal_bypass'):
if self.has_group('portal_backend.group_portal_backend') and self.env.context.get('portal_bypass'):
return True
return super()._is_internal()

@api.constrains('groups_id')
def _check_one_user_type(self):
""" Bypass this constrain since the portal backend users have portal and portal_backend groups.
The native method checks if at least there is one user with two user types so it always gonna return False.
"""
return True


class GroupsView(models.Model):
_inherit = 'res.groups'

@api.model
def _update_user_groups_view(self):
super()._update_user_groups_view()
if self._context.get('install_filename') or self._context.get(MODULE_UNINSTALL_FLAG):
return

view = self.env.ref('base.user_groups_view', raise_if_not_found=False)
arch = etree.fromstring(view.arch)

# Get the user type field name
category_user_type = self.env.ref('base.module_category_user_type')
gs = self.get_application_groups([('category_id', '=', category_user_type.id)])
user_type_field_name = name_selection_groups(gs.ids)

# Get the portal backend field name
group_portal_backend = self.env.ref('portal_backend.group_portal_backend')
category_portal_backend = self.env.ref('portal_backend.category_portal_advanced')

groups_to_move =[]

# Remove from internal user view the portal backend groups. There are two cases:
# Case 1: kind == selection (group string="Advanced portal")
group = arch.xpath("//group[@string='%s']" % category_portal_backend.name)
if group:
group = group[0]
groups_to_move.append(group)
group.getparent().remove(group)

# Case 2: kind == bool (separator string="Child category")
child_category_names = [category.name for category in category_portal_backend.child_ids]
for name in child_category_names:
separator = arch.xpath("//separator[@string='%s']" % name)
if separator:
groups_to_move.append(separator[0])
groups_to_move += self._get_groups_between_separator(separator[0])

# Create new group for portal backend views
new_arch_pb_parent_group = E.group(*groups_to_move, attrs=str({'invisible': [(user_type_field_name, '!=', group_portal_backend.id)]}))

# Set attributes
pb_field_attributes = {'readonly': [(user_type_field_name, '!=', group_portal_backend.id)]}
for field in new_arch_pb_parent_group.iter('field'):
field.set("attrs", json.dumps(pb_field_attributes))

# Add the new group to the arch view
arch.append(new_arch_pb_parent_group)

# Write the arch on the original view
xml_content = etree.tostring(arch)
if xml_content != view.arch:
new_context = dict(view._context)
new_context.pop('install_filename', None)
new_context['lang'] = None
view.with_context(new_context).write({'arch': xml_content})

def _get_groups_between_separator(self, element):
""" Get all the groups after a determined separator.
"""
groups = []
while(True):
next_element = element.getnext()
if next_element.tag == "group":
groups.append(next_element)
element = next_element
elif groups:
for gr in groups:
gr.getparent().remove(gr)
return groups
else:
return
16 changes: 8 additions & 8 deletions portal_backend/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_ir_ui_menu_portal,ir.ui.menu.portal,base.model_ir_ui_menu,base.group_portal,1,0,0,0
access_ir_model_portal,ir.model.portal,base.model_ir_model,base.group_portal,1,0,0,0
access_ir_fields_portal,ir.model.fields.portal,base.model_ir_model_fields,base.group_portal,1,0,0,0
access_ir_groups,ir.model.groups.portal,base.model_res_groups,base.group_portal,1,0,0,0
access_ir_model_data_portal,ir.model.model.data.portal,base.model_ir_model_data,base.group_portal,1,0,1,0
access_ir_attachment_portal,ir_attachment.portal,base.model_ir_attachment,base.group_portal,1,0,0,0
access_mail_activity_type_portal,access_mail_activity_type_portal.portal,mail.model_mail_activity_type,base.group_portal,1,0,0,0
access_mail_channel_portal,mail.group.portal,mail.model_mail_channel,base.group_portal,1,0,1,0
access_ir_ui_menu_portal,ir.ui.menu.portal,base.model_ir_ui_menu,portal_backend.group_portal_backend,1,0,0,0
access_ir_model_portal,ir.model.portal,base.model_ir_model,portal_backend.group_portal_backend,1,0,0,0
access_ir_fields_portal,ir.model.fields.portal,base.model_ir_model_fields,portal_backend.group_portal_backend,1,0,0,0
access_ir_groups,ir.model.groups.portal,base.model_res_groups,portal_backend.group_portal_backend,1,0,0,0
access_ir_model_data_portal,ir.model.model.data.portal,base.model_ir_model_data,portal_backend.group_portal_backend,1,0,1,0
access_ir_attachment_portal,ir_attachment.portal,base.model_ir_attachment,portal_backend.group_portal_backend,1,0,0,0
access_mail_activity_type_portal,access_mail_activity_type_portal.portal,mail.model_mail_activity_type,portal_backend.group_portal_backend,1,0,0,0
access_mail_channel_portal,mail.group.portal,mail.model_mail_channel,portal_backend.group_portal_backend,1,0,1,0
7 changes: 0 additions & 7 deletions portal_backend/security/ir_rules.xml

This file was deleted.

16 changes: 16 additions & 0 deletions portal_backend/security/res_groups.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>

<!-- Parent Category -->
<record model="ir.module.category" id="category_portal_advanced">
<field name="name">Advanced Portal</field>
</record>

<!-- Portal backend user group -->
<record id="group_portal_backend" model="res.groups">
<field name="name">Portal Backend</field>
<field name="category_id" ref="base.module_category_user_type"/>
<field name="implied_ids" eval="[Command.link(ref('base.group_portal'))]"/>
</record>

</odoo>
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions portal_backend/views/portal_templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<odoo>
<!-- Add the button "Apps" for portal users in the dropdown menu in the frontend -->
<template id="link_to_backend" name="Link to backend" inherit_id="portal.user_dropdown">
<a id="o_backend_user_dropdown_link" position="attributes">
<attribute name="groups">base.group_user,portal_backend.group_portal_backend</attribute>
</a>
</template>
</odoo>

0 comments on commit b1430d3

Please sign in to comment.