Skip to content

Commit da41483

Browse files
Add support for custom roles and role permissions management in pgAdmin. #7310
1 parent 73fefb8 commit da41483

File tree

53 files changed

+1259
-138
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1259
-138
lines changed

docs/en_US/images/add_role.png

26 KB
Loading

docs/en_US/images/permissions.png

87.2 KB
Loading

docs/en_US/images/roles.png

45.9 KB
Loading

docs/en_US/images/user.png

-70.4 KB
Binary file not shown.

docs/en_US/images/users.png

62.4 KB
Loading

docs/en_US/user_management.rst

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ When you authenticate with pgAdmin, the server definitions associated with that
1212
login role are made available in the tree control.
1313

1414
Users Tab
15-
*******************
15+
*********
1616
An administrative user can use the *Users* tab to:
1717

1818
* manage pgAdmin users
@@ -21,7 +21,7 @@ An administrative user can use the *Users* tab to:
2121
* deactivate user
2222
* unlock a locked user
2323

24-
.. image:: images/user.png
24+
.. image:: images/users.png
2525
:alt: pgAdmin user management window
2626
:align: center
2727

@@ -78,6 +78,60 @@ users, but otherwise have the same capabilities as those with the *User* role.
7878
* Click the *Help* button (?) to access online help.
7979

8080

81+
Roles Tab
82+
*********
83+
An administrative user can use the *Roles* tab to:
84+
85+
* manage pgAdmin roles
86+
* delete roles
87+
88+
.. image:: images/roles.png
89+
:alt: pgAdmin roles management window
90+
:align: center
91+
92+
Use the *Search* field to specify criteria and review a list of roles
93+
that match the specified criteria. You can enter a value that matches
94+
the following criteria types: *Role Name* or *Description*.
95+
96+
To add a role, click the Add (+) button at the top left corner. It will open a
97+
dialog where you can fill in details for the new role.
98+
99+
.. image:: images/add_role.png
100+
:alt: pgAdmin roles management window add new role
101+
:align: center
102+
103+
Provide information about the new pgAdmin role in the row:
104+
105+
* Use the *Role Name* field to specify a unique name for the role.
106+
* Use the *Description* field to provide a brief description of the role.
107+
108+
To delete a role, click the trash icon to the left of the row and confirm deletion
109+
in the *Delete role?* dialog. If the role is associated with any users or resources,
110+
you may need to reassign those associations before deletion.
111+
112+
Roles allow administrators to group privileges and assign them to users more efficiently.
113+
This helps in managing permissions and access control within the pgAdmin client.
114+
115+
* Click the *Refresh* button to get the latest roles list.
116+
* Click the *Help* button (?) to access online help.
117+
118+
119+
Permissions Tab
120+
***************
121+
An administrative user can use the *Permissions* tab to manage pgAdmin permissions for
122+
a role.
123+
124+
.. image:: images/permissions.png
125+
:alt: pgAdmin permissions management window
126+
:align: center
127+
128+
* Filter permissions using the *Search* field by entering names that match the list.
129+
* Administrators can select permissions from the list of available permissions, and
130+
choose to grant or revoke these permissions for specific roles.
131+
* The permissions are applied to the selected role immediately.
132+
133+
134+
81135
Using 'setup.py' command line script
82136
####################################
83137

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
"""empty message
3+
4+
Revision ID: 89b20ef0d04d
5+
Revises: e982c040d9b5
6+
Create Date: 2025-03-21 13:55:44.614151
7+
8+
"""
9+
import sqlalchemy as sa
10+
from alembic import op
11+
from pgadmin.tools.user_management.PgPermissions import AllPermissionTypes
12+
# revision identifiers, used by Alembic.
13+
revision = '89b20ef0d04d'
14+
down_revision = 'e982c040d9b5'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
op.add_column('role', sa.Column('permissions', sa.Text()))
21+
22+
# get metadata from current connection
23+
meta = sa.MetaData()
24+
# define table representation
25+
meta.reflect(op.get_bind(), only=('role',))
26+
role_table = sa.Table('role', meta)
27+
28+
from pgadmin.tools.user_management.PgPermissions import AllPermissionTypes
29+
op.execute(
30+
role_table.update().where(role_table.c.name == 'User')
31+
.values(permissions=",".join(AllPermissionTypes.__dict__.keys())))
32+
33+
34+
def downgrade():
35+
# pgAdmin only upgrades, downgrade not implemented.
36+
pass

web/pgadmin/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,8 @@ def get_locale():
349349
app.config['SECURITY_MSG_INVALID_PASSWORD'] = \
350350
(gettext("Incorrect username or password."), "error")
351351
app.config['SECURITY_PASSWORD_LENGTH_MIN'] = config.PASSWORD_LENGTH_MIN
352+
app.config['SECURITY_MSG_UNAUTHORIZED'] = \
353+
(gettext("You do not have permission to this resource."), "error")
352354

353355
# Create database connection object and mailer
354356
db.init_app(app)

web/pgadmin/browser/server_groups/servers/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from flask import render_template, request, make_response, jsonify, \
1414
current_app, url_for, session
1515
from flask_babel import gettext
16-
from flask_security import current_user
16+
from flask_security import current_user, permissions_required
1717
from pgadmin.user_login_check import pga_login_required
1818
from psycopg.conninfo import make_conninfo, conninfo_to_dict
1919

@@ -24,6 +24,7 @@
2424
from pgadmin.utils.crypto import encrypt, decrypt, pqencryptpassword
2525
from pgadmin.utils.menu import MenuItem
2626
from pgadmin.tools.sqleditor.utils.query_history import QueryHistory
27+
from pgadmin.tools.user_management.PgPermissions import AllPermissionTypes
2728

2829
import config
2930
from config import PG_DEFAULT_DRIVER
@@ -1080,6 +1081,7 @@ def update_connection_string(manager, server):
10801081
display_conn_string = make_conninfo(**con_info_ord)
10811082
return display_conn_string
10821083

1084+
@permissions_required(AllPermissionTypes.object_register_server)
10831085
@pga_login_required
10841086
def create(self, gid):
10851087
"""Add a server node to the settings database"""

web/pgadmin/browser/server_groups/servers/static/js/server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ define('pgadmin.node.server', [
8181
name: 'create_server_on_sg', node: 'server_group', module: this,
8282
applies: ['object', 'context'], callback: 'show_obj_properties',
8383
category: 'register', priority: 1, label: gettext('Server...'),
84-
data: {action: 'create'}, enable: 'canCreate',
84+
data: {action: 'create'}, enable: 'canCreate', permission: 'object_register_server'
8585
},{
8686
name: 'disconnect_all_servers', node: 'server_group', module: this,
8787
applies: ['object','context'], callback: 'disconnect_all_servers',
@@ -91,7 +91,7 @@ define('pgadmin.node.server', [
9191
name: 'create_server', node: 'server', module: this,
9292
applies: ['object', 'context'], callback: 'show_obj_properties',
9393
category: 'register', priority: 3, label: gettext('Server...'),
94-
data: {action: 'create'}, enable: 'canCreate',
94+
data: {action: 'create'}, enable: 'canCreate', permission: 'object_register_server'
9595
},{
9696
name: 'connect_server', node: 'server', module: this,
9797
applies: ['object', 'context'], callback: 'connect_server',

0 commit comments

Comments
 (0)