Commit b106dbe5 authored by Denis S. Valdenaire's avatar Denis S. Valdenaire

templating users / sessions en base

parent e3e9a36a
......@@ -97,6 +97,7 @@ class data {
public function insert ($query) {
if (!$this->db_handle->query($query)) {
trigger_error($query, E_USER_NOTICE);
throw new data_exception(
mysqli_errno($this->db_handle),
mysqli_error($this->db_handle),
......
<?php
// This class store/retrieve the sessions in/from the database.
// You will need this if you have more than one webserver
// to enable the persistence of sessions accross different front servers.
class session_db extends data {
private $life_time = 0;
public function __construct() {
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'gc')
);
$this->connect();
// session_name("whatever");
$this->life_time = ini_get('session.gc_maxlifetime');
// -- Define a lifetime on session cookie
if ( ini_get('session.use_only_cookies') == 1 && intval($this->life_time)>0 ) {
ini_set('session.cookie_secure', FALSE);
ini_set('session.cookie_httponly', TRUE);
session_set_cookie_params($this->life_time);
}
session_start();
}
public function __destruct() {
// DEBUG trigger_error('session_db::__destruct called', E_USER_NOTICE);
}
public function open() {
// DEBUG trigger_error('session_db::open called', E_USER_NOTICE);
// -- Maintain session cookie updated for each requests
if ( ini_get('session.use_only_cookies')==1 && intval($this->life_time)>0 ) {
setcookie(session_name(),session_id(),(time()+$this->life_time), '/');
}
return true;
}
public function close() {
// DEBUG trigger_error('session_db::close called', E_USER_NOTICE);
$this->gc($this->life_time);
return true;
}
public function read($id) {
// DEBUG trigger_error('session_db::read called', E_USER_NOTICE);
// SQL SELECT sessions
$sql = " SELECT session_data
FROM sessions
WHERE session_key = '$id'";
if ( $this->select($sql, $rset) ) {
if ( $rset->numrows ) {
return base64_decode($rset->value("session_data"));
}
}
return '';
}
public function write($id, $data) {
// DEBUG trigger_error('session_db::write called', E_USER_NOTICE);
// SQL INSERT sessions
$sql = " REPLACE INTO sessions
(session_key, session_expires, session_data)
VALUES ('$id', '".(time() + $this->life_time)."',
'".base64_encode($data)."')";
return $this->insert($sql);
}
public function destroy($id, $key_only = FALSE) {
// DEBUG trigger_error('session_db::destroy called', E_USER_NOTICE);
// SQL DELETE sessions
$sql = " DELETE FROM ".$this->tbpx."sessions
WHERE session_key = '".$id."'";
return $this->delete($sql);
}
public function gc($max) {
// DEBUG trigger_error('session_db::gc called', E_USER_NOTICE);
// SQL DELETE sessions
$sql = " DELETE LOW_PRIORITY FROM sessions
WHERE session_expires < ".(time() - $this->life_time);
return $this->delete($sql);
}
}
<?php
session_start();
function myautoload($class_name) {
if(strpos($class_name, "Controller")) {
include "controllers/". //strtolower(substr($class_name, 0, -10)).".php";
......@@ -14,6 +13,7 @@ spl_autoload_register("myautoload");
include("config/config.php");
global $data;
$data = new data();
$session_db = new session_db();
global $logged_user;
$logged_user = new User(0);
if(!array_key_exists("user_id", $_SESSION)) {
......
{% extends "base.html" %}
{% block title %}Utilisateurs{% endblock %}
{% block content %}
<div class="panel panel-default">
<div class="panel-heading">
<span style="font-size: 150%;" class="glyphicon glyphicon-user"></span>
<span style="font-size: 150%; font-weight: bold">
Utilisateurs
</span>
<span style="font-size: 150%;" class="glyphicon glyphicon-user"></span>
<span style="font-size: 150%; font-weight: bold">
Utilisateurs
</span>
<span class="btn btn-success btn-md" style="float: right" id="new_button">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvel utilisateur...</span>
</span>
<button type="button" class="btn btn-success btn-md" style="float: right"
data-toggle="modal" data-target="#editModal" data-id="0">
<i class="glyphicon glyphicon-plus"></i>
<span>Nouvel utilisateur...</span>
</button>
</div>
</div>
<div class="panel-body">
<table id="users_list" class="col-sm-12" width="100%">
<thead>
<tr>
<th>Nom</th>
<th>Mail</th>
<th>Activé</th>
<th>Actions</th>
</tr>
</thead>
{% include 'ihm_messages.html' %}
<div class="col-sm-12" align="center">
<table id="object_list" style="display:none">
<thead>
<tr>
<th>Nom</th>
<th>Mail</th>
<th>Activé</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{% for key, val in objects %}
<tr>
<td>{{ val.name }}</td>
<tr>
<th>{{ val.name }}</th>
<td>{{ val.email }}</td>
<td><input type="checkbox" id="active_{{ val.id }}" name="active_{{ val.id }}" class="form-control active_cbx"
data-switch-with-ajax {{ val.active ? " checked " }}?></td>
<td>
<a href="index.php?o=users&a=edit&i={{ val.id }}">
<button type="button" class="btn btn-default btn-sm">
<span class="glyphicon glyphicon-edit" aria-hidden="true"></span></button>
</a>
<a onClick="if(confirm('Êtes vous sur ?')) { $('#a').val('delete_user'); $('#i').val('{{ val.id }}'); defaultform.submit()}" href="#">
<button type="button" class="btn btn-danger btn-sm">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</a>
</td>
<td align="center">
<button type="button" class="btn btn-success btn-xs"
data-toggle="modal" data-target="#editModal" data-id="{{ val.id }}">
<i class="glyphicon glyphicon-edit"></i>
</button>
<a href="#" onClick="if(confirm('Êtes vous sur ?')) { $('#a').val('delete'); $('#i').val('{{ val.id }}'); defaultform.submit()}" href="#">
<button type="button" class="btn btn-danger btn-xs">
<span class="glyphicon glyphicon-trash" aria-hidden="true"></span></button>
</a>
</td>
</tr>
{% else %}
<tr>
<td colspan="4">
Aucun utilisateur trouvé. À se demander comment l'utilisateur courant peut être connecté !
</td>
<tr>
<td colspan="4">
Aucun utilisateur trouvé. À se demander comment l'utilisateur courant peut être connecté !
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- end of panel -->
</div>
</div>
<!-- edit modal skel -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel">
<div class="modal-dialog">
<div class="modal-content">
</div>
</div>
</div>
<!-- end edit modal -->
<script>
$(document).ready(function () {
$('#new_button').click(function(){
$('#a').val('new');
$('#o').val('users');
defaultform.submit();
});
$('.active_cbx').bootstrapSwitch({
onText: "Oui",
offText: "Non",
}).on('switchChange.bootstrapSwitch', function(event, state) {
$.ajax({
url: 'api.php?o=users&a=switch_state&i=' + this.name.substr(7)
+ "&state=" + (state ? 1 : 0), // post on the API
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function(){
$(document).ready(function() {
$('#object_list').DataTable({
"autoWidth": false,
"fnDrawCallback": function() {
$(this).show();
}
});
$("#editModal").on("show.bs.modal", function(e) {
var button = $(e.relatedTarget);
if(button.data('id') == 0) {
$(this).find(".modal-content").load("index.php?o=" + $('#o').val() + "&a=new");
} else {
$(this).find(".modal-content").load("index.php?o=" + $('#o').val() + "&a=edit&i=" + button.data('id'));
}
}).on("hidden.bs.modal", function(e) {
$(this).find(".modal-content").empty();
});
$('.active_cbx').bootstrapSwitch({
onText: "Oui",
offText: "Non",
}).on('switchChange.bootstrapSwitch', function(event, state) {
$.ajax({
url: 'api.php?o=users&a=switch_state&i=' + this.name.substr(7)
+ "&state=" + (state ? 1 : 0), // post on the API
type: 'POST',
xhr: function() { // Custom XMLHttpRequest
var myXhr = $.ajaxSettings.xhr();
return myXhr;
},
success: function(){
if(state) {
alert('Le compte utilisateur est activé.');
} else {
alert('Le compte utilisateur est à présent désactivé.');
alert('Le compte utilisateur est à présent désactivé.');
}
},
error: function(){},
cache: false,
contentType: false,
processData: false
});
});
},
error: function(){},
cache: false,
contentType: false,
processData: false
});
});
});
/* FIXME : translation of the table
see https://datatables.net/plug-ins/i18n/French
*/
</script>
{% endblock %}
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