diff --git a/app/Controller/Admin/AdminController.php b/app/Controller/Admin/AdminController.php new file mode 100644 index 0000000..dc0df91 --- /dev/null +++ b/app/Controller/Admin/AdminController.php @@ -0,0 +1,54 @@ +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(); + + } +} \ No newline at end of file diff --git a/app/Controller/Admin/PainelController.php b/app/Controller/Admin/PainelController.php new file mode 100644 index 0000000..265e7d7 --- /dev/null +++ b/app/Controller/Admin/PainelController.php @@ -0,0 +1,16 @@ +view("admin/painel",["title" => "Painel Admin"]); + } + +} \ No newline at end of file diff --git a/app/Controller/UserController.php b/app/Controller/UserController.php index d5a28b5..5103ed7 100755 --- a/app/Controller/UserController.php +++ b/app/Controller/UserController.php @@ -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; @@ -28,7 +30,7 @@ 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() ] @@ -36,6 +38,7 @@ public function index(){ } + /** * Exibe o formulário de criação de usuário * Método de requisição: GET diff --git a/app/Functions/twig.php b/app/Functions/twig.php index 55a0fc3..c278eac 100644 --- a/app/Functions/twig.php +++ b/app/Functions/twig.php @@ -4,6 +4,7 @@ use Core\Flash; use Core\Validate; + $erros = new TwigFunction("erros",function($index){ echo Flash::get($index); }); @@ -18,4 +19,10 @@ }); -return [$erros,$sent,$message]; \ No newline at end of file +$admin = new TwigFunction("admin",function() { + + return (new \App\Model\Admin())->user(); + +}); + +return [$erros,$sent,$message,$admin]; \ No newline at end of file diff --git a/app/Model/Admin.php b/app/Model/Admin.php new file mode 100644 index 0000000..8ce902f --- /dev/null +++ b/app/Model/Admin.php @@ -0,0 +1,19 @@ +select()->where("id",$id)->first(); + + return $user; + } +} \ No newline at end of file diff --git a/app/Model/User.php b/app/Model/User.php index e0ff83b..5084dc1 100755 --- a/app/Model/User.php +++ b/app/Model/User.php @@ -6,5 +6,5 @@ class User extends Model { - protected $table = "user"; + protected $table = "users"; } \ No newline at end of file diff --git a/app/traits/Links.php b/app/traits/Links.php index cabfcc6..e97dfe2 100644 --- a/app/traits/Links.php +++ b/app/traits/Links.php @@ -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 = "
  • [1]
  • "; + $links = "
  • pageRequest()}1\">[1]
  • "; $links .= "
  • - + pageRequest()}{$previous}\" aria-label=\"Previous\"> « Previous @@ -34,12 +38,12 @@ private function next(){ $next = $this->page + 1; $links = "
  • - + pageRequest()}{$next}\" aria-label=\"Next\"> » Next
  • "; - $links .= "
  • pages}\">[{$this->pages}]
  • "; + $links .= "
  • pageRequest()}{$this->pages}\">[{$this->pages}]
  • "; return $links; } @@ -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 .= "
  • {$i}
  • "; + $links .= "
  • pageRequest()}{$i}\">{$i}
  • "; } } diff --git a/app/traits/Read.php b/app/traits/Read.php index 15a543d..af81d89 100644 --- a/app/traits/Read.php +++ b/app/traits/Read.php @@ -4,6 +4,7 @@ namespace App\traits; use App\Model\User; +use Core\Model; use Core\Paginate; @@ -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; } @@ -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(); diff --git a/app/traits/Validations.php b/app/traits/Validations.php index 0ef81cb..49d83d4 100644 --- a/app/traits/Validations.php +++ b/app/traits/Validations.php @@ -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")); } } @@ -70,4 +70,8 @@ public function hasErros(){ return !empty($this->erros); } + public function getErros(){ + return $this->erros; + } + } \ No newline at end of file diff --git a/bootstrap.php b/bootstrap.php index eaee09f..59493b9 100755 --- a/bootstrap.php +++ b/bootstrap.php @@ -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); diff --git a/config.php b/config.php new file mode 100644 index 0000000..fc8643c --- /dev/null +++ b/config.php @@ -0,0 +1,16 @@ + [ + "admin" => [ + "loggedIn" => "admin_login", + "redirect" => "/admin", + "idLoggedIn" => "id_admin" + ], + "user" => [ + "loggedIn" => "user_login", + "redirect" => "/", + "idLoggedIn" => "id_user" + ] + ] +]; \ No newline at end of file diff --git a/public/index.php b/public/index.php index c6b4b17..090a10b 100755 --- a/public/index.php +++ b/public/index.php @@ -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(); \ No newline at end of file diff --git a/src/Login.php b/src/Login.php new file mode 100644 index 0000000..1ab67c0 --- /dev/null +++ b/src/Login.php @@ -0,0 +1,41 @@ +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; + } +} \ No newline at end of file diff --git a/src/Password.php b/src/Password.php new file mode 100644 index 0000000..897c81a --- /dev/null +++ b/src/Password.php @@ -0,0 +1,28 @@ + 12]); + } + + /** + * Verifica a Senha Encriptada + * @param $password + * @return bool + */ + public static function verify($password,$hash):bool{ + + return password_verify($password,$hash); + } +} \ No newline at end of file diff --git a/views/admin/login.html b/views/admin/login.html new file mode 100644 index 0000000..d030979 --- /dev/null +++ b/views/admin/login.html @@ -0,0 +1,18 @@ +{% extends 'master.html' %} + +{% block master %} +
    +
    + +

    Login

    + + + {{ erros("email") }} + + + {{ erros("senha") }} + +

    © Douglas

    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/views/admin/master.html b/views/admin/master.html new file mode 100755 index 0000000..a473dfb --- /dev/null +++ b/views/admin/master.html @@ -0,0 +1,26 @@ + + + + + + + {{ title }} + + + + + + + + +
    + {% block master %}{% endblock %} +
    + + + + + + + + \ No newline at end of file diff --git a/views/admin/painel.html b/views/admin/painel.html new file mode 100644 index 0000000..a570d59 --- /dev/null +++ b/views/admin/painel.html @@ -0,0 +1,7 @@ +{% extends 'master.html' %} + +{% block master %} +

    {{ title }}

    +

    Bem vindo {{ admin().nome }}

    +Sair do sistema +{% endblock %} \ No newline at end of file