我正在尝试编辑当前登录的用户个人资料。我使用Spring Security Service插件进行用户管理。用户(我的应用程序中的订阅者)包含来自不同域的字段,如:
1。用户(app中的订阅者):具有用户名,密码。 2简介:有emailaddress和phonenumber等。 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,但是profile无效。
个人资料域名
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 java.util.Date;
import com.vproc.common.StatusEnum; import com.vproc.enquiry.Discussion; import com.vproc.enquiry.Enquiry; import com.vproc.enquiry.Membership; import com.vproc.enquiry.Notification; import com.vproc.enquiry.SharedEnquiry; import com.vproc.enquiry.Team; import 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.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");
}
}
}
现在,我想使用userSettings
中的方法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);
使用上面的代码,配置文件值为空。
答案 0 :(得分: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
希望有所帮助
答案 1 :(得分: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。