将视图限制为仅受邀用户 Laravel 中间件
Limit a view to only invited users Laravel Middleware
我正在寻找一种方法来仅向获得该视图 link 的特定访问者显示特定视图。我怎样才能制作一个中间件,以便只显示来自特定来源的视图(比如来自 source.blade.php)
我不能将中间件用于来宾或授权,因为那样它会将视图提供给所有授权,但我只想将该视图提供给开始付款并已从具体URL。
我如何设置一个中间件,使其仅在身份验证从另一个视图重定向时才显示视图,例如 - source.blade.php
目前,我的页面是这样设置的
public function __construct()
{
$this->middleware('auth:client');
}
这很好,它只向从客户端身份验证登录的人显示此页面,但问题是,任何客户端都可以访问此页面。
我正在寻找一种方法来制作它,以便它只能由一开始付款并被我的网站重定向的客户查看。也许像
public function __construct()
{
if(redirect_source="source.blade.php") {$this->middleware('auth:client'); }
}
您可以在将用户重定向到您的特定页面时传递令牌。然后使用您的中间件检查该令牌是否有效。
例如,某人开始付款,您在会话中存储该人的用户 ID 或任何唯一标识符的哈希值,然后使用您的 url 中包含相同的哈希值。如果会话中存储的值与 url.
中提供的值相同,则您的中间件可以处理验证
我认为您需要一个可以根据您的用户类型限制权限的解决方案。
如果您想让请求者进入特定的 url/route 而不是在您的视图中进行控制,则可以使用中间件来调节某些参数。
所以如果你想控制它,你可以使用这个解决方案。
namespace App\Laravel\Middleware\Backoffice;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
use Auth, Session;
class ValidSuperUser {
/**
* 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->user()->type != "super_user") {
Session::flash('notification-status','failed');
Session::flash('notification-title',"Access Denied");
Session::flash('notification-msg','You are not allowed to view the page you are tring to access.');
return redirect()->route('backoffice.dashboard');
}
return $next($request);
}
}
在你的 Kernel.php Http 文件夹下声明新的中间件以便使用。
**将其置于受保护的 $routeMiddleware = []
然后将其用于需要帮助此类用户的路线。
$route->group(['middleware' => "aliasofyournewmiddle"],function(){
//some routes here
});
您的新中间件可以根据请求满足任何条件,因此传递给该 url 的任何输入和可用会话都可以在该中间件上使用,根据您希望如何处理这种情况进行调整。
我正在寻找一种方法来仅向获得该视图 link 的特定访问者显示特定视图。我怎样才能制作一个中间件,以便只显示来自特定来源的视图(比如来自 source.blade.php)
我不能将中间件用于来宾或授权,因为那样它会将视图提供给所有授权,但我只想将该视图提供给开始付款并已从具体URL。
我如何设置一个中间件,使其仅在身份验证从另一个视图重定向时才显示视图,例如 - source.blade.php
目前,我的页面是这样设置的
public function __construct()
{
$this->middleware('auth:client');
}
这很好,它只向从客户端身份验证登录的人显示此页面,但问题是,任何客户端都可以访问此页面。
我正在寻找一种方法来制作它,以便它只能由一开始付款并被我的网站重定向的客户查看。也许像
public function __construct()
{
if(redirect_source="source.blade.php") {$this->middleware('auth:client'); }
}
您可以在将用户重定向到您的特定页面时传递令牌。然后使用您的中间件检查该令牌是否有效。
例如,某人开始付款,您在会话中存储该人的用户 ID 或任何唯一标识符的哈希值,然后使用您的 url 中包含相同的哈希值。如果会话中存储的值与 url.
中提供的值相同,则您的中间件可以处理验证我认为您需要一个可以根据您的用户类型限制权限的解决方案。
如果您想让请求者进入特定的 url/route 而不是在您的视图中进行控制,则可以使用中间件来调节某些参数。
所以如果你想控制它,你可以使用这个解决方案。
namespace App\Laravel\Middleware\Backoffice;
use Closure;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\RedirectResponse;
use Auth, Session;
class ValidSuperUser {
/**
* 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->user()->type != "super_user") {
Session::flash('notification-status','failed');
Session::flash('notification-title',"Access Denied");
Session::flash('notification-msg','You are not allowed to view the page you are tring to access.');
return redirect()->route('backoffice.dashboard');
}
return $next($request);
}
}
在你的 Kernel.php Http 文件夹下声明新的中间件以便使用。
**将其置于受保护的 $routeMiddleware = []
然后将其用于需要帮助此类用户的路线。
$route->group(['middleware' => "aliasofyournewmiddle"],function(){
//some routes here
});
您的新中间件可以根据请求满足任何条件,因此传递给该 url 的任何输入和可用会话都可以在该中间件上使用,根据您希望如何处理这种情况进行调整。