manager
User and Group Management Module
This module provides two primary classes - UserManager and GroupManager - to handle
authentication, authorization, and role‑based access control in a lightweight
file‑based persistence system. User data, including hashed passwords, roles,
and lock‑out state, is stored in a JSON file, while group membership and
metadata are kept in a separate JSON file. The module is designed for small
projects or prototyping, offering straightforward APIs for creating users,
managing passwords, locking out accounts, and organizing users into
groups with associated permissions.
Key Features
- Password hashing with Argon2 and secure verification.
- Account lock‑out after configurable failed login attempts.
- User activation/deactivation and role assignment.
- Group management: create, delete, add/remove users, and query memberships.
- Atomic JSON persistence to avoid data corruption.
- Minimal dependencies - relies only on the standard library plus
argon2.
Usage Examples
from pathlib import Path from plantdb.commons.auth.manager import UserManager, GroupManager, Role
Initialize managers (will create JSON files if absent)Link
um = UserManager(users_file=Path('users.json')) gm = GroupManager(groups_file=Path('groups.json'))
Create a new userLink
um.create('alice', 'Alice Smith', 'secure123', roles={Role.ADMIN})
Validate credentialsLink
assert um.validate('alice', 'secure123')
Create a group and add the userLink
group = gm.create_group('admins', creator='alice', users={'alice'}, description='Admin group')
Check membershipLink
assert 'admins' in [g.name for g in gm.get_user_groups('alice')]
GroupExistsError
Link
Bases: Exception
Raised when attempting to create a group that already exists.
GroupManager
Link
GroupManager(groups_file='groups.json')
Bases: object
Manages groups for the RBAC system.
This class handles the creation, modification, and persistence of user groups. Groups are stored in a JSON file and loaded/saved as needed.
Attributes:
| Name | Type | Description |
|---|---|---|
groups_file |
str | Path
|
The path to the JSON file where groups are stored. |
groups |
Dict[str, Group]
|
Dictionary mapping group names to Group objects. |
Initialize the GroupManager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str | Path
|
The path to the JSON file for storing groups. Defaults to |
'groups.json'
|
Source code in plantdb/commons/auth/manager.py
714 715 716 717 718 719 720 721 722 723 724 725 726 | |
add_user_to_group
Link
add_user_to_group(group_name, username)
Add a user to a group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
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/manager.py
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 | |
create_group
Link
create_group(name, creator, users=None, description=None)
Create a new group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The unique name for the group. |
required |
|
str
|
The username of the user creating the group. |
required |
|
Optional[Set[str]]
|
Initial set of users to add to the group. Creator is automatically added. |
None
|
|
Optional[str]
|
Optional description of the group. |
None
|
Returns:
| Type | Description |
|---|---|
Group
|
The created group object. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a group with the same name already exists. |
Source code in plantdb/commons/auth/manager.py
778 779 780 781 782 783 784 785 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 814 815 816 817 818 819 820 821 | |
delete_group
Link
delete_group(name)
Delete a group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the group to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/manager.py
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 | |
get_group
Link
get_group(name)
Get a group by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the group to retrieve. |
required |
Returns:
| Type | Description |
|---|---|
Optional[Group]
|
The group object if it exists, |
Source code in plantdb/commons/auth/manager.py
823 824 825 826 827 828 829 830 831 832 833 834 835 836 | |
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/manager.py
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 | |
group_exists
Link
group_exists(name)
Check if a group exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the group to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the group exists, False otherwise. |
Source code in plantdb/commons/auth/manager.py
935 936 937 938 939 940 941 942 943 944 945 946 947 948 | |
list_groups
Link
list_groups()
Get a list of all groups.
Returns:
| Type | Description |
|---|---|
List[Group]
|
A list of all Group objects. |
Source code in plantdb/commons/auth/manager.py
925 926 927 928 929 930 931 932 933 | |
remove_user_from_group
Link
remove_user_from_group(group_name, username)
Remove a user from a group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
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/manager.py
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 | |
UserManager
Link
UserManager(users_file='users.json', max_login_attempts=3, lockout_duration=900)
Bases: object
UserManager class for managing user data.
The UserManager class provides methods to create, load, save, and manage users. It uses a JSON file to persist user data. It includes functionalities like password hashing and verification, user account lockout management, and handling of guest accounts.
Attributes:
| Name | Type | Description |
|---|---|---|
users_file |
str
|
Path to the JSON file where user data is stored. |
users |
Dict[str, User]
|
Dictionary containing all users, indexed by username. |
GUEST_USERNAME |
str
|
Default username for guest accounts. |
GUEST_PASSWORD |
str
|
Default password for guest accounts. |
Examples:
>>> from pathlib import Path
>>> from tempfile import gettempdir
>>> from plantdb.commons.auth.manager import UserManager
>>> manager = UserManager(users_file=Path(gettempdir())/'users.json')
>>> manager.validate_user_password(manager.GUEST_USERNAME, manager.GUEST_PASSWORD)
True
>>> manager.validate(manager.GUEST_USERNAME, manager.GUEST_PASSWORD)
True
>>> manager.validate('gest', manager.GUEST_PASSWORD)
False
>>> manager.deactivate(manager.get_user(manager.GUEST_USERNAME))
>>> manager.validate(manager.GUEST_USERNAME, manager.GUEST_PASSWORD)
WARNING [UserManager] Account guest is not active.
False
User manager constructor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str or Path
|
The path to the JSON file where user data is stored. Defaults to |
'users.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/manager.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
activate
Link
activate(user)
Activates a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to activate. |
required |
Source code in plantdb/commons/auth/manager.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 | |
create
Link
Create a new user and store the user information in a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The username of the user to create. This will be converted to lowercase. |
required |
|
str
|
The full name of the user to create. |
required |
|
str
|
The password of the user to create. |
required |
|
Role or Set[Role]
|
The roles to associate with the user.
If |
None
|
Examples:
>>> from plantdb.commons.auth.manager import Role, UserManager
>>> manager = UserManager()
>>> manager.create('batman', "Bruce Wayne", "joker", Role.ADMIN)
>>> print(manager.users)
batman
Source code in plantdb/commons/auth/manager.py
279 280 281 282 283 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 322 323 324 325 326 327 328 329 330 331 | |
deactivate
Link
deactivate(user)
Deactivates a user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to deactivate. |
required |
Source code in plantdb/commons/auth/manager.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 | |
exists
Link
exists(username)
Check if a user exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the user to check for existence. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Notes
This function is case-sensitive. For case-insensitive checks, convert both the username and the keys to the lower or upper case before comparison.
Source code in plantdb/commons/auth/manager.py
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
get_token_user
Link
get_token_user(token_payload)
Retrieve a User object based on the provided username.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
The token containing the identifier for the user to be retrieved and special permissions.
Must be a token with the type |
required |
Returns:
| Type | Description |
|---|---|
TokenUser | None
|
An instance of the |
Raises:
| Type | Description |
|---|---|
TypeError
|
If the provided token payload does not correspond to an api token. |
Source code in plantdb/commons/auth/manager.py
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 435 436 437 438 | |
get_user
Link
get_user(username)
Retrieve a User object based on the provided username.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The unique identifier for the user to be retrieved. |
required |
Returns:
| Type | Description |
|---|---|
User | None
|
An instance of the |
Source code in plantdb/commons/auth/manager.py
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
get_user_from_decoded_token
Link
get_user_from_decoded_token(token_payload)
Extract a user representation from a decoded JWT payload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
dict
|
The decoded JWT payload as a |
required |
Returns:
| Type | Description |
|---|---|
User | TokenUser | None
|
|
Source code in plantdb/commons/auth/manager.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
is_active
Link
is_active(username)
Check whether a user account is active.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the user account to check for activity status. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/manager.py
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 | |
is_locked_out
Link
is_locked_out(username)
Check if an account is locked.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The username of the account to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/manager.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
unlock_user
Link
unlock_user(user)
Unlock a specified user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
User
|
The user to unlock. |
required |
Source code in plantdb/commons/auth/manager.py
580 581 582 583 584 585 586 587 588 589 590 | |
update_password
Link
update_password(username, password, new_password)
Update the password of an existing user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The name of the user whose password is to be updated. |
required |
|
str
|
The current password of the user to verify their identity. |
required |
|
str
|
The new password to set for the user. If |
required |
Source code in plantdb/commons/auth/manager.py
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 | |
validate
Link
Validate the user credentials.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The username provided by the user attempting to log in. |
required |
|
str
|
The password provided by the user attempting to log in. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/manager.py
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 | |
validate_user_password
Link
Validate a user's password.
This function checks if the provided plaintext password matches the hashed password stored for the given username.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
|
str
|
The username of the user. |
required |
|
str
|
The plain-text password to validate. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Source code in plantdb/commons/auth/manager.py
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |