Skip to content

Commit 7d8a915

Browse files
Add support for custom roles and role permissions management in pgAdmin. #7310
1 parent 8b4df8b commit 7d8a915

File tree

66 files changed

+1438
-205
lines changed

Some content is hidden

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

66 files changed

+1438
-205
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: 73 additions & 15 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 *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

@@ -108,10 +162,11 @@ email and password. role and active will be optional fields.
108162
109163
/path/to/python /path/to/setup.py add-user user1@gmail.com password
110164
111-
# to specify a role, admin and non-admin users:
165+
# to specify a role, either you can use --admin for Administrator role or provide the
166+
# role using --role. If both are provided --admin will be used:
112167
113168
/path/to/python /path/to/setup.py add-user user1@gmail.com password --admin
114-
/path/to/python /path/to/setup.py add-user user1@gmail.com password --nonadmin
169+
/path/to/python /path/to/setup.py add-user user1@gmail.com password --role Users
115170
116171
# to specify user's status
117172
@@ -132,10 +187,11 @@ followed by email, password and authentication source. email, role and status wi
132187
133188
/path/to/python /path/to/setup.py add-external-user ldapuser ldap --email user1@gmail.com
134189
135-
# to specify a role, admin and non-admin user:
190+
# to specify a role, either you can use --admin for Administrator role or provide the
191+
# role using --role. If both are provided --admin will be used:
136192
137193
/path/to/python /path/to/setup.py add-external-user ldapuser ldap --admin
138-
/path/to/python /path/to/setup.py add-external-user ldapuser ldap --nonadmin
194+
/path/to/python /path/to/setup.py add-external-user ldapuser ldap --role Users
139195
140196
# to specify user's status
141197
@@ -152,10 +208,11 @@ email address. password, role and active are updatable fields.
152208
153209
/path/to/python /path/to/setup.py update-user user1@gmail.com --password new-password
154210
155-
# to specify a role, admin and non-admin user:
211+
# to specify a role, either you can use --admin for Administrator role or provide the
212+
# role using --role. If both are provided --admin will be used:
156213
157-
/path/to/python /path/to/setup.py update-user user1@gmail.com password --role --admin
158-
/path/to/python /path/to/setup.py update-user user1@gmail.com password --role --nonadmin
214+
/path/to/python /path/to/setup.py update-user user1@gmail.com password --admin
215+
/path/to/python /path/to/setup.py update-user user1@gmail.com password --role Users
159216
160217
# to specify user's status
161218
@@ -172,17 +229,18 @@ followed by username and auth source. email, password, role and active are updat
172229
173230
# to change email address:
174231
175-
/path/to/python /path/to/setup.py update-external-user ldap ldapuser --email newemail@gmail.com
232+
/path/to/python /path/to/setup.py update-external-user ldapuser --auth-source ldap --email newemail@gmail.com
176233
177-
# to specify a role, admin and non-admin user:
234+
# to specify a role, either you can use --admin for Administrator role or provide the
235+
# role using --role. If both are provided --admin will be used:
178236
179-
/path/to/python /path/to/setup.py update-user user1@gmail.com password --role --admin
180-
/path/to/python /path/to/setup.py update-user user1@gmail.com password --role --nonadmin
237+
/path/to/python /path/to/setup.py update-external-user user1@gmail.com password --role --admin
238+
/path/to/python /path/to/setup.py update-external-user user1@gmail.com password --role --role Users
181239
182240
# to change user's status
183241
184-
/path/to/python /path/to/setup.py update-user ldap ldapuser --active
185-
/path/to/python /path/to/setup.py update-user ldap ldapuser --inactive
242+
/path/to/python /path/to/setup.py update-user ldapuser --auth-source ldap --active
243+
/path/to/python /path/to/setup.py update-user ldapuser --auth-source ldap --inactive
186244
187245
Delete User
188246
***********

web/migrations/versions/1f0eddc8fc79_.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ def upgrade():
3030
sa.Column('db_res_type', sa.String(length=32),
3131
server_default=RESTRICTION_TYPE_DATABASES))
3232

33+
# For adding custom role permissions
34+
op.add_column('role', sa.Column('permissions', sa.Text()))
35+
36+
# get metadata from current connection
37+
meta = sa.MetaData()
38+
# define table representation
39+
meta.reflect(op.get_bind(), only=('role',))
40+
role_table = sa.Table('role', meta)
41+
42+
from pgadmin.tools.user_management.PgAdminPermissions import AllPermissionTypes
43+
op.execute(
44+
role_table.update().where(role_table.c.name == 'User')
45+
.values(permissions=",".join(AllPermissionTypes.list())))
46+
3347

3448
def downgrade():
3549
# pgAdmin only upgrades, downgrade not implemented.

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("Unauthorised access, permission denied."), "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.PgAdminPermissions import AllPermissionTypes
2728

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

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

web/pgadmin/browser/server_groups/servers/databases/schemas/static/js/schema.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import PGSchema from './schema.ui';
1111
import { getNodePrivilegeRoleSchema } from '../../../../static/js/privilege.ui';
1212
import { getNodeListByName } from '../../../../../../static/js/node_ajax';
13+
import { AllPermissionTypes } from '../../../../../../static/js/constants';
1314

1415
define('pgadmin.node.schema', [
1516
'sources/gettext', 'sources/url_for',
@@ -64,7 +65,8 @@ define('pgadmin.node.schema', [
6465
},{
6566
name: 'generate_erd', node: 'schema', module: this,
6667
applies: ['object', 'context'], callback: 'generate_erd',
67-
priority: 5, label: gettext('ERD For Schema')
68+
priority: 5, label: gettext('ERD For Schema'),
69+
permission: AllPermissionTypes.TOOLS_ERD_TOOL,
6870
}]);
6971
},
7072
can_create_schema: function(node) {

0 commit comments

Comments
 (0)