簡體   English   中英

laravel zizaco / entrust如何進行子登錄/子登錄

[英]laravel zizaco/entrust how to go about doing sublogins/child login

嗨,我在laravel中使用zizaco / entrust。 例如,我正在嘗試設置組登錄角色,我試圖建立一些可以幫助我管理項目的東西。

我希望能夠允許客戶端登錄對我指定的頁面具有只讀訪問權限,而對其他用戶數據不具有只讀訪問權限。 因此,我需要允許注冊的用戶創建額外的用戶並分配角色等。

顯然,我需要創建一個額外的表來存儲和處理客戶端登錄。 但是我不確定如何實現這一點。

我想尋找的是一個父帳戶,然后是子帳戶登錄。

有誰知道可以轉發給我的示例,讓我看看如何實現? 還是有人擁有可以共享的基本解決方案? 或任何一般的幫助。

這是為您准備的路線圖。

創建一個ACL表,建模並添加該表,哪個用戶可以訪問哪些內容

這是一個示例表結構:

id | userId | contentId | contentTable

您可以使用laravel控制器過濾器進行訪問控制。

http://laravel.com/docs/controllers#controller-filters

這是ACL的示例過濾器

Route::filter('ACL', function($route, $request, $contentId, $contentTable) {

$haveAccess = ACL::where('userId', Auth::user()->id,)->where('contentId', $contentId)->where('contentTable', $contentTable)->get();
if (count($haveAccess) < 1) {
    return false;
}

return true;
});

聽起來您正在尋找基於組的權限系統。 通常,在這種情況下,我要做的是創建一個用戶表,一個組表和一個權限表。

然后,我在用戶和組,權限和組之間創建數據透視表,然后讓Laravel為我管理這些數據透視表。 數據透視表應包含其自己的唯一ID列,一個user_id和group_id,以及標准的created_at和Updated_at。 我也包括active和delete_at。 軟刪除需要delete_at。

基本上,最終的結果是用戶屬於組,而權限屬於組。

因此,您創建了三個模塊,用戶(應該已經存在),組,權限。 用戶模塊中的一個示例是:

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';
/**
 * Enable/Disable $softDelete: Disabled by default.
 *
 * @var boolean
 */ 
protected $softDelete = true;
/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password');

/**
 * Create Relationship with Groups Table
 * @return Groups Many-to-many relationship
 */
public function groups()
{
    return $this->belongsToMany('Group')->withPivot('active');
}

為“組”和“權限”執行此操作。

注意:-> withPivot('active'); 說從數據透視表中提取數據時包括我的組數據庫中的活動字段。 通常,數據透視表將僅返回ID關聯。

群組可以是任何您想要的東西,基本上是名稱,描述和ID,並帶有標准created_at Updated_at active / deleted_at字段。 為了獲得權限,我實際上添加了他們有權訪問的路線。 因此,權限表僅由路由名稱和實際路由(例如“ / admin / index”)組成。

完成這些設置后,我將創建一個角色篩選器,該角色篩選器檢查以查看與用戶關聯的組,然后檢查這些組具有的權限。 我將此過濾器粘貼在控制器的__Constructor中,因此,每次訪問任何資源時,它都會檢查以確認實際上允許用戶訪問該部分,如果不允許,則使用帶有重定向的flash數據。

過濾器filters.php:

Route::filter('check_roles', function(){
//detect current route
$route = Route::currentRouteName();

//Laravel gives too much data with the currentRouteName so you'll want to do some reformatting of the $route to match what you put in the DB.

//set some helper variables
$access = false;
$active = false;

//get user id to determine groups
$userID = Auth::user()->id;

//get groups user belongs to
$groups = User::find($userID)->groups;

//now that we know the groups, let's loop through each group and all it's permission and see if our current route and a permission matches
foreach($groups as $group){
    $permissions = Group::find($group->id)->permissions;
    foreach($permissions as $permission){

        if($permission->action == trim($action)){
            $access = true;
            if($permission->pivot->active === '1'){
                $active = true;
                break;
            }

        }
    }
}
//If we found a match, access would be true, if not redirect back to their originating page. they do not have access to view resource
//We have two scenarios, they don't have access period, or they did have access but it was disabled for some reason. 
if($access){
    if(!$active){
        //return to previous page;
        return Redirect::back()->with('message','This permission has been disabled in your account.');
    }
}else if(!$access){
    //return to previous page;
    return Redirect::back()->with('message','You do not have the proper clearence to access this resource.');
}

//if the user isn't redirected, then they have proper access.

 });

在控制器中過濾:

$this->beforeFilter('check_roles', array('except' => array('methodToIgnore','methodToIgnore','methodToIgnore')));

現在為您的實際實現,您可以為客戶端用戶創建自定義組,他們可以創建新用戶(他們可以訪問的特定路由)並設置創建用戶表單,以自動將客戶端創建的用戶放入具有只讀權限的組中訪問該網站。

希望這為您指明了正確的方向。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM