From 105cff50b9d9430da901adba318b47b5e3cbcdd6 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 4 Dec 2016 16:54:36 +0000 Subject: [PATCH 1/2] Added metric visibility options. Closes #2244 --- app/Bus/Commands/Metric/AddMetricCommand.php | 12 +++++- .../Commands/Metric/UpdateMetricCommand.php | 12 +++++- .../Metric/AddMetricCommandHandler.php | 1 + .../Metric/UpdateMetricCommandHandler.php | 1 + app/Composers/Modules/MetricsComposer.php | 40 ++++++++++++++--- app/Http/Controllers/Api/MetricController.php | 6 ++- .../Dashboard/MetricController.php | 8 +++- app/Models/Metric.php | 40 ++++++++++++++++- ...3502_AlterTableMetricsAddVisibleColumn.php | 43 +++++++++++++++++++ resources/lang/en/forms.php | 28 ++++++------ .../views/dashboard/metrics/add.blade.php | 8 ++++ .../views/dashboard/metrics/edit.blade.php | 8 ++++ .../Commands/Metric/AddMetricCommandTest.php | 4 +- .../Metric/UpdateMetricCommandTest.php | 4 +- 14 files changed, 189 insertions(+), 26 deletions(-) create mode 100644 database/migrations/2016_12_04_163502_AlterTableMetricsAddVisibleColumn.php diff --git a/app/Bus/Commands/Metric/AddMetricCommand.php b/app/Bus/Commands/Metric/AddMetricCommand.php index 04ac77a97..39000e82e 100644 --- a/app/Bus/Commands/Metric/AddMetricCommand.php +++ b/app/Bus/Commands/Metric/AddMetricCommand.php @@ -83,6 +83,13 @@ final class AddMetricCommand */ public $order; + /** + * The visibility of the metric. + * + * @var int + */ + public $visible; + /** * The validation rules. * @@ -100,6 +107,7 @@ final class AddMetricCommand 'default_view' => 'required|int|between:0,3', 'threshold' => 'nullable|numeric|between:0,10', 'order' => 'nullable|int', + 'visible' => 'required|int|between:0,2', ]; /** @@ -115,10 +123,11 @@ final class AddMetricCommand * @param int $default_view * @param int $threshold * @param int $order + * @param int $visible * * @return void */ - public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = 0) + public function __construct($name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = 0, $visible = 1) { $this->name = $name; $this->suffix = $suffix; @@ -130,5 +139,6 @@ final class AddMetricCommand $this->default_view = $default_view; $this->threshold = $threshold; $this->order = $order; + $this->visible = $visible; } } diff --git a/app/Bus/Commands/Metric/UpdateMetricCommand.php b/app/Bus/Commands/Metric/UpdateMetricCommand.php index 5eb710390..55939afb1 100644 --- a/app/Bus/Commands/Metric/UpdateMetricCommand.php +++ b/app/Bus/Commands/Metric/UpdateMetricCommand.php @@ -92,6 +92,13 @@ final class UpdateMetricCommand */ public $order; + /** + * The visibility of the metric. + * + * @var int + */ + public $visible; + /** * The validation rules. * @@ -109,6 +116,7 @@ final class UpdateMetricCommand 'default_view' => 'nullable|numeric|between:0,4', 'threshold' => 'nullable|numeric|between:0,10', 'order' => 'nullable|int', + 'visible' => 'required|int|between:0,2', ]; /** @@ -125,10 +133,11 @@ final class UpdateMetricCommand * @param int $default_view * @param int $threshold * @param int|null $order + * @param int $visible * * @return void */ - public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = null) + public function __construct(Metric $metric, $name, $suffix, $description, $default_value, $calc_type, $display_chart, $places, $default_view, $threshold, $order = null, $visible = null) { $this->metric = $metric; $this->name = $name; @@ -141,5 +150,6 @@ final class UpdateMetricCommand $this->default_view = $default_view; $this->threshold = $threshold; $this->order = $order; + $this->visible = $visible; } } diff --git a/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php b/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php index a5733b280..64291cc9c 100644 --- a/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php +++ b/app/Bus/Handlers/Commands/Metric/AddMetricCommandHandler.php @@ -37,6 +37,7 @@ class AddMetricCommandHandler 'default_view' => $command->default_view, 'threshold' => $command->threshold, 'order' => $command->order, + 'visible' => $command->visible, ]); event(new MetricWasAddedEvent($metric)); diff --git a/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php b/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php index e8abda242..0bfc29f5f 100644 --- a/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php +++ b/app/Bus/Handlers/Commands/Metric/UpdateMetricCommandHandler.php @@ -55,6 +55,7 @@ class UpdateMetricCommandHandler 'default_view' => $command->default_view, 'threshold' => $command->threshold, 'order' => $command->order, + 'visible' => $command->visible, ]; return array_filter($params, function ($val) { diff --git a/app/Composers/Modules/MetricsComposer.php b/app/Composers/Modules/MetricsComposer.php index cc0c38a35..cbb48370f 100644 --- a/app/Composers/Modules/MetricsComposer.php +++ b/app/Composers/Modules/MetricsComposer.php @@ -12,6 +12,7 @@ namespace CachetHQ\Cachet\Composers\Modules; use CachetHQ\Cachet\Models\Metric; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Config\Repository; use Illuminate\Contracts\View\View; @@ -30,16 +31,25 @@ class MetricsComposer */ protected $config; + /** + * The user session object. + * + * @var \Illuminate\Contracts\Auth\Guard + */ + protected $guard; + /** * Create a new metrics composer instance. * * @param \Illuminate\Contracts\Config\Repository $config + * @param \Illuminate\Contracts\Auth\Guard $guard * * @return void */ - public function __construct(Repository $config) + public function __construct(Repository $config, Guard $guard) { $this->config = $config; + $this->guard = $guard; } /** @@ -51,12 +61,32 @@ class MetricsComposer */ public function compose(View $view) { - $metrics = null; - if ($displayMetrics = $this->config->get('setting.display_graphs')) { - $metrics = Metric::displayable()->orderBy('order')->orderBy('id')->get(); - } + $displayMetrics = $this->config->get('setting.display_graphs'); + $metrics = $this->getVisibleMetrics($displayMetrics); $view->withDisplayMetrics($displayMetrics) ->withMetrics($metrics); } + + /** + * Get visible grouped components. + * + * @param bool $displayMetrics + * + * @return \Illuminate\Support\Collection|void + */ + protected function getVisibleMetrics($displayMetrics) + { + if (!$displayMetrics) { + return; + } + + $metrics = Metric::displayable(); + + if (!$this->guard->check()) { + $metrics->visible(); + } + + return $metrics->orderBy('order')->orderBy('id')->get(); + } } diff --git a/app/Http/Controllers/Api/MetricController.php b/app/Http/Controllers/Api/MetricController.php index d8b931b01..ddb11f002 100644 --- a/app/Http/Controllers/Api/MetricController.php +++ b/app/Http/Controllers/Api/MetricController.php @@ -86,7 +86,8 @@ class MetricController extends AbstractApiController Binput::get('places', 2), Binput::get('default_view', Binput::get('view', 1)), Binput::get('threshold', 5), - Binput::get('order', 0) + Binput::get('order', 0), + Binput::get('visible', 1) )); } catch (QueryException $e) { throw new BadRequestHttpException(); @@ -116,7 +117,8 @@ class MetricController extends AbstractApiController Binput::get('places'), Binput::get('default_view', Binput::get('view')), Binput::get('threshold'), - Binput::get('order') + Binput::get('order'), + Binput::get('visible') )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/MetricController.php b/app/Http/Controllers/Dashboard/MetricController.php index a451ed444..dd3da8f77 100644 --- a/app/Http/Controllers/Dashboard/MetricController.php +++ b/app/Http/Controllers/Dashboard/MetricController.php @@ -79,7 +79,9 @@ class MetricController extends Controller $metricData['display_chart'], $metricData['places'], $metricData['default_view'], - $metricData['threshold'] + $metricData['threshold'], + 0, // Default order + $metricData['visible'] )); } catch (ValidationException $e) { return cachet_redirect('dashboard.metrics.create') @@ -152,7 +154,9 @@ class MetricController extends Controller Binput::get('display_chart', null, false), Binput::get('places', null, false), Binput::get('default_view', null, false), - Binput::get('threshold', null, false) + Binput::get('threshold', null, false), + null, + Binput::get('visible', null, false) )); } catch (ValidationException $e) { return cachet_redirect('dashboard.metrics.edit', [$metric->id]) diff --git a/app/Models/Metric.php b/app/Models/Metric.php index 1e4670284..abafbd340 100644 --- a/app/Models/Metric.php +++ b/app/Models/Metric.php @@ -36,6 +36,27 @@ class Metric extends Model implements HasPresenter */ const CALC_AVG = 1; + /** + * Viewable only authenticated users. + * + * @var int + */ + const VISIBLE_AUTHENTICATED = 0; + + /** + * Viewable by public. + * + * @var int + */ + const VISIBLE_GUEST = 1; + + /** + * Viewable by nobody. + * + * @var int + */ + const VISIBLE_HIDDEN = 2; + /** * The model's attributes. * @@ -50,6 +71,7 @@ class Metric extends Model implements HasPresenter 'default_view' => 1, 'threshold' => 5, 'order' => 0, + 'visible' => 1, ]; /** @@ -66,6 +88,7 @@ class Metric extends Model implements HasPresenter 'default_view' => 'int', 'threshold' => 'int', 'order' => 'int', + 'visible' => 'int', ]; /** @@ -84,6 +107,7 @@ class Metric extends Model implements HasPresenter 'default_view', 'threshold', 'order', + 'visible', ]; /** @@ -99,6 +123,7 @@ class Metric extends Model implements HasPresenter 'places' => 'required|numeric|between:0,4', 'default_view' => 'required|numeric|between:0,3', 'threshold' => 'required|numeric|between:0,10', + 'visible' => 'required|numeric|between:0,2', ]; /** @@ -113,6 +138,7 @@ class Metric extends Model implements HasPresenter 'default_value', 'calc_type', 'order', + 'visible', ]; /** @@ -134,7 +160,19 @@ class Metric extends Model implements HasPresenter */ public function scopeDisplayable(Builder $query) { - return $query->where('display_chart', '=', true); + return $query->where('display_chart', '=', true)->where('visible', '!=', self::VISIBLE_HIDDEN); + } + + /** + * Finds all metrics which are visible to public. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeVisible(Builder $query) + { + return $query->where('visible', '=', self::VISIBLE_GUEST); } /** diff --git a/database/migrations/2016_12_04_163502_AlterTableMetricsAddVisibleColumn.php b/database/migrations/2016_12_04_163502_AlterTableMetricsAddVisibleColumn.php new file mode 100644 index 000000000..644ce52f8 --- /dev/null +++ b/database/migrations/2016_12_04_163502_AlterTableMetricsAddVisibleColumn.php @@ -0,0 +1,43 @@ +boolean('visible')->after('order')->default(1); + + $table->index('visible'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('metrics', function (Blueprint $table) { + $table->dropColumn('visible'); + }); + } +} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index bab106d7f..eb8cf3e1e 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -120,18 +120,22 @@ return [ // Metric form fields 'metrics' => [ - 'name' => 'Name', - 'suffix' => 'Suffix', - 'description' => 'Description', - 'description-help' => 'You may also use Markdown.', - 'display-chart' => 'Display chart on status page?', - 'default-value' => 'Default value', - 'calc_type' => 'Calculation of metrics', - 'type_sum' => 'Sum', - 'type_avg' => 'Average', - 'places' => 'Decimal places', - 'default_view' => 'Default view', - 'threshold' => 'How many minutes of threshold between metric points?', + 'name' => 'Name', + 'suffix' => 'Suffix', + 'description' => 'Description', + 'description-help' => 'You may also use Markdown.', + 'display-chart' => 'Display chart on status page?', + 'default-value' => 'Default value', + 'calc_type' => 'Calculation of metrics', + 'type_sum' => 'Sum', + 'type_avg' => 'Average', + 'places' => 'Decimal places', + 'default_view' => 'Default view', + 'threshold' => 'How many minutes of threshold between metric points?', + 'visibility' => 'Visibility', + 'visibility_authenticated' => 'Visible to authenticated users', + 'visibility_public' => 'Visible to everybody', + 'visibility_hidden' => 'Always hidden', 'points' => [ 'value' => 'Value', diff --git a/resources/views/dashboard/metrics/add.blade.php b/resources/views/dashboard/metrics/add.blade.php index 017141815..6fe31d8ad 100644 --- a/resources/views/dashboard/metrics/add.blade.php +++ b/resources/views/dashboard/metrics/add.blade.php @@ -66,6 +66,14 @@ {{ trans('forms.metrics.display-chart') }} +
+ + +
diff --git a/resources/views/dashboard/metrics/edit.blade.php b/resources/views/dashboard/metrics/edit.blade.php index d86513e9a..88a7c756d 100644 --- a/resources/views/dashboard/metrics/edit.blade.php +++ b/resources/views/dashboard/metrics/edit.blade.php @@ -66,6 +66,14 @@ {{ trans('forms.metrics.display-chart') }}
+
+ + +
id}}> diff --git a/tests/Bus/Commands/Metric/AddMetricCommandTest.php b/tests/Bus/Commands/Metric/AddMetricCommandTest.php index 0107d6e88..881ba08d4 100644 --- a/tests/Bus/Commands/Metric/AddMetricCommandTest.php +++ b/tests/Bus/Commands/Metric/AddMetricCommandTest.php @@ -39,6 +39,7 @@ class AddMetricCommandTest extends AbstractTestCase 'default_view' => 0, 'threshold' => 0, 'order' => 0, + 'visible' => 1, ]; $object = new AddMetricCommand( @@ -51,7 +52,8 @@ class AddMetricCommandTest extends AbstractTestCase $params['places'], $params['default_view'], $params['threshold'], - $params['order'] + $params['order'], + $params['visible'] ); return compact('params', 'object'); diff --git a/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php b/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php index 048a4ec27..2c9907892 100644 --- a/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php +++ b/tests/Bus/Commands/Metric/UpdateMetricCommandTest.php @@ -41,6 +41,7 @@ class UpdateMetricCommandTest extends AbstractTestCase 'default_view' => 0, 'threshold' => 0, 'order' => 0, + 'visible' => 1, ]; $object = new UpdateMetricCommand( @@ -54,7 +55,8 @@ class UpdateMetricCommandTest extends AbstractTestCase $params['places'], $params['default_view'], $params['threshold'], - $params['order'] + $params['order'], + $params['visible'] ); return compact('params', 'object'); From f2a338ba2d3c92924cd3c5e18ff6a14567d664bf Mon Sep 17 00:00:00 2001 From: James Brooks Date: Sun, 4 Dec 2016 16:54:47 +0000 Subject: [PATCH 2/2] Apply fixes from StyleCI [ci skip] [skip ci] --- app/Composers/Modules/MetricsComposer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Composers/Modules/MetricsComposer.php b/app/Composers/Modules/MetricsComposer.php index cbb48370f..ba49156a9 100644 --- a/app/Composers/Modules/MetricsComposer.php +++ b/app/Composers/Modules/MetricsComposer.php @@ -42,7 +42,7 @@ class MetricsComposer * Create a new metrics composer instance. * * @param \Illuminate\Contracts\Config\Repository $config - * @param \Illuminate\Contracts\Auth\Guard $guard + * @param \Illuminate\Contracts\Auth\Guard $guard * * @return void */