Skip to content

Commit

Permalink
Merge pull request #16 from douglas-88/dev
Browse files Browse the repository at this point in the history
Login test
  • Loading branch information
douglas-88 authored Feb 12, 2020
2 parents 670ab70 + f1daba7 commit 476ee5e
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 11 deletions.
54 changes: 54 additions & 0 deletions app/Controller/Admin/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php


namespace App\Controller\Admin;
use App\Model\User;
use App\Model\Admin;
use Core\Controller;
use Core\Redirect;
use Core\Validate;
use Core\Login;

class AdminController extends Controller
{
public function index(){
session_destroy();
var_dump($_SESSION);
$this->view("admin/login");
}

public function store(){
$validate = new Validate();
$data = $validate->validate([
"email" => "required:email",
"senha" => "required"
]);

if($validate->hasErros()){
foreach($data as $field => $value){
flash("post_".$field,$data[$field]);
}
back();
exit;
}

$login = new Login("admin");
$loginIn = $login->login($data,new Admin);

if($loginIn){
Redirect::redirect("/painel");
exit;
}else{
Redirect::redirect("/admin");
exit;
}

}

public function destroy(){

$login = new Login("admin");
$login->logout();

}
}
16 changes: 16 additions & 0 deletions app/Controller/Admin/PainelController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


namespace App\Controller\Admin;

use Core\Controller;


class PainelController extends Controller
{
public function index(){

$this->view("admin/painel",["title" => "Painel Admin"]);
}

}
5 changes: 4 additions & 1 deletion app/Controller/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

use App\Model\User;
use Core\Controller;
use Core\Login;
use Core\Password;
use Core\Validate;
use Slim\Http\Request;
use Slim\Http\Response;
Expand All @@ -28,14 +30,15 @@ public function index(){

$this->view("home",
[
"users" => $this->users->select()->busca("nome,email")->paginate(5)->get(),
"users" => $this->users->select()->busca("nome,email")->paginate(1)->get(),
"title" => "Listando Usuários",
"links" => $this->users->links()
]
);

}


/**
* Exibe o formulário de criação de usuário
* Método de requisição: GET
Expand Down
9 changes: 8 additions & 1 deletion app/Functions/twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Core\Flash;
use Core\Validate;


$erros = new TwigFunction("erros",function($index){
echo Flash::get($index);
});
Expand All @@ -18,4 +19,10 @@

});

return [$erros,$sent,$message];
$admin = new TwigFunction("admin",function() {

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

});

return [$erros,$sent,$message,$admin];
19 changes: 19 additions & 0 deletions app/Model/Admin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php


namespace App\Model;

use Core\Model;


class Admin extends Model
{
protected $table = "admin";

public function user(){
$id = $_SESSION["id_admin"];
$user = $this->select()->where("id",$id)->first();

return $user;
}
}
2 changes: 1 addition & 1 deletion app/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class User extends Model
{
protected $table = "user";
protected $table = "users";
}
14 changes: 9 additions & 5 deletions app/traits/Links.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ trait Links
{
protected $maxLinks = 4;

private function pageRequest(){
return (!busca()) ? "?page=" : "?s=".busca()."&page=";
}

private function previous(){

if($this->page > 1){

$previous = $this->page - 1;

$links = "<li class=\"page-item\"><a class=\"page-link\" href=\"?page=1\">[1]</a></li>";
$links = "<li class=\"page-item\"><a class=\"page-link\" href=\"{$this->pageRequest()}1\">[1]</a></li>";
$links .= "<li class=\"page-item\">
<a class=\"page-link\" href=\"?page={$previous}\" aria-label=\"Previous\">
<a class=\"page-link\" href=\"{$this->pageRequest()}{$previous}\" aria-label=\"Previous\">
<span aria-hidden=\"true\">&laquo;</span>
<span class=\"sr-only\">Previous</span>
</a>
Expand All @@ -34,12 +38,12 @@ private function next(){
$next = $this->page + 1;

$links = "<li class=\"page-item\">
<a class=\"page-link\" href=\"?page={$next}\" aria-label=\"Next\">
<a class=\"page-link\" href=\"{$this->pageRequest()}{$next}\" aria-label=\"Next\">
<span aria-hidden=\"true\">&raquo;</span>
<span class=\"sr-only\">Next</span>
</a>
</li>";
$links .= "<li class=\"page-item\"><a class=\"page-link\" href=\"?page={$this->pages}\">[{$this->pages}]</a></li>";
$links .= "<li class=\"page-item\"><a class=\"page-link\" href=\"{$this->pageRequest()}{$this->pages}\">[{$this->pages}]</a></li>";

return $links;
}
Expand All @@ -58,7 +62,7 @@ public function links()
for($i = $this->page - $this->maxLinks;$i <= $this->page + $this->maxLinks;$i++){
$active = ($this->page == $i) ? "active" : "";
if($i > 0 && $i <= $this->pages){
$links .= "<li class=\"page-item {$active}\"><a class=\"page-link \" href=\"?page={$i}\">{$i}</a></li>";
$links .= "<li class=\"page-item {$active}\"><a class=\"page-link \" href=\"{$this->pageRequest()}{$i}\">{$i}</a></li>";

}
}
Expand Down
5 changes: 3 additions & 2 deletions app/traits/Read.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace App\traits;

use App\Model\User;
use Core\Model;
use Core\Paginate;


Expand All @@ -15,7 +16,7 @@ trait Read
* @param string $fields
* @return User
*/
public function select($fields = "*"):User{
public function select($fields = "*"){
$this->sql = "SELECT {$fields} FROM {$this->table}";
return $this;
}
Expand Down Expand Up @@ -65,7 +66,7 @@ public function get():array{
* @return $this
* @throws \Exception
*/
public function where():User{
public function where():Model{
$num_args = func_num_args();
$args = func_get_args();

Expand Down
6 changes: 5 additions & 1 deletion app/traits/Validations.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ trait Validations
*/
protected function required($field){

if(empty($_POST[$field])){
if(empty($_POST[$field]) OR !isset($_POST[$field])){
$this->erros[$field][] = flash($field, error("Favor preencha esse campo"));
}
}
Expand Down Expand Up @@ -70,4 +70,8 @@ public function hasErros(){
return !empty($this->erros);
}

public function getErros(){
return $this->erros;
}

}
10 changes: 10 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@
$service = new \App\Controller\UserController;
return $service;
};
$container['AdminController'] = function ($container) {
$service = new \App\Controller\Admin\AdminController;
return $service;
};

$container["PainelController"] = function ($container) {
$service = new \App\Controller\Admin\PainelController;
return $service;
};

$whoops = new Whoops();
$whoops->run($container);
16 changes: 16 additions & 0 deletions config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

return [
"login" => [
"admin" => [
"loggedIn" => "admin_login",
"redirect" => "/admin",
"idLoggedIn" => "id_admin"
],
"user" => [
"loggedIn" => "user_login",
"redirect" => "/",
"idLoggedIn" => "id_user"
]
]
];
6 changes: 6 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
$app->get("/users/delete/{id}","UserController:delete");


$app->get("/admin","AdminController:index");
$app->post("/login","AdminController:store");
$app->get("/logout","AdminController:destroy");
$app->get("/painel","PainelController:index");


$app->run();
41 changes: 41 additions & 0 deletions src/Login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php


namespace Core;

use Core\Model;
use Core\Password;

class Login
{
private $type;

public function __construct($type)
{
$this->type = $type;
}

public function login($data,Model $model){

$config = (object) Load::file("/config.php")["login"][$this->type];
$user = $model->select()->where("email",$data["email"])->first();

if(!$user){
return false;
}
if(Password::verify($data["senha"],$user->senha)){
$_SESSION[$config->loggedIn] = true;
$_SESSION[$config->idLoggedIn] = $user->id;
return true;
}else{
return false;
}
}

public function logout(){
session_destroy();

Redirect::redirect("/admin");
exit;
}
}
28 changes: 28 additions & 0 deletions src/Password.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php


namespace Core;


class Password
{
/**
* Retorna a Senha Encriptada
* @param $password
* @return string
*/
public static function make($password):string{

return password_hash($password, PASSWORD_BCRYPT, ["cost" => 12]);
}

/**
* Verifica a Senha Encriptada
* @param $password
* @return bool
*/
public static function verify($password,$hash):bool{

return password_verify($password,$hash);
}
}
18 changes: 18 additions & 0 deletions views/admin/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{% extends 'master.html' %}

{% block master %}
<div class="col-6 text-center">
<form class="form-signin" action="/login" method="POST">
<img class="mb-4" src="../../assets/brand/bootstrap-solid.svg" alt="" width="72" height="72">
<h1 class="h3 mb-3 font-weight-normal">Login</h1>
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" id="inputEmail" class="form-control" name="email" placeholder="Email" value="{{ sent('email') }}"required autofocus>
{{ erros("email") }}
<label for="inputPassword" class="sr-only">Senha</label>
<input type="password" id="inputPassword" class="form-control" name="senha" placeholder="Senha" value="{{ sent('senha') }}"required>
{{ erros("senha") }}
<button class="btn btn-lg btn-primary btn-block" type="submit">ENTRAR</button>
<p class="mt-5 mb-3 text-muted">&copy; Douglas</p>
</form>
</div>
{% endblock %}
Loading

0 comments on commit 476ee5e

Please sign in to comment.