DirectoryTree / LdapRecord-Laravel

Multi-domain LDAP Authentication & Management for Laravel.
https://ldaprecord.com/docs/laravel/v3
MIT License
501 stars 53 forks source link

[Bug] When using Jetstream with Teams, default team is not created. #298

Closed murrant closed 3 years ago

murrant commented 3 years ago

Environment (please complete the following information):

Describe the bug: When using Fortify to authenticate with LdapRecord, the initial user creation does not create the default team "[User]'s Team". It is assumed this team is created so you get errors because the user has no team.

stevebauman commented 3 years ago

Hi @murrant!

This isn't actually a bug with LdapRecord-Laravel -- this is because Laravel Jetstream assumes the creation of new users will occur through it's interface, which executes the CreateNewUser action that is published into the app folder, thereby creating the default team if it does not exist:

https://github.com/laravel/jetstream/blob/2.x/stubs/app/Actions/Fortify/CreateNewUserWithTeams.php

Since this action is published (i.e. cannot be assumed to be the same in every Laravel Jetstream application) and validates data for user creation, we cannot utilize it.

I would suggest simply listening for the LdapRecord-Laravel Imported event and then creating the team there, as it's done inside the CreateNewUser action:

https://github.com/DirectoryTree/LdapRecord-Laravel/blob/master/src/Events/Import/Imported.php

public function handle(Imported $event)
{
    $user = $event->eloquent;

    $user->ownedTeams()->save(Team::forceCreate([
        'user_id' => $user->id,
        'name' => explode(' ', $user->name, 2)[0]."'s Team",
        'personal_team' => true,
    ]));
}
murrant commented 3 years ago

Perhaps this is just a missing documentation issue then :) Thanks for the detailed response.

stevebauman commented 3 years ago

You're right -- this should be added to the docs! 👍

I'll re-open this issue and then close it again once I've updated them.

stevebauman commented 3 years ago

Completed: http://ldaprecord.com/docs/laravel/v2/auth/database/laravel-jetstream/#default-team-assignment

murrant commented 3 years ago

Great! Thanks @stevebauman