askrobots / dbbasic-accounts
Public
Unix-style user accounts (passwd, groups, permissions) with a web-friendly Python API.
example.py · 2.4 KB · 74 lines
| 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Example usage of dbbasic-accounts - Web-Friendly API |
| 4 | """ |
| 5 | |
| 6 | from dbbasic_accounts import Accounts |
| 7 | |
| 8 | # Initialize (creates ./etc/, ./home/, ./var/mail/ directories) |
| 9 | accounts = Accounts('.', domain='example.com') |
| 10 | |
| 11 | print("=== Web-Friendly API Example ===\n") |
| 12 | |
| 13 | # Register a user (web-style, with email) |
| 14 | user = accounts.register('john@example.com', 'secret123', name='John Doe') |
| 15 | print(f"✓ Registered user: {user.username} (UID {user.uid})") |
| 16 | print(f" Email: john@example.com") |
| 17 | print(f" Fullname: {user.fullname}") |
| 18 | |
| 19 | # Login (authenticate with email) |
| 20 | authenticated_user = accounts.login('john@example.com', 'secret123') |
| 21 | if authenticated_user: |
| 22 | print(f"✓ Login successful for {authenticated_user.username}") |
| 23 | else: |
| 24 | print("✗ Login failed") |
| 25 | |
| 26 | # Add roles (groups) |
| 27 | accounts.add_role('john@example.com', 'editor') |
| 28 | accounts.add_role('john@example.com', 'admin') |
| 29 | print(f"✓ Added roles: editor, admin") |
| 30 | |
| 31 | # Check user roles |
| 32 | roles = accounts.get_roles('john@example.com') |
| 33 | print(f" Roles for john@example.com: {roles}") |
| 34 | |
| 35 | # Get user by different methods |
| 36 | user_by_email = accounts.get_user(email='john@example.com') |
| 37 | user_by_id = accounts.get_user(user_id=1000) |
| 38 | user_by_username = accounts.get_user(username='john') |
| 39 | print(f"✓ Retrieved user by email, ID, and username") |
| 40 | |
| 41 | # Get filesystem paths |
| 42 | home_dir = accounts.get_home_directory('john@example.com') |
| 43 | mailbox = accounts.get_mailbox('john@example.com') |
| 44 | print(f" Home directory: {home_dir}") |
| 45 | print(f" Mailbox: {mailbox}") |
| 46 | |
| 47 | # Change password |
| 48 | accounts.change_password('john@example.com', 'newsecret456') |
| 49 | print(f"✓ Password changed") |
| 50 | |
| 51 | # Register another user |
| 52 | user2 = accounts.register('jane@example.com', 'pass123', name='Jane Smith') |
| 53 | print(f"\n✓ Registered user: {user2.username} (UID {user2.uid})") |
| 54 | |
| 55 | # List all users |
| 56 | print("\n=== All Users ===") |
| 57 | for user in accounts.list_users(): |
| 58 | print(f" {user.username}: {user.fullname} (UID {user.uid})") |
| 59 | |
| 60 | print("\n=== File Structure ===") |
| 61 | print("Check these directories:") |
| 62 | print(" ./etc/passwd.tsv - User database") |
| 63 | print(" ./etc/shadow.tsv - Password hashes") |
| 64 | print(" ./etc/group.tsv - Groups/roles") |
| 65 | print(" ./home/john/ - John's home directory") |
| 66 | print(" ./home/jane/ - Jane's home directory") |
| 67 | print(" ./var/mail/john - John's mailbox") |
| 68 | print(" ./var/mail/jane - Jane's mailbox") |
| 69 | |
| 70 | print("\n=== Unix Commands Work! ===") |
| 71 | print("Try these commands:") |
| 72 | print(" cat etc/passwd.tsv") |
| 73 | print(" ls home/john/") |
| 74 | print(" cat var/mail/john") |