Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions cross_connect_client/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
====================
Cross Connect Client
====================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:f10ceaed1b91df49c3a9b4e8acdef3d412d7ce50105f6f1ca752630fc1559d8e
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fserver--auth-lightgray.png?logo=github
:target: https://github.com/OCA/server-auth/tree/18.0/cross_connect_client
:alt: OCA/server-auth
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/server-auth-18-0/server-auth-18-0-cross_connect_client
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/server-auth&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module allows this odoo instance users to connect directly on
another odoo instance where the module ``cross_connect_server`` is
installed.

**Table of contents**

.. contents::
:local:

Usage
=====

First of all after installing the module, you need to configure the
server connection.

In order to do that, you need to go to the menu
``Settings > Technical > Cross Connect > Cross Connect Servers`` and
create a new server to connect to.

Fill the fields with the server's information :

- Url: The api root path (e.g. ``https://my-remote-odoo.com/api``)
- Api Key: The api-key from the ``cross_connect_server`` configuration

Then click on the ``Sync Cross Connection`` button to check if the
connection is working and to sync the remote server's groups.

After that, you will have to affect the remote groups to the local users
in order for them to be able to connect to the remote server.

Once an user has a remote group, a new top level menu will appear in the
menu bar with the Cross Connect Server's name. Clicking on it will
redirect the user to the remote server logged in as the user.

You can change each menu icon (for use with ``web_responsive`` for
instance) by setting the ``Web Icon Data`` in the server configuration.

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/server-auth/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/server-auth/issues/new?body=module:%20cross_connect_client%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
-------

* Akretion

Contributors
------------

- Florian Mounier florian.mounier@akretion.com

Maintainers
-----------

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

.. |maintainer-paradoxxxzero| image:: https://github.com/paradoxxxzero.png?size=40px
:target: https://github.com/paradoxxxzero
:alt: paradoxxxzero

Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-paradoxxxzero|

This module is part of the `OCA/server-auth <https://github.com/OCA/server-auth/tree/18.0/cross_connect_client>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 2 additions & 0 deletions cross_connect_client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import controllers
from . import models
22 changes: 22 additions & 0 deletions cross_connect_client/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Cross Connect Client",
"version": "18.0.1.0.0",
"author": "Akretion, Odoo Community Association (OCA)",
"summary": "Cross Connect Client allows to connect to a "
"Cross Connect Server enabled odoo instance.",
"category": "Tools",
"depends": ["server_environment"],
"website": "https://github.com/OCA/server-auth",
"data": [
"security/ir_model_access.xml",
"views/cross_connect_server_views.xml",
],
"maintainers": ["paradoxxxzero"],
"demo": [],
"installable": True,
"license": "AGPL-3",
}
1 change: 1 addition & 0 deletions cross_connect_client/controllers/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import cross_connect
25 changes: 25 additions & 0 deletions cross_connect_client/controllers/cross_connect.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.exceptions import UserError
from odoo.http import Controller, request, route


class CrossConnectController(Controller):
@route(
["/cross_connect_server/<int:server_id>"],
methods=["GET"],
type="http",
auth="public",
)
def cross_connect(
self,
server_id,
**params,
):
server = request.env["cross.connect.server"].sudo().browse(server_id)
if not server:
raise UserError(request.env._("Server not found"))

url = server._get_cross_connect_url(**params)
return request.redirect(url, local=False)
2 changes: 2 additions & 0 deletions cross_connect_client/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import cross_connect_server
from . import res_groups
183 changes: 183 additions & 0 deletions cross_connect_client/models/cross_connect_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from urllib.parse import urlencode

import requests

from odoo import api, fields, models
from odoo.exceptions import UserError


class CrossConnectServer(models.Model):
_name = "cross.connect.server"
_description = "Cross Connect Server"
_inherit = "server.env.mixin"

name = fields.Char(
required=True,
help="This name will be used for the new created app",
)
server_url = fields.Char(required=True)
api_key = fields.Char(
required=True,
)
group_ids = fields.One2many(
"res.groups",
inverse_name="cross_connect_server_id",
string="Cross Connect Server Groups",
readonly=True,
)
menu_id = fields.Many2one(
"ir.ui.menu",
string="Menu",
help="Menu to display the Cross Connect Server in the menu",
compute="_compute_menu_id",
store=True,
)
web_icon_data = fields.Binary(
compute="_compute_web_icon_data", inverse="_inverse_web_icon_data"
)

@api.depends("name", "group_ids")
def _compute_menu_id(self):
for record in self:
if not record.group_ids:
if record.menu_id:
if record.menu_id.action:
record.menu_id.action.unlink()
record.menu_id.unlink()
record.menu_id = False
continue

menu_groups = record.group_ids

if not record.menu_id:
action = self.env["ir.actions.act_url"].create(
{
"name": record.name,
"url": f"/cross_connect_server/{record.id}",
"target": "new",
}
)
icon = "cross_connect_client,static/description/web_icon_data.png"
record.menu_id = self.env["ir.ui.menu"].create(
{
"name": record.name,
"action": f"ir.actions.act_url,{action.id}", # noqa
"web_icon": icon,
"groups_id": [(6, 0, menu_groups.ids)],
"sequence": 100,
}
)
else:
record.menu_id.name = record.name
record.menu_id.groups_id = [(6, 0, menu_groups.ids)]

@api.depends("menu_id")
def _compute_web_icon_data(self):
for record in self:
record.web_icon_data = record.menu_id.web_icon_data

def _inverse_web_icon_data(self):
for record in self:
record.menu_id.web_icon_data = record.web_icon_data

def _absolute_url_for(self, path):
return f"{self.server_url.rstrip('/')}/cross_connect/{path.lstrip('/')}"

def _request(self, method, url, headers=None, data=None):
headers = headers or {}
headers["api-key"] = self.api_key
response = requests.request(
method,
self._absolute_url_for(url),
headers=headers,
json=data,
timeout=10,
)
response.raise_for_status()
return response.json()

def _get_cross_connect_url(self, **params):
self.ensure_one()
groups = self.env.user.groups_id & self.group_ids
if not groups:
raise UserError(self.env._("You are not allowed to access this server"))

if not self.env.user.email:
raise UserError(self.env._("User email is required"))

data = {
"id": self.env.user.id,
"name": self.env.user.name,
"login": self.env.user.login,
"email": self.env.user.email,
"lang": self.env.user.lang,
"groups": [group.cross_connect_server_group_id for group in groups],
}

response = self._request("POST", "/access", data=data)
client_id = response.get("client_id")
token = response.get("token")
if not token:
raise UserError(self.env._("Missing token"))

url = f"login/{client_id}/{token}"
if params:
url += "?" + urlencode(params)

return self._absolute_url_for(url)

def _sync_groups(self):
self.ensure_one()
response = self._request("GET", "/sync")
remote_groups = response.get("groups", [])
# Removing groups that are not on the remote server
remote_groups_ids = {remote_group["id"] for remote_group in remote_groups}
self.group_ids.filtered(
lambda group: group.cross_connect_server_group_id not in remote_groups_ids
).write({"cross_connect_server_id": False})

# Create or Update existing groups
for remote_group in remote_groups:
existing_group = self.env["res.groups"].search(
[("cross_connect_server_group_id", "=", remote_group["id"])]
)
if existing_group and not existing_group.cross_connect_server_id:
existing_group.write({"cross_connect_server_id": self.id})
if existing_group:
existing_group.sudo().write(
{
"name": f"{self.name}: {remote_group['name']}",
"comment": remote_group["comment"],
}
)
else:
self.env["res.groups"].sudo().create(
{
"cross_connect_server_id": self.id,
"cross_connect_server_group_id": remote_group["id"],
"name": f"{self.name}: {remote_group['name']}",
"comment": remote_group["comment"],
}
)

def action_sync(self):
for record in self:
record._sync_groups()

def action_disable(self):
for record in self:
record.group_ids.write({"cross_connect_server_id": False})

@property
def _server_env_fields(self):
return {"api_key": {}}

def unlink(self):
for rec in self:
# deleting the groups will delete the menu and related action.
rec.group_ids.unlink()
return super().unlink()
24 changes: 24 additions & 0 deletions cross_connect_client/models/res_groups.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2024 Akretion (http://www.akretion.com).
# @author Florian Mounier <florian.mounier@akretion.com>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


class ResGroups(models.Model):
_inherit = "res.groups"

cross_connect_server_id = fields.Many2one(
"cross.connect.server", string="Originating Cross Connect Server"
)
cross_connect_server_group_id = fields.Integer(
string="Originating Cross Connect Server Group ID"
)

_sql_constraints = [
(
"cross_connect_server_group_id_cross_connect_server_id_unique",
"unique (cross_connect_server_group_id, cross_connect_server_id)",
"Cross Connect Server Group ID must be unique per Cross Connect Server",
)
]
3 changes: 3 additions & 0 deletions cross_connect_client/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["whool"]
build-backend = "whool.buildapi"
1 change: 1 addition & 0 deletions cross_connect_client/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Florian Mounier <florian.mounier@akretion.com>
2 changes: 2 additions & 0 deletions cross_connect_client/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
This module allows this odoo instance users to connect directly on another odoo instance
where the module `cross_connect_server` is installed.
Loading