Add Ldap
This commit is contained in:
+2
-1
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"require": {
|
"require": {
|
||||||
"vlucas/phpdotenv": "^5.7"
|
"vlucas/phpdotenv": "^5.7",
|
||||||
|
"directorytree/ldaprecord": "^3.1"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Generated
+1229
-1
File diff suppressed because it is too large
Load Diff
@@ -1,5 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
|
require_once $_SERVER['DOCUMENT_ROOT'] . '/vendor/autoload.php';
|
||||||
$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']); // dir containing .env
|
$dotenv = Dotenv\Dotenv::createImmutable($_SERVER['DOCUMENT_ROOT']); // dir containing .env
|
||||||
$dotenv->load();
|
$dotenv->load();
|
||||||
|
|||||||
+7
-2
@@ -1,8 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
function hasPermission(int $userId, string $resourceName, string $actionName): bool
|
function hasPermission(string $resourceName, string $actionName): bool
|
||||||
{
|
{
|
||||||
global $conn;
|
global $conn;
|
||||||
|
|
||||||
|
if(!isset($_SESSION['id'])){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
$sql = "
|
$sql = "
|
||||||
SELECT COUNT(*) AS permission_count
|
SELECT COUNT(*) AS permission_count
|
||||||
FROM users u
|
FROM users u
|
||||||
@@ -16,7 +21,7 @@ function hasPermission(int $userId, string $resourceName, string $actionName): b
|
|||||||
|
|
||||||
$stmt = $conn->prepare($sql);
|
$stmt = $conn->prepare($sql);
|
||||||
$stmt->execute([
|
$stmt->execute([
|
||||||
':userId' => $userId,
|
':userId' => $_SESSION['id'],
|
||||||
':resourceName' => $resourceName,
|
':resourceName' => $resourceName,
|
||||||
':actionName' => $actionName,
|
':actionName' => $actionName,
|
||||||
]);
|
]);
|
||||||
|
|||||||
@@ -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"]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
<?php
|
||||||
|
session_destroy();
|
||||||
+2
-1
@@ -10,7 +10,8 @@ CREATE TABLE group_infos (
|
|||||||
CREATE TABLE users (
|
CREATE TABLE users (
|
||||||
user_id INT PRIMARY KEY AUTO_INCREMENT,
|
user_id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
username VARCHAR(100) NOT NULL UNIQUE,
|
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
|
group_id INT NULL, -- nullable if a user can exist with no group yet
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
FOREIGN KEY (group_id) REFERENCES group_infos(group_id) ON DELETE SET NULL
|
FOREIGN KEY (group_id) REFERENCES group_infos(group_id) ON DELETE SET NULL
|
||||||
|
|||||||
Reference in New Issue
Block a user