0

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

1.用户(app中的订阅者):有用户名,密码。2 个人资料:有电子邮件地址和电话号码等。 3. 人:有名字和姓氏。

以上所有域都为用户(订户)提供了完整的配置文件。

现在我想编辑当前登录的用户配置文件,如名字、姓氏或电子邮件。我尝试使用以下代码。

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 = {
        }
    }

个人域:

包 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

导入 java.util.Date;

导入 com.vproc.common.StatusEnum;导入 com.vproc.enquiry.Discussion;导入 com.vproc.enquiry.Enquiry;导入 com.vproc.enquiry.Membership;导入 com.vproc.enquiry.Notification;导入 com.vproc.enquiry.SharedEnquiry;导入 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

  }
}

用户控制器.groovy

package com.vproc.member
import com.vproc.common.StatusEnum
import com.vproc.exception.CustomValidationException;


class UserController extends AbstractS2UiController {

  def saltSource
  def userCache
  def springSecurityService
  def mailService
  def messageSource


  def create = {
    //Subscriber user = lookupUserClass().newInstance(params)
    UserCommand command = new UserCommand()
    [command: command, authorityList: sortedRoles()]
  }


  def save = { UserCommand  command ->
    if (command.hasErrors()) {
      render view: 'create', model: [command: command]
      return
    }

    Subscriber user = lookupUserClass().newInstance(params)
    Profile profile = new Profile(emailAddress : command.emailAddress, phoneNumber: "234555", isDefaultProfile: "true").save()
    Party person = new Person(firstName: command.firstName, lastName: command.lastName, profile: profile).save()
    user.party = person

    if(! user.party.hasErrors()){
      if (params.password) {
        String salt = saltSource instanceof NullSaltSource ? null : params.username
        user.password = springSecurityUiService.encodePassword(params.password, salt)
        user.status = StatusEnum.Active
      }else{
        user.status = StatusEnum.Pending
      }
      Subscriber loggedinSubscriber = Subscriber.get( springSecurityService.principal.id )
      user.customer = loggedinSubscriber.customer
      if (!user.save(flush: true)) {
        flash.message = "not able to save user"
      }
    }
    else{
        flash.message = "not able to save user"
      }

    //addRoles(user)
    //flash.message = "User has been added"
    flash.message = "${message(code: 'default.created.message', args: [message(code: 'user.label', default: 'User'), user.id])}"
    redirect( action : "list" )
  }



  def edit = {
    String username
    def user = params.username ? lookupUserClass().findWhere((usernameFieldName): params.username) : null
    if (!user) user = findById()
    if (!user) return
      return buildUserModel(user)
  }

  // def contacts = Contact.findAllBySubscriber( loggedinSubscriber)

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

现在我想使用userSettingsin中的方法编辑当前登录用户的个人资料usercontroller。我获得了当前登录用户 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);

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

4

2 回答 2

0

好的,我不想了解您的所有域建模,所以我正在回答我在查看您的代码后立即注意到的事情。我希望它有所帮助,否则只需大量使用调试和日志记录来解决您的错误。并阅读文档。

首先,在Profile.groovy(和其他领域)使用映射来定义belongsTo

static belongsTo = [person:Person]

这看起来也不对:

Profile profile = Profile?.get(params.id);

当您对 grovvy 类(大写首字母)具有静态访问权限时,您不需要问号。params.id应该是配置文件的ID ?然后你需要使用这些findBy方法:

Profile profile = Profile.findById(params.id);

然后在下一行中,您必须再次使用belongsTo地图中的密钥,person而不是Person

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

希望能帮助到你

于 2013-10-08T08:12:37.127 回答
0

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

  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。

于 2013-10-09T09:52:57.787 回答