Setting of certain user values

Issue #8 closed
Jan Hartigan created an issue

I'm loving this package so far, but I immediately ran into the issue caused by this section of code:

https://bitbucket.org/atticmedia/anvard/src/e6c6d249abab21d06f674430a061fba4f351e85d/src/Atticmedia/Anvard/Anvard.php?at=master#cl-186

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)

  1. Jon Whittlestone

    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 your Event::listen('Eloquent.saving: User', function($model)

    I tried to put setPasswordConfirmationAttribute() in the Profile model but didn't work.

    Thanks in advance,

    Jon.

  2. Pete Otaqui

    Hi - I'm closing this as it doesn't seem to be an issue requiring attention, please resubmit if needed.

  3. Alex Scotton

    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

  4. Pete Otaqui

    Hi Alex,

    I'm afraid I'm not in a position to maintain this project any more. @that0n3guy might be able to help.

  5. Alex Scotton

    @that0n3guy I'm happy to do the work, as I see it:

    'addtouser' => array(
            "username" => true,
            "email" => true,
            "password" => true,
            "password_confirmation" => true
        ),
    

    Does that sound ok?

  6. Log in to comment