Skip to content
This repository has been archived by the owner on Oct 10, 2018. It is now read-only.

Commit

Permalink
Merge pull request #19 from kontenta/featrure/UserAccountWidget
Browse files Browse the repository at this point in the history
user account widget
  • Loading branch information
erik-epineer authored Oct 4, 2018
2 parents 51dfe47 + db07a14 commit aedea9b
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 19 deletions.
5 changes: 0 additions & 5 deletions resources/views/layouts/master.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

@section('body')
<header data-kontour-section="{{ $view_manager->headerSection() }}">
{{-- TODO: make this logout form part of a user-widget --}}
<form action="{{ route('kontour.logout') }}" method="post">
{{ csrf_field() }}
<button type="submit">Logout</button>
</form>
@foreach($widget_manager->getWidgetsForSection($view_manager->headerSection()) as $widget)
{{ $widget }}
@endforeach
Expand Down
7 changes: 7 additions & 0 deletions resources/views/widgets/userAccount.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<section data-kontour-widget="userAccount">
<span>{{ $user->getDisplayName() }}</span>
<form action="{{ route('kontour.logout') }}" method="post">
{{ csrf_field() }}
<button type="submit">Logout</button>
</form>
</section>
16 changes: 12 additions & 4 deletions src/Providers/KontourServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
namespace Kontenta\KontourSupport\Providers;

use Illuminate\Auth\AuthManager;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;
use Kontenta\KontourSupport\AdminRouteManager;
use Kontenta\KontourSupport\AdminViewManager;
use Kontenta\KontourSupport\AdminWidgetManager;
use Kontenta\KontourSupport\Widgets\MenuWidget;
use Kontenta\KontourSupport\Http\Middleware\AuthenticateAdmin;
use Kontenta\KontourSupport\Http\Middleware\RedirectIfAuthenticated;
use Kontenta\KontourSupport\RecentVisitsRepository;
use Kontenta\KontourSupport\Widgets\ItemHistoryWidget;
use Kontenta\KontourSupport\Widgets\MenuWidget;
use Kontenta\KontourSupport\Widgets\PersonalRecentVisitsWidget;
use Kontenta\KontourSupport\Widgets\TeamRecentVisitsWidget;
use Kontenta\KontourSupport\Http\Middleware\AuthenticateAdmin;
use Kontenta\KontourSupport\Http\Middleware\RedirectIfAuthenticated;
use Kontenta\KontourSupport\Widgets\UserAccountWidget;
use Kontenta\Kontour\Concerns\RegistersAdminRoutes;
use Kontenta\Kontour\Concerns\RegistersAdminWidgets;
use Illuminate\Support\Facades\Event;

class KontourServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -77,6 +78,12 @@ function ($app) {
true
);

$this->app->bindIf(
\Kontenta\Kontour\Contracts\UserAccountWidget::class,
UserAccountWidget::class,
true
);

$this->app->bindIf(
\Kontenta\Kontour\Contracts\RecentVisitsRepository::class,
RecentVisitsRepository::class,
Expand Down Expand Up @@ -153,6 +160,7 @@ protected function registerEventListeners()
protected function registerWidgets()
{
$this->registerAdminWidget($this->app->make(\Kontenta\Kontour\Contracts\MenuWidget::class), $this->app->make(\Kontenta\Kontour\Contracts\AdminViewManager::class)->navSection());
$this->registerAdminWidget($this->app->make(\Kontenta\Kontour\Contracts\UserAccountWidget::class), $this->app->make(\Kontenta\Kontour\Contracts\AdminViewManager::class)->headerSection());
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Widgets/ItemHistoryWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function addEntry(string $action, \DateTime $datetime, AdminUser $user =
$this->entries->push(compact('action', 'datetime', 'user'));
return $this;
}

public function addCreatedEntry(\DateTime $datetime, AdminUser $user = null): ItemHistoryWidgetContract
{
return $this->addEntry('created', $datetime, $user);
Expand All @@ -45,6 +45,6 @@ public function addUpdatedEntry(\DateTime $datetime, AdminUser $user = null): It

public function isAuthorized(Authorizable $user = null): bool
{
return true;
return (bool) $user;
}
}
2 changes: 1 addition & 1 deletion src/Widgets/MenuWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@ public function getHeadings(): Collection

public function isAuthorized(Authorizable $user = null): bool
{
return true;
return (bool) $user;
}
}
2 changes: 1 addition & 1 deletion src/Widgets/PersonalRecentVisitsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function toHtml()

public function isAuthorized(Authorizable $user = null): bool
{
return true;
return (bool) $user;
}

private function getVisits()
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/TeamRecentVisitsWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function toHtml()

public function isAuthorized(Authorizable $user = null): bool
{
return true;
return (bool) $user;
}

private function getVisits()
Expand Down
22 changes: 22 additions & 0 deletions src/Widgets/UserAccountWidget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Kontenta\KontourSupport\Widgets;

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
use Kontenta\Kontour\Contracts\UserAccountWidget as UserAccountWidgetContract;

class UserAccountWidget implements UserAccountWidgetContract
{
public function toHtml()
{
$user = Auth::guard(config('kontour.guard'))->user();
return View::make('kontour::widgets.userAccount', compact('user'))->render();
}

public function isAuthorized(Authorizable $user = null): bool
{
return (bool) $user;
}
}
8 changes: 7 additions & 1 deletion tests/Feature/KontourServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Facades\Route;
use Kontenta\KontourSupport\Tests\IntegrationTest;
use Kontenta\Kontour\Contracts\MenuWidget;
use Kontenta\Kontour\Contracts\UserAccountWidget;

class KontourServiceProviderTest extends IntegrationTest
{
Expand All @@ -20,8 +21,13 @@ public function test_admin_guard_can_be_resolved()
$this->assertInstanceOf(\Illuminate\Contracts\Auth\Guard::class, $guard);
}

public function test_menu_widget_can_resolved()
public function test_menu_widget_can_be_resolved()
{
$this->assertInstanceOf(MenuWidget::class, app(MenuWidget::class));
}

public function test_user_account_widget_can_be_resolved()
{
$this->assertInstanceOf(UserAccountWidget::class, app(UserAccountWidget::class));
}
}
24 changes: 20 additions & 4 deletions tests/Feature/UserlandControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public function test_index_route()
$response->assertSee('<main');
$response->assertSee('UserlandAdminWidget');
$response->assertDontSee('UnauthorizedWidget');
$response->assertSee('<a href="' . route('userland.index') . '">Userland Tool</a>');
$response->assertSee('>main<');

Event::assertDispatched(AdminToolVisited::class, function ($e) {
$now = new \DateTimeImmutable();
return $e->visit->getLink()->getUrl() == route('userland.index') and
Expand All @@ -43,6 +42,23 @@ public function test_index_route()
});
}

public function test_menu_widget()
{
$response = $this->actingAs($this->user)->get(route('userland.index'));

$response->assertSee('<ul data-kontour-widget="menu">');
$response->assertSee('>main<');
$response->assertSee('<a href="' . route('userland.index') . '">Userland Tool</a>');
}

public function test_user_account_widget()
{
$response = $this->actingAs($this->user)->get(route('userland.index'));
$response->assertSee('<section data-kontour-widget="userAccount">');
$response->assertSee($this->user->getDisplayName());
$response->assertSee('<button type="submit">Logout</button>');
}

public function test_recent_visits_widgets()
{
$otherUser = factory(User::class)->create();
Expand Down Expand Up @@ -86,7 +102,7 @@ public function test_item_history_widget()
{
$response = $this->actingAs($this->user)->get(route('userland.edit', 1));
$response->assertSee('<section data-kontour-widget="itemHistory">');
$response->assertSee('<li lang="en" data-kontour-entry-action="created" data-kontour-username="'.$this->user->getDisplayName() .'">');
$response->assertSee('<li lang="en" data-kontour-entry-action="updated" data-kontour-username="'.$this->user->getDisplayName() .'">');
$response->assertSee('<li lang="en" data-kontour-entry-action="created" data-kontour-username="' . $this->user->getDisplayName() . '">');
$response->assertSee('<li lang="en" data-kontour-entry-action="updated" data-kontour-username="' . $this->user->getDisplayName() . '">');
}
}

0 comments on commit aedea9b

Please sign in to comment.