diff --git a/docs/customization/custom-guard.md b/docs/customization/custom-guard.md
index 4e8f51a031..7e4a701653 100644
--- a/docs/customization/custom-guard.md
+++ b/docs/customization/custom-guard.md
@@ -1,14 +1,87 @@
# Custom guard
Starting with Voyager 1.2 you can define a \(custom\) guard which is used throughout Voyager.
-To do so, just bind your auth-guard to `VoyagerAuth`.
-Open your `AuthServiceProvider` and add the following to the register method:
+To do so, just bind the name of your auth-guard to `VoyagerGuard`.
+First, make sure you have defined a guard as per the [Laravel documentation](https://laravel.com/docs/authentication#adding-custom-guards).
+After that open your `AuthServiceProvider` and add the following to the register method:
```php
-$this->app->singleton('VoyagerAuth', function () {
- return Auth::guard('your-custom-guard');
+$this->app->singleton('VoyagerGuard', function () {
+ return 'your-custom-guard-name';
});
```
Now this guard is used instead of the default guard.
+
+# Example - using a different model and table for Admins
+
+First you have to create a new table. Let's call it `admins`:
+```php
+bigIncrements('id');
+ $table->bigInteger('role_id')->unsigned()->nullable();
+ $table->string('name');
+ $table->string('email')->unique();
+ $table->string('avatar')->nullable()->default('users/default.png');
+ $table->string('password')->nullable();
+ $table->string('remember_token')->nullable();
+ $table->text('settings')->nullable()->default(null);
+ $table->timestamps();
+ $table->foreign('role_id')->references('id')->on('roles');
+});
+```
+
+and a model which extends Voyagers user-model:
+
+```php
+ [
+ 'admin' => [
+ 'driver' => 'session',
+ 'provider' => 'admins',
+ ],
+
+ // ...
+],
+```
+And a user provider called `admins`:
+```
+'providers' => [
+ 'admins' => [
+ 'driver' => 'eloquent',
+ 'model' => App\Admin::class,
+ ],
+
+ // ...
+],
+```
+
+Next you have to tell Voyager to use your new guard.
+Open you `AppServiceProvider.php` and add the following to the `register` method:
+
+```php
+public function register()
+{
+ $this->app->singleton('VoyagerGuard', function () {
+ return 'admin';
+ });
+}
+```
+
+{% hint style="info" %}
+Please note that the user-bread is still responsible to edit users - not admins.
+Create a BREAD for the `admins` table if you want to change Admins.
+{% endhint %}
\ No newline at end of file
diff --git a/docs/getting-started/upgrading.md b/docs/getting-started/upgrading.md
index 482e462923..873974039f 100644
--- a/docs/getting-started/upgrading.md
+++ b/docs/getting-started/upgrading.md
@@ -15,6 +15,21 @@ To update to the latest version inside of your composer.json file make sure to u
And then run composer update
-## Troubleshooting
+### Changes to VoyagerAuth
+The `VoyagerAuth` singleton was introduced in Voyager 1.2 and returned an instance of the guard.
+In Voyager 1.3 this singleton was renamed to `VoyagerGuard` and now returns the name of the guard as a string.
+Read more on custom guards [here](../customization/custom-guard.md)
+
+## Update Configuration
+The `voyager.php` configuration file had a few changes.
+
+```
+'user' => [
+ 'namespace' => null,
+],
+```
+was removed. The user-model which will be used in the `voyager:admin` command is now determined based on the [guard](../customization/custom-guard.md).
+
+### Troubleshooting
Be sure to ask us on our slack channel if you are experiencing any issues and we will try and assist. Thanks.
diff --git a/publishable/config/voyager.php b/publishable/config/voyager.php
index 3048b3df31..f56eeb539e 100644
--- a/publishable/config/voyager.php
+++ b/publishable/config/voyager.php
@@ -13,11 +13,6 @@
'user' => [
'add_default_role_on_register' => true,
'default_role' => 'user',
- // Set `namespace` to `null` to use `config('auth.providers.users.model')` value
- // Set `namespace` to a class to override auth user model.
- // However make sure the appointed class must ready to use before installing voyager.
- // Otherwise `php artisan voyager:install` will fail with class not found error.
- 'namespace' => null,
'default_avatar' => 'users/default.png',
'redirect' => '/admin',
],
diff --git a/publishable/config/voyager_dummy.php b/publishable/config/voyager_dummy.php
index e0703ad471..62e72051d4 100644
--- a/publishable/config/voyager_dummy.php
+++ b/publishable/config/voyager_dummy.php
@@ -13,7 +13,6 @@
'user' => [
'add_default_role_on_register' => true,
'default_role' => 'user',
- 'namespace' => null,
'default_avatar' => 'users/default.png',
'redirect' => '/admin',
],
diff --git a/resources/views/dashboard/navbar.blade.php b/resources/views/dashboard/navbar.blade.php
index bf25d41eaf..6fad2afb05 100644
--- a/resources/views/dashboard/navbar.blade.php
+++ b/resources/views/dashboard/navbar.blade.php
@@ -41,8 +41,8 @@ class="caret">
-
{{ app('VoyagerAuth')->user()->name }}
- {{ app('VoyagerAuth')->user()->email }}
+ {{ Auth::user()->name }}
+ {{ Auth::user()->email }}
diff --git a/resources/views/master.blade.php b/resources/views/master.blade.php
index 90fcbc3414..075dc8fc08 100644
--- a/resources/views/master.blade.php
+++ b/resources/views/master.blade.php
@@ -60,10 +60,10 @@
user()->avatar, 'http://') || \Illuminate\Support\Str::startsWith(app('VoyagerAuth')->user()->avatar, 'https://')) {
- $user_avatar = app('VoyagerAuth')->user()->avatar;
+if (\Illuminate\Support\Str::startsWith(Auth::user()->avatar, 'http://') || \Illuminate\Support\Str::startsWith(Auth::user()->avatar, 'https://')) {
+ $user_avatar = Auth::user()->avatar;
} else {
- $user_avatar = Voyager::image(app('VoyagerAuth')->user()->avatar);
+ $user_avatar = Voyager::image(Auth::user()->avatar);
}
?>
diff --git a/resources/views/profile.blade.php b/resources/views/profile.blade.php
index ba4b549139..448f96284c 100644
--- a/resources/views/profile.blade.php
+++ b/resources/views/profile.blade.php
@@ -13,13 +13,15 @@
-
-
{{ ucwords(app('VoyagerAuth')->user()->name) }}
-
{{ ucwords(app('VoyagerAuth')->user()->email) }}
-
{{ app('VoyagerAuth')->user()->bio }}
-
{{ __('voyager::profile.edit') }}
+ alt="{{ Auth::user()->name }} avatar">
+
{{ ucwords(Auth::user()->name) }}
+
{{ ucwords(Auth::user()->email) }}
+
{{ Auth::user()->bio }}
+ @if ($route != '')
+
{{ __('voyager::profile.edit') }}
+ @endif
@stop
diff --git a/src/Commands/AdminCommand.php b/src/Commands/AdminCommand.php
index 498cf1d984..dfc70ebce4 100644
--- a/src/Commands/AdminCommand.php
+++ b/src/Commands/AdminCommand.php
@@ -3,7 +3,9 @@
namespace TCG\Voyager\Commands;
use Illuminate\Console\Command;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Str;
use Symfony\Component\Console\Input\InputOption;
use TCG\Voyager\Facades\Voyager;
@@ -116,7 +118,8 @@ protected function getUser($create = false)
{
$email = $this->argument('email');
- $model = config('voyager.user.namespace') ?: config('auth.providers.users.model');
+ $model = Auth::guard(app('VoyagerGuard'))->getProvider()->getModel();
+ $model = Str::start($model, '\\');
// If we need to create a new user go ahead and create it
if ($create) {
@@ -138,13 +141,13 @@ protected function getUser($create = false)
$this->info('Creating admin account');
- return $model::create([
+ return call_user_func($model.'::create', [
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
]);
}
- return $model::where('email', $email)->firstOrFail();
+ return call_user_func($model.'::where', 'email', $email)->firstOrFail();
}
}
diff --git a/src/FormFields/MediaPickerHandler.php b/src/FormFields/MediaPickerHandler.php
index 9bc5815f7f..cba81dc889 100644
--- a/src/FormFields/MediaPickerHandler.php
+++ b/src/FormFields/MediaPickerHandler.php
@@ -2,6 +2,7 @@
namespace TCG\Voyager\FormFields;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
class MediaPickerHandler extends AbstractHandler
@@ -26,7 +27,7 @@ public function createContent($row, $dataType, $dataTypeContent, $options)
}
if (isset($options->base_path)) {
- $options->base_path = str_replace('{uid}', app('VoyagerAuth')->user()->getKey(), $options->base_path);
+ $options->base_path = str_replace('{uid}', Auth::user()->getKey(), $options->base_path);
if (Str::contains($options->base_path, '{date:')) {
$options->base_path = preg_replace_callback('/\{date:([^\/\}]*)\}/', function ($date) {
return \Carbon\Carbon::now()->format($date[1]);
diff --git a/src/Http/Controllers/Controller.php b/src/Http/Controllers/Controller.php
index 68f85834ec..ffd4ecbfd5 100644
--- a/src/Http/Controllers/Controller.php
+++ b/src/Http/Controllers/Controller.php
@@ -276,21 +276,4 @@ protected function getFieldsWithValidationRules($fieldsConfig)
return !empty($value->details->validation->rule);
});
}
-
- /**
- * Authorize a given action for the current user.
- *
- * @param mixed $ability
- * @param mixed|array $arguments
- *
- * @throws \Illuminate\Auth\Access\AuthorizationException
- *
- * @return \Illuminate\Auth\Access\Response
- */
- public function authorize($ability, $arguments = [])
- {
- $user = app('VoyagerAuth')->user();
-
- return $this->authorizeForUser($user, $ability, $arguments);
- }
}
diff --git a/src/Http/Controllers/VoyagerAuthController.php b/src/Http/Controllers/VoyagerAuthController.php
index 34d4c6ab97..83562212af 100644
--- a/src/Http/Controllers/VoyagerAuthController.php
+++ b/src/Http/Controllers/VoyagerAuthController.php
@@ -4,6 +4,7 @@
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
class VoyagerAuthController extends Controller
@@ -61,6 +62,6 @@ public function redirectTo()
*/
protected function guard()
{
- return app('VoyagerAuth');
+ return Auth::guard(app('VoyagerGuard'));
}
}
diff --git a/src/Http/Controllers/VoyagerBaseController.php b/src/Http/Controllers/VoyagerBaseController.php
index 693eb15c2e..ef5790fb82 100644
--- a/src/Http/Controllers/VoyagerBaseController.php
+++ b/src/Http/Controllers/VoyagerBaseController.php
@@ -5,6 +5,7 @@
use Exception;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use TCG\Voyager\Database\Schema\SchemaManager;
use TCG\Voyager\Events\BreadDataAdded;
@@ -83,7 +84,7 @@ public function index(Request $request)
}
// Use withTrashed() if model uses SoftDeletes and if toggle is selected
- if ($model && in_array(SoftDeletes::class, class_uses($model)) && app('VoyagerAuth')->user()->can('delete', app($dataType->model_name))) {
+ if ($model && in_array(SoftDeletes::class, class_uses($model)) && Auth::user()->can('delete', app($dataType->model_name))) {
$usesSoftDeletes = true;
if ($request->get('showSoftDeleted')) {
diff --git a/src/Http/Controllers/VoyagerController.php b/src/Http/Controllers/VoyagerController.php
index f6721a0be6..2b72a656fe 100644
--- a/src/Http/Controllers/VoyagerController.php
+++ b/src/Http/Controllers/VoyagerController.php
@@ -3,6 +3,7 @@
namespace TCG\Voyager\Http\Controllers;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
@@ -19,7 +20,7 @@ public function index()
public function logout()
{
- app('VoyagerAuth')->logout();
+ Auth::logout();
return redirect()->route('voyager.login');
}
diff --git a/src/Http/Controllers/VoyagerMediaController.php b/src/Http/Controllers/VoyagerMediaController.php
index 32f9fd7db4..ccea6f4b76 100644
--- a/src/Http/Controllers/VoyagerMediaController.php
+++ b/src/Http/Controllers/VoyagerMediaController.php
@@ -4,6 +4,7 @@
use Exception;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
@@ -231,7 +232,7 @@ public function upload(Request $request)
$name = get_file_name($name);
}
} else {
- $name = str_replace('{uid}', app('VoyagerAuth')->user()->getKey(), $request->get('filename'));
+ $name = str_replace('{uid}', Auth::user()->getKey(), $request->get('filename'));
if (Str::contains($name, '{date:')) {
$name = preg_replace_callback('/\{date:([^\/\}]*)\}/', function ($date) {
return \Carbon\Carbon::now()->format($date[1]);
diff --git a/src/Http/Controllers/VoyagerUserController.php b/src/Http/Controllers/VoyagerUserController.php
index 3e4856b52a..69e403af04 100644
--- a/src/Http/Controllers/VoyagerUserController.php
+++ b/src/Http/Controllers/VoyagerUserController.php
@@ -3,23 +3,32 @@
namespace TCG\Voyager\Http\Controllers;
use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
class VoyagerUserController extends VoyagerBaseController
{
public function profile(Request $request)
{
- return Voyager::view('voyager::profile');
+ $route = '';
+ $dataType = Voyager::model('DataType')->where('model_name', Auth::guard(app('VoyagerGuard'))->getProvider()->getModel())->first();
+ if (!$dataType && app('VoyagerGuard') == 'web') {
+ $route = route('voyager.users.edit', Auth::user()->getKey());
+ } elseif ($dataType) {
+ $route = route('voyager.'.$dataType->slug.'.edit', Auth::user()->getKey());
+ }
+
+ return Voyager::view('voyager::profile', compact('route'));
}
// POST BR(E)AD
public function update(Request $request, $id)
{
- if (app('VoyagerAuth')->user()->getKey() == $id) {
+ if (Auth::user()->getKey() == $id) {
$request->merge([
- 'role_id' => app('VoyagerAuth')->user()->role_id,
- 'user_belongsto_role_relationship' => app('VoyagerAuth')->user()->role_id,
- 'user_belongstomany_role_relationship' => app('VoyagerAuth')->user()->roles->pluck('id')->toArray(),
+ 'role_id' => Auth::user()->role_id,
+ 'user_belongsto_role_relationship' => Auth::user()->role_id,
+ 'user_belongstomany_role_relationship' => Auth::user()->roles->pluck('id')->toArray(),
]);
}
diff --git a/src/Http/Middleware/VoyagerAdminMiddleware.php b/src/Http/Middleware/VoyagerAdminMiddleware.php
index 31f93b5211..c4d83edeaa 100644
--- a/src/Http/Middleware/VoyagerAdminMiddleware.php
+++ b/src/Http/Middleware/VoyagerAdminMiddleware.php
@@ -3,6 +3,7 @@
namespace TCG\Voyager\Http\Middleware;
use Closure;
+use Illuminate\Support\Facades\Auth;
class VoyagerAdminMiddleware
{
@@ -16,8 +17,10 @@ class VoyagerAdminMiddleware
*/
public function handle($request, Closure $next)
{
- if (!app('VoyagerAuth')->guest()) {
- $user = app('VoyagerAuth')->user();
+ auth()->setDefaultDriver(app('VoyagerGuard'));
+
+ if (!Auth::guest()) {
+ $user = Auth::user();
app()->setLocale($user->locale ?? app()->getLocale());
return $user->hasPermission('browse_admin') ? $next($request) : redirect('/');
diff --git a/src/Models/Menu.php b/src/Models/Menu.php
index ab8fc7b437..f434d42948 100644
--- a/src/Models/Menu.php
+++ b/src/Models/Menu.php
@@ -3,6 +3,7 @@
namespace TCG\Voyager\Models;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Events\MenuDisplay;
use TCG\Voyager\Facades\Voyager;
@@ -140,7 +141,7 @@ private static function processItems($items)
// Filter items by permission
$items = $items->filter(function ($item) {
- return !$item->children->isEmpty() || app('VoyagerAuth')->user()->can('browse', $item);
+ return !$item->children->isEmpty() || Auth::user()->can('browse', $item);
})->filter(function ($item) {
// Filter out empty menu-items
if ($item->url == '' && $item->route == '' && $item->children->count() == 0) {
diff --git a/src/Models/Page.php b/src/Models/Page.php
index 253be048f2..b01778ce72 100644
--- a/src/Models/Page.php
+++ b/src/Models/Page.php
@@ -3,6 +3,7 @@
namespace TCG\Voyager\Models;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Traits\Translatable;
class Page extends Model
@@ -29,8 +30,8 @@ class Page extends Model
public function save(array $options = [])
{
// If no author has been assigned, assign the current user's id as the author of the post
- if (!$this->author_id && app('VoyagerAuth')->user()) {
- $this->author_id = app('VoyagerAuth')->user()->getKey();
+ if (!$this->author_id && Auth::user()) {
+ $this->author_id = Auth::user()->getKey();
}
parent::save();
diff --git a/src/Models/Post.php b/src/Models/Post.php
index 5168d79833..42cc5bf95e 100644
--- a/src/Models/Post.php
+++ b/src/Models/Post.php
@@ -4,6 +4,7 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
+use Illuminate\Support\Facades\Auth;
use TCG\Voyager\Facades\Voyager;
use TCG\Voyager\Traits\Resizable;
use TCG\Voyager\Traits\Translatable;
@@ -22,8 +23,8 @@ class Post extends Model
public function save(array $options = [])
{
// If no author has been assigned, assign the current user's id as the author of the post
- if (!$this->author_id && app('VoyagerAuth')->user()) {
- $this->author_id = app('VoyagerAuth')->user()->getKey();
+ if (!$this->author_id && Auth::user()) {
+ $this->author_id = Auth::user()->getKey();
}
parent::save();
diff --git a/src/Traits/VoyagerUser.php b/src/Traits/VoyagerUser.php
index b7ce61d400..ba4a2e612c 100644
--- a/src/Traits/VoyagerUser.php
+++ b/src/Traits/VoyagerUser.php
@@ -23,7 +23,7 @@ public function role()
*/
public function roles()
{
- return $this->belongsToMany(Voyager::modelClass('Role'), 'user_roles');
+ return $this->belongsToMany(Voyager::modelClass('Role'), 'user_roles', 'user_id', 'role_id');
}
/**
diff --git a/src/VoyagerServiceProvider.php b/src/VoyagerServiceProvider.php
index 322b647324..079cd13a92 100644
--- a/src/VoyagerServiceProvider.php
+++ b/src/VoyagerServiceProvider.php
@@ -8,6 +8,7 @@
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
@@ -68,8 +69,8 @@ public function register()
return new Voyager();
});
- $this->app->singleton('VoyagerAuth', function () {
- return auth();
+ $this->app->singleton('VoyagerGuard', function () {
+ return config('auth.defaults.guard', 'web');
});
$this->loadHelpers();
@@ -97,10 +98,10 @@ public function register()
public function boot(Router $router, Dispatcher $event)
{
if (config('voyager.user.add_default_role_on_register')) {
- $app_user = config('voyager.user.namespace') ?: config('auth.providers.users.model');
- $app_user::created(function ($user) {
+ $model = Auth::guard(app('VoyagerGuard'))->getProvider()->getModel();
+ call_user_func($model.'::created', function ($user) use ($model) {
if (is_null($user->role_id)) {
- VoyagerFacade::model('User')->findOrFail($user->id)
+ call_user_func($model.'::findOrFail', $user->id)
->setRole(config('voyager.user.default_role'))
->save();
}
diff --git a/src/Widgets/PageDimmer.php b/src/Widgets/PageDimmer.php
index 9a1c51b546..eb19490006 100644
--- a/src/Widgets/PageDimmer.php
+++ b/src/Widgets/PageDimmer.php
@@ -2,6 +2,7 @@
namespace TCG\Voyager\Widgets;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Facades\Voyager;
@@ -42,6 +43,6 @@ public function run()
*/
public function shouldBeDisplayed()
{
- return app('VoyagerAuth')->user()->can('browse', Voyager::model('Page'));
+ return Auth::user()->can('browse', Voyager::model('Page'));
}
}
diff --git a/src/Widgets/PostDimmer.php b/src/Widgets/PostDimmer.php
index 995542ffad..b19c065177 100644
--- a/src/Widgets/PostDimmer.php
+++ b/src/Widgets/PostDimmer.php
@@ -2,6 +2,7 @@
namespace TCG\Voyager\Widgets;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Facades\Voyager;
@@ -42,6 +43,6 @@ public function run()
*/
public function shouldBeDisplayed()
{
- return app('VoyagerAuth')->user()->can('browse', Voyager::model('Post'));
+ return Auth::user()->can('browse', Voyager::model('Post'));
}
}
diff --git a/src/Widgets/UserDimmer.php b/src/Widgets/UserDimmer.php
index dc4d384f83..e0bd6a9749 100644
--- a/src/Widgets/UserDimmer.php
+++ b/src/Widgets/UserDimmer.php
@@ -2,6 +2,7 @@
namespace TCG\Voyager\Widgets;
+use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Facades\Voyager;
@@ -42,6 +43,6 @@ public function run()
*/
public function shouldBeDisplayed()
{
- return app('VoyagerAuth')->user()->can('browse', Voyager::model('User'));
+ return Auth::user()->can('browse', Voyager::model('User'));
}
}
diff --git a/tests/CustomGuardTest.php b/tests/CustomGuardTest.php
deleted file mode 100644
index e6d5e15345..0000000000
--- a/tests/CustomGuardTest.php
+++ /dev/null
@@ -1,140 +0,0 @@
-useAuthorizedCustomGuard();
-
- $middleware = new VoyagerAdminMiddleware();
- $response = new Response();
-
- $actualResponse = $middleware->handle(new Request(), function () use ($response) {
- return $response;
- });
-
- // Correct guard usage will return our $response.
- // Incorrect guard usage will redirect us to login
- $this->assertSame($actualResponse, $response);
- }
-
- /**
- * Voyager postLogin uses custom guard.
- *
- * This test will make sure that the postLogin uses custom guard
- * for authorizing users.
- */
- public function testVoyagerPostLoginUsesCustomGuard()
- {
- $loginData = [
- 'email' => 'voyager@voyager.com',
- 'password' => 'voyager',
- ];
-
- $guard = $this->getMockBuilder(StatefulGuard::class)
- ->setMethods(['guest', 'user', 'attempt'])
- ->getMockForAbstractClass();
-
- $guard->method('guest')
- ->will($this->returnValue(true));
-
- $guard->method('user')
- ->will($this->returnValue(null));
-
- $guard->method('attempt')
- ->will($this->returnValueMap([
- [$loginData, false, true],
- ]));
-
- $this->app->instance('VoyagerAuth', $guard);
-
- $response = $this->json('POST', route('voyager.postlogin'), $loginData);
-
- // Correct guard usage will redirect us to the dashboard.
- // Incorrect guard, and failed login will redirect us to login.
- $response->assertRedirectedTo(route('voyager.dashboard'));
- }
-
- /**
- * Voyager controllers use custom guard.
- *
- * This test will make sure that controllers internal authorize()
- * function uses the custom guard.
- */
- public function testVoyagerControllersUseCustomGuard()
- {
- $this->useAuthorizedCustomGuard();
-
- $response = $this->action('GET', 'TCG\Voyager\Http\Controllers\VoyagerBaseController@index');
-
- // Incorrect guard usage will redirect to login
- $this->assertResponseOk($response);
- }
-
- /**
- * Default guard is used when custom is not defined.
- *
- * This test will make sure that VoyagerAuth defaults to the applications
- * default guard when no custom guard is defined.
- */
- public function testDefaultGuardIsUsedWhenCustomIsNotDefined()
- {
- $this->assertSame($this->app->get('VoyagerAuth')->guard(), Auth::guard());
- }
-
- /**
- * Return mock user where hasPermission always returns true.
- */
- protected function getMockUserWithAllPermissions()
- {
- $user = $this->getMockBuilder(User::class)
- ->setMethods(['hasPermission'])
- ->getMock();
-
- $user->method('hasPermission')
- ->will($this->returnValue(true));
-
- return $user;
- }
-
- /**
- * Sets the application to use custom guard which is authenticated
- * with an mocked admin user.
- */
- protected function useAuthorizedCustomGuard()
- {
- $user = $this->getMockUserWithAllPermissions();
-
- $guard = $this->getMockBuilder(StatefulGuard::class)
- ->setMethods(['guest', 'check', 'user'])
- ->getMockForAbstractClass();
-
- $guard->method('guest')
- ->will($this->returnValue(false));
-
- $guard->method('check')
- ->will($this->returnValue(false));
-
- $guard->method('user')
- ->will($this->returnValue($user));
-
- $this->app->instance('VoyagerAuth', $guard);
-
- return $guard;
- }
-}