This commit is contained in:
2026-09-10 15:10:59 +02:00
parent 9016fb7baf
commit b8f9430827
8 changed files with 1339 additions and 8 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
{
"require": {
"vlucas/phpdotenv": "^5.7"
"vlucas/phpdotenv": "^5.7",
"directorytree/ldaprecord": "^3.1"
}
}
Generated
+1229 -1
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -1,5 +1,7 @@
<?php
session_start();
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']); // dir containing .env
$dotenv->load();
+7 -2
View File
@@ -1,8 +1,13 @@
<?php
function hasPermission(int $userId, string $resourceName, string $actionName): bool
function hasPermission(string $resourceName, string $actionName): bool
{
global $conn;
if(!isset($_SESSION['id'])){
return false;
}
$sql = "
SELECT COUNT(*) AS permission_count
FROM users u
@@ -16,7 +21,7 @@ function hasPermission(int $userId, string $resourceName, string $actionName): b
$stmt = $conn->prepare($sql);
$stmt->execute([
':userId' => $userId,
':userId' => $_SESSION['id'],
':resourceName' => $resourceName,
':actionName' => $actionName,
]);
+92
View File
@@ -0,0 +1,92 @@
<?php
require_once $_SERVER['DOCUMENT_ROOT'] . '/php/auto/auto.php';
use LdapRecord\Connection;
$nimrodLDAP = new Connection([
'hosts' => [getenv('LDAP_HOST')],
'port' => 389,
'base_dn' => getenv('LDAP_BASEDN'),
'username' => getenv('LDAP_USER'),
'password' => getenv('LDAP_SECRET')
]);
if (!isset($_POST['uporabnik']) || !isset($_POST['geslo'])) {
echo json_encode(["napaka" => "manjkajo podatki"]);
exit;
}
$username = $_POST['uporabnik'];
$geslo = $_POST['geslo'];
try {
$nimrodLDAP->connect();
} catch (Exception $e) {
echo 'napaka pri povezavi na LDap';
echo $e;
exit;
}
$LUser = $nimrodLDAP->query()->findBy('samaccountname', $username);
if (empty($LUser)) {
echo json_encode(["napaka" => "uporabnik ni najden"]);
exit;
}
$vGrupah = $LUser['memberof'];
$dGrupe = [
'cn=Nimrod_Web,ou=Nimrod OU,dc=nimrod,dc=local',
];
$razlikaGrup = array_intersect(
array_map('strtolower', $vGrupah),
array_map('strtolower', $dGrupe)
);
if (count($razlikaGrup) <= 0) {
echo json_encode(["napaka" => "nimaš pravic skupinskih"]);
exit;
}
$emailUserja = $LUser['userprincipalname'][0];
$user = $LUser['distinguishedname'][0];
if (!$nimrodLDAP->auth()->attempt($user, $geslo, $stayAuthenticated = true)) {
echo json_encode(["napaka" => "napacno geslo"]);
exit;
}
$uporabnik = $LUser['displayname'][0];
$email = $LUser['mail'][0];
$find = $conn->prepare("SELECT user_id, username FROM users WHERE email = :email");
$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']]);
$_SESSION['id'] = $obstojeciUporabnik['user_id'];
$_SESSION['username'] = $obstojeciUporabnik['username'];
} else {
$insert = $conn->prepare("INSERT INTO users (username, email, display_name) VALUES (:username, :email, :display_name)");
$insert->execute([':username' => $username, ':email' => $email, ':display_name' => $uporabnik]);
$_SESSION['id'] = $conn->lastInsertId();
$_SESSION['username'] = $username;
}
$_SESSION['displayname'] = $uporabnik;
$_SESSION['email'] = $email;
echo json_encode(["res" => "uspesno"]);
+2
View File
@@ -0,0 +1,2 @@
<?php
session_destroy();
+2 -1
View File
@@ -10,7 +10,8 @@ CREATE TABLE group_infos (
CREATE TABLE users (
user_id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(100) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL,
display_name VARCHAR(255) NULL,
group_id INT NULL, -- nullable if a user can exist with no group yet
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (group_id) REFERENCES group_infos(group_id) ON DELETE SET NULL
View File