2013-10-08 2 views
0

현재 로그인 한 사용자 프로필을 수정하려고합니다. 나는 사용자 관리를 위해 Spring Security Service 플러그인을 사용했다. 사용자 (내 응용 프로그램의 가입자)는 다음과 같이 다른 도메인에서 온 필드를 포함합니다.Grails에서 현재 로그인 한 사용자 프로필을 편집 할 수 없습니다.

1. 사용자 (사용자 가입 자) : 사용자 이름, 비밀번호를가집니다. 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에 로그인했지만 프로필이 null입니다.

프로필 도메인 :

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.Exquiry; 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;

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

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는 지금은 현재 로그인의 사용자 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); 

위의 코드를 사용하면 프로필 값이 null입니다.

답변

0

좋아 도메인 모델링을 모두 이해하고 싶지 않으므로 코드를 살펴본 직후에 알아 챘습니다. 도움이 되었으면합니다. 그렇지 않으면 디버그를 사용하고 오류를 해결하기 위해 많은 로깅을 사용하십시오. 그리고 문서를 읽으십시오.Profile.groovy (다른 도메인)이지도를 사용하는 모든의

첫째, belongsTo을 정의 :

static belongsTo = [person:Person] 

을 마우스 오른쪽 보이지 않는 :

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

당신이에 대한 정적 액세스 할 때 grovvy 클래스 (대문자 첫 글자)는 questionmark가 필요하지 않습니다. params.id은 프로필의 ID로되어 있습니까? Person 대신 그래서, 당신이 다시 belongsTo 맵에서 키를 사용할 필요가 다음 줄에 다음 person

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

: 그럼 당신은 findBy 방법을 사용할 필요가

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

그것이

희망이 도움
+0

안녕하세요 @ 모에티, 당신과 같이 시도했지만 얻지 못했습니다. 내가 로그인 한 사용자 ID가 loggedinSubscriber에 의해 문제가 있다고 생각합니다. 그러나 프로필이나 사람과 함께 사용하는 방법이 있습니다. 두 번째로 프로필이나 사람을 직접 사용하여 유형 캐스팅을 사용하여 로그인 한 사용자를 얻을 수 있습니까? –

+2

'get' 호출은 쿼리 캐시만을 사용할 수있는 파인더 호출보다 훨씬 지능적으로 캐시되기 때문에'findById'보다'get'을 사용하는 것이 더 좋습니다. –

+0

안녕하세요 @moeti 프로필과 사람이 현재 로그인 한 사용자의 ID를 가진 loggedinSubscriber를 사용하는 방법. 나는 제안을 시도했지만 프로필과 사람은 null입니다. –

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에게 감사드립니다.

관련 문제