<commit_before><?php

namespace TypiCMS\Modules\Users\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use TypiCMS\Modules\Core\Filters\FilterOr;
use TypiCMS\Modules\Core\Http\Controllers\BaseApiController;
use TypiCMS\Modules\Users\Models\User;

class ApiController extends BaseApiController
{
    public function index(Request $request): LengthAwarePaginator
    {
        $data = QueryBuilder::for(User::class)
            ->allowedSorts(['first_name', 'last_name', 'email', 'superuser'])
            ->allowedFilters([
                AllowedFilter::custom('first_name,last_name,email', new FilterOr()),
            ])
            ->paginate($request->input('per_page'));

        return $data;
    }

    public function updatePreferences(Request $request): void
    {
        $user = $request->user();
        $user->preferences = array_merge((array) $user->preferences, request()->all());
        $user->save();
    }

    public function destroy(User $user): JsonResponse
    {
        if (method_exists($user, 'mollieCustomerFields')) {
            if ($user->hasRunningSubscription()) {
                return response()->json([
                    'error' => true,
                    'message' => __('The user :name can not be deleted because he has a running subscription.', ['name' => "{$user->first_name} {$user->last_name}"]),
                ], 403);
            }
        }
        $deleted = $user->delete();

        return response()->json([
            'error' => !$deleted,
        ]);
    }
}
<commit_msg>Avoid user deleting himself.<commit_after><?php

namespace TypiCMS\Modules\Users\Http\Controllers;

use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;
use TypiCMS\Modules\Core\Filters\FilterOr;
use TypiCMS\Modules\Core\Http\Controllers\BaseApiController;
use TypiCMS\Modules\Users\Models\User;

class ApiController extends BaseApiController
{
    public function index(Request $request): LengthAwarePaginator
    {
        $data = QueryBuilder::for(User::class)
            ->allowedSorts(['first_name', 'last_name', 'email', 'superuser'])
            ->allowedFilters([
                AllowedFilter::custom('first_name,last_name,email', new FilterOr()),
            ])
            ->paginate($request->input('per_page'));

        return $data;
    }

    public function updatePreferences(Request $request): void
    {
        $user = $request->user();
        $user->preferences = array_merge((array) $user->preferences, request()->all());
        $user->save();
    }

    public function destroy(User $user): JsonResponse
    {
        if (auth()->user()->id === $user->id) {
            return response()->json([
                'error' => true,
                'message' => __('You can not delete yourself.'),
            ], 403);
        }
        if (method_exists($user, 'mollieCustomerFields')) {
            if ($user->hasRunningSubscription()) {
                return response()->json([
                    'error' => true,
                    'message' => __('The user :name can not be deleted because he has a running subscription.', ['name' => "{$user->first_name} {$user->last_name}"]),
                ], 403);
            }
        }
        $deleted = $user->delete();

        return response()->json([
            'error' => !$deleted,
        ]);
    }
}

{'fraction_non_alphanumeric': 0.14483906770255273, 'fraction_numerical': 0.0024972253052164264, 'mean_word_length': 2.5834990059642147, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '510934', 'n_tokens_mistral': 1007, 'n_tokens_neox': 1012, 'n_words': 199}