feat: Kanban profiles, a Usuários page, and machines that route orders
Every operator could do everything, and accounts were made from the console. Each account now has a profile, checked by the server on every action: Administrador (everything, plus users and machines), Gestão (everything but users), Operador (moves cards and handles artwork) and Vendedor (reads orders, quotes and Números, changes nothing). The superadmin is the system's own account, set from the console and kept by the account OPERATOR_EMAIL seeds; administrators cannot change it, and nobody can delete, disable or demote themselves. The Usuários tab creates, edits, disables and deletes users with a first password (there is no e-mail yet), and registers machines with the products each prints. An order goes to the first active machine that prints its product, or to the one chosen in its panel; an operator tied to machines sees and downloads only their orders. Everyone changes their own password in the account menu, which now holds the name and Sair too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -114,13 +114,47 @@ def login_failed(email):
|
||||
"""Count a failed sign-in (or registration attempt) against the account."""
|
||||
rate_limit('auth-account', email, 1_000_000)
|
||||
|
||||
def operator(request: Request):
|
||||
# What each Kanban profile may do. "view" is reading; "produce" moves cards and
|
||||
# handles artwork; "manage" covers quotes, cancellations, payments and the
|
||||
# integrations; "users" is the Usuários page.
|
||||
ROLES = ('superadmin', 'admin', 'gestao', 'operador', 'vendedor')
|
||||
CAPABILITIES = {
|
||||
'view': set(ROLES),
|
||||
'produce': {'superadmin', 'admin', 'gestao', 'operador'},
|
||||
'manage': {'superadmin', 'admin', 'gestao'},
|
||||
'users': {'superadmin', 'admin'},
|
||||
}
|
||||
|
||||
|
||||
def signed_in(request: Request):
|
||||
"""The operator behind this session, active, with the profile read now: a
|
||||
role changed or an account disabled takes effect on the next request."""
|
||||
token = request.cookies.get('dtf_operator', '')
|
||||
if not token or len(token)>128:
|
||||
raise HTTPException(401, 'Entre no Kanban.')
|
||||
digest = hashlib.sha256(token.encode()).hexdigest()
|
||||
with connect() as c:
|
||||
row = c.execute('SELECT username FROM dtf_local.operator_sessions WHERE token_hash=%s AND expires_at>now()', (digest,)).fetchone()
|
||||
row = c.execute('''SELECT o.id,o.email,o.name,o.role FROM dtf_local.operator_sessions s
|
||||
JOIN dtf_local.operators o ON o.email=s.username AND o.active
|
||||
WHERE s.token_hash=%s AND s.expires_at>now()''', (digest,)).fetchone()
|
||||
if row:
|
||||
row['machines'] = [str(r['machine_id']) for r in c.execute(
|
||||
'SELECT machine_id FROM dtf_local.operator_machines WHERE operator_id=%s', (row['id'],)).fetchall()]
|
||||
if not row:
|
||||
raise HTTPException(401, 'Sua sessão expirou. Entre de novo.')
|
||||
return row['username']
|
||||
request.state.operator = row
|
||||
return row
|
||||
|
||||
|
||||
def can(capability):
|
||||
"""A dependency that lets through the profiles allowed to do this, and
|
||||
returns the operator's e-mail, as the routes record it."""
|
||||
def check(request: Request):
|
||||
account = signed_in(request)
|
||||
if account['role'] not in CAPABILITIES[capability]:
|
||||
raise HTTPException(403, 'Seu perfil não permite esta ação.')
|
||||
return account['email']
|
||||
return check
|
||||
|
||||
|
||||
operator = can('view')
|
||||
|
||||
Reference in New Issue
Block a user