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

feat: add laravel telescope and admin user #2313

Merged
merged 9 commits into from
Jan 18, 2019
Merged
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
7 changes: 5 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ APP_ENV=local
# to false.
APP_DEBUG=false

# Enable Laravel Telescope https://laravel.com/docs/5.7/telescope
# default: false
TELESCOPE_ENABLED=false

# The encryption key. This is the most important part of the application. Keep
# this secure otherwise, everyone will be able to access your application.
# Must be 32 characters long exactly.
Expand Down Expand Up @@ -174,5 +178,4 @@ ENABLE_WEATHER=false
# https://darksky.net/dev/register
# Darksky provides an api with 1000 free API calls per day
# You need to enable the weather above if you provide an API key here.
DARKSKY_API_KEY=

DARKSKY_API_KEY=
2 changes: 2 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ UNRELEASED CHANGES:

New features:

* Add Laravel Telescope (deactivated by default)
* Add notion of instance administrator for a user
* Add ability to name u2f security keys and to delete register ones
* Add ability to add a comment when rating your day in the journal
* Add API methods to manage genders
Expand Down
74 changes: 74 additions & 0 deletions app/Console/Commands/SetUserAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Console\Commands;

use App\Models\User\User;
use Illuminate\Console\Command;

class SetUserAdmin extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monica:admin'.
' {--email= : The email of the user whose admin status you want to change}'.
' {--force : Run without asking for confirmation}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Toggle administrator privileges for a user';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
// retrieve the email from the option
$email = $this->option('email');

// if no email was passed to the option, prompt the user to enter the email
if (! $email) {
$email = $this->ask('What is the user’s email?');
}

// retrieve the user with the specified email
$user = User::where('email', $email)->first();

if (! $user) {
// show an error and exist if the user does not exist
$this->error('No user with that email.');

return;
}

// Print a warning
if ($user->admin) {
$this->warn($user->email.' will be removed from the administrators of this instance');
} else {
$this->warn($user->email.' will be added to the administrators of this instance');
}

// ask for confirmation if not forced
if (! $this->option('force') && ! $this->confirm('Do you wish to continue?')) {
return;
}

// toglle admin status
$user->admin = ! $user->admin;
$user->save();

// Show new status
if ($user->admin) {
$this->info($user->email.' has been added to the administrators of this instance');
} else {
$this->info($user->email.' has been removed from the administrators of this instance');
}
}
}
4 changes: 4 additions & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Kernel extends ConsoleKernel
'App\Console\Commands\SetupTest',
'App\Console\Commands\SetupFrontEndTest',
'App\Console\Commands\SetPremiumAccount',
'App\Console\Commands\SetUserAdmin',
'App\Console\Commands\Update',
'App\Console\Commands\MigrateDatabaseCollation',
'App\Console\Commands\OneTime\MoveAvatars',
Expand All @@ -48,5 +49,8 @@ protected function schedule(Schedule $schedule)
if (config('trustedproxy.cloudflare')) {
$schedule->command('cloudflare:reload')->daily();
}
if (config('telescope.enabled')) {
$schedule->command('telescope:prune --hours=48')->daily();
}
}
}
3 changes: 3 additions & 0 deletions app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class SettingsController
'sessions',
'statistics',
'subscriptions',
'telescope_entries',
'telescope_entries_tags',
'telescope_monitoring',
'terms',
'u2f_key',
'users',
Expand Down
1 change: 1 addition & 0 deletions app/Models/User/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class User extends Authenticatable implements MustVerifyEmail
*/
protected $casts = [
'profile_new_life_event_badge_seen' => 'boolean',
'admin' => 'boolean',
];

/**
Expand Down
66 changes: 66 additions & 0 deletions app/Providers/TelescopeServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Providers;

use Laravel\Telescope\Telescope;
use Illuminate\Support\Facades\Gate;
use Laravel\Telescope\IncomingEntry;
use Laravel\Telescope\TelescopeApplicationServiceProvider;

class TelescopeServiceProvider extends TelescopeApplicationServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->hideSensitiveRequestDetails();

Telescope::filter(function (IncomingEntry $entry) {
if ($this->app->isLocal()) {
return true;
}

return $entry->isReportableException() ||
$entry->isFailedJob() ||
$entry->isScheduledTask() ||
$entry->hasMonitoredTag();
});
}

/**
* Prevent sensitive request details from being logged by Telescope.
*
* @return void
*/
protected function hideSensitiveRequestDetails()
{
if ($this->app->isLocal()) {
return;
}

Telescope::hideRequestParameters(['_token']);

Telescope::hideRequestHeaders([
'cookie',
'x-csrf-token',
'x-xsrf-token',
]);
}

/**
* Register the Telescope gate.
*
* This gate determines who can access Telescope in non-local environments.
*
* @return void
*/
protected function gate()
{
Gate::define('viewTelescope', function ($user) {
return $user->admin;
});
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"laravel/framework": "5.7.*",
"laravel/passport": "^7.0",
"laravel/socialite": "^4.0",
"laravel/telescope": "^1.0",
"laravel/tinker": "^1.0",
"league/flysystem-aws-s3-v3": "~1.0",
"league/flysystem-cached-adapter": "^1.0",
Expand Down
118 changes: 115 additions & 3 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading