1. Go to this page and download the library: Download verseles/possession library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
verseles / possession example snippets
use Verseles\Possession\Traits\ImpersonatesUsers;
class User extends Authenticatable
{
use ImpersonatesUsers;
// Add your possession logic
public function canPossess()
{
return $this->is_admin; // Your admin check logic
}
public function canBePossessed()
{
return !$this->is_admin; // Example restriction
}
}
use Verseles\Possession\Facades\Possession;
// Possess a user (accepts ID, email, or User model instance)
try {
Possession::possess($targetUser);
} catch (\Verseles\Possession\Exceptions\ImpersonationException $e) {
// Handle exception
return redirect()->back()->withErrors(['impersonation' => $e->getMessage()]);
}
// Stop possessing
try {
Possession::unpossess();
} catch (\Verseles\Possession\Exceptions\ImpersonationException $e) {
// Handle exception
return redirect()->back()->withErrors(['impersonation' => $e->getMessage()]);
}