Skip to content

Commit

Permalink
Add authenticator for the Sense controllers (#9)
Browse files Browse the repository at this point in the history
Add authenticator for the Sense controllers
  • Loading branch information
antonkomarev committed Sep 25, 2018
1 parent 0ae072f commit 5bde570
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 9 deletions.
12 changes: 10 additions & 2 deletions CHANGELOG.md
@@ -1,7 +1,15 @@
# Changelog

## 1.0.0 - 2018-09-25
All notable changes to `laravel-sense` will be documented in this file.

## [0.2.0] - 2018-09-26

### Added

- ([#9](https://github.com/cybercog/laravel-sense/pull/9)) Dashboard Authenticator

## 0.1.0 - 2018-09-25

- Initial release

All notable changes to `laravel-sense` will be documented in this file.
[0.2.0]: https://github.com/cybercog/laravel-sense/compare/0.1.0...0.2.0
18 changes: 15 additions & 3 deletions README.md
Expand Up @@ -12,11 +12,12 @@

## Introduction

**DON'T USE IT ON PRODUCTION! IT WILL ENORMOUSLY SLOW DOWN APPLICATION!**

When you feel that your application is starting to run slower, be careful, it can become a smelling zombie! Open application black box in a minutes!
> Beware! If you feel that your application starts to run slower, it can become a smelling zombie!
Laravel Sense provides a dashboard for application profiling. Sense allows you to easily monitor key metrics such as HTTP requests & Eloquent queries.
Open application black box in a minutes!

**DON'T USE IT ON PRODUCTION! IT WILL ENORMOUSLY SLOW DOWN APPLICATION!**

### Requests list

Expand Down Expand Up @@ -83,7 +84,18 @@ $ php artisan vendor:publish --provider="Cog\Laravel\Sense\Providers\SenseServic

## Usage

### Dashboard Authentication

Sense exposes a dashboard at `/sense`. By default, you will only be able to access this dashboard in the `local` environment.
To define a more specific access policy for the dashboard, you should use the `\Cog\Laravel\Sense\Authentication\Services\Authenticator::using` method.
The `using` method accepts a callback which should return `true` or `false`, indicating whether the user should have access to the Sense dashboard.
Typically, you should call `Authenticator::using` in the boot method of your `AppServiceProvider`:

```php
\Cog\Laravel\Sense\Authentication\Services\Authenticator::auth(function ($request) {
// return true / false;
});
```

## Changelog

Expand Down
51 changes: 51 additions & 0 deletions src/Authentication/Services/Authenticator.php
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of Laravel Authenticator.
*
* (c) Anton Komarev <a.komarev@cybercog.su>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Sense\Authentication\Services;

use Closure;
use Illuminate\Http\Request;

class Authenticator
{
/**
* The callback that should be used to authenticate Sense users.
*
* @var \Closure
*/
public static $using;

/**
* Determine if the given request can access the Sense dashboard.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
public static function check(Request $request): bool
{
return (static::$using ?: function () {
return app()->environment('local');
})($request);
}

/**
* Set the callback that should be used to authenticate Sense users.
*
* @param \Closure $callback
* @return void
*/
public static function using(Closure $callback): void
{
static::$using = $callback;
}
}
1 change: 0 additions & 1 deletion src/Http/Controllers/AppController.php
Expand Up @@ -38,6 +38,5 @@ public function __invoke($requestId = null)
->get();

return view('sense::requests', compact('summaries'));

}
}
3 changes: 2 additions & 1 deletion src/Http/Controllers/Controller.php
Expand Up @@ -13,6 +13,7 @@

namespace Cog\Laravel\Sense\Http\Controllers;

use Cog\Laravel\Sense\Http\Middleware\Authenticate;
use Illuminate\Routing\Controller as BaseController;

class Controller extends BaseController
Expand All @@ -24,6 +25,6 @@ class Controller extends BaseController
*/
public function __construct()
{
// $this->middleware(Authenticate::class);
$this->middleware(Authenticate::class);
}
}
31 changes: 31 additions & 0 deletions src/Http/Middleware/Authenticate.php
@@ -0,0 +1,31 @@
<?php

/*
* This file is part of Laravel Sense.
*
* (c) Anton Komarev <a.komarev@cybercog.su>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Sense\Http\Middleware;

use Cog\Laravel\Sense\Authentication\Services\Authenticator;

class Authenticate
{
/**
* Handle the incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return \Illuminate\Http\Response|null
*/
public function handle($request, $next)
{
return Authenticator::check($request) ? $next($request) : abort(403);
}
}
2 changes: 1 addition & 1 deletion src/Providers/SenseServiceProvider.php
Expand Up @@ -43,7 +43,7 @@ protected function registerDbConnection(): void
{
if (is_null($config = config('database.connections.sense'))) {
$defaultConnection = config('database.default');
if (is_null($fallbackConfig = config("database.connections.{$defaultConnection}"))){
if (is_null($fallbackConfig = config("database.connections.{$defaultConnection}"))) {
throw new \Exception('Database connection [sense] has not been configured.');
}
config(['database.connections.sense' => $fallbackConfig]);
Expand Down
4 changes: 3 additions & 1 deletion src/Request/Id.php
Expand Up @@ -9,6 +9,8 @@
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Cog\Laravel\Sense\Request;

use Illuminate\Support\Facades\Log;
Expand All @@ -30,4 +32,4 @@ public static function make(): string

return '';
}
}
}

0 comments on commit 5bde570

Please sign in to comment.