Ember SimpleAuth是在itemController上授权的

时间:2014-08-07 13:31:04

标签: ember.js ember-simple-auth

在我的应用中,我需要根据这样的多对多关系为用户提供访问权限:

App.User = DS.Model.extend({
  fullname: DS.attr('string'),
  password: DS.attr('string'),
  administrator: DS.attr('boolean'),
  organisation_users: DS.hasMany('organisation_user', {async: true})
});

App.OrganisationUser = DS.Model.extend({
  organisation: DS.belongsTo('organisation', {async: true}),
  user: DS.belongsTo('user', {async: true}),
  administrator: DS.attr('boolean')
});

App.Organisation = DS.Model.extend({
  fullname: DS.attr('string', {defaultValue: 'Unnamed University'}),
  description: DS.attr('string'),
  organisation_users: DS.hasMany('organisation_user', {asynch: false}),
});

我正在使用Ember SimpleAuth进行身份验证。所以基于this answer to a previous question我已经在我的OrganisationController上实现了isAuthorised,如下所示:

App.OrganisationController = Ember.Controller.extend({

  //Determine whether the logged in user is authorised
  isAuthorised: function() {
    if(!this.get('session.isAuthenticated')) {
      console.log("Not logged in...");
      return false;
    }

    console.log("Does user [" + this.get('session.user.id') + "] have access to organisation [" + this.get('model.id') + "]?");
    if(this.get('session.user.administrator')) {
      console.log("Yes, because they are an administrator.");
      return true;
    } else {
      console.log("Well, they are not an administrator so...");
    }
    console.log("We need to check " + this.get('model.fullname') + " for authorised users.");
    this.get('model.organisation_users').forEach(function(org_user){
      org_user.get('user').then(function(user) {
        console.log(user.get('fullname') + " has access");
      });
    });
  }.property('model', 'session.user'),

问题是,我不知道如何从中返回一个值。此外,当我加载OrganisationRoute它似乎工作正常,但当我加载不同的路线并转换到此路线时,它失败了

Uncaught TypeError: Cannot read property 'resolve' of undefined

Uncaught Error: Something you did caused a view to re-render after it rendered but before it was inserted into the DOM.

1 个答案:

答案 0 :(得分:0)

我认为你的错误源于这段代码(我认为你有一些侧载问题):

this.get('model.organisation_users').forEach(function(org_user){
  org_user.get('user').then(function(user) { // org_user hasn't loaded its user record?
    console.log(user.get('fullname') + " has access");
  });
});

我可能会采用这样的模式:

return this.get('organisation_users').any(function (item) {
  return item.get('user.id') === this.get('session.user.id');
});

.any将枚举,直到回调返回true。如果为true,则外部范围将返回true,这就是您想要的。

如果没有匹配,你会得到假。很简单。

这个难题的另一个部分是侧面加载,正如我在回答中提到的那样。

相关问题