"""Per-user access control for Kronic's AI Command panel. A user with chat_only access can converse with the model but cannot request Balroc tool execution via the Kronic bridge. A user with tools access gets whatever tools the separately configured Balroc harness permits. Do not treat this as a per-tool allowlist inside Balroc. """ from __future__ import annotations from media_accounts import AccountError, open_database, session_user, require_admin def _ensure_schema(db): db.execute('''CREATE TABLE IF NOT EXISTS ai_command_access ( user_id INTEGER PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, can_chat INTEGER NOT NULL DEFAULT 0 CHECK(can_chat IN (0,1)), can_use_tools INTEGER NOT NULL DEFAULT 0 CHECK(can_use_tools IN (0,1)) )''') def access_for_user(user): if bool(user['is_platform_admin']): return {'can_chat': True, 'can_use_tools': True, 'is_platform_admin': True} with open_database() as db: _ensure_schema(db) record = db.execute('SELECT can_chat,can_use_tools FROM ai_command_access WHERE user_id=?', (int(user['id']),)).fetchone() from admin_policies import policies rules=policies(int(user['id'])) chat=bool(record and record['can_chat']) and all(r.get('ai_chat',True) for r in rules) tools=chat and bool(record and record['can_use_tools']) and all(r.get('ai_tools',True) for r in rules) return {'can_chat': chat, 'can_use_tools': tools, 'is_platform_admin': False} def access_for_token(token): user = session_user(token) return access_for_user(user) def require_ai_access(token): result = access_for_token(token) if not result['can_chat']: raise AccountError(403, 'AI Command access has not been enabled for this account.') return result def admin_list_ai_access(token): require_admin(token) with open_database() as db: _ensure_schema(db) rows = db.execute('''SELECT u.id,u.username,u.status,u.is_platform_admin, COALESCE(a.can_chat,0) AS can_chat, COALESCE(a.can_use_tools,0) AS can_use_tools FROM users u LEFT JOIN ai_command_access a ON a.user_id=u.id ORDER BY u.is_platform_admin DESC,u.username COLLATE NOCASE''').fetchall() return {'users': [{'id': int(u['id']), 'username': u['username'], 'status': u['status'], 'is_platform_admin': bool(u['is_platform_admin']), 'can_chat': bool(u['is_platform_admin'] or u['can_chat']), 'can_use_tools': bool(u['is_platform_admin'] or (u['can_chat'] and u['can_use_tools']))} for u in rows]} def admin_set_ai_access(token, data): require_admin(token) try: user_id = int(data['user_id']) except (KeyError, ValueError, TypeError): raise AccountError(400, 'A valid user_id is required.') chat = data.get('can_chat') tools = data.get('can_use_tools') if type(chat) is not bool or type(tools) is not bool: raise AccountError(400, 'can_chat and can_use_tools must be booleans.') if tools and not chat: raise AccountError(400, 'Tool access requires chat access.') with open_database() as db: _ensure_schema(db) user = db.execute('SELECT id,is_platform_admin FROM users WHERE id=?', (user_id,)).fetchone() if not user: raise AccountError(404, 'User not found.') if user['is_platform_admin']: raise AccountError(403, 'Platform administrator AI access cannot be revoked here.') db.execute('''INSERT INTO ai_command_access(user_id,can_chat,can_use_tools) VALUES(?,?,?) ON CONFLICT(user_id) DO UPDATE SET can_chat=excluded.can_chat, can_use_tools=excluded.can_use_tools''', (user_id, int(chat), int(tools))) db.commit() return {'ok': True, 'user_id': user_id, 'can_chat': chat, 'can_use_tools': tools}