Skip to content

Commit

Permalink
Merge pull request #37 from onesteinbv/16.0_base_list_owned_databases
Browse files Browse the repository at this point in the history
[ADD] base_list_owned_databases
  • Loading branch information
MrGigSolutions authored Apr 18, 2024
2 parents 8d5ead0 + 6c8b22c commit d17becc
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions base_list_owned_databases/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import patches
20 changes: 20 additions & 0 deletions base_list_owned_databases/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2017-2023 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

{
"name": "List Owned Databases",
"summary": "When using the database selector, the list of databases "
"now consists of any databases the current user has "
"ownership of through role membership, rather than only "
"those specifically owned by the user. "
"Must be declared as a server-wide module.",
"author": "Onestein",
"website": "https://www.onestein.nl",
"category": "Tools",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"depends": [
"base",
],
"installable": True,
}
1 change: 1 addition & 0 deletions base_list_owned_databases/patches/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import db
69 changes: 69 additions & 0 deletions base_list_owned_databases/patches/db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Copyright 2017-2023 Onestein (<https://www.onestein.eu>)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
# This monkey patch allows list databases to find databases of which the current
# user is the owner, but also those that have an owner role that the current
# user is a member of.
import logging
from contextlib import closing

import odoo
import odoo.release
import odoo.sql_db
import odoo.tools

_logger = logging.getLogger(__name__)


def list_dbs(force=False):
if not odoo.tools.config["list_db"] and not force:
raise odoo.exceptions.AccessDenied()

if not odoo.tools.config["dbfilter"] and odoo.tools.config["db_name"]:
# In case --db-filter is not provided and --database is passed, Odoo will not
# fetch the list of databases available on the postgres server and instead will
# use the value of --database as comma seperated list of exposed databases.
res = sorted(db.strip() for db in odoo.tools.config["db_name"].split(","))
return res

chosen_template = odoo.tools.config["db_template"]
templates_list = tuple({"postgres", chosen_template})
db = odoo.sql_db.db_connect("postgres")
with closing(db.cursor()) as cr:
try:
# ### START OF PATCH ####
# cr.execute(
# "select datname from pg_database where datdba=(select usesysid from pg_user where usename=current_user) and not datistemplate and datallowconn and datname not in %s order by datname",
# (templates_list,))
cr.execute(
"""SELECT datname
FROM pg_database
WHERE NOT datistemplate AND datallowconn AND datname NOT IN %s
AND datdba IN
(
WITH RECURSIVE membership_tree(grpid, userid) AS (
-- Get all roles and list them as their own group as well
SELECT pg_roles.oid, pg_roles.oid
FROM pg_roles
UNION ALL
-- Now add all group membership
SELECT m_1.roleid, t_1.userid
FROM pg_auth_members m_1, membership_tree t_1
WHERE m_1.member = t_1.grpid
)
SELECT DISTINCT t.grpid
FROM membership_tree t, pg_roles r, pg_roles m
WHERE t.grpid = m.oid AND t.userid = r.oid
AND t.userid IN (SELECT usesysid FROM pg_user WHERE usename=current_user)
)
ORDER BY datname;""",
(templates_list,),
)
# ### END OF PATCH ###
res = [odoo.tools.ustr(name) for (name,) in cr.fetchall()]
except Exception:
_logger.exception("Listing databases failed:")
res = []
return res


odoo.service.db.list_dbs = list_dbs

0 comments on commit d17becc

Please sign in to comment.