enabling guest user

main
Steffen Pohle 2 weeks ago
parent 5850b97c54
commit 7dd4ec8876

@ -14,6 +14,44 @@ function getBrowserIdentifier() {
return 'unknown';
}
function isGuestModeEnabled() {
return defined('GUEST_USER_ENABLED') && GUEST_USER_ENABLED === true;
}
function getGuestUsername() {
return defined('GUEST_USERNAME') ? (string) GUEST_USERNAME : 'guest';
}
function isGuestUsername($username) {
return isGuestModeEnabled() && strtolower(trim((string) $username)) === strtolower(getGuestUsername());
}
function isGuestSession() {
return isGuestModeEnabled() && !empty($_SESSION['auth_user']['username']) && isGuestUsername($_SESSION['auth_user']['username']);
}
function createGuestUser() {
return [
'username' => getGuestUsername(),
'role' => 'guest',
'guest' => true,
'email' => '',
'passwordHash' => ''
];
}
function loginAsGuest() {
if (!isGuestModeEnabled()) {
return false;
}
$_SESSION['auth_user'] = [
'username' => getGuestUsername(),
'role' => 'guest'
];
return true;
}
function getRememberMeCookieName($username) {
return 'kai_remember_' . sanitizeUsername($username);
}
@ -136,6 +174,17 @@ function getUserChatPath($username) {
return USER_CONFIG_DIR . '/' . sanitizeUsername($username) . '-chat.php';
}
function getConfiguredDefaultModel() {
$defaultModel = defined('DEFAULT_MODEL') ? trim((string) DEFAULT_MODEL) : '';
return $defaultModel !== '' ? $defaultModel : 'llama3';
}
function getDefaultModelForUser($username) {
$config = getUserConfig($username);
$storedModel = trim((string) ($config['defaultModel'] ?? ''));
return $storedModel !== '' ? $storedModel : getConfiguredDefaultModel();
}
function readUserConfig($username) {
$path = getUserConfigPath($username);
if (!file_exists($path)) {
@ -159,7 +208,8 @@ function writeUserConfig($username, $config) {
$data['role'] = (string) ($data['role'] ?? 'user');
$data['email'] = (string) ($data['email'] ?? '');
$data['passwordHash'] = (string) ($data['passwordHash'] ?? '');
$data['defaultModel'] = (string) ($data['defaultModel'] ?? '');
$storedDefaultModel = trim((string) ($data['defaultModel'] ?? ''));
$data['defaultModel'] = $storedDefaultModel !== '' ? $storedDefaultModel : getConfiguredDefaultModel();
$php = "<?php\n\nreturn " . var_export($data, true) . ";\n";
return file_put_contents(getUserConfigPath($username), $php) !== false;
@ -268,7 +318,7 @@ function saveUsers($users, $previousUsers = null) {
$entry['role'] = (string) ($entry['role'] ?? 'user');
$entry['email'] = (string) ($entry['email'] ?? '');
$entry['passwordHash'] = (string) ($entry['passwordHash'] ?? '');
$entry['defaultModel'] = (string) ($entry['defaultModel'] ?? '');
$entry['defaultModel'] = trim((string) ($entry['defaultModel'] ?? '')) !== '' ? trim((string) $entry['defaultModel']) : getConfiguredDefaultModel();
$normalizedUsers[] = $entry;
}
@ -355,7 +405,7 @@ function createUser($username, $password, $role = 'user', $email = '') {
'email' => $email,
'passwordHash' => hashPassword($password),
'role' => $role,
'defaultModel' => ''
'defaultModel' => getConfiguredDefaultModel()
];
if (!writeUserChat($username, [])) {
@ -449,6 +499,10 @@ function getAuthenticatedUser() {
return null;
}
if (isGuestSession()) {
return createGuestUser();
}
return findUserByUsername($_SESSION['auth_user']['username'] ?? '');
}

@ -51,7 +51,9 @@ if (isset($_GET['action']) && $_GET['action'] === 'models') {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$input = json_decode(file_get_contents('php://input'), true);
$model = $input['model'] ?? 'llama3';
$model = isset($input['model']) && trim((string) $input['model']) !== ''
? (string) $input['model']
: getConfiguredDefaultModel();
$messages = $input['messages'] ?? [];
$username = '';
if (!empty($_SESSION['auth_user']['username'])) {

@ -2,3 +2,7 @@
define('USER_CONFIG_DIR', '/var/lib/KaI/users');
define('OLLAMA_URL', 'http://127.0.0.1:11434');
define('GUEST_USER_ENABLED', true);
define('GUEST_USERNAME', 'guest');
define('DEFAULT_MODEL', 'llama3.2:latest');

@ -7,12 +7,17 @@ if (session_status() === PHP_SESSION_NONE) {
require_once __DIR__ . '/default.php';
require_once __DIR__ . '/auth.php';
if (!isLoggedIn() && isGuestModeEnabled()) {
loginAsGuest();
}
if (!isLoggedIn()) {
header('Location: login.php');
exit;
}
$authUser = getAuthenticatedUser();
$isGuestSession = isGuestSession();
?>
<!DOCTYPE html>
<html lang="en">
@ -35,10 +40,13 @@ $authUser = getAuthenticatedUser();
<div class="chat-header">
<h2>I'm KaI your - Ollama Chat</h2>
<div class="controls">
<span style="font-size:12px; color:#aaa;">Signed in as <?php echo htmlspecialchars($authUser['username'] ?? ''); ?></span>
<span style="font-size:12px; color:#aaa;">Signed in as <?php echo htmlspecialchars(($authUser['username'] ?? 'guest')); ?></span>
<?php if (($authUser['role'] ?? 'user') === 'admin'): ?>
<a href="useradmin.php" style="color:#89b4fa; text-decoration:none;">User Admin</a>
<?php endif; ?>
<?php if (isGuestModeEnabled()): ?>
<a href="login.php" style="color:#89b4fa; text-decoration:none;">Login</a>
<?php endif; ?>
<a href="logout.php" style="color:#f38ba8; text-decoration:none;">Logout</a>
<select id="model-select">
<option>Loading models...</option>

@ -11,6 +11,12 @@ $errors = [];
$message = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!empty($_POST['guest']) && isGuestModeEnabled()) {
loginAsGuest();
header('Location: index.php');
exit;
}
$username = trim($_POST['username'] ?? '');
$password = (string) ($_POST['password'] ?? '');
@ -38,7 +44,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
}
}
if (isLoggedIn()) {
if (isLoggedIn() && !isGuestSession()) {
header('Location: index.php');
exit;
}
@ -65,6 +71,7 @@ if (empty(loadUsers())) {
label { display: block; margin-top: 12px; font-weight: bold; }
input { width: 100%; padding: 10px; margin-top: 6px; border-radius: 6px; border: 1px solid #45475a; background: #313244; color: #fff; }
button { margin-top: 16px; width: 100%; padding: 10px; border: none; border-radius: 6px; background: #89b4fa; color: #11111b; font-weight: bold; cursor: pointer; }
.guest-btn { background: #a6e3a1; color: #11111b; }
.error { color: #f38ba8; margin-top: 12px; }
.message { color: #a6e3a1; margin-top: 12px; }
.small { font-size: 0.9rem; color: #999; margin-top: 12px; }
@ -73,6 +80,12 @@ if (empty(loadUsers())) {
<body>
<div class="card">
<h1>KaI Login</h1>
<?php if (isGuestModeEnabled()): ?>
<form method="post">
<input type="hidden" name="guest" value="1">
<button type="submit" class="guest-btn">Continue as Guest</button>
</form>
<?php endif; ?>
<form method="post">
<label for="username">Username</label>
<input id="username" name="username" required>

@ -7,6 +7,34 @@ require_once __DIR__ . '/auth.php';
requireAuthentication();
$username = getAuthenticatedUsername();
$action = $_GET['action'] ?? '';
$defaultModel = getConfiguredDefaultModel();
if (isGuestSession()) {
switch ($action) {
case 'load-config':
echo json_encode(['defaultModel' => $defaultModel]);
exit;
case 'save-config':
echo json_encode(['success' => true, 'defaultModel' => $defaultModel]);
exit;
case 'list':
echo json_encode([]);
exit;
case 'load':
echo json_encode(['error' => 'Guest users do not have saved chats.']);
exit;
case 'save':
echo json_encode(['success' => true, 'id' => '', 'title' => '']);
exit;
case 'delete':
echo json_encode(['success' => true]);
exit;
default:
echo json_encode(['error' => 'Unknown action.']);
exit;
}
}
function createTitle($messages) {
foreach ($messages as $msg) {
@ -21,12 +49,10 @@ function createTitle($messages) {
return 'Untitled Chat';
}
$action = $_GET['action'] ?? '';
switch ($action) {
case 'load-config':
$config = getUserConfig($username);
echo json_encode([
'defaultModel' => (string) ($config['defaultModel'] ?? '')
'defaultModel' => getDefaultModelForUser($username)
]);
exit;
@ -43,7 +69,9 @@ switch ($action) {
}
$config = getUserConfig($username);
$config['defaultModel'] = isset($input['defaultModel']) ? (string) $input['defaultModel'] : '';
$config['defaultModel'] = isset($input['defaultModel']) && trim((string) $input['defaultModel']) !== ''
? (string) $input['defaultModel']
: getConfiguredDefaultModel();
if (!saveUserConfigData($username, $config)) {
echo json_encode(['error' => 'Unable to save config.']);
@ -113,7 +141,9 @@ switch ($action) {
}
$title = createTitle($messages);
$model = isset($input['model']) ? (string) $input['model'] : '';
$model = isset($input['model']) && trim((string) $input['model']) !== ''
? (string) $input['model']
: getDefaultModelForUser($username);
$chat = [
'id' => $id,
'title' => $title,

Loading…
Cancel
Save