Commit 3565c7e8 authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

Création d'un module démo avec bdd sqlite minimale.

parent 57f16dae
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Demo;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
class Module
{
public function onBootstrap(MvcEvent $e)
{
$e->getApplication()->getServiceManager()->get('translator');
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
/* Active un layout spécial si la requête est de type AJAX. Valable pour TOUS les modules de l'application. */
$eventManager->getSharedManager()->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch',
function (MvcEvent $e) {
$request = $e->getRequest();
if ($request instanceof HttpRequest && $request->isXmlHttpRequest()) {
$e->getTarget()->layout('layout/ajax.phtml');
}
}
);
}
public function getConfig()
{
return include __DIR__ . '/config/module.config.php';
}
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}
}
# Module Démo Unicaen
## Création de la base données de démo
Créez la base de données sqlite3 de demo [`data/db/demo.sqlite`](data/db/demo.sqlite) :
$ bin/create_demo_db.sh
Vérifiez la création du schéma :
$ sqlite3 data/db/demo.sqlite ".schema"
CREATE TABLE user
(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username VARCHAR(255) DEFAULT NULL,
email VARCHAR(255) DEFAULT NULL,
display_name VARCHAR(64) DEFAULT NULL,
password VARCHAR(128) NOT NULL,
state SMALLINT default 1
, PASSWORD_RESET_TOKEN varchar2(256) DEFAULT NULL);
CREATE UNIQUE INDEX user_unique_username ON user(username);
CREATE UNIQUE INDEX USER_PASSWORD_RESET_TOKEN_UN ON user (PASSWORD_RESET_TOKEN);
CREATE TABLE user_role (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
role_id VARCHAR(64) NOT NULL,
is_default TINYINT(1) NOT NULL DEFAULT 0,
parent_id INTEGER NULL DEFAULT NULL,
ldap_filter varchar(255) DEFAULT NULL,
FOREIGN KEY (parent_id) REFERENCES user_role (id) ON DELETE SET NULL
);
CREATE UNIQUE INDEX role_unique_role_id ON user_role(role_id);
CREATE INDEX role_idx_parent_id ON user_role(parent_id);
CREATE TABLE user_role_linker (
user_id INTEGER NOT NULL,
role_id INTEGER NOT NULL,
PRIMARY KEY (user_id, role_id),
FOREIGN KEY (role_id) REFERENCES user_role (id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
CREATE INDEX user_role_linker_idx_role_id ON user_role_linker(role_id);
CREATE INDEX user_role_linker_idx_user_id ON user_role_linker(user_id);
CREATE TABLE categorie_privilege (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
code VARCHAR(150) NOT NULL,
libelle VARCHAR(200) NOT NULL,
ordre INTEGER
);
CREATE UNIQUE INDEX categorie_unique_code ON categorie_privilege(code);
CREATE TABLE privilege (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
categorie_id INTEGER NOT NULL,
code VARCHAR(150) NOT NULL,
libelle VARCHAR(200) NOT NULL,
ordre INTEGER,
FOREIGN KEY (categorie_id) REFERENCES categorie_privilege (id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX privilege_unique_code ON privilege(code);
CREATE TABLE role_privilege (
role_id INTEGER NOT NULL,
privilege_id INTEGER NOT NULL,
PRIMARY KEY (role_id,privilege_id),
FOREIGN KEY (role_id) REFERENCES user_role (id) ON DELETE CASCADE,
FOREIGN KEY (privilege_id) REFERENCES privilege (id) ON DELETE CASCADE
);
CREATE INDEX idx_role_id on role_privilege(role_id);
CREATE INDEX idx_privilege_id on role_privilege(privilege_id);
Interrogez la table `user` pour vérifier la présence de l'utilisateur `demo`, exemple :
$ sqlite3 data/db/demo.sqlite "select * from user;"
1|demo|demo@mail.fr|Demo|$2y$10$GmGzPXsLK5Kts30ZrS9QnOLsNYDgZ62797Kitp4Z1nWHlB1g7DZma|1|
NB: l'utilisateur `admin` a pour mot de passe `admin`, chiffré avec Bcrypt comme ceci :
`php --run 'require "vendor/autoload.php"; $bcrypt = new Zend\Crypt\Password\Bcrypt(); var_dump($bcrypt->create("xxx"));'`
## Configuration du module
- Les fichiers de configuration locaux et globaux des bibliothèques utilisées se trouvent dans
[`config/other`](config/other).
- Reportez-vous aux docs des modules concernés pour personnaliser ces fichiers de configuration selon vos besoins :
- [unicaen/app](https://git.unicaen.fr/lib/unicaen/app)
- [unicaen/auth](https://git.unicaen.fr/lib/unicaen/auth)
- [unicaen/code](https://git.unicaen.fr/lib/unicaen/code)
- La configuration Doctrine d'acc_s à la base de données se trouve dans
[`config/module.config.php`](config/module.config.php).
#!/usr/bin/env bash
DIR=$(cd `dirname $0` && pwd)
DBPATH=${DIR}/../data/db/demo.sqlite
sqlite3 ${DBPATH} < ${DIR}/../data/demo.sqlite.sql && \
chmod 777 ${DBPATH} && \
chmod 777 `dirname ${DBPATH}`
<?php
namespace Demo;
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => 'Doctrine\DBAL\Driver\PDOSqlite\Driver',
'params' => [
'path' => __DIR__ . '/../data/db/demo.sqlite',
],
],
],
],
'bjyauthorize' => [
'guards' => [
'BjyAuthorize\Guard\Controller' => [
['controller' => 'Demo\Controller\Index', 'roles' => []],
],
],
],
'router' => [
'routes' => [
'home' => [
'type' => 'Zend\Mvc\Router\Http\Literal',
'options' => [
'route' => '/',
'defaults' => [
'controller' => 'Demo\Controller\Index', // <-- change here
'action' => 'index',
],
],
],
],
],
'service_manager' => [
'factories' => [
],
],
'translator' => [
'locale' => 'fr_FR', // en_US
'translation_file_patterns' => [
[
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
],
],
],
'controllers' => [
'invokables' => [
'Demo\Controller\Index' => Controller\IndexController::class,
],
],
'view_manager' => [
'template_path_stack' => [
__DIR__ . '/../view',
],
],
];
CREATE TABLE IF NOT EXISTS user
(
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
username VARCHAR(255) DEFAULT NULL,
email VARCHAR(255) DEFAULT NULL,
display_name VARCHAR(64) DEFAULT NULL,
password VARCHAR(128) NOT NULL,
state SMALLINT default 1
);
ALTER TABLE user ADD PASSWORD_RESET_TOKEN varchar(256) DEFAULT NULL;
CREATE UNIQUE INDEX user_unique_username ON user(username);
CREATE UNIQUE INDEX USER_PASSWORD_RESET_TOKEN_UN ON user (PASSWORD_RESET_TOKEN);
CREATE TABLE IF NOT EXISTS user_role (
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
role_id VARCHAR(64) NOT NULL,
is_default TINYINT(1) NOT NULL DEFAULT 0,
parent_id INTEGER NULL DEFAULT NULL,
ldap_filter varchar(255) DEFAULT NULL,
FOREIGN KEY (parent_id) REFERENCES user_role (id) ON DELETE SET NULL
);
CREATE UNIQUE INDEX role_unique_role_id ON user_role(role_id);
CREATE INDEX role_idx_parent_id ON user_role(parent_id);
CREATE TABLE IF NOT EXISTS user_role_linker (
user_id INTEGER NOT NULL,
role_id INTEGER NOT NULL,
PRIMARY KEY (user_id, role_id),
FOREIGN KEY (role_id) REFERENCES user_role (id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
);
CREATE INDEX user_role_linker_idx_role_id ON user_role_linker(role_id);
CREATE INDEX user_role_linker_idx_user_id ON user_role_linker(user_id);
CREATE TABLE IF NOT EXISTS categorie_privilege (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
code VARCHAR(150) NOT NULL,
libelle VARCHAR(200) NOT NULL,
ordre INTEGER
);
CREATE UNIQUE INDEX categorie_unique_code ON categorie_privilege(code);
CREATE TABLE IF NOT EXISTS privilege (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
categorie_id INTEGER NOT NULL,
code VARCHAR(150) NOT NULL,
libelle VARCHAR(200) NOT NULL,
ordre INTEGER,
FOREIGN KEY (categorie_id) REFERENCES categorie_privilege (id) ON DELETE CASCADE
);
CREATE UNIQUE INDEX privilege_unique_code ON privilege(code);
CREATE TABLE IF NOT EXISTS role_privilege (
role_id INTEGER NOT NULL,
privilege_id INTEGER NOT NULL,
PRIMARY KEY (role_id,privilege_id),
FOREIGN KEY (role_id) REFERENCES user_role (id) ON DELETE CASCADE,
FOREIGN KEY (privilege_id) REFERENCES privilege (id) ON DELETE CASCADE
);
CREATE INDEX idx_role_id on role_privilege(role_id);
CREATE INDEX idx_privilege_id on role_privilege(privilege_id);
-- Données
INSERT INTO user_role (id, role_id, is_default, parent_id) VALUES
(1, 'Standard', 1, NULL),
(2, 'Gestionnaire', 0, 1),
(3, 'Super-gestionnaire', 0, 2),
(4, 'Administrateur', 0, 3);
INSERT INTO categorie_privilege (id, code, libelle, ordre) VALUES
(1, 'droit', 'Gestion des droits', 1);
INSERT INTO privilege (id, categorie_id, code, libelle, ordre) VALUES
(1, 1, 'role-visualisation', 'Rôles - Visualisation', 1),
(2, 1, 'role-edition', 'Rôles - Édition', 2),
(3, 1, 'privilege-visualisation', 'Privilèges - Visualisation', 3),
(4, 1, 'privilege-edition', 'Privilèges - Édition', 4);
INSERT INTO role_privilege (role_id, privilege_id) VALUES
(4, 1),
(4, 2),
(4, 3),
(4, 4);
INSERT INTO user (username, email, display_name, password, state) VALUES
-- utilisateur demo/azerty
('demo', 'demo@mail.fr', 'Demo', '$2y$10$PxXnVLYnGEzEnfqPqRKJSe9AabocES2H4bBK5VzzJlzuj1rVt7Lwu', 1);
INSERT INTO user_role_linker(user_id, role_id)
SELECT u.id, r.id FROM user u, user_role r WHERE u.username = 'demo' and r.role_id = 'Standard';
<?php
namespace Demo\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
return new ViewModel();
}
}
<div class="jumbotron">
<h1>Bienvenue dans le module <br>Unicaen de démo</h1>
<?php if (!$this->identity()): ?>
<p class="text-danger">
<strong>
<?php echo $this->translate("Vous n'êtes pas connecté-e."); ?>
</strong>
</p>
<p>
<a class="btn btn-success btn-lg" href="<?php echo $this->url('zfcuser/login') ?>">
<?php echo $this->translate('Connectez-vous...') ?>
</a>
</p>
<?php endif ?>
</div>
<?php
echo $this->content;
\ No newline at end of file
Supports Markdown
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