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

0 downloads

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
users.py · 8.4 KB · 191 lines
1"""Users management admin interface"""
2
3from dbbasic_accounts import Accounts
4
5
6def handle(request):
7 """
8 Handle /admin/accounts/users requests.
9
10 Lists all users with their roles, status, and last login.
11 """
12 # Initialize accounts system
13 accounts = Accounts()
14
15 # Get all users
16 users = accounts.list_users()
17
18 # Build user data for display
19 user_data = []
20 for user in users:
21 # Extract email from fullname (format: "Name <email>" or just "email")
22 email = user.fullname
23 if '<' in email and '>' in email:
24 email = email.split('<')[1].split('>')[0]
25
26 # Get user's roles (groups)
27 roles = accounts.get_roles(email)
28
29 # Determine primary role for badge
30 primary_role = 'User'
31 badge_class = 'badge-green'
32 if 'admin' in roles or 'admins' in roles:
33 primary_role = 'Admin'
34 badge_class = 'badge-red'
35 elif 'editor' in roles or 'editors' in roles:
36 primary_role = 'Editor'
37 badge_class = 'badge-blue'
38
39 # Get initials for avatar
40 name_parts = user.fullname.split('<')[0].strip().split()
41 initials = ''.join([p[0].upper() for p in name_parts[:2]]) if name_parts else user.username[:2].upper()
42
43 user_data.append({
44 'id': user.uid,
45 'username': user.username,
46 'email': email,
47 'fullname': user.fullname.split('<')[0].strip() or user.username,
48 'initials': initials,
49 'role': primary_role,
50 'badge_class': badge_class,
51 'status': 'Active', # TODO: track last login
52 'status_class': 'status-active',
53 'last_login': '—', # TODO: implement last login tracking
54 })
55
56 # Build HTML
57 from dbbasic_admin.admin import build_nav
58 nav_items = build_nav()
59 nav_html = "".join(f'<li><a href="{item["href"]}">{item.get("icon", "")} {item["label"]}</a></li>' for item in nav_items)
60
61 # Build users table rows
62 users_html = ""
63 for u in user_data:
64 users_html += f"""
65 <tr>
66 <td><input type="checkbox"></td>
67 <td>
68 <div class="user-cell">
69 <div class="user-avatar">{u['initials']}</div>
70 <span class="user-name">{u['fullname']}</span>
71 </div>
72 </td>
73 <td>{u['email']}</td>
74 <td><span class="badge {u['badge_class']}">{u['role']}</span></td>
75 <td>{u['last_login']}</td>
76 <td><span class="status-dot {u['status_class']}"></span> {u['status']}</td>
77 <td>
78 <div class="action-buttons">
79 <button class="btn-icon" title="Edit">✏️</button>
80 <button class="btn-icon" title="Delete">🗑️</button>
81 </div>
82 </td>
83 </tr>
84 """
85
86 html_content = f"""<!DOCTYPE html>
87<html lang="en">
88<head>
89 <meta charset="UTF-8">
90 <meta name="viewport" content="width=device-width, initial-scale=1.0">
91 <title>Users - Admin</title>
92 <style>
93 * {{ margin: 0; padding: 0; box-sizing: border-box; }}
94 body {{ font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; display: flex; height: 100vh; background: #f5f5f5; }}
95 .sidebar {{ width: 250px; background: #2c3e50; color: white; padding: 20px; overflow-y: auto; }}
96 .sidebar h1 {{ font-size: 24px; margin-bottom: 30px; color: #ecf0f1; }}
97 .sidebar ul {{ list-style: none; }}
98 .sidebar li {{ margin-bottom: 10px; }}
99 .sidebar a {{ color: #ecf0f1; text-decoration: none; display: block; padding: 10px; border-radius: 5px; transition: background 0.2s; }}
100 .sidebar a:hover, .sidebar a.active {{ background: #34495e; }}
101 .content {{ flex: 1; padding: 40px; overflow-y: auto; }}
102 .header {{ background: white; padding: 30px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; display: flex; justify-content: space-between; align-items: center; }}
103 .header h2 {{ color: #2c3e50; margin: 0; }}
104 .btn-primary {{ background: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; font-size: 14px; }}
105 .btn-primary:hover {{ background: #2980b9; }}
106 .card {{ background: white; padding: 20px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }}
107 .table-container {{ overflow-x: auto; }}
108 table {{ width: 100%; border-collapse: collapse; }}
109 th, td {{ padding: 12px; text-align: left; border-bottom: 1px solid #ddd; }}
110 th {{ background: #f8f9fa; font-weight: 600; }}
111 .user-cell {{ display: flex; align-items: center; gap: 10px; }}
112 .user-avatar {{ width: 40px; height: 40px; border-radius: 50%; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; font-size: 14px; }}
113 .badge {{ display: inline-block; padding: 4px 12px; border-radius: 12px; font-size: 12px; font-weight: 600; }}
114 .badge-red {{ background: #fee; color: #c00; }}
115 .badge-blue {{ background: #e3f2fd; color: #1976d2; }}
116 .badge-green {{ background: #e8f5e9; color: #388e3c; }}
117 .status-dot {{ display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }}
118 .status-active {{ background: #4caf50; }}
119 .status-inactive {{ background: #999; }}
120 .action-buttons {{ display: flex; gap: 5px; }}
121 .btn-icon {{ background: none; border: none; cursor: pointer; font-size: 16px; padding: 5px; }}
122 .btn-icon:hover {{ opacity: 0.7; }}
123 .breadcrumb {{ color: #666; font-size: 14px; }}
124 .filters-card {{ background: white; padding: 15px; border-radius: 10px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); margin-bottom: 20px; }}
125 .filters-row {{ display: flex; gap: 10px; align-items: center; }}
126 .search-input {{ flex: 1; padding: 8px 12px; border: 1px solid #ddd; border-radius: 5px; }}
127 .filter-select {{ padding: 8px 12px; border: 1px solid #ddd; border-radius: 5px; background: white; }}
128 .btn-secondary {{ background: #6c757d; color: white; border: none; padding: 8px 16px; border-radius: 5px; cursor: pointer; }}
129 </style>
130</head>
131<body>
132 <div class="sidebar"><h1>Admin</h1><ul>{nav_html}</ul></div>
133 <div class="content">
134 <div class="header">
135 <div>
136 <h2>Users</h2>
137 <div class="breadcrumb">Home / Users</div>
138 </div>
139 <button class="btn-primary">+ New User</button>
140 </div>
141
142 <div class="filters-card">
143 <div class="filters-row">
144 <input type="search" placeholder="Search users..." class="search-input">
145 <select class="filter-select">
146 <option>All Roles</option>
147 <option>Admin</option>
148 <option>Editor</option>
149 <option>Author</option>
150 </select>
151 <select class="filter-select">
152 <option>All Status</option>
153 <option>Active</option>
154 <option>Inactive</option>
155 </select>
156 <button class="btn-secondary">Filter</button>
157 </div>
158 </div>
159
160 <div class="card">
161 <h3 style="margin-bottom: 20px;">All Users ({len(user_data)})</h3>
162 <div class="table-container">
163 <table>
164 <thead>
165 <tr>
166 <th><input type="checkbox"></th>
167 <th>User</th>
168 <th>Email</th>
169 <th>Role</th>
170 <th>Last Login</th>
171 <th>Status</th>
172 <th>Actions</th>
173 </tr>
174 </thead>
175 <tbody>
176 {users_html}
177 </tbody>
178 </table>
179 </div>
180 </div>
181 </div>
182</body>
183</html>"""
184
185 # Return HTML response using dbbasic_web format
186 try:
187 from dbbasic_web.responses import html as html_response
188 return html_response(html_content)
189 except ImportError:
190 # Fallback if dbbasic_web not available
191 return (200, [('content-type', 'text/html; charset=utf-8')], [html_content.encode()])