parent
fe7a7e5970
commit
09b547e18f
8 changed files with 161 additions and 13 deletions
@ -0,0 +1,36 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Models; |
||||||
|
|
||||||
|
use Illuminate\Foundation\Auth\User as Authenticatable; |
||||||
|
use Illuminate\Notifications\Notifiable; |
||||||
|
use Laravel\Passport\HasApiTokens; |
||||||
|
use Spatie\Permission\Traits\HasRoles; |
||||||
|
|
||||||
|
class GKUser extends Authenticatable |
||||||
|
{ |
||||||
|
use HasApiTokens, Notifiable, HasRoles; |
||||||
|
|
||||||
|
protected $table = 'gk_user'; |
||||||
|
|
||||||
|
protected $connection = 'db_sunday'; |
||||||
|
|
||||||
|
protected $fillable = [ |
||||||
|
'name', |
||||||
|
'email', |
||||||
|
'password', |
||||||
|
]; |
||||||
|
|
||||||
|
protected $hidden = [ |
||||||
|
'password', |
||||||
|
'remember_token', |
||||||
|
]; |
||||||
|
|
||||||
|
protected function casts(): array |
||||||
|
{ |
||||||
|
return [ |
||||||
|
'email_verified_at' => 'datetime', |
||||||
|
'password' => 'hashed', |
||||||
|
]; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,37 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Passport; |
||||||
|
|
||||||
|
use Laravel\Passport\Bridge\User; |
||||||
|
use Laravel\Passport\Bridge\UserRepository as PassportUserRepository; |
||||||
|
use League\OAuth2\Server\Entities\ClientEntityInterface; |
||||||
|
use Illuminate\Support\Facades\Hash; |
||||||
|
use App\Models\GKUser; |
||||||
|
|
||||||
|
class CustomUserRepository extends PassportUserRepository |
||||||
|
{ |
||||||
|
public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $client) |
||||||
|
{ |
||||||
|
$user = GKUser::where('email', $username)->first(); |
||||||
|
|
||||||
|
if (! $user) { |
||||||
|
return; |
||||||
|
} |
||||||
|
|
||||||
|
$hashed = $user->password; |
||||||
|
|
||||||
|
if (!Hash::needsRehash($hashed)) { |
||||||
|
if (!Hash::check($password, $hashed)) { |
||||||
|
return; |
||||||
|
} |
||||||
|
} elseif (md5($password) !== $hashed) { |
||||||
|
return; |
||||||
|
} else { |
||||||
|
// ✅ Nếu MD5 khớp → nâng cấp lên bcrypt |
||||||
|
$user->password = Hash::make($password); |
||||||
|
$user->save(); |
||||||
|
} |
||||||
|
|
||||||
|
return new User($user->getAuthIdentifier()); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace App\Auth\Providers; |
||||||
|
|
||||||
|
use Illuminate\Auth\EloquentUserProvider; |
||||||
|
use Illuminate\Contracts\Auth\Authenticatable; |
||||||
|
use Illuminate\Support\Facades\Hash; |
||||||
|
|
||||||
|
class MD5UserProvider extends EloquentUserProvider |
||||||
|
{ |
||||||
|
public function validateCredentials(Authenticatable $user, array $credentials) |
||||||
|
{ |
||||||
|
$plain = $credentials['password']; |
||||||
|
$hashed = $user->getAuthPassword(); |
||||||
|
|
||||||
|
// Nếu là Bcrypt → dùng bình thường |
||||||
|
if (!Hash::needsRehash($hashed)) { |
||||||
|
return Hash::check($plain, $hashed); |
||||||
|
} |
||||||
|
|
||||||
|
// Nếu là MD5 → kiểm tra tay |
||||||
|
if (md5($plain) === $hashed) { |
||||||
|
// Tự động upgrade |
||||||
|
$user->password = Hash::make($plain); |
||||||
|
$user->save(); |
||||||
|
|
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue