Перенаправить пользователя на другую страницу в зависимости от роли в базе данных Slim 3, Twig & Eloquent - PullRequest
0 голосов
/ 18 октября 2018

Я затрудняюсь, перенаправляя пользователей на нужную страницу при входе в систему. Я имею в виду, что если пользователь в базе данных имеет роль администратора, его следует перенаправить на admin.twig (но это может произойтину, будь HTML или PHP файл, я просто использую веточку) или если есть клиент, его нужно отправить на customer.twig и т. д ....

Допустим, в моей базе данных есть следующие пользователи:

 id name email           password  role

 1  Sam  Johndoe@gmail.com pass123   0

 2  John Johndoe@gmail.com pass123   2

Допустим, admin = 0, customer = 1 и client = 2

Так что в приведенном выше примере Джон будет администратором, а Сэм - клиентом

Вот моя файловая структура

   ├── Slim-3-app
     ├── app
       ├── Auth
           Auth.php
       ├── Controllers
         |──Auth
            AuthController.php
       ├── Middleware
       ├── Models
             User.php
       ├── Validation
       ├── routes.php
     ├── bootstrap
         app.php
     ├── resources
       ├── views
         home.twig
         admin.twig
         client.twig
         csutomer.twig    

Routes.php:

$app->get('/', 'HomeController:index')->setName('home');
$app->get('/admin', 'AdminController:adminControllerFunction')->setName('admin');
$app->get('/customer', 'CustomerController:customerControllerFunction')->setName('customer');
$app->get('/client', 'ClientController:clientControllerFunction')->setName('client');

User.php:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = [
        'name',
        'email',
        'password',
        'role'
    ];

    public function setPassword($password)
    {
        $this->update([
            'password' => password_hash($password, PASSWORD_DEFAULT)
        ]);
    }
}

Auth.php:

<?php

namespace App\Auth;

use App\Models\User;

class Auth
{
    public function user()
    {
        if (isset($_SESSION['user'])) {
            return User::find($_SESSION['user']);
        }
    }

    public function check()
    {
        if (isset($_SESSION['user'])) {
            return isset($_SESSION['user']);
        }
    }

    public function checkRole($role)
    {
        $role = User::where('role' , $role)->first();

    }


    public function attempt($email, $password)
    {
        // get the data of the attempted user
        $user = User::where('email' , $email)->first();


        // check if the user exists
        if (!$user) {
            return false;
        }


        // check if password is valid
        if (password_verify($password, $user->password)) {
            $_SESSION['user'] = $user->id;
            return true;
        }

        return false;
    }

    public function logout()
    {
        unset($_SESSION['user']);
    }
}

AuthController.php:

<?php

namespace App\Controllers\Auth;

use App\Models\User;
use App\Controllers\Controller;
use Respect\Validation\Validator as v;
class AuthController extends Controller
{
    public function getSignOut($request, $response)
    {
        $this->auth->logout();
        // flash message
        $this->flash->addMessage('error', 'You have been signed out');
        return $response->withRedirect($this->router->pathFor('home'));
    }
    // signin controller
    public function getSignIn($request, $response)
    {
        return $this->view->render($response, 'auth/signin.twig');
    }

    public function postSignIn($request, $response)
    {
        // use the attempt class
        $auth = $this->auth->attempt(
            $request->getParam('email'),
            $request->getParam('password'),
            $request->getParam('role')
        );

        if (!$auth) {
            // flash message
            $this->flash->addMessage('error', 'Could not sign you in with those details');

            return $response->withRedirect($this->router->pathFor('auth.signin'));
        }

        // flash message
        $this->flash->addMessage('success', 'Successfully signed in');
        return $response->withRedirect($this->router->pathFor('home'));

        // if(checkrole() = 0 ){
        //  $this->flash->addMessage('success', 'Admin Successfully signed in');
        //  return $response->withRedirect($this->router->pathFor('home'));
        // } else {
        //  $this->flash->addMessage('success', 'Admin Successfully signed in');
        //  return $response->withRedirect($this->router->pathFor('home'));
        // }
        // This does not work but I need something like this
    }

    // signup controller
    public function getSignUp($request, $response)
    {
        return $this->view->render($response, 'auth/signup.twig');
    }

    public function postSignUp($request, $response)
    {

        $validation = $this->validator->validate($request, [
            'email' => v::noWhitespace()->notEmpty()->emailAvailable(),
            'name' => v::notEmpty()->alpha(),
            'password' => v::noWhitespace()->notEmpty(),
        ]);

        if ($validation->failed()) {
            return $response->withRedirect($this->router->pathFor('auth.signup'));
        }

        $user = User::create([
            'email' => $request->getParam('email'),
            'name' => $request->getParam('name'),
            'password' => password_hash($request->getParam('password'), PASSWORD_DEFAULT),
            'role' => $request->getParam('role'),
        ]);

        // flash a message
        $this->flash->addMessage('info', 'You have been signed up');

        // log the user directly in
        $this->auth->attempt($user->email, $request->getParam('password'));

        return $response->withRedirect($this->router->pathFor('home'));
    }
}

app.php

<?php

use Respect\Validation\Validator as v;

<?php

use Respect\Validation\Validator as v;

session_start();

require __DIR__ . '/../vendor/autoload.php';

$app = new \Slim\App([
    'settings' => [
        'displayErrorDetails' => true,
        'db' => [
            'driver'    => 'mysql',
            'host'      => 'localhost',
            'database'  => 'eshop',
            'username'  => 'root',
            'password'  => '',
            'charset'   => 'utf8',
            'collation' => 'utf8_unicode_ci',
            'prefix'    => '',
        ]
    ],

]);

$container = $app->getContainer();

// setup illuminate (Model generator)
$capsule = new Illuminate\Database\Capsule\Manager;
$capsule->addConnection($container['settings']['db']);
$capsule->setAsGlobal();
$capsule->bootEloquent();

$container['validator'] = function ($container) {
    return new App\Validation\Validator;
};

// add Illuminate package
$container['db'] = function ($container) use ($capsule){
    return $capsule;
};

// add Auth class
$container['auth'] = function($container){
    return new \App\Auth\Auth;
};

// add Slim Flash messages
$container['flash'] = function () {
    return new \Slim\Flash\Messages();
};

// add views to the application
$container['view'] = function($container){
    $view = new \Slim\Views\Twig(__DIR__ . '/../resources/views', [
        'cache' => false,
    ]);

    $view->addExtension(new Slim\Views\TwigExtension(
        $container->router,
        $container->request->getUri()
    ));

    // let the view have access to auth controller
    $view->getEnvironment()->addGlobal('auth', [
        'check' => $container->auth->check(),
        'user' => $container->auth->user()
    ]);

    // let the view have access to flash messages
    $view->getEnvironment()->addGlobal('flash', $container->flash);

    return $view;
};

$container['HomeController'] = function($container){
    return new \App\Controllers\HomeController($container);
};


$container['AdminController'] = function($container){
    return new \App\Controllers\AdminController($container);
};

$container['CustomerController'] = function($container){
    return new \App\Controllers\CustomerController($container);
};

$container['ClientController'] = function($container){
    return new \App\Controllers\ClientController($container);
};

$container['AuthController'] = function($container){
    return new \App\Controllers\Auth\AuthController($container);
};


$container['PasswordController'] = function($container){
    return new \App\Controllers\Auth\PasswordController($container);
};

// add Slim CSRF
$container['csrf'] = function($container){
    return new \Slim\Csrf\Guard;
};

// give back errors
$app->add(new \App\Middelware\ValidationErrorsMiddelware($container));

// give back the old input
$app->add(new \App\Middelware\OldInputMiddelware($container));

// give back a csrf generated key
$app->add(new \App\Middelware\CsrfViewMiddelware($container));

// run the crsf check
$app->add($container->csrf);

// setup custom rules
v::with('App\\Validation\\Rules\\');

require  __DIR__ . '/../app/routes.php';

Я попытался проверить роль в Authcontroller, а затем перенаправить на нужный маршрут, используя иоператор if (закомментированный выше ^), но это, к сожалению, не работает.

...