I have a n00b question. But here it goes. I am working on hashing
passwords in my model using beforeSave() but when I do so the
beforeSave() method gets called even when I am creating a blank user
object. Not sure what is going on here. Here is the code.
//Callback in the model
beforeSave("saltPassword");
//Method being called checks that pass is defined before salting and
hashing
private function saltPassword(){
if(isDefined("this.password")){
this.passwordSalt = CreateUUID();
this.password = getPasswordHash(this.password,this.passwordSalt);
}
}
//Hash the pass
public function getPasswordHash(password, salt){
arguments.pw = hash(arguments.password & arguments.salt, 'SHA-512');
for(i=0; i LTE 1025; i=i+1){
arguments.pw = hash(arguments.password & arguments.salt, 'SHA-512');
}
return
arguments.pw;
}
//Register function -------------
public void function register() {
contentFor(title="Register");
usesLayout("/layouts/plain");
user = model('user').new();
}
public void function goRegister() {
user = model('user').new(params.user);
user.save();
if(user.hasErrors()){
renderPage(action="register");
}
else{
redirectTo(route="userHome",success="You successfully registered!");
}
}
//------------------------------
Not quite sure what is going on. I thought the beforeSave() was called
before an object was saved, not when one was created. Or maybe I am
not making any sense at all. Any thoughts?
Nathan Stanford II