我正在更新Laravel 5.2项目以基于我从SSO令牌中提取的允许用户ID数组来限制访问。我的应用程序的设置是创建了一个自定义的中间件类来处理SSO。我希望其ID位于受限数组中的用户无法访问某些页面。它可以进行一半:其ID被认为是受限的用户可以允许访问页面数组。但是,这些页面使用API调用,虽然用户可以访问这些页面,但是当他们查看它们时,由于API调用受到限制,这些页面已损坏。
这是我的中间件中的相关代码段:
//Check to see if user is limited access user
$user_id = $request->server('ssotokenid');
if(in_array($user_id, $this->restrictedUsers))
{
//If restricted user, ensure that requested page is within allowable access array
$uri = $request->path();
//If allowed page
if(in_array($uri, $this->allowedPagesArray))
return $next($request);
else
return redirect('/restricted-landing-page');
}
//Continue on to oher stuff here...this works fine
这里是我的routes.php中的页面示例;请注意,我使用的是控制器方法而不是get:
Route::controller('/subdashboard/subpage', 'Dashboard\PageController');
这里是此控制器内某些方法的示例:
class PageController extends DataController
{
protected $dashboard = 'my-dashboard';
protected $page = 'my-page';
private $table = 'my-db-table';
/**
* Render page
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\View\View
*/
public function getIndex()
{
return view("$this->dashboard/$this->page", [
'appUrl' => $this->rootPath,
]);
}
/**
* An example method and first API call that runs when the page loads
* @param none
* @return array
*/
public function getData()
{
$data= $this->db->table($this->table)
->lists('myfield');
return response()->json($data);
}
我的页面查看文件提取了一个js文件。加载的第一件事是对控制器中的getData方法的API调用。示例:
function init()
{
//This should call the getData method in the above controller
//This fails here
$.getJSON(apiURL + '/data/')
.done( function(returnData) {
//do stuff with json response
})
}
上述API调用失败,因为HTTP请求正在解析为允许的页面/数据URL。用户可以转到允许的页面,但是此后的任何操作都会失败,因为请求没有重定向到允许的页面/数据中,而是重定向到“限制登陆页面”。
我已尝试使用strpos来确定URL是否包含允许的页面,但这不起作用。
//If allowed page
if(in_array($uri, $this->allowedPagesArray))
return $next($request);
else
{
foreach($this->allowedPagesArrayas $page)
{
if(strpos($page, $uri) !== false)
return $next($request);
}
return redirect('/restricted-landing-page');
}
有人对如何解决这个问题有任何想法吗?
我是个白痴。我在strpos()中使用的参数顺序错误。当我更正此问题时,它工作正常。另外,我想到我可以摆脱in_array比较,而只需使用strpos。
所以代替这个...
//If allowed page
if(in_array($uri, $this->allowedPagesArray))
return $next($request);
else
{
foreach($this->allowedPagesArrayas $page)
{
if(strpos($page, $uri) !== false)
return $next($request);
}
return redirect('/restricted-landing-page');
}
我将其更新为:
foreach($this->allowedPagesArrayas $page)
{
if(strpos($uri, $page) !== false)
return $next($request);
}
return redirect('/restricted-landing-page');