101 lines
2.9 KiB
PHP
101 lines
2.9 KiB
PHP
<?php
|
|
|
|
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')],
|
|
'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(["error" => "error.user.login.missing"]);
|
|
exit;
|
|
}
|
|
|
|
$username = $_POST['uporabnik'];
|
|
$geslo = $_POST['geslo'];
|
|
|
|
try {
|
|
$nimrodLDAP->connect();
|
|
} catch (Exception $e) {
|
|
echo json_encode(["error" => "error.user.login.connect"]);
|
|
exit;
|
|
}
|
|
|
|
$LUser = $nimrodLDAP->query()->findBy('samaccountname', $username);
|
|
if (empty($LUser)) {
|
|
echo json_encode(["error" => "error.user.login.notFound"]);
|
|
exit;
|
|
}
|
|
|
|
$emailUserja = $LUser['userprincipalname'][0];
|
|
$user = $LUser['distinguishedname'][0];
|
|
if (!$nimrodLDAP->auth()->attempt($user, $geslo, $stayAuthenticated = true)) {
|
|
echo json_encode(["error" => "error.user.login.wrongPassword"]);
|
|
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(["error" => "error.user.login.groupRight"]);
|
|
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" => "res.user.login.true",
|
|
"displayname" => $_SESSION['displayname']
|
|
]);
|