admin
admin copied to clipboard
Meta could have Valuer/FormattedValuer/Setter that pass/expect value instead of record
Supose this from docs; The valuer cant be used on other resources, "admins" for example.
user.Meta(&admin.Meta{Name: "Password",
Type: "password",
Valuer: func(interface{}, *qor.Context) interface{} { return "" },
Setter: func(record interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if newPassword := utils.ToString(metaValue.Value); newPassword != "" {
bcryptPassword, _ := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
record.(*models.User).EncryptedPassword = string(bcryptPassword)
}
},
})
Passing value on Setter would allow to do this:
type Password string
type Admin struct {
....
VeryEncryptedPassword Password
OldVeryEncryptedPassword Password
}
type User struct {
.....
EncryptedPassword Password
OldEncryptedPassword Password
}
func (p Password) ConfigureQorMeta(metaor resource.Metaor) {
meta := metaor.(*admin.Meta)
meta.SetValuer(func(interface{}, *qor.Context) interface{} { return "" })
meta.SetSetter(func(value interface{}, metaValue *resource.MetaValue, context *qor.Context) {
if newPassword := utils.ToString(metaValue.Value); newPassword != "" {
bcryptPassword, _ := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost)
value = &Password(bcryptPassword)
}
}
}
Note different struct field names and multiple fields with Password type, disallowing using reflection searching for a specifig field name and/or type. (On this specific example, on the Setter, we could change metaValue and call the default Setter(like rich editor do), but this cant be done on Valuer/FormattedValuer)