Laravel 5.2管理仪表板 [英] Laravel 5.2 admin dashboard

查看:90
本文介绍了Laravel 5.2管理仪表板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要一些laravel大师的帮助. 我要使管理员帐户登录和仪表板.

Some laravel master's help needed. I want to make admin account login and dashboard.

开箱即用的laravel使用表users提供身份验证.我添加了表roles和列users(role_id),因此我可以使用不同的用户. 在大多数情况下,许多小时的搜索无济于事,这是将本地身份验证与针对不同用户的两个表进行复制的愚蠢方式.

Out of the box laravel provides authentication with table users. I've added table roles and a column users(role_id) so i can differ users. Many hours of searching didnt help cause in most cases it was dumb way of duplicating of native authentication with two tables for different users.

Kernel.php

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
    ],
    'api' => [
        'throttle:60,1',
    ],
    'admin' => [
        'web',
        'auth',
    ],
];

routes.php

Route::group(['middleware' => 'admin'], function () {
    Route::get('admin', 'LoginController@showLoginForm');
    Route::post('admin', 'LoginController@authenticate');

    Route::get('dashboard', function () {
        return view('admin.dashboard');
    });
});

LoginController.php

public function showLoginForm()
{
    return view('admin.login');
}

public function authenticate(Request $request)
{
    $credential = [
        'email' => $request['email'],
        'password' => $request['password']
    ];

    if (Auth::attempt($credential) && $this->authAdmin($credential['email']))
    {
        //SOMETHING I DONT KNOW YET
        //BUT THEN
        return redirect()->route('dashboard');
    }
}

protected function authAdmin($email = null)
{
    $user = User::where('email', $email)->first();

    if ($user->role_id == '2')
    {
        return true;
    }
    return false;
}

当我进入/dashboard时,我看到基本的登录表单,当我输入凭据时,我将被登录,但是会话与简单用户相同.我不确定我的 LoginController 方法.问题是:如何区分会话以创建管理员帐户?需要有关上述代码的一些建议.

When i go /dashboard i see basic login form and when i enter credentials i become logged, but session is the same with simple user. I am not sure about my LoginController methods. The question is: how to differ sessions to make admin account ? Some advices about above code desired.

推荐答案

我离我很近.因此,这是我对自己的问题的解决方案.

I was pretty close. So here's my solution to my own question.

首先,我添加了方法来检查用户模型 User.php

First of all i've added method to check the role to user model User.php

在我看来,这是

public function isAdmin() {
    $st = false;
    if ($this->role_id == 2) {
        $st = true;
    }
    return $st;
}

然后我创建了中间件 IsAdmin.php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class IsAdmin
{
    public function handle($request, Closure $next)
    {
        if (Auth::check() && Auth::user()->isAdmin()) { //check the proper role
            return $next($request);
        }
        else {
            return response()
                ->view('admin.forbidden')
                ->header('Content-Type', 'text/html');
        }
    }
}

接下来,我编辑了 Kernel.php

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'admin' => \App\Http\Middleware\IsAdmin::class, //my middleware
];

routes.php 看上去

Route::group(['middleware' => 'web'], function () {

    Route::group(['middleware' => 'admin'], function() {
        Route::get('/dashboard', 'LoginController@dashboard');
    });
});

LoginController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use Auth;

class LoginController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function dashboard()
    {
        return view('admin.dashboard');
    }
}

您可以通过这种方式简单地限制或允许对任何角色执行操作.我希望这会对某人有所帮助.

You can simply restrict or allow actions to any role this way. I hope this will help someone.

这篇关于Laravel 5.2管理仪表板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆