From 6ada04efdc67c4bb1a15b8e856c8754088d7d669 Mon Sep 17 00:00:00 2001 From: Douglas Date: Tue, 18 Feb 2020 22:25:03 -0300 Subject: [PATCH] Implementando Migration User and Role --- app/Controller/Admin/LoginController.php | 4 +- app/Functions/twig.php | 2 +- app/Model/{Admin.php => User.php} | 4 +- phinx.yml | 4 +- src/Login.php | 6 +-- src/Middleware.php | 2 +- ...p => 20200219004035_create_role_table.php} | 11 +++-- .../20200219004255_create_users_table.php | 45 +++++++++++++++++++ src/db/seeds/RolesSeeder.php | 29 ++++++++++++ src/db/seeds/UserSeeder.php | 33 ++++++++++++++ 10 files changed, 123 insertions(+), 17 deletions(-) rename app/Model/{Admin.php => User.php} (78%) rename src/db/migrations/{20200210185041_first_migration.php => 20200219004035_create_role_table.php} (71%) mode change 100755 => 100644 create mode 100644 src/db/migrations/20200219004255_create_users_table.php create mode 100644 src/db/seeds/RolesSeeder.php create mode 100644 src/db/seeds/UserSeeder.php diff --git a/app/Controller/Admin/LoginController.php b/app/Controller/Admin/LoginController.php index 3ef718c..43b36c5 100644 --- a/app/Controller/Admin/LoginController.php +++ b/app/Controller/Admin/LoginController.php @@ -11,7 +11,7 @@ use Core\Validate; use Slim\Http\Request; use Slim\Http\Response; -use App\Model\Admin; +use App\Model\User; class LoginController extends Controller { @@ -33,7 +33,7 @@ public function create() } /** - * Processa Formulário de criação + * Processa Formulário de Login */ public function store(Request $request, Response $response) { diff --git a/app/Functions/twig.php b/app/Functions/twig.php index c278eac..efdecc9 100755 --- a/app/Functions/twig.php +++ b/app/Functions/twig.php @@ -21,7 +21,7 @@ $admin = new TwigFunction("admin",function() { - return (new \App\Model\Admin())->user(); + return (new \App\Model\User())->user(); }); diff --git a/app/Model/Admin.php b/app/Model/User.php similarity index 78% rename from app/Model/Admin.php rename to app/Model/User.php index d589edb..ac8f72c 100755 --- a/app/Model/Admin.php +++ b/app/Model/User.php @@ -6,9 +6,9 @@ use Core\Model; -class Admin extends Model +class User extends Model { - protected $table = "admin"; + protected $table = "users"; public function user(){ $id = $_SESSION["loginInfo"]["idUser"]; diff --git a/phinx.yml b/phinx.yml index 6d34be4..152fc11 100755 --- a/phinx.yml +++ b/phinx.yml @@ -17,9 +17,9 @@ environments: development: adapter: mysql host: localhost - name: projeto_resgate + name: twig_slim user: root - pass: '1475' + pass: 'nokia5233' port: 3306 charset: utf8 diff --git a/src/Login.php b/src/Login.php index 87a2b1b..aea2488 100755 --- a/src/Login.php +++ b/src/Login.php @@ -3,7 +3,7 @@ namespace Core; -use App\Model\Admin; +use App\Model\User; use Core\Model; use Core\Password; @@ -19,7 +19,7 @@ public function __construct($type = null) public function login($data){ $config = (object) Load::file("/config.php"); - $user = (new Admin())->select()->where("email",$data["email"])->first(); + $user = (new User())->select()->where("email",$data["email"])->first(); if(!$user){ return false; @@ -27,7 +27,7 @@ public function login($data){ if(Password::verify($data["password"],$user->password)){ $_SESSION["loginInfo"]["loggedIn"] = true; $_SESSION["loginInfo"]["idUser"] = $user->id; - $_SESSION["loginInfo"]["roleUser"] = $user->role; + $_SESSION["loginInfo"]["roleUser"] = $user->role_id; return true; }else{ return false; diff --git a/src/Middleware.php b/src/Middleware.php index 2b055bb..87c8802 100644 --- a/src/Middleware.php +++ b/src/Middleware.php @@ -3,7 +3,7 @@ namespace Core; -use App\Model\Admin; +use App\Model\User; use Slim\Http\Request; use Slim\Http\Response; use Core\Login; diff --git a/src/db/migrations/20200210185041_first_migration.php b/src/db/migrations/20200219004035_create_role_table.php old mode 100755 new mode 100644 similarity index 71% rename from src/db/migrations/20200210185041_first_migration.php rename to src/db/migrations/20200219004035_create_role_table.php index 84ac65b..53618a3 --- a/src/db/migrations/20200210185041_first_migration.php +++ b/src/db/migrations/20200219004035_create_role_table.php @@ -2,7 +2,7 @@ use Phinx\Migration\AbstractMigration; -class FirstMigration extends AbstractMigration +class CreateRoleTable extends AbstractMigration { /** * Change Method. @@ -31,11 +31,10 @@ class FirstMigration extends AbstractMigration */ public function change() { - $table = $this->table("disciplinas"); - $table->addColumn("nome","string",["limit" => 50]); - $table->addColumn("descricao","text"); - $table->addColumn("criado_por","integer",["limit" => 10]); - $table->addColumn("data_criacao",'timestamp', ['default' => 'CURRENT_TIMESTAMP']); + $table = $this->table("roles"); + $table->addColumn("name","string",["limit" => 50]); + $table->addColumn("description","text"); + $table->addColumn("created",'timestamp', ['default' => 'CURRENT_TIMESTAMP']); $table->create(); } } diff --git a/src/db/migrations/20200219004255_create_users_table.php b/src/db/migrations/20200219004255_create_users_table.php new file mode 100644 index 0000000..159d8d6 --- /dev/null +++ b/src/db/migrations/20200219004255_create_users_table.php @@ -0,0 +1,45 @@ +table("users"); + $table->addColumn("name","string",["limit" => 255]); + $table->addColumn("password","string",["limit" => 60]); + $table->addColumn("email","string",["limit" => 70]); + $table->addColumn("phone","string",["limit" => 11]); + $table->addColumn("avatar","string",["limit" => 300]); + $table->addColumn('role_id', 'integer', ['null' => false]); + $table->addForeignKey('role_id', 'roles', 'id'); + $table->addColumn("created",'timestamp', ['default' => 'CURRENT_TIMESTAMP']); + $table->create(); + } +} diff --git a/src/db/seeds/RolesSeeder.php b/src/db/seeds/RolesSeeder.php new file mode 100644 index 0000000..5736c87 --- /dev/null +++ b/src/db/seeds/RolesSeeder.php @@ -0,0 +1,29 @@ + 'admin', + 'description' => "Administrador do Sistema", + 'created' => date('Y-m-d H:i:s') + ] + ]; + + $roles = $this->table('roles'); + $roles->insert($data)->saveData(); + } +} diff --git a/src/db/seeds/UserSeeder.php b/src/db/seeds/UserSeeder.php new file mode 100644 index 0000000..a1c15a8 --- /dev/null +++ b/src/db/seeds/UserSeeder.php @@ -0,0 +1,33 @@ + 'admin', + 'email' => 'admin@gmail.com', + 'password' => '$2y$12$o0vmN5EMkgjEiBxtRot3T.JN.Yvhu1TxYpI3qfG0Xl4QvxImE0.ZC', + 'phone' => '21993725886', + 'avatar' => '', + 'role_id' => 1, + 'created' => date('Y-m-d H:i:s') + ] + ]; + + $users = $this->table('users'); + $users->insert($data)->saveData(); + } +}