RestrictionApi Module
Purpose
Provides a new restriction settings backend interface to add access and visibility restrictions to models.
Access
Ability to set a password against a model to prevent access to its page and sub-pages.
Visibility
Ability to prevent the display of a model in search and block listings.
Also comes with a scope to filter out models with visibility restrictions from custom listings.
Usage
- Update your model to support restriction settings:
- Make your model implement the interface
Modules\RestrictionApi\Contracts\HasRestrictionInterfaceand use the traitModules\RestrictionApi\Concerns\HasRestrictions - Add a new
restriction_settingsaction url and context sub link to your model for the dashboard. e.g
'restriction_settings' => route( ProjectRouteService::DASH_PROJECT_RESTRICTION_SETTINGS_ROUTE_NAME.'index', ['your-model' => $this->id], false )- If the model is indexed by elasticsearch:
- Update the model prepare() function to get the value of the restricted_listing_visibility attribute:
$searchable['restricted_listing_visibility'] = $this->restricted_listing_visibility;- Add the
restricted_listing_visibilityattribute to the mappableAs() function:
'restricted_listing_visibility' => 'boolean',
- Make your model implement the interface
- Add new restriction settings endpoints to the dashboard controller of your model:
- Make sure your controller uses the trait
Modules\RestrictionApi\Http\Controllers\Concerns\RestrictionSettingsDashboardControllerTrait - Add 2x new functions to render settings form and update:
/** * View restriction settings (visibility, access, etc...) */ public function restrictionSettingsIndex(Project $project): Response { return $this->renderRestrictionSettings( $project->append(self::$globalAppends), $project->getRestrictionSettingsUpdateUrl( ProjectRouteService::DASH_PROJECT_BASE_ROUTE_NAME, [ProjectRouteService::PROJECT_ROUTE_MODEL_KEY => $project->id] ), $this->parentRoute, ); } /** * Store or update a new restriction. */ public function restrictionSettingsUpdate(DashboardRestrictionSettingsFormRequest $request, Project $project): RedirectResponse { return $this->saveRestrictionSettings($request, $project); } - Make sure your controller uses the trait
- Update your model routes:
- Add backend routes to control the restriction settings:
Route::name(BaseRouteServiceProvider::removeDashPrefixFromName(self::DASH_PROJECT_RESTRICTION_SETTINGS_ROUTE_NAME)) ->prefix(RestrictionRouteService::RESTRICTION_SETTINGS_ROUTE) ->middleware('can:'.PermissionActions::UPDATE.','.ProjectPermissions::SUBJECT) ->group(fn () => RestrictionRouteService::registerDashboardSettingsRoutes($projectController));- Wrap all frontend routes of your restricted model around the restricted access middleware to prevent access to models with restrictions and redirect to auth form:
Route::middleware('restricted.access:'.self::PROJECT_ROUTE_MODEL_KEY)->group( function () use ($projectController) { // Define project resource routes. Route::resource(self::PROJECT_ROUTE_RESOURCE, $projectController)->only([ 'index', 'show', ]); } );- Wrap frontend routes of any child models of your restricted model if it is a parentable, e.g for a child Event:
Route::middleware('restricted.access:'.ParentableContentService::PARENTABLE_MODEL_ROUTE_KEY) ->group(fn () => EventRouteService::registerWebRoutes(...$controllerClasses)); - Add feature tests to cover access and visibility restrictions.