无法在Grails中编辑当前登录的用户配置文件

无法在Grails中编辑当前登录的用户配置文件,grails,groovy,Grails,Groovy,我正在尝试编辑当前登录的用户配置文件。我使用Spring安全服务插件进行用户管理。用户(在我的应用程序中是订户)包含来自不同域的字段,如: 1。用户(应用程序中的订户):具有用户名、密码。 2个人资料:有电子邮件地址和电话号码等。 3.人物:有姓和名。 以上所有域都为用户(订户)创建了一个完整的配置文件 现在我想编辑当前登录的用户配置文件,如firstname、lastname或email。 我尝试了以下代码 def userSettings = { Subscriber loggedi

我正在尝试编辑当前登录的用户配置文件。我使用Spring安全服务插件进行用户管理。用户(在我的应用程序中是订户)包含来自不同域的字段,如:

1。用户(应用程序中的订户):具有用户名、密码。 2个人资料:有电子邮件地址和电话号码等。 3.人物:有姓和名。

以上所有域都为用户(订户)创建了一个完整的配置文件

现在我想编辑当前登录的用户配置文件,如firstname、lastname或email。 我尝试了以下代码

def userSettings = {
    Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
    if (loggedinSubscriber){
    Profile profile = Profile?.get(params.id);
    Party person = profile?.Person?.get(params.id);
    if (!person){
      flash.message = "could not find user with ${params.id}"
      redirect action: list
    }
    else
    [person: person, authorityList: sortedRoles()]
    }
    else {
      redirect(controller: "login" , action:"login");
    }
  }
但它没有起作用。这里我得到了当前登录的用户id,但配置文件为空

配置文件域:

package com.vproc.member

import java.util.Date;

import com.vproc.enquiry.Enquiry;
import com.vproc.enquiry.Membership;
import com.vproc.enquiry.Team;

    class Profile {

        String emailAddress  //  field governed by privacy policy
        String phoneNumber   //  field governed by privacy policy
        Date dateCreated
        Date lastUpdated
        boolean isDefaultProfile
        static belongsTo = [ Person]
        //ProfilePrivacyLevelEnum privacyLevel = ProfilePrivacyLevelEnum.Private

        static constraints = {
        }
    }
个人域:

package com.vproc.member

import java.util.Date;

import com.vproc.enquiry.Enquiry;
import com.vproc.enquiry.Membership;
import com.vproc.enquiry.Team;

    class Profile {

        String emailAddress  //  field governed by privacy policy
        String phoneNumber   //  field governed by privacy policy
        Date dateCreated
        Date lastUpdated
        boolean isDefaultProfile
        static belongsTo = [ Person]
        //ProfilePrivacyLevelEnum privacyLevel = ProfilePrivacyLevelEnum.Private

        static constraints = {
        }
    }
包com.vproc.member

import com.vproc.enquiry.Enquiry;
import com.vproc.enquiry.Membership;
import com.vproc.enquiry.Notification;
import com.vproc.enquiry.Team;

class Person extends Party{

    String firstName
    String lastName

    Profile profile
    static belongsTo = [Organization]

    static constraints = {
        lastName nullable:true
        firstName blank:false

    }

}
订户域: 包com.vproc.member

import com.vproc.enquiry.Enquiry;
import com.vproc.enquiry.Membership;
import com.vproc.enquiry.Notification;
import com.vproc.enquiry.Team;

class Person extends Party{

    String firstName
    String lastName

    Profile profile
    static belongsTo = [Organization]

    static constraints = {
        lastName nullable:true
        firstName blank:false

    }

}
导入java.util.Date

导入com.vproc.common.StatusEnum; 导入com.vproc.enquiry.Discussion; import com.vproc.enquiry.enquiry; 导入com.vproc.enquiry.Membership; 导入com.vproc.enquiry.Notification; 导入com.vproc.enquiry.sharedequiry; 导入com.vproc.enquiry.Team; 导入com.vproc.order.Seat

class Subscriber extends PartyRole{

  transient springSecurityService

  String username
  String password
  boolean enabled
  boolean accountExpired
  boolean accountLocked
  boolean passwordExpired
  StatusEnum status
  Date dateCreated
  Date lastUpdated
  List<Contact> contacts ;

  static belongsTo = [ customer: Customer]
  static hasMany = [seats: Seat, ownedEnquiries: Enquiry,enquiresSharedWith: SharedEnquiry,]


  static constraints = {
  //  username  validator : { val , obj ->
              //   if (obj.status != StatusEnum.Pending)
              //      val!= null
               //  }
    username unique: true
    password validator : { val , obj ->
                  if (obj.status != StatusEnum.Pending)
                    val != null
               }

    contacts nullable: true
    notifications nullable : true
    username nullable: true
    password nullable: true

  }
}
现在我想使用
usercontroller
中的
userSettings
方法编辑当前登录用户的配置文件。我获得了当前登录用户id的id,但我无法将该id与个人资料和个人信息一起使用

Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
    if (loggedinSubscriber){
    Profile profile = Profile?.get(params.id);
    Party person = profile?.Person?.get(params.id);

使用上面的代码,配置文件值为空。

好的,我不想理解你所有的领域建模,所以我在看了你的代码后马上回答了我注意到的问题。我希望它能有所帮助,否则只需大量使用调试和日志来解决错误。并阅读文档

首先,在
Profile.groovy
(和其他域)中,使用映射来定义
以下内容:

static belongsTo = [person:Person]
这看起来也不对:

Profile profile = Profile?.get(params.id);
当您静态访问grovvy类(大写首字母)时,您不需要问号。
params.id
应该是配置文件的id吗?然后您需要使用
findBy
方法:

Profile profile = Profile.findById(params.id);
然后在下一行中,您必须再次使用
下一行中的键来映射,因此
person
而不是
person

// no get or findBy required
Party person = profile?.person

希望它有帮助

我得到了以下代码的解决方案:

  def userSettings = {
    Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
    Party person = Person?.get(loggedinSubscriber.party.id)
    Profile profile = person?.profile
    [userInstance: profile, authorityList: sortedRoles()]
  }

谢谢BurtBeckwith和moeTi。

hi@moeTi,我试过用这种方式和你交流,但没有成功。我想问题在于我是通过loggedinSubscriber登录用户id的。但是,有没有办法将其与个人资料或个人资料一起使用呢。第二,我可以直接使用profile或person通过类型转换来登录用户吗?使用
get
比使用
findById
更好,因为
get
调用的缓存要比只能使用查询缓存的finder调用智能得多。hi@moeti如何使用loggedinSubscriber,它具有当前登录的id在用户中使用个人资料和个人信息。我尝试了你的建议,但是profile和person为null。hi@BurtBeckwith如何使用loggedinSubscriber,该用户的id为当前登录的用户profile和person。我尝试了moeTi的建议,但个人资料和个人资料无效。任何idea@BurtBeckwith哇,谢谢,我甚至不知道这是可能的。今天学到了一些新东西