Commit 701e6b5f authored by Ryan Wade's avatar Ryan Wade

Adding initial login capability

parent 7bcce3dc
<?php
define("AUTH_LEVEL_ADMIN" , 4);
define("AUTH_LEVEL_DESIGNER" , 3);
define("AUTH_LEVEL_PUBLIShER" , 2);
define("AUTH_LEVEL_WRITER" , 1);
define("AUTH_LEVEL_NONE" , 0);
session_start();
if(isset($_POST["user"]))
{
include "./SQL.php";
$SQL = new SQL();
$Auth = new Authentication($SQL->GET_TABLE(1));
$Auth->Authenticate($_POST["user"], $_POST["pass"]);
echo json_encode($Auth->isAuthenticated());
}
class Authentication
{
private $SQL_TABLE = null;
function Authentication($SQL_TABLE)
{
$this->SQL_TABLE = $SQL_TABLE;
}
function isAuthenticated()
{
if(isset($_SESSION['isAUTHENTICATED']))
return $_SESSION['isAUTHENTICATED'];
return false;
}
//return true if user has the specified permission level or higher
function hasPermissions($permissions)
{
if($permissions <= getPermissions())
return true;
return false;
}
//return number signifying permissions
function getPermissions()
{
if(!isAuthenticated())
return AUTH_LEVEL_NONE;
return $this->SQL_TABLE->GET_FIRST_ROW_BY_ATTRIBUTE(0,$_SESSION["AUTH_user"])->GET_ATTRIBUTE(2);
}
function Authenticate($user, $pass)
{
foreach($this->SQL_TABLE->GET_ROWS() as $candidate)
{
if($candidate->GET_ATTRIBUTE(0) == strtolower($user))
{
$_SESSION["AUTH_User"] = $user;
if($candidate->GET_ATTRIBUTE(1) == hash('sha256',$pass))
$_SESSION['isAUTHENTICATED'] = true;
else
$_SESSION["isAUTHENTICATED"] = false;
return;
}
}
}
function TerminateUser()
{
session_destroy();
header("location: ?id=-2");
}
}
?>
\ No newline at end of file
......@@ -17,6 +17,7 @@
}
class SQL_TABLE
{
private $rows = null;
function SQL_TABLE($src)
{
$this->rows = $src;
......@@ -42,6 +43,14 @@
return null;
return $Query;
}
public function GET_FIRST_ROW_BY_ATTRIBUTE($attr, $id)
{
return GET_ROWS_BY_ATTRIBUTE($attr, $id)[0];
}
public function GET_ROWS()
{
return $this->rows;
}
}
class SQL
{
......
......@@ -5,9 +5,11 @@
public function render()
{
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<link rel='stylesheet' type='text/css' href='global.css'>
<link rel='stylesheet' type='text/css' href='login.css'>
<link rel='stylesheet' type='text/css' href='sitemap.css'>
<link rel='stylesheet' type='text/css' href='home.css'>
<link rel='stylesheet' type='text/css' href='global.css'>
<link rel='stylesheet' type='text/css' href='header.css'>
<link rel='stylesheet' type='text/css' href='nav.css'>
<link rel='stylesheet' type='text/css' href='title.css'>
......
......@@ -20,7 +20,10 @@
}
function getUsers()
{
return null;
return
[new SQL_ROW(["ryan",hash('sha256',"password"),AUTH_LEVEL_ADMIN]),
new SQL_ROW(["amanda",hash('sha256',"password"),AUTH_LEVEL_NONE])
];
}
function getAdmin()
{
......
......@@ -12,8 +12,11 @@
{
?>
<footer id="footer">
<div class="footerText">
<a class="footerLink" href="<?php echo $this->link;?>?id=-1">Site Map</a>
<div class="footerText left">
<ul>
<li><a class="footerLink" href="<?php echo $this->link;?>?id=-2">Login</a></li>
<li><a class="footerLink" href="<?php echo $this->link;?>?id=-1">Site Map</a></li>
</ul>
</div>
</footer>
<?php
......
#login
{
text-align: left;
}
#logindiv
{
margin-top: 50px;
margin-bottom: 150px;
width: 250px;
padding: 30px;
border: 3px solid black;
border-radius:30px;
box-shadow: 0px 0px 50px rgba(78, 153, 184, 1);;
-webkit-border-radius:30px;
background-color: rgba(78, 153, 184, 1);;
}
#lWarning
{
color: red;
}
\ No newline at end of file
<?php
class Login
{
private $SQL_TABLE = null;
private $Auth = null;
private $link = null;
function Login($SQL_TABLE, $Auth, $link)
{
$this->SQL_TABLE = $SQL_TABLE;
$this->Auth = $Auth;
$this->link = $link;
}
function render()
{
if($this->Auth->isAuthenticated())
header("location: ".$this->link);
?>
<div id="login">
<div id="logindiv" class="center">
<span class="center nopadding">Enter Credentials</span></br></br>
<span class="left nopadding">Username: </span><input type="text" id="txt_user" value=""/></br>
<span class="left nopadding">Password: </span><input type="password" id="txt_pass" value=""/></br></br>
<div id="lWarning"></div>
<span class="center nopadding"><input type="button" id="btn_login" value="Login" /></span>
</div>
</div>
<script>
$("#btn_login").click(function(){
$.post("./Authentication.php",
{
user: $("#txt_user").val(),
pass: $("#txt_pass").val()
},
function(data,status){
$("#lWarning").html(data);
var obj = JSON.parse(data);
if(obj==false)
$("#lWarning").html("Invalid user name or password");
else
{
$("#lWarning").html("");
window.location.href = <?php echo $this->link; ?>;
}
});
});
</script>
<?php
}
}
?>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment