LoginSignup
1
2

More than 5 years have passed since last update.

laravel-5-boilerplateのミドルウェア

Last updated at Posted at 2015-09-02

RouteNeedsRoleOrPermission.phpを読んでみる

<?php namespace App\Http\Middleware;

use Closure;
use App\Services\Access\Traits\AccessParams;

/**
 * Class RouteNeedsRole
 * @package App\Http\Middleware
 */
class RouteNeedsRoleOrPermission {

    //①トレイトを利用
    use AccessParams;

    /**
     * @param $request
     * @param callable $next
     * @param null $params
     * @return bool|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Symfony\Component\HttpFoundation\Response
     */
    public function handle($request, Closure $next, $params = null)
    {
     //②AccessParams.phpのメソッド
        $assets = $this->getAssets($request, $params);

        if ($assets['needsAll']) {
            if (! access()->hasRoles($assets['roles'], true) || ! access()->canMultiple($assets['permissions'], true)) {
                return $this->getRedirectMethodAndGo($request, $params);
            }
        } else {
            if (! access()->hasRoles($assets['roles'], false) && ! access()->canMultiple($assets['permissions'], false)) {
                return $this->getRedirectMethodAndGo($request, $params);
            }
        }

        return $next($request);
    }
}

①トレイトを利用してapp/Services/Access/TraitsAccessParams.phpを読み込み

phpのトレイトについては以下のページを参考に
http://blog.toshimaru.net/php-trait/

②AccessParams.phpのメソッドを利用している

いくつかのメッソドをピックアップ

public function getAssets($request, $params) {
    $assets['roles'] = $this->getRoles($request, $params);
    $assets['permissions'] = $this->getPermissions($request, $params);
    $assets['needsAll'] = $this->getNeedsAll($request, $params);
    return $assets;
}

private function getRoles($request, $params) {
    return !is_null($params) ? $this->getParamFromController($params, "role") : $this->getParamFromRoute($request, "role");
}

private function getParamFromRoute($request, $param) {
    $return = [];

    $route = $request->route();
    $actions = $route->getAction();

    //Param isn't needed for this request
    if (! isset($actions[$param])) return false;

    //Flash session message
    if ($param == "with")
        if (is_array($actions[$param]) && count($actions[$param]) == 2)
            return ['key' => $actions[$param][0], 'message' => $actions[$param][1]];

    if (is_array($actions[$param]))
        return array_merge($return, $actions[$param]);

    $return[] = $actions[$param];

    return $return;
}

$request->route()->getAction();
の結果は

array(11) {
  ["middleware"]=>
  array(2) {
    [0]=>
    string(4) "auth"
    [1]=>
    string(33) "access.routeNeedsRoleOrPermission"
  }
  ["role"]=>
  array(1) {
    [0]=>
    string(13) "Administrator"
  }
  ["permission"]=>
  array(1) {
    [0]=>
    string(12) "view_backend"
  }
  ["redirect"]=>
  string(1) "/"
  ["with"]=>
  array(2) {
    [0]=>
    string(12) "flash_danger"
    [1]=>
    string(34) "You do not have access to do that."
  }
  ["as"]=>
  string(17) "backend.dashboard"
  ["uses"]=>
  string(54) "App\Http\Controllers\Backend\DashboardController@index"
  ["controller"]=>
  string(54) "App\Http\Controllers\Backend\DashboardController@index"
  ["namespace"]=>
  string(28) "App\Http\Controllers\Backend"
  ["prefix"]=>
  string(6) "/admin"
  ["where"]=>
  array(0) {
  }
}
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2