Setting of certain user values
I'm loving this package so far, but I immediately ran into the issue caused by this section of code:
Namely, I manage roles as a Many-To-Many without a "role_id" foreign key. Since you asked "how to fix?" in the linked comment, I thought I might provide some suggestions.
Firstly, it probably makes sense to provide config options for the exact column names of the "username", "email", and "password" fields. I'd even considering giving the option to not include those columns at all. This would allow you to do:
if (Config::get('anvard::db.username_field'))
{
$user->{Config::get('anvard::db.username_field')} = $adapter_profile->email;
}
The other thing is, I don't think you need to set a role_id explicitly at all. Eloquent comes with mutators and events built in so that you don't have to think about giving users so much customization. I would even suggest getting rid of the password and password_confirmation fields because all of that can be set up by the developer if they wish and it generalizes your package code.
As it stands, I don't need to hack the anvard core because it's easy enough for me to use mutators and events to undo the hard-coding in Anvard.php. However, it would be nice not to have to do that ;). For anyone wondering how this is done, you must simply use an Eloquent mutator like this:
/**
* Undoes the setting of the role_id, password, and password_confirmation attributes in anvard
*/
public function setPasswordConfirmationAttribute()
{
$this->__unset('role_id');
$this->__unset('username');
$this->__unset('password');
$this->__unset('password_confirmation');
}
Note that I'm doing this for the password_confirmation attribute because it's the last one that's set here: https://bitbucket.org/atticmedia/anvard/src/e6c6d249abab21d06f674430a061fba4f351e85d/src/Atticmedia/Anvard/Anvard.php?at=master#cl-191
Then if you want to run some standard registration code, it's easy enough to hook into:
Event::listen('Eloquent.saving: User', function($model)
{
if (!$model->exists)
{
//your registration code here
}
});
Comments (9)
-
-
Ahh, it goes in the User model of course. :)
-
Hi - I'm closing this as it doesn't seem to be an issue requiring attention, please resubmit if needed.
-
- changed status to closed
-
I agree with Jan that needs to resolving. I've come across this issue myself and am surprised to not see a config for the Username field.
@pete_otaqui If I were to submit this change in a PR is it likely to be accepted?
Thanks
-
Hi Alex,
I'm afraid I'm not in a position to maintain this project any more. @that0n3guy might be able to help.
-
I'll take a look at it, though I don't have much time at the moment.
-
@that0n3guy I'm happy to do the work, as I see it:
- Add if clause to this line that checks config.
- Add a UserFields array to config/db.php like this:
'addtouser' => array( "username" => true, "email" => true, "password" => true, "password_confirmation" => true ),
Does that sound ok?
-
Create a PR and I'll look at it please.
- Log in to comment
Ahh this is exactly what I'm after - in that I want omit the storing of role_id and password_confirmation. As well as altering the column name case from email/password to Email/Password.
Where would I put the
setPasswordConfirmationAttribute()
function and where would I put yourEvent::listen('Eloquent.saving: User', function($model)
I tried to put
setPasswordConfirmationAttribute()
in the Profile model but didn't work.Thanks in advance,
Jon.