rbac
Role-Based Access Control (RBAC) Manager
Provides a comprehensive framework for handling user permissions, roles, and group management within the PlantDB ecosystem. It supports fine‑grained access control for scans and user accounts, allowing administrators to create, modify, and revoke permissions with ease.
Key Features
- Role and permission hierarchy with ADMIN, CONTRIBUTOR, and READER roles.
- Group‑based sharing of scans and dynamic membership handling.
- Decorator‑based permission checks (
requires_permission) for method protection. - User account lifecycle operations: activation, deactivation, unlocking.
- Full scan access control: ownership, group sharing, and global role fallback.
Usage Examples
from plantdb.commons.auth.rbac import RBACManager, Permission, User rbac = RBACManager()
Create a new user and assign a roleLink
user = rbac.create_user('alice', roles={'CONTRIBUTOR'})
Create a group and add the userLink
rbac.create_group(user, 'researchers') rbac.add_user_to_group(user, 'researchers', 'alice')
Check if the user can read a scanLink
scan_meta = {'owner': 'bob', 'sharing': ['researchers']} can_read = rbac.can_access_scan(user, scan_meta, Permission.READ) print(can_read) True
RBACManager
Link
RBACManager(users_file='users.json', groups_file='groups.json', max_login_attempts=3, lockout_duration=900)
Manage Role-Based Access Control (RBAC) for users and permissions.
This class provides methods to determine which permissions a user has,
check if a user has a specific permission, and verify if a user can access
or perform operations on scans based on their roles and permissions.
Attributes
Attributes
logger : logging.logger
The logger to use with this class.
users : plantdb.commons.auth.manager.UserManager
Manager for handling user operations.
groups : plantdb.commons.auth.manager.GroupManager
Manager for handling group operations.
Examples
Examples
>>> from plantdb.commons.auth.rbac import RBACManager
>>> rbac = RBACManager()
>>> # Get the 'guest' user and have a look at its permissions
>>> guest = rbac.get_guest_user()
v >>> rbac.get_user_permissions(guest)
{
Initialize the RBACManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
Path to the JSON file acting as users' database. Default is 'users.json'. |
'users.json'
|
|
str | Path
|
Path to the JSON file acting as groups' database. Defaults to "groups.json". |
'groups.json'
|
|
int
|
Maximum number of failed login attempts before account lockout. Defaults to |
3
|
|
int or timedelta
|
Account lockout duration in seconds. Defaults to |
900
|
Source code in plantdb/commons/auth/rbac.py
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | |
activate
Link
activate(requesting_user, username)
Activate a user account - requires MANAGE_USERS permission
Source code in plantdb/commons/auth/rbac.py
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 | |
add_user_to_group
Link
add_user_to_group(user, group_name, username_to_add)
Add a user to a group if the requesting user has permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to add a member. |
required |
|
str
|
The name of the group. |
required |
|
str
|
The username to add to the group. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 | |
can_access_scan
Link
can_access_scan(user, scan_metadata, operation)
Check if a user can perform a specific operation on a scan dataset.
This method implements the complete access control logic, including:
- Owner-based access (owners get a
CONTRIBUTORrole for their scans) - Group-based access (shared group members get a
CONTRIBUTORrole) - Global role-based access (fallback to user's global role)
- Admin override (admins can do everything)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User | TokenUser
|
The user requesting access. |
required |
|
dict
|
The scan metadata containing 'owner' and optional 'sharing' fields. |
required |
|
Permission
|
The operation/permission being requested. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
can_add_to_group
Link
can_add_to_group(user, group_name)
Check if a user can add members to a specific group.
Users can add members to a group if: 1. They are an admin (MANAGE_GROUPS permission), OR 2. They are a member of the group
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to add members. |
required |
|
str
|
The name of the group. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 | |
can_create_group
Link
can_create_group(user)
Check if a user can create groups.
Any user with a role that has a MANAGE_GROUPS permission can create groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | |
can_create_user
Link
can_create_user(user)
Check if a user can create new users.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
269 270 271 272 273 274 275 276 277 278 279 280 281 282 | |
can_delete_group
Link
can_delete_group(user)
Check if a user can delete a group.
Only users with the MANAGE_GROUPS permission can delete groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to delete the group. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
can_manage_groups
Link
can_manage_groups(user)
Check if a user can manage groups (create/delete groups).
Any user with a role that has a MANAGE_GROUPS permission can create groups.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
can_modify_scan_owner
Link
can_modify_scan_owner(user, scan_metadata)
Check if a user can modify the 'owner' field of a scan.
Only users with permission to MANAGE_USERS can modify scan ownership.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to modify ownership. |
required |
|
dict
|
The scan metadata. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 | |
can_modify_scan_sharing
Link
can_modify_scan_sharing(user, scan_metadata)
Check if a user can modify the 'sharing' field of a scan.
Users can modify sharing if they have WRITE permission for the scan (i.e., they are the owner, in a shared group, or have a global CONTRIBUTOR+ role).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to modify sharing. |
required |
|
dict
|
The scan metadata. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 | |
create_group
Link
create_group(user, group_name, users=None, description=None)
Create a new group if the user has permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user creating the group. |
required |
|
str
|
The unique name for the group. |
required |
|
Optional[Set[str]]
|
Initial set of users to add to the group. |
None
|
|
Optional[str]
|
Optional description of the group. |
None
|
Returns:
| Type | Description |
|---|---|
Optional[Group]
|
The created group object if successful, |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a group with the same name already exists. |
Source code in plantdb/commons/auth/rbac.py
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
create_user
Link
create_user(requesting_user, username, fullname, password, roles=None)
Create a new user account.
This method verifies that the requesting user has the MANAGE_USERS permission,
logs the creation attempt, and forwards the actual creation to the underlying
UserManager. It returns the newly created User object on success or None
if the operation is not permitted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user performing the creation request. |
required |
|
str
|
Desired username for the new account. |
required |
|
str
|
The full name of the user to create. |
required |
|
str
|
Password for the new account. |
required |
|
Role | set[Role] | None
|
Set of roles to assign to the new user.
If |
None
|
Returns:
| Type | Description |
|---|---|
User | None
|
The created |
Source code in plantdb/commons/auth/rbac.py
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 | |
deactivate
Link
deactivate(requesting_user, username)
Deactivate a user account - requires MANAGE_USERS permission
Source code in plantdb/commons/auth/rbac.py
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 | |
delete_group
Link
delete_group(user, group_name)
Delete a group if the user has permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to delete the group. |
required |
|
str
|
The name of the group to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 | |
ensure_scan_owner
Link
ensure_scan_owner(scan_metadata)
Ensure a scan has an owner field, defaulting to guest if missing.
This method should be called when loading or creating scans to ensure the owner field is always present.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
The scan metadata to check and potentially modify. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
The metadata updated with the 'owner' field. |
Source code in plantdb/commons/auth/rbac.py
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 | |
get_accessible_scans_for_user
Link
get_accessible_scans_for_user(user, all_scan_metadata)
Filter scans to only include those the user has READ access to.
This method can be used to implement scan listing with proper access control.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting scan access. |
required |
|
Dict[str, dict]
|
Dictionary mapping scan IDs to their metadata. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, dict]
|
Dictionary of scan IDs to metadata for scans the user can read. |
Source code in plantdb/commons/auth/rbac.py
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 | |
get_effective_role_for_scan
Link
get_effective_role_for_scan(user, scan_metadata)
Get the effective role a user has for a specific scan dataset.
This method determines the user's role based on: 1. Dataset ownership (an owner gets CONTRIBUTOR role) 2. Group sharing (shared group members get CONTRIBUTOR role) 3. User's global role (fallback)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User | TokenUser
|
The user to check. |
required |
|
dict
|
The scan metadata containing 'owner' and optional 'sharing' fields. |
required |
Returns:
| Type | Description |
|---|---|
Role
|
The effective role for this specific scan dataset. |
Source code in plantdb/commons/auth/rbac.py
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 | |
get_guest_user
Link
get_guest_user()
Retrieves the guest user from the user repository.
Returns:
| Type | Description |
|---|---|
User
|
The User object corresponding to the guest username. |
Source code in plantdb/commons/auth/rbac.py
259 260 261 262 263 264 265 266 267 | |
get_scan_permissions
Link
get_scan_permissions(user, scan_metadata)
Get the set of permissions a user has for a specific scan dataset.
This method considers the user's effective role for the specific scan, taking into account ownership and group sharing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User | TokenUser
|
The user to check permissions for. |
required |
|
dict
|
The scan metadata containing 'owner' and optional 'sharing' fields. |
required |
Returns:
| Type | Description |
|---|---|
Set[Permission]
|
The set of permissions the user has for this specific scan. |
Source code in plantdb/commons/auth/rbac.py
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 | |
get_user_groups
Link
get_user_groups(username)
Get all groups that a user belongs to.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The username to search for. |
required |
Returns:
| Type | Description |
|---|---|
List[Group]
|
A list of Group objects that the user belongs to. |
Source code in plantdb/commons/auth/rbac.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 | |
get_user_permissions
Link
get_user_permissions(user)
Get the set of permissions a user has based on their assigned roles.
This function returns a set containing all permissions directly assigned to the user as well as those inherited from any roles they are part of. The resulting set is a union of both direct and indirect permissions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User | TokenUser
|
A |
required |
Returns:
| Type | Description |
|---|---|
Set[Permission]
|
A set containing all permissions that the specified user has access to, including those inherited from roles. |
Notes
The result depends on the role_permissions attribute of the class instance.
Ensure that this dictionary is properly initialized before calling this method.
See Also
plantdb.commons.auth.models.User : Represents a user with permissions and roles. plantdb.commons.auth.models.Permission : Represents a permission that can be assigned to users or roles. plantdb.commons.auth.models.Role : Represents a role with specific permissions.
Examples:
>>> from plantdb.commons.auth.models import Permission
>>> from plantdb.commons.auth.models import User
>>> role_admin = Role('admin')
>>> role_user = Role('user')
>>> permission_a = Permission() # Mocking a specific permission
>>> permission_b = Permission() # Mocking another specific permission
>>> user = User(permissions={permission_a}, roles={role_user})
>>> role_permissions = {Role('admin'): {permission_b}}
>>> user.get_user_permissions(user)
{<__main__.Permission object at 0x...>}
Source code in plantdb/commons/auth/rbac.py
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | |
get_user_scan_role_summary
Link
get_user_scan_role_summary(user, scan_metadata)
Get a summary of the user's access to a specific scan.
This is useful for debugging and user interfaces to show access levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to analyze. |
required |
|
dict
|
The scan metadata. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
A dictionary containing access information including: |
Source code in plantdb/commons/auth/rbac.py
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | |
has_permission
Link
has_permission(user, permission)
Check if a user has a specific permission.
This function determines whether the given user has the specified permission, including administrative privileges.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User | TokenUser
|
The User object to check for permissions. |
required |
|
Permission
|
The permission level or type to verify against the user's permissions. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Notes
The function checks for the specific permission in the user's permission set.
See Also
get_user_permissions : Retrieve the list of permissions a user has.
Source code in plantdb/commons/auth/rbac.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
has_role
Link
Check if a user has a specific role.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user object to check for roles. |
required |
|
Role
|
The role to verify against the user's roles. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | |
is_guest_user
Link
is_guest_user(user)
Check if the given user is the guest user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user object to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 | |
list_groups
Link
list_groups(user)
List all groups if the user has permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting the group list. |
required |
Returns:
| Type | Description |
|---|---|
Optional[List[Group]]
|
A list of all groups if the user has permission, None otherwise. |
Source code in plantdb/commons/auth/rbac.py
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 | |
remove_user_from_group
Link
remove_user_from_group(user, group_name, username_to_remove)
Remove a user from a group if the requesting user has permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user requesting to remove a member. |
required |
|
str
|
The name of the group. |
required |
|
str
|
The username to remove from the group. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 | |
unlock
Link
unlock(requesting_user, username)
Unlock a user account - requires MANAGE_USERS permission
Source code in plantdb/commons/auth/rbac.py
870 871 872 873 874 875 876 877 878 879 880 881 882 883 | |
valid_sharing_groups
Link
valid_sharing_groups(sharing_groups)
Validate groups in the sharing list if they exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
List[str]
|
List of group names to validate. |
required |
Returns:
| Type | Description |
|---|---|
list
|
The list of valid groups; can be empty if none of the provided groups exist. |
Source code in plantdb/commons/auth/rbac.py
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 | |
validate_scan_metadata_access
Link
validate_scan_metadata_access(user, old_metadata, new_metadata)
Validate that a user can make the proposed metadata changes.
This method checks if the user has permission to modify specific fields like 'owner' and 'sharing' based on the RBAC rules.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user attempting to modify metadata. |
required |
|
dict
|
The current scan metadata. |
required |
|
dict
|
The proposed new metadata. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/rbac.py
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 | |
validate_sharing_groups
Link
validate_sharing_groups(sharing_groups)
Validate that all groups in the sharing list exist.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
List[str]
|
List of group names to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if all groups exist, False otherwise. |
Source code in plantdb/commons/auth/rbac.py
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | |
requires_permission
Link
requires_permission(required_permissions)
Decorator to check if the specified user has the required permission(s).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
Union[Permission, List[Permission]]
|
A single permission string or list of permission that the user must have to access the decorated method. |
required |
Source code in plantdb/commons/auth/rbac.py
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |