F
Filament7mo ago
ruddy

impersonate unverified user

I'm facing an issue where I'm unable to impersonate a user who hasn't verified their email. As an admin, I'm trying to impersonate the user even though their email is unverified. However, the system still prompts me to verify their email. Could you please provide guidance on this?
8 Replies
Matthew
Matthew7mo ago
You need to check the user model, if you have the requirement, that a user needs to have verified email If that's so, then you cannot impersonate
ruddy
ruddy7mo ago
As an admin, I believe I am able to do that by bypassing this requirement. Something like this:
return $panel
->id('contractor')
->path('contractor')
->colors([
'primary' => Color::Green,
])
->login()
->passwordReset()
->emailVerification() //Here you are forcing the user to verify email
return $panel
->id('contractor')
->path('contractor')
->colors([
'primary' => Color::Green,
])
->login()
->passwordReset()
->emailVerification() //Here you are forcing the user to verify email
And I have checked this class EmailVerificationPrompt

public function mount(): void
{
/** @var MustVerifyEmail $user */
$user = Filament::auth()->user();

before:
if ($user->hasVerifiedEmail()) { //To me the issue is here
redirect()->intended(Filament::getUrl());
}

//If there is a way to inject a boolean into the mount function to instruct the system to verify the email if the boolean is true, otherwise not.
after:

if ($user->hasVerifiedEmail() && $shouldVerifyEmail) { //shouldVerifyEmail will be the boolean from panel configuration
redirect()->intended(Filament::getUrl());
}

}

public function mount(): void
{
/** @var MustVerifyEmail $user */
$user = Filament::auth()->user();

before:
if ($user->hasVerifiedEmail()) { //To me the issue is here
redirect()->intended(Filament::getUrl());
}

//If there is a way to inject a boolean into the mount function to instruct the system to verify the email if the boolean is true, otherwise not.
after:

if ($user->hasVerifiedEmail() && $shouldVerifyEmail) { //shouldVerifyEmail will be the boolean from panel configuration
redirect()->intended(Filament::getUrl());
}

}
return $panel
->id('contractor')
->path('contractor')
->colors([
'primary' => Color::Green,
])
->login()
->passwordReset()
->emailVerification(fn() => auth()->user()->isAdmin() ? false : true)
return $panel
->id('contractor')
->path('contractor')
->colors([
'primary' => Color::Green,
])
->login()
->passwordReset()
->emailVerification(fn() => auth()->user()->isAdmin() ? false : true)
My question is, is there another way to currently achieve something like this?
Jordy
Jordy7mo ago
just fill in the 'email verified at' col on said user or just yk, actually verify the email? if you dont want it disable it? its yes or no xd
ruddy
ruddy7mo ago
I (admin) don't want to mark 'email verified' because I still need regular users to verify their emails.
Jordy
Jordy7mo ago
why do u even need to impersonate them? sounds like bad practice to begin with only in tests you "act as" a user
awcodes
awcodes7mo ago
Create a custom EmailVerification class and pass that into the emailVerification() method
Hussain4real
Hussain4real7mo ago
use Laravel Gate before() method to bypass as a super admin
ruddy
ruddy7mo ago
I ended up fixing the issue like this.
public function canImpersonate()
{
return $this->isSuperAdmin();
}

public function hasVerifiedEmail()
{
$manager = app()->make(ImpersonateManager::class);
$admin = $manager->getImpersonator();

if(is_null($admin)) {
return ! is_null($this->email_verified_at);
}

return true;
}
public function canImpersonate()
{
return $this->isSuperAdmin();
}

public function hasVerifiedEmail()
{
$manager = app()->make(ImpersonateManager::class);
$admin = $manager->getImpersonator();

if(is_null($admin)) {
return ! is_null($this->email_verified_at);
}

return true;
}
And customer email verification works as well. Thank you all.
Want results from more Discord servers?
Add your server
More Posts