diff --git a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php index e68f24fd..2982e495 100644 --- a/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/AddComponentGroupCommand.php @@ -39,6 +39,13 @@ final class AddComponentGroupCommand */ public $collapsed; + /** + * Is the component visible to public? + * + * @var int + */ + public $visible; + /** * The validation rules. * @@ -48,6 +55,7 @@ final class AddComponentGroupCommand 'name' => 'required|string', 'order' => 'int', 'collapsed' => 'int|between:0,3', + 'visible' => 'bool', ]; /** @@ -56,13 +64,15 @@ final class AddComponentGroupCommand * @param string $name * @param int $order * @param int $collapsed + * @param int $visible * * @return void */ - public function __construct($name, $order, $collapsed) + public function __construct($name, $order, $collapsed, $visible) { $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; + $this->visible = (int) $visible; } } diff --git a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php index a20c4944..9f1dbd96 100644 --- a/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php +++ b/app/Bus/Commands/ComponentGroup/UpdateComponentGroupCommand.php @@ -48,6 +48,13 @@ final class UpdateComponentGroupCommand */ public $collapsed; + /** + * Is the component visible to public? + * + * @var int + */ + public $visible; + /** * The validation rules. * @@ -57,6 +64,7 @@ final class UpdateComponentGroupCommand 'name' => 'string', 'order' => 'int', 'collapsed' => 'int|between:0,3', + 'visible' => 'bool', ]; /** @@ -66,14 +74,16 @@ final class UpdateComponentGroupCommand * @param string $name * @param int $order * @param int $collapsed + * @param int $visible * * @return void */ - public function __construct(ComponentGroup $group, $name, $order, $collapsed) + public function __construct(ComponentGroup $group, $name, $order, $collapsed, $visible) { $this->group = $group; $this->name = $name; $this->order = (int) $order; $this->collapsed = $collapsed; + $this->visible = (int) $visible; } } diff --git a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php index 9066df51..b2a935a8 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/AddComponentGroupCommandHandler.php @@ -30,6 +30,7 @@ class AddComponentGroupCommandHandler 'name' => $command->name, 'order' => $command->order, 'collapsed' => $command->collapsed, + 'visible' => $command->visible, ]); event(new ComponentGroupWasAddedEvent($group)); diff --git a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php index 4ea40186..ef0f3a07 100644 --- a/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php +++ b/app/Bus/Handlers/Commands/ComponentGroup/UpdateComponentGroupCommandHandler.php @@ -46,6 +46,7 @@ class UpdateComponentGroupCommandHandler 'name' => $command->name, 'order' => $command->order, 'collapsed' => $command->collapsed, + 'visible' => $command->visible, ]; return array_filter($params, function ($val) { diff --git a/app/Composers/Modules/ComponentsComposer.php b/app/Composers/Modules/ComponentsComposer.php index a5df06ba..b581e045 100644 --- a/app/Composers/Modules/ComponentsComposer.php +++ b/app/Composers/Modules/ComponentsComposer.php @@ -13,6 +13,7 @@ namespace CachetHQ\Cachet\Composers\Modules; use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\View\View; /** @@ -23,6 +24,25 @@ use Illuminate\Contracts\View\View; */ class ComponentsComposer { + /** + * The user session object. + * + * @var \Illuminate\Contracts\Auth\Guard + */ + protected $guard; + + /** + * Creates a new components composer instance. + * + * @param \Illuminate\Contracts\Auth\Guard $guard + * + * @return void + */ + public function __construct(Guard $guard) + { + $this->guard = $guard; + } + /** * Index page view composer. * @@ -32,12 +52,28 @@ class ComponentsComposer */ public function compose(View $view) { - // Component & Component Group lists. - $usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id'); - $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); - $ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); + $componentGroups = $this->getVisibleGroupedComponents(); + $ungroupedComponents = Component::ungrouped()->get(); $view->withComponentGroups($componentGroups) ->withUngroupedComponents($ungroupedComponents); } + + /** + * Get visible grouped components. + * + * @return \Illuminate\Support\Collection + */ + protected function getVisibleGroupedComponents() + { + $componentGroupsBuilder = ComponentGroup::query(); + if (!$this->guard->check()) { + $componentGroupsBuilder->visible(); + } + + $usedComponentGroups = Component::grouped()->pluck('group_id'); + + return $componentGroupsBuilder->used($usedComponentGroups) + ->get(); + } } diff --git a/app/Console/Commands/DemoSeederCommand.php b/app/Console/Commands/DemoSeederCommand.php index a682ddd8..283f6d61 100644 --- a/app/Console/Commands/DemoSeederCommand.php +++ b/app/Console/Commands/DemoSeederCommand.php @@ -106,10 +106,12 @@ class DemoSeederCommand extends Command 'name' => 'Websites', 'order' => 1, 'collapsed' => 0, + 'visible' => ComponentGroup::VISIBLE_AUTHENTICATED, ], [ 'name' => 'Alt Three', 'order' => 2, 'collapsed' => 1, + 'visible' => ComponentGroup::VISIBLE_GUEST, ], ]; diff --git a/app/Http/Controllers/Api/ComponentGroupController.php b/app/Http/Controllers/Api/ComponentGroupController.php index 9c424b79..4804511d 100644 --- a/app/Http/Controllers/Api/ComponentGroupController.php +++ b/app/Http/Controllers/Api/ComponentGroupController.php @@ -16,6 +16,7 @@ use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand; use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand; use CachetHQ\Cachet\Models\ComponentGroup; use GrahamCampbell\Binput\Facades\Binput; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Database\QueryException; use Illuminate\Support\Facades\Request; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; @@ -29,6 +30,23 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; */ class ComponentGroupController extends AbstractApiController { + /** + * The user session object. + * + * @var \Illuminate\Contracts\Auth\Guard + */ + protected $guard; + + /** + * Creates a new component group controller instance. + * + * @param \Illuminate\Contracts\Auth\Guard $guard + */ + public function __construct(Guard $guard) + { + $this->guard = $guard; + } + /** * Get all groups. * @@ -37,6 +55,9 @@ class ComponentGroupController extends AbstractApiController public function getGroups() { $groups = ComponentGroup::query(); + if (!$this->guard->check()) { + $groups = ComponentGroup::visible(); + } $groups->search(Binput::except(['sort', 'order', 'per_page'])); @@ -74,7 +95,8 @@ class ComponentGroupController extends AbstractApiController $group = dispatch(new AddComponentGroupCommand( Binput::get('name'), Binput::get('order', 0), - Binput::get('collapsed', 0) + Binput::get('collapsed', 0), + Binput::get('visible', ComponentGroup::VISIBLE_AUTHENTICATED) )); } catch (QueryException $e) { throw new BadRequestHttpException(); @@ -97,7 +119,8 @@ class ComponentGroupController extends AbstractApiController $group, Binput::get('name'), Binput::get('order'), - Binput::get('collapsed') + Binput::get('collapsed'), + Binput::get('visible') )); } catch (QueryException $e) { throw new BadRequestHttpException(); diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 38a021f4..6f0f0f2a 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -277,7 +277,8 @@ class ComponentController extends Controller $group = dispatch(new AddComponentGroupCommand( Binput::get('name'), Binput::get('order', 0), - Binput::get('collapsed') + Binput::get('collapsed'), + Binput::get('visible') )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.add') @@ -304,7 +305,8 @@ class ComponentController extends Controller $group, Binput::get('name'), $group->order, - Binput::get('collapsed') + Binput::get('collapsed'), + Binput::get('visible') )); } catch (ValidationException $e) { return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id]) diff --git a/app/Http/Controllers/Dashboard/DashboardController.php b/app/Http/Controllers/Dashboard/DashboardController.php index 9d2d2c17..94d07c47 100644 --- a/app/Http/Controllers/Dashboard/DashboardController.php +++ b/app/Http/Controllers/Dashboard/DashboardController.php @@ -17,6 +17,7 @@ use CachetHQ\Cachet\Models\Component; use CachetHQ\Cachet\Models\ComponentGroup; use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\Subscriber; +use Illuminate\Contracts\Auth\Guard; use Illuminate\Routing\Controller; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Config; @@ -52,16 +53,25 @@ class DashboardController extends Controller */ protected $feed; + /** + * The user session object. + * + * @var \Illuminate\Contracts\Auth\Guard + */ + protected $guard; + /** * Creates a new dashboard controller instance. * * @param \CachetHQ\Cachet\Integrations\Feed $feed + * @param \Illuminate\Contracts\Auth\Guard $guard * * @return void */ - public function __construct(Feed $feed) + public function __construct(Feed $feed, Guard $guard) { $this->feed = $feed; + $this->guard = $guard; $this->startDate = new Date(); $this->dateTimeZone = Config::get('cachet.timezone'); } @@ -86,9 +96,9 @@ class DashboardController extends Controller $components = Component::orderBy('order')->get(); $incidents = $this->getIncidents(); $subscribers = $this->getSubscribers(); - $usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id'); - $componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get(); - $ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get(); + + $componentGroups = $this->getVisibleGroupedComponents(); + $ungroupedComponents = Component::ungrouped()->get(); $welcomeUser = !Auth::user()->welcomed; if ($welcomeUser) { @@ -174,4 +184,22 @@ class DashboardController extends Controller return $allSubscribers; } + + /** + * Get visible grouped components. + * + * @return \Illuminate\Support\Collection + */ + protected function getVisibleGroupedComponents() + { + $componentGroupsBuilder = ComponentGroup::query(); + if (!$this->guard->check()) { + $componentGroupsBuilder = ComponentGroup::visible(); + } + + $usedComponentGroups = Component::grouped()->pluck('group_id'); + + return $componentGroupsBuilder->used($usedComponentGroups) + ->get(); + } } diff --git a/app/Models/Component.php b/app/Models/Component.php index dd87b646..98f5c899 100644 --- a/app/Models/Component.php +++ b/app/Models/Component.php @@ -188,6 +188,35 @@ class Component extends Model implements HasPresenter return $query->where('enabled', false); } + /** + * Finds all ungrouped components. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeUngrouped(Builder $query) + { + return $query->enabled() + ->where('group_id', 0) + ->orderBy('order') + ->orderBy('created_at'); + } + + /** + * Finds all grouped components. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeGrouped(Builder $query) + { + return $query->enabled() + ->where('group_id', '>', 0) + ->groupBy('group_id'); + } + /** * Returns all of the tags on this component. * diff --git a/app/Models/ComponentGroup.php b/app/Models/ComponentGroup.php index 345f2210..5f881dbd 100644 --- a/app/Models/ComponentGroup.php +++ b/app/Models/ComponentGroup.php @@ -15,13 +15,29 @@ use AltThree\Validator\ValidatingTrait; use CachetHQ\Cachet\Models\Traits\SearchableTrait; use CachetHQ\Cachet\Models\Traits\SortableTrait; use CachetHQ\Cachet\Presenters\ComponentGroupPresenter; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\Collection; use McCool\LaravelAutoPresenter\HasPresenter; class ComponentGroup extends Model implements HasPresenter { use SearchableTrait, SortableTrait, ValidatingTrait; + /** + * Viewable only authenticated users. + * + * @var int + */ + const VISIBLE_AUTHENTICATED = 0; + + /** + * Viewable by public. + * + * @var int + */ + const VISIBLE_GUEST = 1; + /** * The model's attributes. * @@ -30,6 +46,7 @@ class ComponentGroup extends Model implements HasPresenter protected $attributes = [ 'order' => 0, 'collapsed' => 0, + 'visible' => 0, ]; /** @@ -41,6 +58,7 @@ class ComponentGroup extends Model implements HasPresenter 'name' => 'string', 'order' => 'int', 'collapsed' => 'int', + 'visible' => 'int', ]; /** @@ -48,7 +66,7 @@ class ComponentGroup extends Model implements HasPresenter * * @var string[] */ - protected $fillable = ['name', 'order', 'collapsed']; + protected $fillable = ['name', 'order', 'collapsed', 'visible']; /** * The validation rules. @@ -59,6 +77,7 @@ class ComponentGroup extends Model implements HasPresenter 'name' => 'required|string', 'order' => 'int', 'collapsed' => 'int', + 'visible' => 'bool', ]; /** @@ -71,6 +90,7 @@ class ComponentGroup extends Model implements HasPresenter 'name', 'order', 'collapsed', + 'visible', ]; /** @@ -83,6 +103,7 @@ class ComponentGroup extends Model implements HasPresenter 'name', 'order', 'collapsed', + 'visible', ]; /** @@ -141,4 +162,30 @@ class ComponentGroup extends Model implements HasPresenter { return ComponentGroupPresenter::class; } + + /** + * Finds all component groups 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); + } + + /** + * Finds all used component groups. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Support\Collection $usedComponentGroups + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeUsed(Builder $query, Collection $usedComponentGroups) + { + return $query->whereIn('id', $usedComponentGroups) + ->orderBy('order'); + } } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 3c70bdbe..de96e720 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -15,6 +15,7 @@ use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Models\IncidentTemplate; use CachetHQ\Cachet\Models\Metric; use CachetHQ\Cachet\Models\MetricPoint; +use CachetHQ\Cachet\Models\Setting; use CachetHQ\Cachet\Models\Subscriber; use CachetHQ\Cachet\Models\Subscription; use CachetHQ\Cachet\Models\User; @@ -35,6 +36,7 @@ $factory->define(ComponentGroup::class, function ($faker) { 'name' => $faker->words(2, true), 'order' => 0, 'collapsed' => random_int(0, 3), + 'visible' => $faker->boolean(), ]; }); @@ -77,6 +79,13 @@ $factory->define(MetricPoint::class, function ($faker) { ]; }); +$factory->define(Setting::class, function ($faker) { + return [ + 'name' => 'app_name', + 'value' => 'Cachet Test Demo', + ]; +}); + $factory->define(Subscriber::class, function ($faker) { return [ 'email' => $faker->safeEmail, diff --git a/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php new file mode 100644 index 00000000..4d933d3e --- /dev/null +++ b/database/migrations/2016_07_25_052444_AlterTableComponentGroupsAddVisibleColumn.php @@ -0,0 +1,46 @@ +tinyInteger('visible') + ->after('order') + ->unsigned() + ->default(\CachetHQ\Cachet\Models\ComponentGroup::VISIBLE_AUTHENTICATED); + + $table->index('visible'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('component_groups', function (Blueprint $table) { + $table->dropColumn('visible'); + }); + } +} diff --git a/resources/lang/en/forms.php b/resources/lang/en/forms.php index fac80593..f73059cc 100644 --- a/resources/lang/en/forms.php +++ b/resources/lang/en/forms.php @@ -77,11 +77,14 @@ return [ 'enabled' => 'Component enabled?', 'groups' => [ - 'name' => 'Name', - 'collapsing' => 'Choose visibility of the group', - 'visible' => 'Always expanded', - 'collapsed' => 'Collapse the group by default', - 'collapsed_incident' => 'Collapse the group, but expand if there are issues', + 'name' => 'Name', + 'collapsing' => 'Expand/Collapse options', + 'visible' => 'Always expanded', + 'collapsed' => 'Collapse the group by default', + 'collapsed_incident' => 'Collapse the group, but expand if there are issues', + 'visibility' => 'Visibility', + 'visibility_public' => 'Visible to public', + 'visibility_authenticated' => 'Visible only to logged in users', ], ], diff --git a/resources/views/dashboard/components/groups/add.blade.php b/resources/views/dashboard/components/groups/add.blade.php index dc843f9d..2b6923a1 100644 --- a/resources/views/dashboard/components/groups/add.blade.php +++ b/resources/views/dashboard/components/groups/add.blade.php @@ -29,6 +29,13 @@ +