Laravel 5中间件用户授权

时间:2015-05-12 14:03:01

标签: laravel-5

我一直在使用laravel 5,我想根据用户类中的id创建一个Autorisation路由。它看起来像这样:

<?php namespace App;

use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;

class User extends Model implements AuthenticatableContract, CanResetPasswordContract {

use Authenticatable, CanResetPassword;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'id',
    'email',
    'password',
    'AutorisationId',
    'Firstname',
    'Lastname',
    'OrganisationId'
];

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = ['password', 'remember_token'];


/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function autorisation()
{
    return $this->HasOne('App\Autorisation', 'id', 'AutorisationId');
}

/**
 * @return \Illuminate\Database\Eloquent\Relations\HasOne
 */
public function organisation()
{
    return $this->HasOne('App\Organisation', 'id', 'OrganisationId');
}

}

我想在中间件类中查看autorisationId将它们重定向到正确的页面。

<?php namespace App\Http\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;

class RedirectIfAuthenticated {

/**
 * The Guard implementation.
 *
 * @var Guard
 */
protected $auth;

/**
 * Create a new filter instance.
 *
 * @param  Guard  $auth
 * @return void
 */
public function __construct(Guard $auth)
{
    $this->auth = $auth;
}

/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{
    if ($this->auth->check()) {

        if (\Auth::user()->autorisationId === 1) {
            return redirect('admin');
        } else if (\Auth::user()->autorisationId === 2) {
            return redirect('organisation');
        } else if (\Auth::user()->autorisationId === 3) {
            return redirect('guest');
        }

        return $next($request);
    }

}
}

route.php是:     

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', 'WelcomeController@index');

Route::get('home', 'HomeController@index');

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Route::get('admin', 'AdminController@index');
Route::resource('admin/systemData', 'Admin\systemDataController');
Route::resource('admin/Organisations', 'Admin\OrganisationController');
Route::post('admin/Organisations/activeOrganisation', 'Admin\OrganisationController@setOrgansationActive');
Route::get('admin/logout', 'AdminController@Logout');

Route::resource('organisation', 'Organisation\admin\adminController');
Route::get('organisation/logout', 'AdminController@Logout');
Route::resource('guest', 'Organisation\guest\guestController');
Route::get('guest/logout', 'AdminController@Logout');

但它始终会重新发布到管理页面。我在这做错了什么?我搜索了很多这个问题,但我尝试的每个解决方案都没有用。

0 个答案:

没有答案