我一直在努力寻找一种正确扩展spring security ui插件的方法。 我想做的事情显然很简单:我有3个由脚本创建的域类:
我想扩展用户域类,以便添加名字,姓氏,国家,dateofBirth,性别和可见性功能,我想出了这个想法:
我创建了其他2个域类,RegisterUser和UserInfo。
代码如下:
class User {
transient springSecurityService
String username
String password
long phone
String email
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
static hasMany = [openIds: OpenID]
static transients = ['springSecurityService']
static constraints = {
username blank: false, unique: true
password blank: false
phone unique:true , nullable:false, blank: false, length:10, range: 3200000000..3940000000
email email:true, nullable:false, blank:false
}
static mapping = {
phone column :'idUser'
email column : 'email'
username column : 'username'
password column: '`password`'
}
Set<Role> getAuthorities() {
UserRole.findAllByUser(this).collect { it.role }
}
def beforeInsert() {
encodePassword()
}
def beforeUpdate() {
if (isDirty('password')) {
encodePassword()
}
}
protected void encodePassword() {
password = springSecurityService?.passwordEncoder ? springSecurityService.encodePassword(password) : password
}
}
class UserInfo implements Comparable {
String firstname
String lastname
String country
Date date_of_birth
String gender
static belongsTo = RegisteredUser
static mapping = {
firstname column : 'firstname'
lastname column : 'surname'
date_of_birth column : 'date_of_birth'
country column : 'country'
gender column : 'gender'
}
static constraints = {
firstname blank:false, matches:"[a-zA-Z]+"
lastname blank:false, matches:"[a-zA-Z]+"
country blank:true, nullable:true
date_of_birth nullable:true, unique:false
gender blank:true, nullable:true, inList:["Male", "Female"]
}
@Override
public int compareTo(Object o) {
// Compare two user basing on their phone numbers and username
RegisteredUser u = (RegisteredUser) o
if(u.info.phone == this.phone){
if(u.info.email == this.email){
return 0;
}
return -1;
}
return 1;
}
}
class RegisteredUser {
User user
UserInfo info
boolean visible
//Not related to spring security
static hasOne = UserInfo
static hasMany = [friendships : Friendship, blocked: Block, trips : Trip]
static constraints = {
}
/**
* Programmatic exercise...? Do it in a more elegant (groovy) and less java way
* @return
*/
static List<RegisteredUser> getContactList_Java(){
def friendshipList =
Friendship.getFriendships(this)
def contactList = new ArrayList<RegisteredUser>()
for(Friendship f : friendshipList){
if(f.user1.equals(this)){
friendshipList.add(f.user2)
}else{
friendshipList.add(f.user1)
}
}
}
/**
* Programmatic exercise?... Done! ;D
* @return
*/
static List<RegisteredUser> getContactList(){
Friendship.getFriendships(){((Friendship)it).otherUser(this)}.collect()
}
/**
*
* method user_exists
* Parameters:
* @param user
* @return
* method type:
* Dynamically chosen
*/
static user_exists(User u){
if(User.where{
username == u.username}.count() > 0){
true
}
false
}
/**
*
* method findByPhone
* Parameters:
* @param number
* @return
* method type:
* Dynamically chosen
*/
def findByPhone(int number){
User.findByPhone(number)
}
/**
*
* method findBySomeInfo
* Parameters:
* @param infos
* @return
* method type:
* Dynamically chosen
*/
def findByMail(String mail){
User.findByEmail(mail)
}
/**
*
* method findByName
* Parameters:
* @param firstname
* @return
* method type:
* Dynamically chosen
*/
def findByName(String firstname){
UserInfo.findByFirstname(firstname)
}
/**
*
* method findSentRequests
* Parameters:
* @return
* method type:
* Dynamically chosen
*/
def findSentRequests(){
Request.findBySender(this)
}
/**
*
* method findReceivedRequest
* Parameters:
* @return
* method type:
* Dynamically chosen
*/
def findReceivedRequest(){
Request.findByReceiver(this)
}
/**
*
* Method findSentActiveRequest
* Parameters:
* @return
* Method type:
* Dynamically defined (Default)
*/
def findSentActiveRequest(){
Request.findBySenderAndStatus(this, util.Status.ACTIVE)
}
}
如果我通过bootstrap实例化注册用户,则应用程序根本看不到用户。
我看不出我明显犯的错误。
答案 0 :(得分:0)
我建议你删除RegisteredUser类。我认为一个好的解决方案可能是一对一的关系中有一个User Class和Profile Class:
class User{
transient springSecurityService
String username
String password
long phone
String email
boolean enabled = true
boolean accountExpired
boolean accountLocked
boolean passwordExpired
...
static hasOne = [profile: UserProfile]
...
}
class UserProfile {
User user
String firstname
String lastname
String country
Date date_of_birth
String gender
...
}