area17/twill
Twill is an open-source Laravel package for building a custom CMS fast. It provides a polished admin UI with prebuilt features and Vue components, stays flexible and extensible, works headless or integrated, and lets you use your own models with no lock-in.
This is a simple solution to allow site administrators to manage frontend user profiles from the CMS. It can be used as a starting point to implement a user approval workflow and content access restrictions.
Objectives:
Profiles section to the CMS for site administratorsProfile records to users upon
registrationRequirements:
This module will be used to attach extra information to User records:
php artisan twill:make:module Profiles
Update the generated migration to add the required fields:
:::filename:::
database/migrations/2021_08_01_204153_create_profiles_tables.php
:::#filename:::
{
"file": "./manage_frontend_user_profiles_from_twill/2021_08_01_204153_create_profiles_tables.php",
"focusMethods": "up"
}
Then, run the migration:
php artisan migrate
Edit the fillable fields to match the new schema and add the Profile > User relationship:
:::filename:::
resources/views/admin/repeaters/tasks.blade.php
:::#filename:::
{
"file": "./manage_frontend_user_profiles_from_twill/Profile.php",
"focusMethods": "user",
"focusProperties": "fillable"
}
Define the User > Profile relationship and use the model's booted() method to
hook into the created event. When a user is created through the Laravel Breeze
user registration form, automatically create and assign a Profile record.
Finally, define the name attribute accessor. This will allow existing
Laravel Breeze code to access the user name from the attached profile:
:::filename:::
app/Models/User.php
:::#filename:::
{
"file": "./manage_frontend_user_profiles_from_twill/User.php",
"collapseAll": "",
"focusMethods": ["profile", "booted", "getNameAttribute"]
}
Define our custom name column to be used instead of the default title and
prevent administrators from creating and deleting profiles in the CMS
:::filename:::
app/Http/Controllers/Twill/ProfileController.php
:::#filename:::
{
"file": "./manage_frontend_user_profiles_from_twill/ProfileController.php",
"collapseAll": "",
"focusProperties": ["titleColumnKey", "indexOptions"]
}
:::filename:::
resources/views/twill/profiles/form.blade.php
:::#filename:::
[@extends](https://github.com/extends)('twill::layouts.form')
[@section](https://github.com/section)('contentFields')
<x-twill::input
type="textarea"
name="description"
label="Description"
:maxlength="1000"
/>
<x-twill::checkbox {{-- [tl! ++] --}}
name="is_vip"{{-- [tl! ++] --}}
label="Can access all VIP content"{{-- [tl! ++] --}}
/>
[@stop](https://github.com/stop)
:::filename:::
app/Http/Requests/Twill/ProfileRequest.php
:::#filename:::
{
"file": "./manage_frontend_user_profiles_from_twill/ProfileRequest.php",
"collapseAll": "",
"focusMethods": ["rulesForUpdate"]
}
Add the module to your twill-navigation.php and to your admin.php
routes and you are done!
Site administrators now have access to a Profiles section in the CMS,
to edit basic user information.
Within your site's views and controllers, you can access the profile
information for the current user via Auth::user()->profile.
Upon registration, a user profile is created with a draft status (ie.
not published). This can be used to implement a user approval workflow:
[@if](https://github.com/if) (Auth::user()->profile->published)
{{-- Account has been approved, show the dashboard --}}
[@else](https://github.com/else)
{{-- Account has not yet been approved, show "pending approval" message --}}
[@endif](https://github.com/endif)
The same technique also applies for granular access control (e.g. a VIP section with additional content).
A frontend form can be added to allow users to edit their descriptions. As always, make sure to sanitize user input before storing it in the database.
For simplicity, this implementation prevents site administrators from creating and deleting users from the CMS.
A few methods from ModuleRepository can be extended in
ProfileRepository to implement the feature:
afterSave(),
for user creationafterDelete(),
for user deletionHow can I help you explore Laravel packages today?