1

我的理解基於角色的訪問控制是我需要採取以下步驟:如何實現與AngularFire

  • 爲只讀上訪問數據
  • 使用安全規則的用戶角色角色控制訪問
  • 檢查路由器的作用

有官方文件如何處理安全規則不同的例子,但我無法弄清楚浩w檢查路由器中的角色。假設我有一個僅限管理員的區域,如果某個不是管理員的人嘗試訪問該頁面,我希望該用戶被重定向。

我目前繼official example using UI-Router,所以這是我的代碼:

app.config(["$stateProvider", function ($stateProvider) { 
$stateProvider 
.state("home", { 
    // the rest is the same for ui-router and ngRoute... 
    controller: "HomeCtrl", 
    templateUrl: "views/home.html", 
    resolve: { 
    // controller will not be loaded until $waitForSignIn resolves 
    // Auth refers to our $firebaseAuth wrapper in the factory below 
    "currentAuth": ["Auth", function(Auth) { 
     // $waitForSignIn returns a promise so the resolve waits for it to complete 
     return Auth.$waitForSignIn(); 
    }] 
    } 
}) 
.state("account", { 
    // the rest is the same for ui-router and ngRoute... 
    controller: "AccountCtrl", 
    templateUrl: "views/account.html", 
    resolve: { 
    // controller will not be loaded until $requireSignIn resolves 
    // Auth refers to our $firebaseAuth wrapper in the factory below 
    "currentAuth": ["Auth", function(Auth) { 
     // $requireSignIn returns a promise so the resolve waits for it to complete 
     // If the promise is rejected, it will throw a $stateChangeError (see above) 
     return Auth.$requireSignIn(); 
    }] 
    } 
}); 
}]); 

我猜我會在決心以檢查用戶的角色,但我會如何訪問數據從那裏的數據庫?

更新:

我想安德烈的解決方案,但 「waitForAuth」(執行console.log( 「測試1」)從來沒有觸發 「waitForSignIn」 的確,雖然,但再沒有任何反應 - 沒有錯誤消息。

.state('superadmin-login', { 
    url: '/superadmin', 
    templateUrl: 'views/superadmin-login.html', 
    'waitForAuth': ['Auth', function (Auth) { 
     console.log('test1'); 
     // $requireAuth returns a promise so the resolve waits for it to complete 
     // If the promise is rejected, it will throw a $stateChangeError (see above) 
     return Auth.refAuth().$waitForSignIn(); 
    }], 
}) 
.state('superadmin', { 
    url: '/center-of-the-universe', 
    templateUrl: 'views/superadmin.html', 
    resolve: { 
     // YOUR RESOLVES GO HERE 
     // controller will not be loaded until $requireAuth resolves 
     // Auth refers to our $firebaseAuth wrapper in the example above 
     'currentAuth': ['Auth', function (Auth) { 
      console.log('test2'); 
      // $requireAuth returns a promise so the resolve waits for it to complete 
      // If the promise is rejected, it will throw a $stateChangeError (see above) 
      return Auth.refAuth().$requireSignIn(); 
     }], 
     //Here i check if a user has admin rights, note that i pass currentAuth and waitForAuth to this function to make sure those are resolves before this function 
     hasAdminAccess: function (currentAuth, waitForAuth, Rights) { 
      console.log('test'); 
      return Rights.hasAdminAccess(currentAuth); 
     } 
    } 
}) 

回答

3

這裏是我是如何做到的

首先我犯了一個工廠,以檢查用戶是否具有正確的權限:

angular.module('rights.services', []) 
.factory('Rights', function ($q) { 
    var ref = firebase.database().ref(); 

    return { 
     hasAdminAccess: function (user) { 
      var deferred = $q.defer(); 
      ref.child("Rights").child("Admin").child(user.uid).once('value').then(function (snapshot) { 
       if (snapshot.val()) { 
        deferred.resolve(true); 
       } 
       else{ 
        deferred.reject("NO_ADMIN_ACCESS"); 
       } 
      }); 
      return deferred.promise; 
     } 
    }; 
}); 

其次我用這個工廠裏面的決心:

.state('logged', { 
      url: '', 
      abstract: true, 
      templateUrl: helper.basepath('app.html'), 
      resolve: { 
        // YOUR RESOLVES GO HERE 
        // controller will not be loaded until $requireAuth resolves 
        // Auth refers to our $firebaseAuth wrapper in the example above 
        "currentAuth": ["Auth", function (Auth) { 
         // $requireAuth returns a promise so the resolve waits for it to complete 
         // If the promise is rejected, it will throw a $stateChangeError (see above) 
         return Auth.refAuth().$requireSignIn(); 
        }], 
        "waitForAuth": ["Auth", function (Auth) { 
         // $requireAuth returns a promise so the resolve waits for it to complete 
         // If the promise is rejected, it will throw a $stateChangeError (see above) 
         return Auth.refAuth().$waitForSignIn(); 
        }], 
        //Here i check if a user has admin rights, note that i pass currentAuth and waitForAuth to this function to make sure those are resolves before this function 
        hasAdminAccess: function (currentAuth, waitForAuth, Rights) { 
         return Rights.hasLightAccess(currentAuth); 
        } 
       }) 
     }) 

請記住您保存火力用戶角色可以從我怎麼做,在這個例子中不同的方式。這是(的一部分),它的外觀在火力點:

{"moderators": 
    { 
    "0123eeca-ee0e-4ff1-9d13-43b8914999a9" : true, 
    "3ce9a153-eea8-498f-afad-ea2a92d79950" : true, 
    "571fa880-102d-4372-be8d-328ed9e7c9de" : true 
    } 
}, 
{"Admins": 
    { 
    "d3d4effe-318a-43e1-a7b6-d7faf3f360eb" : true 
    } 
} 

而且這些節點的安全規則:

"Admins": { 
    "$uid": { 
     //No write rule so admins can only be added inside the firebase console 
     ".read": "auth != null && auth.uid ==$uid" 
    } 
}, 
"Moderators" : { 
    //Admins are able to see who the moderators are and add/delete them 
    ".read" : "(auth != null) && (root.child('Admins').hasChild(auth.uid))", 
    ".write" : "(auth != null) && (root.child('Admins').hasChild(auth.uid))", 
    "$uid": { 
     ".read": "auth != null && auth.uid ==$uid" 
    } 
} 
+0

大答案安德烈。你可以添加一個你用於權限的JSON結構的片段嗎?還有可能還有安全規則(因爲這是確保你的規則得到執行的唯一方法)? –

+0

非常感謝您的幫助André,我非常感謝。請參閱上面的更新 - 我不確定是否因爲我不使用抽象狀態而錯誤地實現了您的解決方案? – user3255061

+0

@ user3255061它與抽象狀態沒有任何關係,但事實上你有不同的狀態waitForAuth。您也可以將其置於其他狀態或從hasAdminAccess功能中刪除它。那麼它應該工作。 –