2017-08-25 43 views
1

我有一個管理員部分,可以限制用戶只搜索選定的國家/地區。因此,如果我選擇用戶只能搜索美國和法國,這些國家的ID將作爲字符串存儲在表中。所以它會是1,2;如何限制用戶更改URL中的參數

但是讓我們說,用戶只能在美國進行搜索,所以ID爲1

當用戶開始搜索我得到這個值,並將其存儲在變量:

$searchableCountry = explode(",", Input::get('searchableCountry')); 

這是搜索條件:

if ($searchableCountry != null) { 
      $results = $cl->Filter('country', $searchableCountry); 
     } 

而且我得到的URL是這樣的: http://myapp.com/search?q=Adidas&searchableCountry=1

但用戶可以編輯URL並更改searchableCountry值並搜索其他國家。我怎樣才能避免這種情況?這可以通過Session完成嗎?如果是,請給我一個例子?

BTW搜索我用這個包https://github.com/sngrl/sphinxsearch

PS我不得不使用GET方法!

回答

0

如果你想限制你的用戶只能做點什麼,你不應該讓他們選擇他們想要的,你必須修改你的規則。

$results = $cl->Filter('country', $config['country']['us']); 

如果你想向他們發放許可,更多的國家根據它們是一級搜索,你必須添加一些更多的權限設置,如用戶級別,因此,如果他們訪問http://myapp.com/search?q=Adidas&searchableCountry=3,因爲他們可以返回一個錯誤沒有許可。

例如我的想法,尚未測試,但應該工作。

<?php 

//your user data 
$user = json_decode('{ 
    "user_id" : NumberLong(1004109307), 
    "fullname" : "Johnny", 
    "email" : "[email protected]", 
    "phone" : "123456789", 
    "role" : "basic", //wich user can search US,FRANCE for country id is [1,2] 
    "create_time" : NumberLong(1503537866) 
}', true); //retun an array 

//Your country id config somewhere 
$country_list = array(
    'US' => 1, 
    'FR' => 2, 
    'JP' => 3, 
    'CN' => 4, 
); 
//Your config somewhere 
$searchable_country_config = array(
    'basic' => array(
     $country_list['US'], //1 
     $country_list['FR'], //2 
    ), 
    'premium' => array(
     $country_list['JP'], 
     $country_list['CN'], 
    ), 
); 

$searchable_request = (Input::get('searchableCountry')) ? Input::get('searchableCountry') : 1; // 2 for example 
$searchable_permission = $searchable_country_config[$user['role']]; 

//lets check that does this user have permission on their request 
if(in_array($searchable_request,$searchable_permission)) 
{ 
    $results = $cl->Filter('country', $searchable_request); 
} else { 
    return response()->json(array(
     'status' => 'error', 
     'message' => 'You have not access for this country' 
    )); 
} 
+0

我有用戶角色(基本,高級,管理等...)。但我不知道如何將它與URL限制連接起來? – harunB10

+0

@ harunB10查看我的更新 – vietnguyen09