Skip to content

Commit

Permalink
Merge e64e318 into 11dcd26
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Oct 23, 2019
2 parents 11dcd26 + e64e318 commit c6fc8fc
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 11 deletions.
5 changes: 4 additions & 1 deletion composer.json
Expand Up @@ -32,7 +32,10 @@
"autoload": {
"psr-4": {
"Lab404\\Impersonate\\": "src/"
}
},
"files": [
"src/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
13 changes: 3 additions & 10 deletions src/ImpersonateServiceProvider.php
Expand Up @@ -73,30 +73,23 @@ protected function registerBladeDirectives()
{
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
$bladeCompiler->directive('impersonating', function () {
$guard = $this->app['impersonate']->getCurrentAuthGuardName();
return "<?php if (app()['auth']->guard('$guard')->check() && app()['auth']->guard('$guard')->user()->isImpersonated()): ?>";
return "<?php if (is_impersonating()) : ?>";
});

$bladeCompiler->directive('endImpersonating', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('canImpersonate', function () {
$guard = $this->app['impersonate']->getCurrentAuthGuardName();
return "<?php if (app()['auth']->guard('$guard')->check()
&& app()['auth']->guard('$guard')->user()->canImpersonate()): ?>";
return "<?php if (can_impersonate()) : ?>";
});

$bladeCompiler->directive('endCanImpersonate', function () {
return '<?php endif; ?>';
});

$bladeCompiler->directive('canBeImpersonated', function ($expression) {
$user = trim($expression);
$guard = $this->app['impersonate']->getCurrentAuthGuardName();

return "<?php if (app()['auth']->guard('$guard')->check()
&& app()['auth']->guard('$guard')->user()->id != {$user}->id && {$user}->canBeImpersonated()): ?>";
return "<?php if (can_be_impersonated({$expression})) : ?>";
});

$bladeCompiler->directive('endCanBeImpersonated', function () {
Expand Down
54 changes: 54 additions & 0 deletions src/helpers.php
@@ -0,0 +1,54 @@
<?php

use Illuminate\Contracts\Auth\Authenticatable;

if (! function_exists('can_impersonate')) {

/**
* Check whether the current user is authorized to impersonate.
*
* @param null $guard
* @return bool
*/
function can_impersonate(string $guard = null): bool
{
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();

return app('auth')->guard($guard)->check() && app('auth')->guard($guard)->canImpersonate();
}
}

if (! function_exists('can_be_impersonated')) {

/**
* Check whether the specified user can be impersonated.
*
* @param Authenticatable $user
* @param string|null $guard
* @return bool
*/
function can_be_impersonated(Authenticatable $user, string $guard = null): bool
{
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();

return app('auth')->guard($guard)->check()
&& app('auth')->guard($guard)->user()->id != $user->id
&& $user->canBeImpersonated();
}
}

if (! function_exists('is_impersonating')) {

/**
* Check whether the current user is being impersonated.
*
* @param string|null $guard
* @return bool
*/
function is_impersonating(string $guard = null): bool
{
$guard = $guard ?? app('impersonate')->getCurrentAuthGuardName();

return app('auth')->guard($guard)->check() && app('auth')->guard($guard)->user()->isImpersonated();
}
}

0 comments on commit c6fc8fc

Please sign in to comment.