Ember SimpleAuth是在itemController上授权的 [英] Ember SimpleAuth isAuthorised on itemController

查看:133
本文介绍了Ember SimpleAuth是在itemController上授权的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要用户根据这样的多对多关系访问组织:

  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({
organization:DS.belongsTo('organization',{async:true}),
user:DS.belongsTo( 'user',{async:true}),
管理员: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进行身份验证。所以基于上一个问题的答案,我已经实现了我的组织控制器的授权,如下所示:

  App.OrganisationController = Ember.Controller.extend({

//确定登录的用户是否被授权
isAuthorised:function(){
if(!this.get('session.isAuthenticated')){
console.log(Not logged in ...);
return false;
}

console.log(用户[+ this.get('session.user.id')+]可以访问组织[+ this.get(' model.id')+]?);
if(this.get('session.user.administrator')){
console.log(是的,因为他们是管理员。 );
return true;
} else {
console.log(嗯,他们不是管理员那么...);
}
控制台。 log(我们需要检查+ this.get('model.fullname')+用于授权用户。);
this.get('model.organisation_users')forEach(function(org_user){
org_user.get('user')。then(function(user){
console.log(user.get 'fullname')+有访问权限);
});
});
} .property('model','session.user'),

问题是,我不知道如何从中返回值。此外,当我加载OrganisationRoute它似乎工作正常,但当我加载不同的路由和转换到这条路由,它失败与



未捕获TypeError:无法读取未定义的属性'resolve'





未捕获的错误:您已经做了一些事情导致在渲染之后再渲染,但在它被插入到DOM之前。

解决方案

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

  this.get('model.organisation_users')forEach(function(org_user){
org_user.get('user')。then(function(user){// org_user hasn没有加载用户记录?
console.log(user.get('fullname')+have access);
});
});

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


$ b $ ($($)$ {$ $ $ $ $ $ $ return $。 get('session.user.id');
});

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



如果没有匹配,则会导致错误。相当直截了当。



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


In my app I need users to be given access to organisations according to a many-to-many relationship like this:

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}),
});

I am using Ember SimpleAuth for authentication. So based on this answer to a previous question I have implemented isAuthorised on my OrganisationController like this:

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'),

The problem is, I don't know how to return a value from this. Also, when I load the OrganisationRoute it seems to work OK but when I load a different route and transition into this route, it fails with

Uncaught TypeError: Cannot read property 'resolve' of undefined

and

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

解决方案

I think your bug is stemming from this chunk of code (you are having some sideloading issues, I think):

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");
  });
});

I would probably go with a pattern like this:

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

.any will enumerate until the callback returns true. If true, the outer scope will return true, which is what you want.

If nothing matches, you get false. Pretty straightforward.

The other piece to this puzzle is the sideloading, as I mentioned earlier in the answer.

这篇关于Ember SimpleAuth是在itemController上授权的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆