laravellaravel-fortify

How to modify fortify CreatesNewUsers.php interface?


I need to modify /vendor/laravel/fortify/src/Contracts/CreatesNewUsers.php interface and to add 1 more bool parameter, as using CreateNewUser in different places of the app validations rules are different, say in some places password is not filled on user creation, but must be separate function.

So I copied file /project/resources/fortify/CreatesNewUsers.php with content :

<?php

namespace Laravel\Fortify\Contracts;

interface CreatesNewUsers
{
    public function create(array $input, bool $makeValidation);
}

and in app/Actions/Fortify/CreateNewUser.php I modified :

<?php

namespace App\Actions\Fortify;

use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
//use Laravel\Fortify\Contracts\CreatesNewUsers;
use Resources\Fortify\CreatesNewUsers;  // Reference to my interface
use Laravel\Jetstream\Jetstream;

class CreateNewUser implements CreatesNewUsers
{
    use PasswordValidationRules;
public function create(array $input, bool $makeValidation)
    {
...

But trying to use this class I got error

Interface "Resources\Fortify\CreatesNewUsers" not found

Which is the valid way ?

Thanks!


Solution

  • I moved interface at file app/Actions/Fortify/CreatesNewUsers.php :

    <?php
    namespace App\Actions\Fortify;
    
    interface CreatesNewUsers
    {
        public function create(array $input, bool $make_validation, array $hasPermissions);
    }
    

    and modified app/Actions/Fortify/CreateNewUser.php :

    <?php
    
    namespace App\Actions\Fortify;
    
    use App\Models\User;
    use Illuminate\Database\QueryException;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Support\Facades\Validator;
    use DB;
    
    use App\Actions\Fortify\CreatesNewUsers;
    
    use Laravel\Jetstream\Jetstream;
    use Spatie\Permission\Models\Permission;
    
    class CreateNewUser implements CreatesNewUsers
    {
        use PasswordValidationRules;
    
        /**
         * Validate and create a newly registered user.
         *
         * @param array $input
         *
         * @return \App\Models\User
         */
        public function create(array $input, bool $make_validation, array $hasPermissions)
        {
            if ($make_validation) {
                $userValidationRulesArray = User::getUserValidationRulesArray(null, '', []);
                if (\App::runningInConsole()) {
                    unset($userValidationRulesArray['password_2']);
                }
    
    
                $validator = Validator::make($input, $userValidationRulesArray);//->validate();
                if ($validator->fails()) {
                    $errorMsg = $validator->getMessageBag();
                    if (\App::runningInConsole()) {
                        echo '::$errorMsg::' . print_r($errorMsg, true) . '</pre>';
                    }
    
                    return $errorMsg;
                }
            } // if($make_validation) {
    
            $newUserData = [
                'name'         => $input['name'],
                'email'        => $input['email'],
                'account_type' => $input['account_type'],
                'phone'        => $input['phone'],
                'website'      => $input['website'],
                'notes'        => $input['notes'],
                'first_name'   => $input['first_name'],
                'last_name'    => $input['last_name'],
            ];
            if (isset($input['password'])) {
                $newUserData['password'] = Hash::make($input['password']);
            }
            if (isset($input['status'])) {
                $newUserData['status'] = $input['status'];
            }
            if (isset($input['activated_at'])) {
                $newUserData['activated_at'] = $input['activated_at'];
            }
            if (isset($input['avatar'])) {
                $newUserData['avatar'] = $input['avatar'];
            }
    
            try {
                DB::beginTransaction();
    
                $newUser = User::create($newUserData);
                foreach ($hasPermissions as $nextHasPermission) {
                    $appAdminPermission = Permission::findByName($nextHasPermission);
                    if ($appAdminPermission) {
                        $newUser->givePermissionTo($appAdminPermission);
                    }
    
                }
                DB::commit();
                return $newUser;
    
            } catch (QueryException $e) {
                DB::rollBack();
                if (\App::runningInConsole()) {
                    echo '::$e->getMessage()::' . print_r($e->getMessage(), true) . '</pre>';
                }
            }
            return false;
        }
    }
    

    It allows me to use CreateNewUser from different parts of app, like seeders, adminarea, user registration with different behaviour. For me it seems good way of using fortify and CreateNewUser...