<commit_before><?php

namespace Sebastienheyd\Boilerplate\Controllers\Auth;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->redirectTo = route(config('boilerplate.app.redirectTo','boilerplate.home'));

        $userModel = config('auth.providers.users.model');
        $this->firstUser = $userModel::all()->count() === 0;
        
        if(!$this->firstUser && !config('boilerplate.auth.register')) {
            abort('404');
        }

        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'last_name' => 'required|max:255',
            'first_name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => '<PASSWORD>',
        ]);
    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return view('boilerplate::auth.register', ['firstUser' => $this->firstUser]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $userModel = config('auth.providers.users.model');
        $roleModel = config('laratrust.role');

        $user = $userModel::create([
            'active' => true,
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => <PASSWORD>($data['password']),
            'last_login' => Carbon::now()->toDateTimeString()
        ]);

        if ($this->firstUser) {
            $admin = $roleModel::whereName('admin')->first();
            $user->attachRole($admin);
        } else {
            $role = $roleModel::whereName(config('boilerplate.auth.register_role'))->first();
            $user->attachRole($role);
        }

        return $user;
    }
}
<commit_msg>Fix user registration when user is deleted<commit_after><?php

namespace Sebastienheyd\Boilerplate\Controllers\Auth;

use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->redirectTo = route(config('boilerplate.app.redirectTo','boilerplate.home'));

        $userModel = config('auth.providers.users.model');
        $this->firstUser = $userModel::all()->count() === 0;
        
        if(!$this->firstUser && !config('boilerplate.auth.register')) {
            abort('404');
        }

        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'last_name' => 'required|max:255',
            'first_name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users,email,NULL,id,deleted_at,NULL',
            'password' => '<PASSWORD>',
        ]);
    }

    /**
     * Show the application registration form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showRegistrationForm()
    {
        return view('boilerplate::auth.register', ['firstUser' => $this->firstUser]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $userModel = config('auth.providers.users.model');
        $roleModel = config('laratrust.role');

        $user = $userModel::withTrashed()->updateOrCreate(['email' => $data['email']], [
            'active' => true,
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => <PASSWORD>($<PASSWORD>['password']),
            'last_login' => Carbon::now()->toDateTimeString()
        ]);

        if ($this->firstUser) {
            $admin = $roleModel::whereName('admin')->first();
            $user->attachRole($admin);
        } else {
            $user->restore();
            $role = $roleModel::whereName(config('boilerplate.auth.register_role'))->first();
            $user->roles()->sync([$role->id]);
        }

        return $user;
    }
}

{'fraction_non_alphanumeric': 0.18469191270860078, 'fraction_numerical': 0.00417201540436457, 'mean_word_length': 1.9894484412470024, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5889996', 'n_tokens_mistral': 1706, 'n_tokens_neox': 1672, 'n_words': 346}