我在登录时将用户重定向到正确的页面时遇到了麻烦。我的意思是,如果数据库中的用户具有admin角色,则应将其重定向到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
因此在上面的示例中,John将是管理员,而Sam将是客户
这是我的文件结构
├── 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语句(上面已注释掉^)将其重定向到所需的路由,但不幸的是,该操作不起作用。