Skip to content

Instantly share code, notes, and snippets.

@alanpich
Last active December 11, 2015 13:29
Show Gist options
  • Save alanpich/4608096 to your computer and use it in GitHub Desktop.
Save alanpich/4608096 to your computer and use it in GitHub Desktop.
<?php
namespace OS\ConMan;
use \Slim\Extras\Views\Twig;
class ConMan {
protected $slim;
protected $view;
protected $userID = 0;
public $user = FALSE;
protected $authenticated = FALSE;
function __construct(){
session_start();
/* Load Slim with Twig for templating */
\Slim\Extras\Views\Twig::$twigDirectory = ROOT.'vendor/Twig';
\Slim\Extras\Views\Twig::$twigTemplateDirs = ROOT.'app/templates/';
$this->slim = new \Slim\Slim(array(
'view' => '\Slim\Extras\Views\Twig'
));
/* Configure Twig view */
$this->view = $this->slim->view();
$this->view->setTemplatesDirectory( ROOT.'app/templates/');
/* Load xPDO ORM */
$this->loadORM();
/* Authenticate user */
$this->authenticateRequest();
$this->doRequestRouting();
}//
/**
* Initialize xPDO ORM database wrapper
*/
protected function loadORM(){
require ROOT.'vendor/xpdo/xpdo.class.php';
$dsn = 'mysql:host='.DBHOST.';dbname='.DBNAME.';charset='.DBCHARSET;
$this->xpdo = new \xPDO($dsn,DBUSER,DBPASS);
}
/**
* Check if a user is authenticated
*/
public function authenticateRequest(){
// First, check if we have a session token and accept it
if(isset($_SESSION['user_id']) && is_numeric($_SESSION['user_id'])){
$uid = (int) $_SESSION['user_id'];
// Try to load user object from db
$user = $this->xpdo->getObject('User',$uid);
if($user instanceof User){
$this->userID = (int) $_SESSION['user_id'];
$this->user = $user;
$this->authenticated = TRUE;
return;
}
};
// Then check to see if we've been passed login params
if(isset($_POST['email']) && isset($_POST['password'])){
$user = $this->xpdo->getObject('User',array(
'email' => $_POST['email']
));
if($user instanceof User){
if($user->checkPassword($_POST['password'])){
$this->userID = (int) $_SESSION['user_id'];
$this->user = $user;
$this->authenticated = TRUE;
return;
};
};
$this->slim->flashNow('login_error',"Incorrect username or password");
$this->slim->flash('login_error',"Incorrect username or password (flashNext)");
};
// Otherwise, user is NOT authenticated
$this->userID = FALSE;
$this->user = FALSE;
$this->authenticated = FALSE;
}//
public function doRequestRouting(){
// Reference self
$conman = $this;
// Show login window if not already authenticated
if(!$this->authenticated){
$this->view_login();
return;
}
// View all the cards associated with a project
$this->slim->map('/:clientName/:projectName',function($clientName,$projectName) use ($conman) {
})->via('GET','POST');
/* HOMPAGE */
$this->slim->map('/',function() use ($conman) {
$conman->view_clientOverview();
})->via('GET');
$this->slim->run();
}//
/**
* Display login window
*/
public function view_login(){
$this->view->setData(array(
'title' => 'This is my dynamic twiggy title!'
));
$this->slim->render('login.twig');
}//
/**
* Display view of all the cards in a project
* @param $clientName
* @param $projectName
*/
public function view_projectCards($clientName,$projectName){
}//
public function view_clientOverview(){
$this->view->setData(array(
'title' => 'This is my dynamic twiggy title!'
));
$this->slim->render('basepage.twig');
}//
};// end class ConMan
<?php
/* ... */
// Then check to see if we've been passed login params
if(isset($_POST['email']) && isset($_POST['password'])){
$user = $this->xpdo->getObject('User',array(
'email' => $_POST['email']
));
if($user instanceof User){
if($user->checkPassword($_POST['password'])){
$this->userID = (int) $_SESSION['user_id'];
$this->user = $user;
$this->authenticated = TRUE;
return;
};
};
$this->slim->flashNow('login_error',"Incorrect username or password");
$this->slim->flash('login_error',"Incorrect username or password (flashNext)");
};
{% extends "basepage.twig" %}
{% block pagecontent %}
<form class="form-signin" method="post" action="">
<h2 class="form-signin-heading">Please sign in</h2>
<input type="text" name="email" class="input-block-level" placeholder="Email address">
<input type="password" name="password" class="input-block-level" placeholder="Password">
<div class="error">{{ flash.login_error }}</div>
<button class="btn btn-large btn-primary" type="submit">Sign in</button>
</form>
{% endblock %}
<?php
$this->view = $this->slim->view();
$this->view->setData(array(
'title' => 'This is my dynamic twiggy title!'
));
$this->slim->render('login.twig');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment