api code global jain
This commit is contained in:
57
app/Rules/CheckUniqueEmailHashValue.php
Normal file
57
app/Rules/CheckUniqueEmailHashValue.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
namespace App\Rules;
|
||||
|
||||
use Illuminate\Contracts\Validation\Rule;
|
||||
use App\Models\User;
|
||||
|
||||
class CheckUniqueEmailHashValue implements Rule
|
||||
{
|
||||
|
||||
public $id;
|
||||
|
||||
/**
|
||||
* Create a new rule instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($id = null)
|
||||
{
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the validation rule passes.
|
||||
*
|
||||
* @param string $attribute
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
public function passes($attribute, $value)
|
||||
{
|
||||
$hashed = md5($value);
|
||||
$user = User::where('email_hash', $hashed);
|
||||
|
||||
if (isset($this->id) && !empty($this->id)) {
|
||||
$user = $user->where('id', '!=', $this->id);
|
||||
}
|
||||
|
||||
$user = $user->first();
|
||||
|
||||
if ($user === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation error message.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function message()
|
||||
{
|
||||
return trans('validation.unique', ['attribute' => 'email']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user