Skip to content

Commit

Permalink
TA#TA#63329 [ADD] multi_website_user_access
Browse files Browse the repository at this point in the history
  • Loading branch information
majouda committed Jun 27, 2024
1 parent 43c489a commit 99f96b3
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 91 deletions.
12 changes: 5 additions & 7 deletions multi_website_user_access/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,20 @@ Multi Website User Access
=========================
This module allows to restrict user access to specific websites.

This will not affect user portal page (url accessed on `/my`).

Usage
-----
After installation, this module will make as default the main website of Odoo and add it to all users as allowed website.

By default, this module allows for public users to access the website when not logged in
else restrict access to the website for each type of user.
After installation, this module will make as default the main website and add it to all users as allowed website.

As a `Website / Editor and Designer` user, I go to the `Website > Configuration > Websites`.
I select one of the websites to switch to form view.
I see that a new `Default Website` check box is present.

.. image:: static/description/website_default_option.png

If I check the box and save, this will be added by default in allowed `Multi Websites` in user form when creating a new one of changing user type (after saving).
On create a new user, the default website will be added to the allowed websites of this user.

- All internal users have by default access to all existing websites.
- All portal users have by default access to the main website.

Use Case
--------
Expand Down
11 changes: 8 additions & 3 deletions multi_website_user_access/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2024 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import models # noqa: F401
from . import models

Check notice on line 4 in multi_website_user_access/__init__.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

multi_website_user_access/__init__.py#L4

'.models' imported but unused (F401)

from odoo import api, SUPERUSER_ID

Expand All @@ -11,6 +11,11 @@ def set_default_website(cr, registry):
default_website = env.ref("website.default_website")
default_website.write({"default_website": True})
# Set default website for all existing users
env["res.users"].search([]).write(
{"allowed_website_ids": [(4, default_website.id)]}
internal_users = (
env["res.users"].with_context(active_test=False).search([("share", "=", False)])
)
external_users = (
env["res.users"].with_context(active_test=False).search([("share", "=", True)])
)
internal_users.write({"website_ids": [(6, 0, env["website"].search([]).ids)]})
external_users.write({"website_ids": [(4, default_website.id)]})
35 changes: 23 additions & 12 deletions multi_website_user_access/i18n/fr.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ msgid ""
msgstr ""
"Project-Id-Version: Odoo Server 14.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-06-20 09:45+0000\n"
"PO-Revision-Date: 2024-06-20 09:45+0000\n"
"POT-Creation-Date: 2024-06-27 01:42+0000\n"
"PO-Revision-Date: 2024-06-27 01:42+0000\n"
"Last-Translator: \n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
Expand All @@ -21,18 +21,24 @@ msgid "403: Forbidden"
msgstr "403 : Interdit"

#. module: multi_website_user_access
#: model:ir.model.fields,field_description:multi_website_user_access.field_res_users__allowed_website_ids
#: model:ir.model.fields,field_description:multi_website_user_access.field_website__user_ids
msgid "Allowed Users"
msgstr "Utilisateurs autorisés"

#. module: multi_website_user_access
#: model:ir.model.fields,field_description:multi_website_user_access.field_res_users__website_ids
msgid "Allowed Websites"
msgstr "Site web autorisés"
msgstr "Sites web autorisés"

#. module: multi_website_user_access
#: model:ir.model.fields,help:multi_website_user_access.field_website__default_website
msgid ""
"Check this box to determine this website as the default value for portal "
"and public user access."
msgstr ""
"Cocher cette case pour définir ce site web comme valeur par défaut sur les accès des utilisateurs portails "
"et publics. Plusieurs sites web peuvent être définis par défaut."
"Cocher cette case pour définir ce site web comme valeur par défaut sur les "
"accès des utilisateurs portails et publics. Plusieurs sites web peuvent être"
" définis par défaut."

#. module: multi_website_user_access
#: model:ir.model.fields,field_description:multi_website_user_access.field_website__default_website
Expand Down Expand Up @@ -66,10 +72,20 @@ msgid "Multi Websites"
msgstr "Multi Sites web"

#. module: multi_website_user_access
#: model:ir.model.fields,help:multi_website_user_access.field_res_users__allowed_website_ids
#: model:ir.model.fields,field_description:multi_website_user_access.field_res_users__websites_count
msgid "Number of Websites"
msgstr "Nombres des sites Web"

#. module: multi_website_user_access
#: model:ir.model.fields,help:multi_website_user_access.field_res_users__website_ids
msgid "Restrict user access to specific websites."
msgstr "Restreindre l’accès des utilisateurs à des sites Web spécifiques."

#. module: multi_website_user_access
#: model_terms:ir.ui.view,arch_db:multi_website_user_access.page_403
msgid "The page you were looking for could not be authorized."
msgstr "La page que vous recherchiez n'a pas pu être autorisée."

#. module: multi_website_user_access
#: model:ir.model,name:multi_website_user_access.model_res_users
msgid "Users"
Expand All @@ -84,8 +100,3 @@ msgstr "Vue"
#: model:ir.model,name:multi_website_user_access.model_website
msgid "Website"
msgstr "Site web"

#. module: multi_website_user_access
#: model_terms:ir.ui.view,arch_db:multi_website_user_access.page_403
msgid "You are not allowed to access to this page you are looking for."
msgstr "Vous n'êtes pas autorisé à accéder à la page que vous recherchez."
2 changes: 1 addition & 1 deletion multi_website_user_access/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2024 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from . import website, res_users, ir_ui_view # noqa: F401
from . import website, res_users, ir_ui_view

Check notice on line 4 in multi_website_user_access/models/__init__.py

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

multi_website_user_access/models/__init__.py#L4

'.website' imported but unused (F401)
29 changes: 12 additions & 17 deletions multi_website_user_access/models/ir_ui_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@

from odoo import models
from odoo.http import request
from odoo.addons.website.models import ir_http


class View(models.Model):
class IrUiView(models.Model):

_inherit = "ir.ui.view"

def _render(self, values=None, engine="ir.qweb", minimal_qcontext=False):
"""
Handle website page rendering if user is allowed to view it.
Allow user portal page to be displayed.
"""
if hasattr(request, "website") and hasattr(request, "httprequest"):
if (
request.httprequest.path.split("/")[1] != "my"
and not request.website.is_public_user()
and request.website.id not in request.env.user.allowed_website_ids.ids
):
return super(
View,
self.browse(self.get_view_id("multi_website_user_access.page_403")),
)._render(values, engine=engine, minimal_qcontext=minimal_qcontext)
return super(View, self)._render(
values, engine=engine, minimal_qcontext=minimal_qcontext
is_frontend = ir_http.get_request_website()
Website = self.env["website"]
website_id = is_frontend and Website.get_current_website() or Website
if website_id and website_id.id not in request.env.user.website_ids.ids:
return super(
IrUiView,
self.browse(self.get_view_id("multi_website_user_access.page_403")),
)._render(values, engine=engine, minimal_qcontext=minimal_qcontext)
return super(IrUiView, self)._render(
values=values, engine=engine, minimal_qcontext=minimal_qcontext
)
52 changes: 10 additions & 42 deletions multi_website_user_access/models/res_users.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright 2024 Numigi (tm) and all its contributors (https://bit.ly/numigiens)
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import api, fields, models
from odoo import fields, models


class Users(models.Model):
Expand All @@ -10,50 +10,18 @@ class Users(models.Model):
def _get_default_website(self):
return self.env["website"].search([("default_website", "=", True)])

allowed_website_ids = fields.Many2many(
website_ids = fields.Many2many(
"website",
"website_users_rel",
"user_id",
"wid",
default=_get_default_website,
string="Allowed Websites",
store=True,
readonly=False,
help="Restrict user access to specific websites.",
)
websites_count = fields.Integer(
compute="_compute_websites_count", string="Number of Websites"
)

@api.model_create_multi
def create(self, vals_list):
user_groups, grp_portal, grp_public = self._get_user_groups()
for vals in vals_list:
self._assign_portal_public_user(vals, user_groups, grp_portal, grp_public)
users = super(Users, self).create(vals_list)
return users

def write(self, vals):
user_groups, grp_portal, grp_public = self._get_user_groups()
self._assign_portal_public_user(vals, user_groups, grp_portal, grp_public)
return super(Users, self).write(vals)

def _assign_portal_public_user(self, vals, user_groups, grp_portal, grp_public):
if user_groups in vals:
if vals.get(user_groups) in [grp_portal, grp_public]:
default_website = self.env["website"].search(
[("default_website", "=", True)]
)
website_commands = [(4, website.id) for website in default_website]
if vals.get("allowed_website_ids"):
vals["allowed_website_ids"] = (
website_commands + vals["allowed_website_ids"]
)
else:
vals["allowed_website_ids"] = website_commands
return vals

def _get_user_groups(self):
grp_internal = self.env.ref("base.group_user")
grp_portal = self.env.ref("base.group_portal")
grp_public = self.env.ref("base.group_public")
user_groups = "sel_groups_%s_%s_%s" % (
grp_internal.id,
grp_portal.id,
grp_public.id,
)
return user_groups, grp_portal.id, grp_public.id
def _compute_websites_count(self):
self.websites_count = self.env["website"].sudo().search_count([])
3 changes: 3 additions & 0 deletions multi_website_user_access/models/website.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ class Website(models.Model):
"for portal and public user access."
),
)
user_ids = fields.Many2many(
"res.users", "website_users_rel", "wid", "user_id", string="Allowed Users"
)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions multi_website_user_access/views/res_users_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
<field name="inherit_id" ref="base.view_users_form"/>
<field name="arch" type="xml">
<xpath expr="//notebook/page[@name='access_rights']/group[1]" position="after">
<group string="Multi Websites">
<field name="allowed_website_ids" widget="many2many_tags"/>
<group string="Multi Websites" attrs="{'invisible': [('websites_count', '&lt;=', 1)]}">
<field name="website_ids" widget="many2many_tags" options="{'no_create': True}"/>
<field name="websites_count" invisible="1"/>
</group>
</xpath>
</field>
Expand Down
12 changes: 5 additions & 7 deletions multi_website_user_access/views/website_templates.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
<odoo>

<template id="page_403" name="Page Access Forbidden">
<t t-call="web.frontend_layout">
<div id="wrap">
<div class="container">
<h1 class="mt-5">403: Forbidden</h1>
<p>You are not allowed to access to this page you are looking for.</p>
</div>
<div id="wrap">
<div class="container">
<h1 class="mt-5">403: Forbidden</h1>
<p>The page you were looking for could not be authorized.</p>
</div>
</t>
</div>
</template>

</odoo>

0 comments on commit 99f96b3

Please sign in to comment.