1
0
Эх сурвалжийг харах

Auth is working + added home page displaying username

Clément K 2 жил өмнө
parent
commit
3fdd8eaf04

+ 25 - 3
auth.php

@@ -7,10 +7,32 @@ if ($_SERVER['REQUEST_METHOD'] != 'POST') {
     exit();
 }
 
-if (!isset($_POST['login'], $_POST['password'])) {
+if (!isset($_POST['id'], $_POST['password'])) {
     header('Location: signin.php');
     exit();
 }
 
-$login = htmlspecialchars($_POST['login']);
-$passwd = htmlspecialchars($_POST['password']);
+$id = htmlspecialchars($_POST['id']);
+
+$ini = parse_ini_file('includes/config.ini');
+
+try {
+    $ldapconn = ldap_connect($ini['hostname'], $ini['port']);
+    ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
+    ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);
+} catch (Exception $e) {
+    die ('ERROR: ' . $e->getMessage());
+}
+
+if ($ldapconn) {
+    $ldapbind = ldap_bind($ldapconn, "uid=" . ldap_escape($id) . ',' . $ini["binddn"], $_POST['password']);
+    if ($ldapbind) {
+        $_SESSION['user'] = $id;
+        header('Location: home.php');
+        exit();
+    } else {
+       $_SESSION['message'] = "Wrong username or password";
+       header('Location: signin.php');
+       exit();
+    }
+}    

+ 23 - 0
home.php

@@ -0,0 +1,23 @@
+<?php
+
+session_start();
+
+if (!isset($_SESSION['user'])) {
+    header('Location: signin.php');
+    exit();
+}
+
+include 'vendor/autoload.php';
+
+try {
+    $loader = new Twig\Loader\FilesystemLoader('templates');
+    $twig = new Twig\Environment($loader);
+    $twig->addGlobal('session', $_SESSION);
+
+    $template = $twig->load('home.html.twig');
+
+    echo $template->render();
+} catch (Exception $e) {
+    die ('ERROR: ' . $e->getMessage());
+}
+

+ 3 - 0
signin.php

@@ -1,10 +1,13 @@
 <?php
 
+session_start();
+
 include 'vendor/autoload.php';
 
 try {
     $loader = new Twig\Loader\FilesystemLoader('templates');
     $twig = new Twig\Environment($loader);
+    $twig->addGlobal('session', $_SESSION);
 
     $template = $twig->load('signin.html.twig');
 

+ 9 - 0
templates/home.html.twig

@@ -0,0 +1,9 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <title>BIM</title>
+    </head>
+    <body>
+        <h1>Hello {{session.user}}</h1>
+    </body>
+</html>

+ 3 - 3
templates/signin.html.twig

@@ -4,7 +4,7 @@
         <title>BIM</title>
     </head>
     <body>
-        <form method="post" accept-charset="UTF-8" action="auth.php">
+        <form method="post" accept-charset="UTF-8" action="../auth.php">
         
             <h3><label for="id">Username</label></h3>
             <input id="id" type="text" name="id">
@@ -14,8 +14,8 @@
         
             <input type="submit" value="login">
         
-            {% if error_message is defined %}
-                <p>{{ error_message }}</p>
+            {% if session.message is defined %}
+                <p>{{ session.message }}</p>
             {% endif %}
         </form>