Work on login

This commit is contained in:
2026-09-11 22:23:00 +02:00
parent abf00ac401
commit fb09048e45
14 changed files with 168 additions and 26 deletions
+5 -5
View File
@@ -1,8 +1,8 @@
<?php
$servername = getenv('DB_HOSTNAME');
$username = getenv('DB_USERNAME');
$password = getenv('DB_PASS');
$dbname = getenv('DB_NAME');
$servername = $_ENV['DB_HOSTNAME'];
$username = $_ENV['DB_USERNAME'];
$password = $_ENV['DB_PASS'];
$dbname = $_ENV['DB_NAME'];
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
@@ -11,7 +11,7 @@ try {
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch(PDOException $e) {
echo json_encode(["error" => "error.connect.faild"]);
echo json_encode(["error" => "error.connect.faild", "exc" => $e->getMessage()]);
exit;
}
+7 -3
View File
@@ -1,11 +1,12 @@
<?php
function hasPermission(string $resourceName, string $actionName): bool
function hasPermission(string $resourceName, string $actionName)
{
global $conn;
if(!isset($_SESSION['id'])){
return false;
http_response_code(401);
exit;
}
$sql = "
@@ -28,5 +29,8 @@ function hasPermission(string $resourceName, string $actionName): bool
$result = $stmt->fetch();
return ((int) $result['permission_count']) > 0;
if(((int) $result['permission_count']) <= 0){
http_response_code(403);
exit;
}
}
+9
View File
@@ -0,0 +1,9 @@
<?php
if (!isset($_SESSION['id'])) {
http_response_code(401);
exit;
}
http_response_code(200);
echo 'ok';
+27 -6
View File
@@ -2,6 +2,24 @@
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/auto/auto.php';
if ($_ENV['DEV_EMAIL']) {
$email = $_ENV['DEV_EMAIL'];
$find = $conn->prepare("SELECT user_id, username, display_name FROM users WHERE email = :email");
$find->execute([':email' => $email]);
$obstojeciUporabnik = $find->fetch();
$_SESSION['id'] = $obstojeciUporabnik['user_id'];
$_SESSION['username'] = $obstojeciUporabnik['username'];
$_SESSION['displayname'] = $obstojeciUporabnik['display_name'];
$_SESSION['email'] = $email;
echo json_encode([
"res" => "res.user.login.true",
"displayname" => $_SESSION['displayname']
]);
exit;
}
use LdapRecord\Connection;
$nimrodLDAP = new Connection([
'hosts' => [getenv('LDAP_HOST')],
@@ -32,7 +50,7 @@ if (empty($LUser)) {
exit;
}
$emailUserja = $LUser['userprincipalname'][0];
$emailUserja = $LUser['userprincipalname'][0];
$user = $LUser['distinguishedname'][0];
if (!$nimrodLDAP->auth()->attempt($user, $geslo, $stayAuthenticated = true)) {
echo json_encode(["error" => "error.user.login.wrongPassword"]);
@@ -40,8 +58,8 @@ if (!$nimrodLDAP->auth()->attempt($user, $geslo, $stayAuthenticated = true)) {
}
$vGrupah = $LUser['memberof'];
$dGrupe = [
'cn=Nimrod_Web,ou=Nimrod OU,dc=nimrod,dc=local',
$dGrupe = [
'cn=Nimrod_Web,ou=Nimrod OU,dc=nimrod,dc=local',
];
$razlikaGrup = array_intersect(
@@ -61,8 +79,8 @@ $find->execute([':email' => $email]);
$obstojeciUporabnik = $find->fetch();
if ($obstojeciUporabnik) {
$update = $conn->prepare("UPDATE users SET display_name = :display_name WHERE user_id = :user_id");
$update->execute([':display_name' => $uporabnik, ':user_id' => $obstojeciUporabnik['user_id']]);
//$update = $conn->prepare("UPDATE users SET display_name = :display_name WHERE user_id = :user_id");
//$update->execute([':display_name' => $uporabnik, ':user_id' => $obstojeciUporabnik['user_id']]);
$_SESSION['id'] = $obstojeciUporabnik['user_id'];
$_SESSION['username'] = $obstojeciUporabnik['username'];
@@ -76,4 +94,7 @@ if ($obstojeciUporabnik) {
$_SESSION['displayname'] = $uporabnik;
$_SESSION['email'] = $email;
echo json_encode(["res" => "res.user.login.true"]);
echo json_encode([
"res" => "res.user.login.true",
"displayname" => $_SESSION['displayname']
]);
+10
View File
@@ -0,0 +1,10 @@
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/auto/auto.php';
$sql = "SELECT username, email, group_id FROM users;";
$res = $conn->query($sql);
echo json_encode($res);