[英]What is the most effective way to translate string names from database in Laravel
[英]Most effective way of protecting a view in Laravel 4.2?
因此,似乎這個問題對於身份驗證和密碼有些困惑。 我會澄清。
在我的應用中,任何經過身份驗證的用戶都可以創建團隊並成為其隊長 。 但是在此過程中,系統會提示他們定義一個團隊密碼 ,該密碼必須用於編輯團隊信息,例如頭像,電子郵件,成員等。
因此,我需要使版本視圖僅可供該團隊的隊長訪問,並且前提是他具有正確的團隊密碼 。 我所說的“身份驗證”是指檢查該用戶是否是隊長,以及團隊密碼是否是數據庫中團隊的密碼( $team->password
)。 這與經過Sentry認證的用戶無關。 對於那個很抱歉。
因此,我需要保護該團隊的版本不受可能嘗試訪問它的任何其他用戶的影響。 希望現在清楚了。
我認為只有特定用戶才能看到該視圖,並且只有他輸入正確的密碼(例如在個人信息編輯頁面或類似的內容中)時,才能看到該視圖。 因此,即使該視圖的路由設置為GET,也需要保護該視圖免受惡意攻擊。
我嘗試了兩種方法,但沒有一種有效。
@if
子句顯示視圖。 (不起作用:密碼需要通過表單發送。它不是用戶的密碼,因此我無法通過視圖內的Auth::User()
或Sentry::getUser()
訪問它。) 所以我的問題是:是否有更簡單的方法來實現? 什么是最常用或最好的方法? 這是我第一個使用Laravel的真實應用程序,因此我對這些東西還沒有經驗。
提前致謝!
我記得您的最后一個問題,但由於您未提供最新信息,所以我也無法發布任何內容。 但是,您正在使它變得復雜。
Route.php
Route::get('login', 'LoginController@getLogin');
Route::post('login', 'LoginController@postLogin');
Route::group(['before' => 'authentication'], function(){
Route::get('profile' => 'ProfileController@profile');
//All protected routes here
});
我在這里所做的是,我創建了一個過濾器authentication
,該authentication
將針對組內的所有路由運行。
現在,讓我們定義過濾器。
Route::filter('authentication', function()
{
if ( ! Sentry::check())
{
return Redirect::action('LoginController@getLogin');
}
});
這只是一個簡單的過濾器,它將檢查用戶是否已登錄。 如果用戶未登錄,它將把用戶重定向到將提供該表單的login
路由。
控制器:
public function getLogin()
{
if(Sentry::check())
{
return Redirect::action('ProfileController@profile');
}
return View::make('login');
}
public function postLogin()
{
//put the validation rules here and validate. as far as i remember, you know how to do it.
if($validator->passes())
{
try
{
// Login credentials
$credentials = array(
'email' => 'john.doe@example.com',
'password' => 'password',
);
// Authenticate the user
$user = Sentry::authenticate($credentials, false);
return Redirect::action('ProfileController@profile');
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\WrongPasswordException $e)
{
echo 'Wrong password, try again.';
}
catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
{
echo 'User was not found.';
}
catch (Cartalyst\Sentry\Users\UserNotActivatedException $e)
{
echo 'User is not activated.';
}
// The following is only required if the throttling is enabled
catch (Cartalyst\Sentry\Throttling\UserSuspendedException $e)
{
echo 'User is suspended.';
}
catch (Cartalyst\Sentry\Throttling\UserBannedException $e)
{
echo 'User is banned.';
}
}
}
在catch塊中,執行所需的操作。 例如,如果您想從中將錯誤重定向到登錄名,則將錯誤添加到消息袋中(如果您不知道如何操作, 請單擊此處以獲取詳細信息)並重定向到登錄表單。
或者,如果它是ajax數據,則可以將錯誤作為json返回,然后在客戶端解析它們,同時在ajax失敗時顯示錯誤消息。
如果用戶未登錄,則訪問所有那些受保護的路由將引起重定向,並且用戶將被重定向到登錄表單。 成功登錄后,他將被重定向到他的個人資料頁面。 另一方面,如果已登錄的用戶嘗試轉到登錄表單,則由於已登錄的用戶不應看到該登錄表單,因此該用戶將被重定向到個人資料頁面。
它比您想象的要容易。
偽代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.