Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manually add Auth guard to distinguish between different scopes. #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ By default, the package will disable itself on any domains that don't have a TLD

The path to the application User model. This will be used to retrieve the users displayed in the select dropdown. This must be an Eloquent Model instance. This is set to `App\User` by default.

**sudosu.current_guard `string`**

If your site has multiple login modules, you can set the `Auth guard` to select who you are going to use `sudo-su`.


## Disclaimer - DANGER!
This package can pose a serious security issue if used incorrectly, as anybody will be able to take control of any user's account. Please ensure that the service provider is only registered when the app is in a debug/local environment.
Expand Down
16 changes: 15 additions & 1 deletion config/sudosu.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,18 @@

'user_model' => App\User::class

];

/*
|-------------------------------------------------------------------------
| Auth Guard
|-------------------------------------------------------------------------
| If in my application has more than one domain and login entry.
| For example:
| the 'www.domain.com' guard is 'web',
| the 'bbs.domain.com' guard is 'bbs'
| So, I wangt to set the current guard is 'bbs'
|-------------------------------------------------------------------------
*/
'current_guard' => 'bbs',

];
8 changes: 4 additions & 4 deletions resources/views/user-selector.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
<div class="sudoSu__interface {{ $hasSudoed ? 'sudoSu__interface--hasSudoed' : '' }} hidden" id="sudosu-js-interface">
@if ($hasSudoed)
<div class="sudoSu__infoLine">
You are using account: <span>{{ $currentUser->name }}</span>
You are using account: <span>{{ isset($currentUser) && $currentUser ? $currentUser->name : '' }}</span>
</div>

@if ($originalUser)
<div class="sudoSu__infoLine">
You are logged in as: <span>{{ $originalUser->name }}</span>
You are logged in as: <span>{{ isset($originalUser) && $originalUser ? $originalUser->name : '' }}</span>
</div>
@endif

<form action="{{ route('sudosu.logout') }}" method="post">
{!! csrf_field() !!}
<input type="submit" class="sudoSu__resetBtn" value="{{ $originalUser ? 'Return to original user' : 'Log out' }}">
<input type="submit" class="sudoSu__resetBtn" value="{{ isset($originalUser) && $originalUser ? 'Return to original user' : 'Log out' }}">
</form>
@endif

Expand All @@ -45,4 +45,4 @@
const element = document.getElementById('sudosu-js-interface');

btn.addEventListener('click', event => element.classList.toggle('hidden'));
</script>
</script>
4 changes: 3 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,14 @@ protected function registerViews()
// Add an inline view composer for the user-selector
View::composer('sudosu::user-selector', function ($view) {
$sudosu = App::make(SudoSu::class);

$guard = $sudosu->getCurrentGuard();

$view->with([
'users' => $sudosu->getUsers(),
'hasSudoed' => $sudosu->hasSudoed(),
'originalUser' => $sudosu->getOriginalUser(),
'currentUser' => Auth::user()
'currentUser' => Auth::guard($guard)->user()
]);
});
}
Expand Down
9 changes: 8 additions & 1 deletion src/SudoSu.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ class SudoSu
protected $session;
protected $sessionKey = 'sudosu.original_id';
protected $usersCached = null;
protected $currentGuard;

public function __construct(Application $app, AuthManager $auth, SessionManager $session)
{
$this->app = $app;
$this->auth = $auth;
$this->currentGuard = $this->getCurrentGuard();
$this->auth = $auth->guard($this->currentGuard);
$this->session = $session;
}

Expand Down Expand Up @@ -113,4 +115,9 @@ protected function getUserModel()
$userModel = Config::get('sudosu.user_model');
return $this->app->make($userModel);
}

public function getCurrentGuard()
{
return Config::get('sudosu.current_guard');
}
}