❓ Support
J James Pence Dec 11, 2024

Seemless Laravel (11) user integration

Hello,
I'm looking for a suggested solution to prevent having a Create Account form show, I'd rather the user already managed by the base application be allowed, based on permission, to have a proper user automatically in the waterhole authorization system..
Create form.png
I was thinking of having a middleware run for the Waterhole routes that will add the desired user to the waterhole users table in the case of accessing with a "new" Laravel user. Unless there is something already in waterhole that has eluded me so far.

Thank you,
James

⁨7⁩ ⁨Comments⁩

In reply to G Gabriel Silva

I did and the toWaterholeUser function is getting the values to populate the form, but the form is still showing and expecting the Create Account submission button to be pressed to actually add the user to the waterhole users table.

Toby Toby Waterhole Founder Dec 13, 2024
In reply to J James Pence

Hi @James Pence, this is a current limitation of the Laravel auth integration, to be added in a future release. Your proposed workaround sounds reasonable - let me know how this goes, here to answer any questions if needed.

You can do something like this:

On your registration form/admin user page, you can add waterhole user after you created your base user.

Screenshot 2024-12-17 at 10.45.40 AM.png
Screenshot 2024-12-17 at 10.45.51 AM.png

Update the columns base on your needs. You can even add them to a specific group.
You can add a listener/observer to always sync them on created/updated/deleted etc.

In reply to G Gabriel Silva

I created a simplified pair of models in my namespace WaterholeUser and WaterholeRole. they correspond to User and Group within waterhole.
I also have permissions of "forum_use", and "forum_manage" assigned to roles in the main app.

// in base application User.php
 public function toWaterholeUser(): ?PendingUser
        {
            if( $this->email ){
                // Check if the user already exists in Waterhole.dev
                $waterholeUser = WaterholeUser::getUserByEmail( $this->email );
                if( !$waterholeUser ){
                    // Create a new Waterhole.dev user account
                    $waterholeUser = WaterholeUser::createDefaultUser( [
                                                                           'email'    => $this->email,
                                                                           'username' => $this->getFullnameAttribute(),
                                                                       ] );
                    // Update the Waterhole.dev user's roles to match the Laravel user's roles
                    $role_ids=[];
                    if( $this->hasPermissionTo( 'forum_use' ) ){
                        $role_ids[] =[ WaterholeRole::where( 'name', 'Member' )->first()->id ];
                    }else if( $this->hasPermissionTo( 'forum_manage' ) ){
                        $role_ids[] = WaterholeRole::where( 'name','Admin')->first()->id;
                    }else {
                        $role_ids[] = WaterholeRole::where( 'name','Guest')->first()->id;
                    }

                    $waterholeUser->roles()->sync( $role_ids );
                }
            }

            return new PendingUser(
                identifier: $this->getAuthIdentifier(),
                email: $this->email,
                name: $this->getFullnameAttribute()
            );
    }