Skip to content

Commit

Permalink
Merge pull request #18 from douglas-88/projeto-final
Browse files Browse the repository at this point in the history
Implementando Migration User and Role
  • Loading branch information
douglas-88 authored Feb 19, 2020
2 parents e694ba6 + 6ada04e commit c27992a
Show file tree
Hide file tree
Showing 10 changed files with 123 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/Controller/Admin/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
{
Expand Down
2 changes: 1 addition & 1 deletion app/Functions/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

$admin = new TwigFunction("admin",function() {

return (new \App\Model\Admin())->user();
return (new \App\Model\User())->user();

});

Expand Down
4 changes: 2 additions & 2 deletions app/Model/Admin.php → app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"];
Expand Down
4 changes: 2 additions & 2 deletions phinx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions src/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Core;

use App\Model\Admin;
use App\Model\User;
use Core\Model;
use Core\Password;

Expand All @@ -19,15 +19,15 @@ 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;
}
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;
Expand Down
2 changes: 1 addition & 1 deletion src/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use Phinx\Migration\AbstractMigration;

class FirstMigration extends AbstractMigration
class CreateRoleTable extends AbstractMigration
{
/**
* Change Method.
Expand Down Expand Up @@ -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();
}
}
45 changes: 45 additions & 0 deletions src/db/migrations/20200219004255_create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

use Phinx\Migration\AbstractMigration;

class CreateUsersTable extends AbstractMigration
{
/**
* Change Method.
*
* Write your reversible migrations using this method.
*
* More information on writing migrations is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
*
* The following commands can be used in this method and Phinx will
* automatically reverse them when rolling back:
*
* createTable
* renameTable
* addColumn
* addCustomColumn
* renameColumn
* addIndex
* addForeignKey
*
* Any other destructive changes will result in an error when trying to
* rollback the migration.
*
* Remember to call "create()" or "update()" and NOT "save()" when working
* with the Table class.
*/
public function change()
{
$table = $this->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();
}
}
29 changes: 29 additions & 0 deletions src/db/seeds/RolesSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php


use Phinx\Seed\AbstractSeed;

class RolesSeeder extends AbstractSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$data = [
[
'name' => 'admin',
'description' => "Administrador do Sistema",
'created' => date('Y-m-d H:i:s')
]
];

$roles = $this->table('roles');
$roles->insert($data)->saveData();
}
}
33 changes: 33 additions & 0 deletions src/db/seeds/UserSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php


use Phinx\Seed\AbstractSeed;

class UserSeeder extends AbstractSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$data = [
[
'name' => 'admin',
'email' => '[email protected]',
'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();
}
}

0 comments on commit c27992a

Please sign in to comment.