Unix-style user accounts (passwd, groups, permissions) with a web-friendly Python API.

0 downloads

dbbasic-accounts/example.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic-accounts.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic-accounts.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic-accounts.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

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