From 3d43a6433fdfb50ebe0b3967f2206a6f5f37e2cc Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 3 May 2018 08:46:18 +0200 Subject: [PATCH 01/69] feat:add connexion tab --- app/Controllers/Connection.php | 15 ++++++++ app/Project.php | 15 ++++++++ app/Templates/default/connection.html.twig | 40 ++++++++++++++++++++++ app/Templates/partials/nav.html.twig | 6 ++++ app/init.php | 3 ++ public/index.php | 18 ++++++++-- 6 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 app/Controllers/Connection.php create mode 100644 app/Templates/default/connection.html.twig diff --git a/app/Controllers/Connection.php b/app/Controllers/Connection.php new file mode 100644 index 0000000..92c175b --- /dev/null +++ b/app/Controllers/Connection.php @@ -0,0 +1,15 @@ +render($this->container->project->pathFor('connection.html.twig'), [ + 'class' => 'connection' + ]); + } +} diff --git a/app/Project.php b/app/Project.php index 65f0f40..ffb2b61 100644 --- a/app/Project.php +++ b/app/Project.php @@ -14,6 +14,7 @@ class Project private $project_path; private $templates_path; private $enable_contact = true; + private $enable_connection = true; private $footer_links = [ 'GLPI project' => [ 'faclass' => 'fa fa-globe', @@ -107,6 +108,10 @@ public function setConfig($config) $this->enable_contact = (bool)$config['enable_contact']; } + if (isset($config['enable_connection'])) { + $this->enable_connection = (bool)$config['enable_connection']; + } + if (isset($config['schema'])) { $this->setSchemaConfig($config['schema']); } @@ -462,6 +467,16 @@ public function hasContactPage() return $this->enable_contact; } + /** + * Is connection page active for current project + * + * @return boolean + */ + public function hasConnectionPage() + { + return $this->enable_connection; + } + /** * Get footer links * diff --git a/app/Templates/default/connection.html.twig b/app/Templates/default/connection.html.twig new file mode 100644 index 0000000..8ff61d0 --- /dev/null +++ b/app/Templates/default/connection.html.twig @@ -0,0 +1,40 @@ +{% extends "partials/base.html.twig" %} +{% block title %}Connexion{% endblock %} + +{% block header %} +{% set myheader= { + 'title': ' Connection', + 'text': 'Connect to manage json flows entries', +} %} +{{ parent() }} +{% endblock %} + +{% block content %} +
+ +
+
+ + +
+ +
+ + +
+ + + + {{ csrf.field | raw }} +
+ +
+{% endblock %} + +{% block userscripts %} +{{ parent() }} + + + +{% endblock %} + diff --git a/app/Templates/partials/nav.html.twig b/app/Templates/partials/nav.html.twig index 494e995..6f06fa6 100644 --- a/app/Templates/partials/nav.html.twig +++ b/app/Templates/partials/nav.html.twig @@ -22,6 +22,12 @@ Contact {% endif %} + {% if enable_connection %} + + + Connection + {% endif %} diff --git a/app/init.php b/app/init.php index 7d2b0a5..1c12f0d 100644 --- a/app/init.php +++ b/app/init.php @@ -110,6 +110,9 @@ //enable contact page $env->addGlobal('enable_contact', $c->project->hasContactPage()); + //enable connection page + $env->addGlobal('enable_connection', $c->project->hasConnectionPage()); + //footer links $env->addGlobal('footer_links', $c->project->getFooterLinks()); diff --git a/public/index.php b/public/index.php index 6030776..6db8554 100644 --- a/public/index.php +++ b/public/index.php @@ -19,11 +19,23 @@ } /** References */ + +// connection +$app->get('/connection', 'GLPI\Telemetry\Controllers\Connection:view') + ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) + ->add($container['csrf']) + ->setName('connection'); +$app->post('/connection', 'GLPI\Telemetry\Controllers\Connection:send') + ->add($recaptcha) + ->add($container['csrf']) + ->setName('sendConnection'); + + //References list $app->get('/reference[/page/{page:\d+}]', 'GLPI\Telemetry\Controllers\Reference:view') - ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) - ->add($container['csrf']) - ->setName('reference'); + ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) + ->add($container['csrf']) + ->setName('reference'); //References filtering $app->map( From 2dd22a72f579e650095191c9877ecf17a33da458 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 3 May 2018 14:34:24 +0200 Subject: [PATCH 02/69] add tuupola/slim-basic-auth: PSR-7 and PSR-15 HTTP Basic --- composer.json | 3 +- composer.lock | 137 ++++++++++++++++++++++++++++++++++---------------- 2 files changed, 96 insertions(+), 44 deletions(-) diff --git a/composer.json b/composer.json index 3ddcb46..58881c8 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,8 @@ "nicmart/numbers": "^0.1.0", "zendframework/zend-cache": "^2.7", "robmorgan/phinx": "^0.9.1", - "akrabat/rka-scheme-and-host-detection-middleware": "^0.3.0" + "akrabat/rka-scheme-and-host-detection-middleware": "^0.3.0", + "tuupola/slim-basic-auth": "^2.3" }, "require-dev": { "atoum/atoum": "^3.2", diff --git a/composer.lock b/composer.lock index a1fc5d6..39bf493 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "6d91050cb80d88b2d0c10ae4dd0acaff", + "hash": "a9e2740ee32b27904a2ab86a8af58adb", + "content-hash": "a8befddd36b71349805413f72e03e887", "packages": [ { "name": "akrabat/rka-scheme-and-host-detection-middleware", @@ -52,7 +53,7 @@ "middleware", "psr7" ], - "time": "2016-11-13T11:55:26+00:00" + "time": "2016-11-13 11:55:26" }, { "name": "container-interop/container-interop", @@ -83,7 +84,7 @@ ], "description": "Promoting the interoperability of container objects (DIC, SL, etc.)", "homepage": "https://github.com/container-interop/container-interop", - "time": "2017-02-14T19:40:03+00:00" + "time": "2017-02-14 19:40:03" }, { "name": "doctrine/inflector", @@ -150,7 +151,7 @@ "singularize", "string" ], - "time": "2015-11-06T14:35:42+00:00" + "time": "2015-11-06 14:35:42" }, { "name": "geggleto/psr7-recaptcha", @@ -195,7 +196,7 @@ "middleware", "slim" ], - "time": "2015-11-16T15:35:16+00:00" + "time": "2015-11-16 15:35:16" }, { "name": "google/recaptcha", @@ -240,7 +241,7 @@ "recaptcha", "spam" ], - "time": "2017-03-09T18:44:34+00:00" + "time": "2017-03-09 18:44:34" }, { "name": "illuminate/container", @@ -283,7 +284,7 @@ ], "description": "The Illuminate Container package.", "homepage": "http://laravel.com", - "time": "2016-08-01T13:49:14+00:00" + "time": "2016-08-01 13:49:14" }, { "name": "illuminate/contracts", @@ -325,7 +326,7 @@ ], "description": "The Illuminate Contracts package.", "homepage": "http://laravel.com", - "time": "2016-08-08T11:46:08+00:00" + "time": "2016-08-08 11:46:08" }, { "name": "illuminate/database", @@ -385,7 +386,7 @@ "orm", "sql" ], - "time": "2016-08-25T07:01:20+00:00" + "time": "2016-08-25 07:01:20" }, { "name": "illuminate/pagination", @@ -429,7 +430,7 @@ ], "description": "The Illuminate Pagination package.", "homepage": "http://laravel.com", - "time": "2016-08-01T13:49:14+00:00" + "time": "2016-08-01 13:49:14" }, { "name": "illuminate/support", @@ -488,7 +489,7 @@ ], "description": "The Illuminate Support package.", "homepage": "http://laravel.com", - "time": "2016-08-05T14:49:58+00:00" + "time": "2016-08-05 14:49:58" }, { "name": "justinrainbow/json-schema", @@ -554,7 +555,7 @@ "json", "schema" ], - "time": "2017-10-10T13:22:37+00:00" + "time": "2017-10-10 13:22:37" }, { "name": "kanellov/slim-twig-flash", @@ -609,7 +610,7 @@ "twig", "view" ], - "time": "2016-12-29T22:07:19+00:00" + "time": "2016-12-29 22:07:19" }, { "name": "mledoze/countries", @@ -655,7 +656,7 @@ "xml", "yaml" ], - "time": "2016-09-01T09:41:00+00:00" + "time": "2016-09-01 09:41:00" }, { "name": "monolog/monolog", @@ -733,7 +734,7 @@ "logging", "psr-3" ], - "time": "2017-06-19T01:22:40+00:00" + "time": "2017-06-19 01:22:40" }, { "name": "nesbot/carbon", @@ -786,7 +787,7 @@ "datetime", "time" ], - "time": "2017-01-16T07:55:07+00:00" + "time": "2017-01-16 07:55:07" }, { "name": "nicmart/numbers", @@ -827,7 +828,7 @@ } ], "description": "Format numbers in various formats, like scientific notation or unit-suffix notation", - "time": "2013-09-15T19:12:52+00:00" + "time": "2013-09-15 19:12:52" }, { "name": "nikic/fast-route", @@ -870,7 +871,7 @@ "router", "routing" ], - "time": "2017-01-19T11:35:12+00:00" + "time": "2017-01-19 11:35:12" }, { "name": "paragonie/random_compat", @@ -918,7 +919,7 @@ "pseudorandom", "random" ], - "time": "2017-03-13T16:22:52+00:00" + "time": "2017-03-13 16:22:52" }, { "name": "phpmailer/phpmailer", @@ -995,7 +996,7 @@ } ], "description": "PHPMailer is a full-featured email creation and transfer class for PHP", - "time": "2017-08-28T11:12:07+00:00" + "time": "2017-08-28 11:12:07" }, { "name": "pimple/pimple", @@ -1045,7 +1046,7 @@ "container", "dependency injection" ], - "time": "2017-07-23T07:32:15+00:00" + "time": "2017-07-23 07:32:15" }, { "name": "psr/container", @@ -1094,7 +1095,7 @@ "container-interop", "psr" ], - "time": "2017-02-14T16:28:37+00:00" + "time": "2017-02-14 16:28:37" }, { "name": "psr/http-message", @@ -1144,7 +1145,7 @@ "request", "response" ], - "time": "2016-08-06T14:39:51+00:00" + "time": "2016-08-06 14:39:51" }, { "name": "psr/log", @@ -1191,7 +1192,7 @@ "psr", "psr-3" ], - "time": "2016-10-10T12:19:37+00:00" + "time": "2016-10-10 12:19:37" }, { "name": "robmorgan/phinx", @@ -1261,7 +1262,7 @@ "migrations", "phinx" ], - "time": "2017-09-09T13:54:33+00:00" + "time": "2017-09-09 13:54:33" }, { "name": "slim/csrf", @@ -1311,7 +1312,7 @@ "middleware", "slim" ], - "time": "2016-12-20T20:45:43+00:00" + "time": "2016-12-20 20:45:43" }, { "name": "slim/flash", @@ -1359,7 +1360,7 @@ "provider", "slim" ], - "time": "2016-11-11T16:29:19+00:00" + "time": "2016-11-11 16:29:19" }, { "name": "slim/slim", @@ -1430,7 +1431,7 @@ "micro", "router" ], - "time": "2017-03-19T17:55:20+00:00" + "time": "2017-03-19 17:55:20" }, { "name": "slim/twig-view", @@ -1480,7 +1481,7 @@ "twig", "view" ], - "time": "2017-09-20T19:47:37+00:00" + "time": "2017-09-20 19:47:37" }, { "name": "symfony/config", @@ -1542,7 +1543,7 @@ ], "description": "Symfony Config Component", "homepage": "https://symfony.com", - "time": "2017-10-04T18:56:58+00:00" + "time": "2017-10-04 18:56:58" }, { "name": "symfony/console", @@ -1610,7 +1611,7 @@ ], "description": "Symfony Console Component", "homepage": "https://symfony.com", - "time": "2017-10-02T06:42:24+00:00" + "time": "2017-10-02 06:42:24" }, { "name": "symfony/debug", @@ -1666,7 +1667,7 @@ ], "description": "Symfony Debug Component", "homepage": "https://symfony.com", - "time": "2017-10-02T06:42:24+00:00" + "time": "2017-10-02 06:42:24" }, { "name": "symfony/filesystem", @@ -1715,7 +1716,7 @@ ], "description": "Symfony Filesystem Component", "homepage": "https://symfony.com", - "time": "2017-10-03T13:33:10+00:00" + "time": "2017-10-03 13:33:10" }, { "name": "symfony/polyfill-mbstring", @@ -1774,7 +1775,7 @@ "portable", "shim" ], - "time": "2017-06-14T15:44:48+00:00" + "time": "2017-06-14 15:44:48" }, { "name": "symfony/translation", @@ -1839,7 +1840,7 @@ ], "description": "Symfony Translation Component", "homepage": "https://symfony.com", - "time": "2017-10-02T06:42:24+00:00" + "time": "2017-10-02 06:42:24" }, { "name": "symfony/yaml", @@ -1894,7 +1895,57 @@ ], "description": "Symfony Yaml Component", "homepage": "https://symfony.com", - "time": "2017-10-05T14:43:42+00:00" + "time": "2017-10-05 14:43:42" + }, + { + "name": "tuupola/slim-basic-auth", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/tuupola/slim-basic-auth.git", + "reference": "0015eb872edf4796c71feaa127b3886884bb7ca6" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tuupola/slim-basic-auth/zipball/0015eb872edf4796c71feaa127b3886884bb7ca6", + "reference": "0015eb872edf4796c71feaa127b3886884bb7ca6", + "shasum": "" + }, + "require": { + "php": "^5.5 || ^7.0", + "psr/http-message": "^1.0" + }, + "require-dev": { + "overtrue/phplint": "^0.2.4", + "phpunit/phpunit": "^4.3", + "squizlabs/php_codesniffer": "^2.3", + "zendframework/zend-diactoros": "^1.3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Slim\\Middleware\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mika Tuupola", + "email": "tuupola@appelsiini.net", + "homepage": "http://www.appelsiini.net/" + } + ], + "description": "PSR-7 HTTP Basic Authentication Middleware", + "homepage": "http://www.appelsiini.net/projects/slim-basic-auth", + "keywords": [ + "auth", + "middleware", + "psr-7" + ], + "time": "2017-09-19 12:46:08" }, { "name": "twig/twig", @@ -1959,7 +2010,7 @@ "keywords": [ "templating" ], - "time": "2017-09-27T18:06:46+00:00" + "time": "2017-09-27 18:06:46" }, { "name": "zendframework/zend-cache", @@ -2028,7 +2079,7 @@ "cache", "zf2" ], - "time": "2016-12-16T11:35:47+00:00" + "time": "2016-12-16 11:35:47" }, { "name": "zendframework/zend-eventmanager", @@ -2082,7 +2133,7 @@ "events", "zf2" ], - "time": "2017-07-11T19:17:22+00:00" + "time": "2017-07-11 19:17:22" }, { "name": "zendframework/zend-servicemanager", @@ -2145,7 +2196,7 @@ "servicemanager", "zf" ], - "time": "2017-03-01T22:08:02+00:00" + "time": "2017-03-01 22:08:02" }, { "name": "zendframework/zend-stdlib", @@ -2190,7 +2241,7 @@ "stdlib", "zf2" ], - "time": "2016-09-13T14:38:50+00:00" + "time": "2016-09-13 14:38:50" } ], "packages-dev": [ @@ -2275,7 +2326,7 @@ "test", "unit testing" ], - "time": "2017-09-07T08:27:13+00:00" + "time": "2017-09-07 08:27:13" }, { "name": "atoum/telemetry-extension", @@ -2336,7 +2387,7 @@ "test", "unit testing" ], - "time": "2017-10-02T22:09:26+00:00" + "time": "2017-10-02 22:09:26" } ], "aliases": [], From 8345d277a4fbc46f1922d4291d9efa2be44456f6 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 3 May 2018 14:35:48 +0200 Subject: [PATCH 03/69] add db migration for user --- .../20180503073814_install_users.php | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 db/migrations/20180503073814_install_users.php diff --git a/db/migrations/20180503073814_install_users.php b/db/migrations/20180503073814_install_users.php new file mode 100644 index 0000000..07b809d --- /dev/null +++ b/db/migrations/20180503073814_install_users.php @@ -0,0 +1,70 @@ +table('users'); + $table + ->addColumn('user', 'string', ['length' => 32, 'null' => true]) + ->addColumn('hash', 'string', ['length' => 255, 'null' => true]) + ->addColumn('email', 'string', ['length' => 505, 'null' => true]) + ->addColumn('is_admin', 'boolean', ['null' => true]) + ->create() + ; + + $table = $this->table('reference'); + $table + ->addColumn('user_id', 'integer', ['null' => true]) + ->addForeignKey( + 'user_id', + 'users', + 'id', + ['delete'=> 'CASCADE','constraint' => 'telemetry_users_reference_id_fkey'] + ) + ->addIndex(['user_id'], ['unique' => true]) + ->update() + ; + } + + public function down() + { + + + $table = $this->table('reference'); + $table + ->dropForeignKey( + 'telemetry_users_reference_id_fkey' + ) + ->removeColumn('user_id', 'integer', ['null' => true]) + ->update() + ; + + $this->dropTable('users'); + } +} From 4f1a4e0a830671ff1c6737b623602dfc7b225b55 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 3 May 2018 14:37:23 +0200 Subject: [PATCH 04/69] add button for connection --- app/Templates/default/connection.html.twig | 2 +- app/init.php | 3 +++ public/index.php | 31 +++++++++++++++------- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/app/Templates/default/connection.html.twig b/app/Templates/default/connection.html.twig index 8ff61d0..e531424 100644 --- a/app/Templates/default/connection.html.twig +++ b/app/Templates/default/connection.html.twig @@ -1,5 +1,5 @@ {% extends "partials/base.html.twig" %} -{% block title %}Connexion{% endblock %} +{% block title %}Connection{% endblock %} {% block header %} {% set myheader= { diff --git a/app/init.php b/app/init.php index 1c12f0d..4a066c8 100644 --- a/app/init.php +++ b/app/init.php @@ -113,6 +113,9 @@ //enable connection page $env->addGlobal('enable_connection', $c->project->hasConnectionPage()); + //enable register page + $env->addGlobal('enable_connection', $c->project->hasRegisterPage()); + //footer links $env->addGlobal('footer_links', $c->project->getFooterLinks()); diff --git a/public/index.php b/public/index.php index 6db8554..aa1eca9 100644 --- a/public/index.php +++ b/public/index.php @@ -18,19 +18,30 @@ ->setName('sendContact'); } -/** References */ -// connection -$app->get('/connection', 'GLPI\Telemetry\Controllers\Connection:view') - ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) - ->add($container['csrf']) - ->setName('connection'); -$app->post('/connection', 'GLPI\Telemetry\Controllers\Connection:send') - ->add($recaptcha) - ->add($container['csrf']) - ->setName('sendConnection'); +if ($container->project->hasConnectionPage()) { + // connection + $app->get('/connection', 'GLPI\Telemetry\Controllers\Connection:view') + ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) + ->add($container['csrf']) + ->setName('connection'); + $app->post('/connection', 'GLPI\Telemetry\Controllers\Connection:send') + ->add($container['csrf']) + ->setName('sendConnection'); +} +if ($container->project->hasRegisterPage()) { + // register + $app->get('/register', 'GLPI\Telemetry\Controllers\Register:view') + ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) + ->add($container['csrf']) + ->setName('register'); + $app->post('/register', 'GLPI\Telemetry\Controllers\Register:send') + ->add($container['csrf']) + ->setName('sendRegister'); +} +/** References */ //References list $app->get('/reference[/page/{page:\d+}]', 'GLPI\Telemetry\Controllers\Reference:view') ->add(new GLPI\Telemetry\Middleware\CsrfView($container)) From 2645b72a5b25bdf31ddb6f91df0469edb62871b3 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 3 May 2018 14:38:14 +0200 Subject: [PATCH 05/69] WIP implement register --- app/Controllers/Register.php | 15 ++++++++ app/Project.php | 15 ++++++++ app/Templates/default/register.html.twig | 45 ++++++++++++++++++++++++ app/Templates/partials/nav.html.twig | 6 ++++ 4 files changed, 81 insertions(+) create mode 100644 app/Controllers/Register.php create mode 100644 app/Templates/default/register.html.twig diff --git a/app/Controllers/Register.php b/app/Controllers/Register.php new file mode 100644 index 0000000..f2e6e8b --- /dev/null +++ b/app/Controllers/Register.php @@ -0,0 +1,15 @@ +render($this->container->project->pathFor('register.html.twig'), [ + 'class' => 'register' + ]); + } +} diff --git a/app/Project.php b/app/Project.php index ffb2b61..2644078 100644 --- a/app/Project.php +++ b/app/Project.php @@ -15,6 +15,7 @@ class Project private $templates_path; private $enable_contact = true; private $enable_connection = true; + private $enable_register = true; private $footer_links = [ 'GLPI project' => [ 'faclass' => 'fa fa-globe', @@ -112,6 +113,10 @@ public function setConfig($config) $this->enable_connection = (bool)$config['enable_connection']; } + if (isset($config['enable_register'])) { + $this->enable_register = (bool)$config['enable_register']; + } + if (isset($config['schema'])) { $this->setSchemaConfig($config['schema']); } @@ -477,6 +482,16 @@ public function hasConnectionPage() return $this->enable_connection; } + /** + * Is register page active for current project + * + * @return boolean + */ + public function hasRegisterPage() + { + return $this->enable_register; + } + /** * Get footer links * diff --git a/app/Templates/default/register.html.twig b/app/Templates/default/register.html.twig new file mode 100644 index 0000000..bc82d41 --- /dev/null +++ b/app/Templates/default/register.html.twig @@ -0,0 +1,45 @@ +{% extends "partials/base.html.twig" %} +{% block title %}Register{% endblock %} + +{% block header %} +{% set myheader= { + 'title': ' Register', + 'text': 'Register to manage json flows entries', +} %} +{{ parent() }} +{% endblock %} + +{% block content %} +
+ +
+
+ + +
+ +
+ + +
+ +
+ + +
+ + + + {{ csrf.field | raw }} +
+ +
+{% endblock %} + +{% block userscripts %} +{{ parent() }} + + + +{% endblock %} + diff --git a/app/Templates/partials/nav.html.twig b/app/Templates/partials/nav.html.twig index 6f06fa6..1889154 100644 --- a/app/Templates/partials/nav.html.twig +++ b/app/Templates/partials/nav.html.twig @@ -28,6 +28,12 @@ Connection {% endif %} + {% if enable_register %} + + + Register + {% endif %} From 8362e686cc8f7eedbfd2133599ec1fb52f217853 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 17 May 2018 13:52:19 +0200 Subject: [PATCH 06/69] Login - Logoff - Register --- app/Controllers/Connection.php | 58 +++++++++++++++ app/Controllers/Register.php | 29 ++++++++ app/Models/Authentication.php | 28 ++++++++ app/Models/Register.php | 90 ++++++++++++++++++++++++ app/Models/User.php | 80 +++++++++++++++++++++ app/Templates/default/register.html.twig | 13 +++- 6 files changed, 296 insertions(+), 2 deletions(-) create mode 100644 app/Models/Authentication.php create mode 100644 app/Models/Register.php create mode 100644 app/Models/User.php diff --git a/app/Controllers/Connection.php b/app/Controllers/Connection.php index 92c175b..7f7681d 100644 --- a/app/Controllers/Connection.php +++ b/app/Controllers/Connection.php @@ -3,6 +3,7 @@ use GLPI\Telemetry\Controllers\ControllerAbstract; use Slim\Http\Request; use Slim\Http\Response; +use GLPI\Telemetry\Models\Authentication as AuthenticationModel; class Connection extends ControllerAbstract { @@ -12,4 +13,61 @@ public function view() 'class' => 'connection' ]); } + + public function send(Request $req, Response $res) + { + $post = $req->getParsedBody(); + + $auth_ref = new AuthenticationModel; + $auth = $auth_ref->newInstance(); + + if($auth->Authenticate($post)) { + // store a message for user (displayed after redirect) + $msg_text = "You are now connected !"; + $this->container->flash->addMessage( + 'success', + $msg_text + ); + $this->container->flash->addMessage( + 'success', + 'Welcome '.$post['username'] + ); + + + $this->setUserSession([ + 'username' => $post['username'], + 'user_info' => $auth->getUserInfo() + ]); + + //redirect + return $res->withRedirect($this->container->router->pathFor('telemetry')); + + } else { + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'warn', + 'Wrong username or password' + ); + //redirect + return $res->withRedirect($this->container->router->pathFor('connection')); + + } + + } + + public function setUserSession($tab) + { + $_SESSION['user']['id'] = $tab['user_info']['id']; + $_SESSION['user']['username'] = $tab['username']; + $_SESSION['user']['is_admin'] = $tab['user_info']['is_admin']; + $_SESSION['user']['mail'] = $tab['user_info']['mail']; + $_SESSION['user']['references'] = $tab['user_info']['references_info']; + $_SESSION['user']['references_count'] = $tab['user_info']['references_count']; + } + + public function disconnect(Request $req, Response $res) + { + unset($_SESSION['user']); + return $res->withRedirect($this->container->router->pathFor('telemetry')); + } } diff --git a/app/Controllers/Register.php b/app/Controllers/Register.php index f2e6e8b..0133547 100644 --- a/app/Controllers/Register.php +++ b/app/Controllers/Register.php @@ -1,6 +1,7 @@ 'register' ]); } + + public function send(Request $req, Response $res) + { + $post = $req->getParsedBody(); + + $register_ref = new RegisterModel; + $register_model = $register_ref->newInstance(); + + $tab = $register_model->checkRegister($post); + + if($tab['status'] === '200'){ + $type = 'success'; + $redirect = 'connection'; + } else { + $type = 'error'; + $redirect = 'register'; + } + + $msg_text = $tab['msg']; + $this->container->flash->addMessage( + $type, + $msg_text + ); + + + //redirect + return $res->withRedirect($this->container->router->pathFor($redirect)); + } } diff --git a/app/Models/Authentication.php b/app/Models/Authentication.php new file mode 100644 index 0000000..b508b29 --- /dev/null +++ b/app/Models/Authentication.php @@ -0,0 +1,28 @@ +newInstance(); + + + if ($user_model->isExist($post)) { + $this->user_info = $user_model->getUserInfo(); + return true; + } else { + return false; + } + } + + + public function getUserInfo() + { + return $this->user_info; + } +} diff --git a/app/Models/Register.php b/app/Models/Register.php new file mode 100644 index 0000000..58da5bd --- /dev/null +++ b/app/Models/Register.php @@ -0,0 +1,90 @@ + '', + 'msg' => '' + ]; + protected $guarded = [ + 'id' + ]; + + + public function checkRegister($post) + { + $user = htmlentities($post['username']); + $hash = htmlentities($post['password']); + $mail = htmlentities($post['mail']); + $admin = false; + + if(!$this->is_valid_password($hash)) { + $this->setTabReturn('406', 'Password is not compliant'); + return $this->tab; + } + + $hash = password_hash($post['password'], PASSWORD_DEFAULT); + + return $this->insertRegister($user, $hash, $mail, $admin); + } + + + /** + * Length 6 chars + * At least one lowercase letter + * At least one uppercase letter + * At least one digit + **/ + private function is_valid_password($password) { + return preg_match('#^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$#', $password) ? TRUE : FALSE; + } + + + private function insertRegister($user, $hash, $mail, $admin) + { + + // test if the user already exist + $user_ref = new UserModel(); + $user_model = $user_ref->newInstance(); + if($user_model->usernameExist($user)){ + $this->setTabReturn('417', 'Registration failed, user already exist'); + return $this->tab; + } + + + $tmp = + [ + 'user' => $user, + 'hash' => $hash, + 'email' => $mail, + 'is_admin' => $admin + ] + ; + + + $status = $user_model->insert($tmp); + + if($status === TRUE) { + $this->setTabReturn('200', 'Registration done'); + return $this->tab; + } else { + $this->setTabReturn('417', 'Registration failed'); + return $this->tab; + } + } + + /** + * Status 200 OK + * Status 406 Not Acceptable + * Status 417 Expectation Failed + **/ + private function setTabReturn($status, $msg) + { + $this->tab['status'] = $status; + $this->tab['msg'] = $msg; + } + +} diff --git a/app/Models/User.php b/app/Models/User.php new file mode 100644 index 0000000..c402d3f --- /dev/null +++ b/app/Models/User.php @@ -0,0 +1,80 @@ +id = $tab['id']; + $this->username = $tab['username']; + $this->is_admin = $tab['is_admin']; + $this->mail = $tab['mail']; + + $res = $this->getReferences($this->id); + $this->references_info = $res['info']; + $this->references_count = $res['count']; + } + + //return true if user exist + public function usernameExist($username) + { + return $this->where('user', '=', $username)->first()->exists; + } + + + public function isExist($post) + { + $bool_username = $this->usernameExist($post['username']); + + if($bool_username) { + $res_bdd = $this::where('user', "=", $post['username'])->firstOrFail(); + $hash = $res_bdd->attributes["hash"]; + $check_pw = password_verify($post['password'], $hash); + if($check_pw) { + $this->setUserInfo([ + 'username' => $post['username'], + 'is_admin' => $res_bdd->attributes["is_admin"], + 'mail' => $res_bdd->attributes["email"], + 'id' => $res_bdd->attributes["id"] + ]); + return true; + } + } + return false; + } + + public function getUserInfo() + { + return + [ + 'id' => $this->id, + 'username' => $this->username, + 'mail' => $this->mail, + 'references_info' => $this->references_info, + 'references_count' => $this->references_count, + 'is_admin' => $this->is_admin + ]; + } + + public function getReferences($user_id) + { + $reference_ref = new ReferenceModel; + $res_bdd = $reference_ref::where('user_id', "=", $user_id)->get(); + $count = 0; + $tmp = []; + foreach ($res_bdd as $key => $value) { + $count++; + $tmp[] = $value->attributes; + } + return ['info' => $tmp, 'count' => $count]; + } +} diff --git a/app/Templates/default/register.html.twig b/app/Templates/default/register.html.twig index bc82d41..9655a6a 100644 --- a/app/Templates/default/register.html.twig +++ b/app/Templates/default/register.html.twig @@ -12,6 +12,15 @@ {% block content %}
+
+
    +
  • Password must be at least 8 characters.
  • +
  • Password must have at least one uppercase letter.
  • +
  • Password must have at least one lowercase letter.
  • +
  • Password must have at least one digit.
  • +
+
+
@@ -24,8 +33,8 @@
- - + +
From 883bd59faa4e93d66a4f1a69049faf020585df79 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 17 May 2018 13:56:41 +0200 Subject: [PATCH 07/69] Login is mandatory to add reference - State management --- app/Controllers/Reference.php | 89 ++++++++++++++++++++++- app/Models/Reference.php | 53 +++++++++++++- app/Templates/default/reference.html.twig | 35 ++++++++- 3 files changed, 170 insertions(+), 7 deletions(-) diff --git a/app/Controllers/Reference.php b/app/Controllers/Reference.php index c044ee3..4637cd6 100644 --- a/app/Controllers/Reference.php +++ b/app/Controllers/Reference.php @@ -65,7 +65,7 @@ function ($key) use ($join_table) { ) ) ); - $model->where('is_displayed', '=', true); + $model->where('status', '=', '2'); $model->orderBy( $order_table . '.' . $order_field, $_SESSION['reference']['sort'] @@ -99,7 +99,8 @@ function ($key) use ($join_table) { 'pagination' => $references->appends($_GET)->render(), 'orderby' => $_SESSION['reference']['orderby'], 'sort' => $_SESSION['reference']['sort'], - 'dyn_refs' => $dyn_refs + 'dyn_refs' => $dyn_refs, + 'user' => $_SESSION['user'] ]); } @@ -127,6 +128,11 @@ public function register(Request $req, Response $res) // alter data $ref_data['country'] = strtolower($ref_data['country']); + if($_SESSION['user'] != null){ + $ref_data['user_id'] = $_SESSION['user']['id']; + }else{ + $ref_data['user_id'] = null; + } // create reference in db if ('' == $ref_data['uuid']) { @@ -140,6 +146,8 @@ public function register(Request $req, Response $res) ); } + + if (false !== $dyn_ref) { $dref = new DynamicReference(); $dynamics = $dref->newInstance(); @@ -177,6 +185,81 @@ public function register(Request $req, Response $res) return $res->withRedirect($this->container->router->pathFor('reference')); } + public function update(Request $req, Response $res) + { + $post = $req->getParsedBody(); + + // clean data + unset($post['g-recaptcha-response']); + unset($post['csrf_name']); + unset($post['csrf_value']); + + $ref_data = $post; + $dyn_data = []; + + $dyn_ref = $this->container->project->getDynamicReferences(); + if (false !== $dyn_ref) { + foreach (array_keys($dyn_ref) as $ref) { + if (isset($post[$ref])) { + $dyn_data[$ref] = (int)$post[$ref]; + unset($ref_data[$ref]); + } + } + } + + // alter data + $ref_data['country'] = strtolower($ref_data['country']); + $ref_data['status'] = 1; + if($_SESSION['user'] != null){ + $ref_data['user_id'] = $_SESSION['user']['id']; + }else{ + $ref_data['user_id'] = null; + } + + //ref + $reference = ReferenceModel::updateOrCreate( + ['id' => $ref_data['id']], + $ref_data + ); + + $ref = new ReferenceModel(); + $model = $ref->newInstance(); + $model->updateStatus($reference['id'], 1); + + //dynamic ref + $dref = new DynamicReference(); + $dynamics = $dref->newInstance(); + $dynamics->setTable($this->container->project->getSlug() . '_reference'); + + $exists = $dynamics->where('reference_id', $reference['id'])->get(); + + if(1 === $exists->count()){ + $dynamics + ->where('reference_id', '=', $reference['id']) + ->update($dyn_data); + } + + // redirect to ok page + return $res->withRedirect($this->container->router->pathFor('profile')); + } + + public function delete(Request $req, Response $res) + { + $post = $req->getParsedBody(); + + $dref = new DynamicReference(); + $dynamics = $dref->newInstance(); + $dynamics->setTable($this->container->project->getSlug() . '_reference'); + $dynamics->where('reference_id', $post['ref_id'])->forceDelete(); + + $ref = new ReferenceModel(); + $model = $ref->newInstance(); + $model->where('id', $post['ref_id'])->forceDelete(); + + // redirect to ok page + return $res->withRedirect($this->container->router->pathFor('profile')); + } + public function filter(Request $req, Response $res, array $args) { $get = $req->getQueryParams(); @@ -194,4 +277,4 @@ public function filter(Request $req, Response $res, array $args) return $res->withRedirect($this->container->router->pathFor('reference')); } -} +} \ No newline at end of file diff --git a/app/Models/Reference.php b/app/Models/Reference.php index f42bf6c..792fd7e 100644 --- a/app/Models/Reference.php +++ b/app/Models/Reference.php @@ -1,10 +1,12 @@ where('is_displayed', '=', true); + return $query->where('status', '=', 2); + } + + public function updateStatus($id, $status) + { + return $this::where('id', '=', $id)->update(['status' => $status]); + } + + + /** + * Find emails + * + * @param reference id + * + * @return an array [user's email, reference's email] + * Return array even if only one of this is null. + **/ + public function findMails($ref_id) + { + $ref_user = new UserModel(); + $user = $ref_user->newInstance(); + + $res_ref = $this::where('id', '=', $ref_id)->first(); + $ref_mail = $res_ref->attributes['email']; + $ref_user_id = $res_ref->attributes['user_id']; + + $res_user = $user::where('id', '=', $ref_user_id)->first(); + $user_mail = $res_user->attributes['email']; + + return + [ + 'user_mail' => $user_mail, + 'ref_mail' => $ref_mail + ]; + } + + + public function statusIntToText($status) + { + switch($status){ + case "0" : return "denied"; + break; + case "1" : return "pending"; + break; + case "2" : return "accepted"; + break; + default : return false; + } } } diff --git a/app/Templates/default/reference.html.twig b/app/Templates/default/reference.html.twig index 053c9b2..5ba7993 100644 --- a/app/Templates/default/reference.html.twig +++ b/app/Templates/default/reference.html.twig @@ -7,11 +7,19 @@ {% endblock %} {% block header %} -{% set myheader= { +{% if user.id != null %} + {% set myheader= { 'title': ' References', 'text': 'We maintain a public database of registered %s installations. You may want register your own!'|format(project_name), -} %} + } %} +{% else %} + {% set myheader= { + 'title': ' References', + 'text': 'We maintain a public database of registered %s installations. + You may want register your own!'|format(project_name), + } %} +{% endif %} {{ parent() }} {% endblock %} @@ -81,6 +89,29 @@ {{ references.render() |raw }} +{% if user.id == null %} + +{% endif %} + From c45a74e56253fb5d823c070b5d9c1b1615e99a30 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Thu, 17 May 2018 14:13:35 +0200 Subject: [PATCH 13/69] migration file for the features --- .../20180503073814_install_users.php | 27 ++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/db/migrations/20180503073814_install_users.php b/db/migrations/20180503073814_install_users.php index 07b809d..e6a4197 100644 --- a/db/migrations/20180503073814_install_users.php +++ b/db/migrations/20180503073814_install_users.php @@ -2,6 +2,7 @@ use Phinx\Migration\AbstractMigration; +use GLPI\Telemetry\Models\Reference as ReferenceModel; class InstallUsers extends AbstractMigration { @@ -25,6 +26,10 @@ class InstallUsers extends AbstractMigration * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. + * + * Your database must be empty. + * From default an admin account is create. There is no way to remove it from the application. + * You should delete it from the database. */ public function up() { @@ -38,30 +43,46 @@ public function up() ->create() ; + $this + ->insert('users', + [ + 'user' => 'admin', + 'hash' => password_hash('admin', PASSWORD_DEFAULT), + 'is_admin' => true, + 'email' => 'admin@admin.fr' + ] + ) + ; + + $table = $this->table('reference'); $table ->addColumn('user_id', 'integer', ['null' => true]) + ->addColumn('status', 'integer', ['default' => 1, 'null' => true]) + ->removeColumn('is_displayed') ->addForeignKey( 'user_id', 'users', 'id', ['delete'=> 'CASCADE','constraint' => 'telemetry_users_reference_id_fkey'] ) - ->addIndex(['user_id'], ['unique' => true]) + ->addIndex(['user_id'], ['unique' => false]) ->update() ; + + } public function down() { - - $table = $this->table('reference'); $table ->dropForeignKey( 'telemetry_users_reference_id_fkey' ) ->removeColumn('user_id', 'integer', ['null' => true]) + ->removeColumn('status', 'integer', ['null' => true]) + ->addColumn('is_displayed', 'boolean', ['default' => false, 'null' => true]) ->update() ; From d03354f13909561f50e8062a5dbfb4d00272fea8 Mon Sep 17 00:00:00 2001 From: Aymeric Lafontaine Date: Tue, 22 May 2018 09:54:35 +0200 Subject: [PATCH 14/69] Refactor code to update profile --- app/Controllers/Connection.php | 22 +--- app/Controllers/Profile.php | 71 ++++++++++++- app/Controllers/Reference.php | 18 ++++ app/Models/Authentication.php | 35 +++++-- app/Models/Register.php | 6 +- app/Models/User.php | 73 +++++--------- app/Templates/default/connection.html.twig | 2 +- app/Templates/default/profile.html.twig | 87 +++++++++++++++- public/css/base.css | 11 +- public/index.php | 10 ++ public/js/profile_action.js | 111 +++++++++++++++++++++ 11 files changed, 362 insertions(+), 84 deletions(-) diff --git a/app/Controllers/Connection.php b/app/Controllers/Connection.php index 7f7681d..ac2da08 100644 --- a/app/Controllers/Connection.php +++ b/app/Controllers/Connection.php @@ -14,7 +14,7 @@ public function view() ]); } - public function send(Request $req, Response $res) + public function send(Request $req, Response $res, $redirect='telemetry') { $post = $req->getParsedBody(); @@ -30,17 +30,13 @@ public function send(Request $req, Response $res) ); $this->container->flash->addMessage( 'success', - 'Welcome '.$post['username'] + 'Welcome '.$post['user'] ); - - $this->setUserSession([ - 'username' => $post['username'], - 'user_info' => $auth->getUserInfo() - ]); + $_SESSION['user'] = $auth->getUser()['attributes']; //redirect - return $res->withRedirect($this->container->router->pathFor('telemetry')); + return $res->withRedirect($this->container->router->pathFor($redirect)); } else { // store a message for user (displayed after redirect) @@ -55,16 +51,6 @@ public function send(Request $req, Response $res) } - public function setUserSession($tab) - { - $_SESSION['user']['id'] = $tab['user_info']['id']; - $_SESSION['user']['username'] = $tab['username']; - $_SESSION['user']['is_admin'] = $tab['user_info']['is_admin']; - $_SESSION['user']['mail'] = $tab['user_info']['mail']; - $_SESSION['user']['references'] = $tab['user_info']['references_info']; - $_SESSION['user']['references_count'] = $tab['user_info']['references_count']; - } - public function disconnect(Request $req, Response $res) { unset($_SESSION['user']); diff --git a/app/Controllers/Profile.php b/app/Controllers/Profile.php index 09a7cf7..daeff6f 100644 --- a/app/Controllers/Profile.php +++ b/app/Controllers/Profile.php @@ -2,6 +2,8 @@ use GLPI\Telemetry\Controllers\ControllerAbstract; use GLPI\Telemetry\Models\Reference as ReferenceModel; +use GLPI\Telemetry\Models\User as UserModel; +use GLPI\Telemetry\Models\Register as RegisterModel; use Slim\Http\Request; use Slim\Http\Response; @@ -36,7 +38,6 @@ public function view(Request $req, Response $res, array $args) $ref = new ReferenceModel(); $ref_model = $ref->newInstance(); $_SESSION['user']['references_count'] = $ref_model->where('user_id', $_SESSION['user']['id'])->get()->count(); - $_SESSION['user']['references'] = $ref_model->where('user_id', $_SESSION['user']['id'])->get(); // manage sorting if (isset($get['orderby'])) { @@ -138,4 +139,72 @@ public function filter(Request $req, Response $res, array $args) return $res->withRedirect($this->container->router->pathFor('profile')); } + + public function userUpdate(Request $req, Response $res) + { + $post = $req->getParsedBody(); + + $user = htmlentities($post['name']); + $mail = htmlentities($post['mail']); + $pass = htmlentities($post['new_password']); + $confirm_pass = htmlentities($post['confirm_password']); + + $user_ref = new UserModel(); + $user_model = $user_ref->newInstance(); + + $register_ref = new RegisterModel(); + $register_model = $register_ref->newInstance(); + + $tmp = + [ + 'username' => $user, + 'email' => $mail + ]; + + if(empty($pass) xor empty($confirm_pass) || $pass !== $confirm_pass || !$register_model->is_valid_password($pass)){ + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'warn', + 'There is a problem with your password. Can\'t update your profile' + ); + // redirect to ok page + return $res->withRedirect($this->container->router->pathFor('profile')); + } elseif(!empty($pass) && !empty($confirm_pass)) { + $tmp['hash'] = password_hash($pass, PASSWORD_DEFAULT); + } + + + + if(!empty($user) && preg_match('/[a-zA-Z]/', $user)){ + + if($user_model->usernameExist($user) && $user != $_SESSION['user']['username']){ + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'warn', + 'This username already exist. Can\'t update your profile' + ); + // redirect to ok page + return $res->withRedirect($this->container->router->pathFor('profile')); + } + $user_model->where('username', '=', $_SESSION['user']['username'])->update($tmp); + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'success', + 'Update done !' + ); + + //reload user informations + $_SESSION['user'] = $user_model->getUser($user)['attributes']; + + } else { + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'warn', + 'You must fill the username field with letters to update your profile.' + ); + } + + // redirect to ok page + return $res->withRedirect($this->container->router->pathFor('profile')); + } } diff --git a/app/Controllers/Reference.php b/app/Controllers/Reference.php index 4637cd6..122c370 100644 --- a/app/Controllers/Reference.php +++ b/app/Controllers/Reference.php @@ -237,6 +237,18 @@ public function update(Request $req, Response $res) $dynamics ->where('reference_id', '=', $reference['id']) ->update($dyn_data); + + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'success', + 'Update done !' + ); + } else { + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'warn', + 'Can\'t update your reference, please contact an administrator.' + ); } // redirect to ok page @@ -256,6 +268,12 @@ public function delete(Request $req, Response $res) $model = $ref->newInstance(); $model->where('id', $post['ref_id'])->forceDelete(); + // store a message for user (displayed after redirect) + $this->container->flash->addMessage( + 'success', + 'Successful deletion !' + ); + // redirect to ok page return $res->withRedirect($this->container->router->pathFor('profile')); } diff --git a/app/Models/Authentication.php b/app/Models/Authentication.php index b508b29..b39b9e5 100644 --- a/app/Models/Authentication.php +++ b/app/Models/Authentication.php @@ -4,16 +4,12 @@ class Authentication extends \Illuminate\Database\Eloquent\Model { - protected $user_info; + protected $table = 'users'; + protected $user; function Authenticate($post) { - $user_ref = new UserModel(); - $user_model = $user_ref->newInstance(); - - - if ($user_model->isExist($post)) { - $this->user_info = $user_model->getUserInfo(); + if ($this->isExist($post) != false) { return true; } else { return false; @@ -21,8 +17,29 @@ function Authenticate($post) { } - public function getUserInfo() + public function getUser() + { + return $this->user; + } + + + public function isExist($post) { - return $this->user_info; + $user_ref = new UserModel(); + $user_model = $user_ref->newInstance(); + $bool_username = $user_model->usernameExist($post['user']); + + if($bool_username) { + $user_obj = $user_model::where('username', '=', $post['user'])->first(); + + $check_pw = password_verify($post['password'], $user_obj->hash); + if($check_pw) { + $this->user = $user_obj; + $user_obj->setUserInfo(); + return $user_obj; + } + } + return false; } + } diff --git a/app/Models/Register.php b/app/Models/Register.php index 58da5bd..d0aa534 100644 --- a/app/Models/Register.php +++ b/app/Models/Register.php @@ -33,12 +33,12 @@ public function checkRegister($post) /** - * Length 6 chars + * Length 8 chars * At least one lowercase letter * At least one uppercase letter * At least one digit **/ - private function is_valid_password($password) { + public function is_valid_password($password) { return preg_match('#^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$#', $password) ? TRUE : FALSE; } @@ -57,7 +57,7 @@ private function insertRegister($user, $hash, $mail, $admin) $tmp = [ - 'user' => $user, + 'username' => $user, 'hash' => $hash, 'email' => $mail, 'is_admin' => $admin diff --git a/app/Models/User.php b/app/Models/User.php index c402d3f..b827e15 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -7,49 +7,15 @@ class User extends \Illuminate\Database\Eloquent\Model protected $table = 'users'; protected $id; protected $username; - protected $is_admin; //Boolean - protected $mail; - protected $references_info; protected $references_count; + protected $guarded = [ + '' + ]; + protected $fillable = array('username', 'is_admin', 'mail'); - public function setUserInfo($tab) + public function setUserInfo() { - $this->id = $tab['id']; - $this->username = $tab['username']; - $this->is_admin = $tab['is_admin']; - $this->mail = $tab['mail']; - - $res = $this->getReferences($this->id); - $this->references_info = $res['info']; - $this->references_count = $res['count']; - } - - //return true if user exist - public function usernameExist($username) - { - return $this->where('user', '=', $username)->first()->exists; - } - - - public function isExist($post) - { - $bool_username = $this->usernameExist($post['username']); - - if($bool_username) { - $res_bdd = $this::where('user', "=", $post['username'])->firstOrFail(); - $hash = $res_bdd->attributes["hash"]; - $check_pw = password_verify($post['password'], $hash); - if($check_pw) { - $this->setUserInfo([ - 'username' => $post['username'], - 'is_admin' => $res_bdd->attributes["is_admin"], - 'mail' => $res_bdd->attributes["email"], - 'id' => $res_bdd->attributes["id"] - ]); - return true; - } - } - return false; + $this->references_count = $this->getReferencesCount($this->id); } public function getUserInfo() @@ -59,22 +25,29 @@ public function getUserInfo() 'id' => $this->id, 'username' => $this->username, 'mail' => $this->mail, - 'references_info' => $this->references_info, 'references_count' => $this->references_count, 'is_admin' => $this->is_admin ]; } - public function getReferences($user_id) + public function getReferencesCount($user_id) + { + return ReferenceModel::where('user_id', "=", $user_id)->get()->count(); + } + + public function getUser($name) + { + $test = $this::where('username', '=', $name)->first(); + return $test; + } + + //return true if user exist + public function usernameExist($username) { - $reference_ref = new ReferenceModel; - $res_bdd = $reference_ref::where('user_id', "=", $user_id)->get(); - $count = 0; - $tmp = []; - foreach ($res_bdd as $key => $value) { - $count++; - $tmp[] = $value->attributes; + if(! is_null($this->where('username', '=', $username)->first())){ + return true; + }else{ + return false; } - return ['info' => $tmp, 'count' => $count]; } } diff --git a/app/Templates/default/connection.html.twig b/app/Templates/default/connection.html.twig index e531424..11bb4eb 100644 --- a/app/Templates/default/connection.html.twig +++ b/app/Templates/default/connection.html.twig @@ -15,7 +15,7 @@
- +
diff --git a/app/Templates/default/profile.html.twig b/app/Templates/default/profile.html.twig index 321f812..5164640 100644 --- a/app/Templates/default/profile.html.twig +++ b/app/Templates/default/profile.html.twig @@ -27,16 +27,18 @@ Mail Number of references Admin + {{user_session['username']}} - {{user_session['mail']}} + {{user_session['email']}} {{user_session['references_count']}} {% if user_session['is_admin'] == true %}Yes {% else %}No {% endif %} + @@ -119,7 +121,88 @@ + + + + {{ csrf.field | raw }} + + +