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

Fix twill model extensibility [#2479] #2498

Merged
merged 4 commits into from
Feb 28, 2024
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
3 changes: 1 addition & 2 deletions src/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace A17\Twill;

use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\User;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

Expand Down Expand Up @@ -159,7 +158,7 @@ public function boot()

$this->define('publish-user', function ($user) {
return $this->authorize($user, function ($user) {
$editedUserObject = User::find(request('id'));
$editedUserObject = twillModel('user')::find(request('id'));
return $this->userHasRole(
$user,
[TwillPermissions::roles()::ADMIN]
Expand Down
2 changes: 1 addition & 1 deletion src/Commands/ListBlocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ protected function getBlocks(): Collection
// We do not render this.
unset($data['rules'], $data['rulesForTranslatedFields']);

$data['block class'] = $block::class !== 'A17\Twill\Services\Blocks\Block' ? get_class($block) : 'default';
$data['block class'] = $block::class !== Block::class ? get_class($block) : 'default';

$list[] = $data;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Helpers/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use A17\Twill\Facades\TwillBlocks;
use A17\Twill\Facades\TwillCapsules;
use A17\Twill\Models\Model;
use A17\Twill\Services\Blocks\Block;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -208,6 +209,7 @@ function fix_directory_separator($path)
}

if (! function_exists('twillModel')) {
/** @return class-string<Model>|Model It returns a class string but this is for the correct type hints */
function twillModel($model): string
{
return config("twill.models.$model")
Expand Down
3 changes: 1 addition & 2 deletions src/Http/Controllers/Admin/ModuleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use A17\Twill\Models\Behaviors\HasSlug;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\Contracts\TwillSchedulableModel;
use A17\Twill\Models\Group;
use A17\Twill\Repositories\ModuleRepository;
use A17\Twill\Services\Breadcrumbs\Breadcrumbs;
use A17\Twill\Services\Forms\Fields\BaseFormField;
Expand Down Expand Up @@ -2674,7 +2673,7 @@ protected function respondWithJson($message, $variant)
protected function getGroupUserMapping()
{
if (config('twill.enabled.permissions-management')) {
return Group::with('users')->get()
return twillModel('group')::with('users')->get()
->mapWithKeys(function ($group) {
return [$group->id => $group->users()->pluck('id')->toArray()];
})->toArray();
Expand Down
7 changes: 3 additions & 4 deletions src/Http/Controllers/Admin/ResetPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace A17\Twill\Http\Controllers\Admin;

use A17\Twill\Models\User;
use Illuminate\Config\Repository as Config;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -82,7 +81,7 @@ public function broker()

protected function sendResetResponse(Request $request, $response)
{
$user = User::where('email', $request->input('email'))->first();
$user = twillModel('user')::where('email', $request->input('email'))->first();
if (!$user->isActivated()) {
$user->registered_at = Carbon::now();
$user->save();
Expand Down Expand Up @@ -156,12 +155,12 @@ private function getUserFromToken($token)
$clearToken = DB::table($this->config->get('auth.passwords.twill_users.table', 'twill_password_resets'))->where('token', $token)->first();

if ($clearToken) {
return User::where('email', $clearToken->email)->first();
return twillModel('user')::where('email', $clearToken->email)->first();
}

foreach (DB::table($this->config->get('auth.passwords.twill_users.table', 'twill_password_resets'))->get() as $passwordReset) {
if (Hash::check($token, $passwordReset->token)) {
return User::where('email', $passwordReset->email)->first();
return twillModel('user')::where('email', $passwordReset->email)->first();
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
use A17\Twill\Enums\PermissionLevel;
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\Group;
use A17\Twill\Models\Permission;
use A17\Twill\Models\Role;
use A17\Twill\Models\User;
use A17\Twill\Services\Listings\Columns\Image;
use A17\Twill\Services\Listings\Columns\Text;
use A17\Twill\Services\Listings\Filters\QuickFilter;
Expand Down Expand Up @@ -99,7 +96,7 @@ public function __construct(Application $app, Request $request, AuthFactory $aut

TwillPermissions::showUserSecondaryNavigation();

$this->filters['role'] = User::getRoleColumnName();
$this->filters['role'] = twillModel('user')::getRoleColumnName();
}

public function getIndexTableColumns(): TableColumns
Expand Down Expand Up @@ -137,7 +134,7 @@ public function getIndexTableColumns(): TableColumns
);
$tableColumns->add(
Text::make()
->field(User::getRoleColumnName())
->field(twillModel('user')::getRoleColumnName())
->title('Role')
->customRender(function (TwillModelContract $user) {
if (TwillPermissions::enabled()) {
Expand Down Expand Up @@ -325,7 +322,7 @@ public function resendRegistrationEmail($userId)
private function getGroupPermissionMapping()
{
if (config('twill.enabled.permissions-management')) {
return Group::with('permissions')->get()
return twillModel('group')::with('permissions')->get()
->mapWithKeys(function ($group) {
return [$group->id => $group->permissions];
})->toArray();
Expand All @@ -338,7 +335,7 @@ private function getGroups()
{
if (config('twill.enabled.permissions-management')) {
// Forget first one because it's the "Everyone" group and we don't want to show it inside admin.
return Group::with('permissions')->pluck('name', 'id')->forget(1);
return twillModel('group')::with('permissions')->pluck('name', 'id')->forget(1);
}

return [];
Expand All @@ -347,7 +344,7 @@ private function getGroups()
private function getRoleList()
{
if (config('twill.enabled.permissions-management')) {
return Role::accessible()->published()->get()->map(function ($role) {
return twillModel('role')::accessible()->published()->get()->map(function ($role) {
return ['value' => $role->id, 'label' => $role->name];
})->toArray();
}
Expand Down
8 changes: 3 additions & 5 deletions src/Http/Requests/Admin/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace A17\Twill\Http\Requests\Admin;

use A17\Twill\Models\Role;
use A17\Twill\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use PragmaRX\Google2FA\Google2FA;
Expand Down Expand Up @@ -58,7 +56,7 @@ public function rules(): array
}
},
'force-2fa-disable-challenge' => function ($attribute, $value, $fail) {
$user = User::findOrFail($this->route('user'));
$user = twillModel('user')::findOrFail($this->route('user'));
if ($this->get('google_2fa_enabled') || ! $user->google_2fa_enabled) {
return;
}
Expand Down Expand Up @@ -95,12 +93,12 @@ private function getRoleValidator($baseRule = [])
{
if (config('twill.enabled.permissions-management')) {
// Users can't assign roles above their own
$accessibleRoleIds = Role::accessible()->pluck('id')->toArray();
$accessibleRoleIds = twillModel('role')::accessible()->pluck('id')->toArray();
$baseRule[] = Rule::in($accessibleRoleIds);
} else {
$baseRule[] = 'not_in:SUPERADMIN';
}

return [User::getRoleColumnName() => $baseRule];
return [twillModel('user')::getRoleColumnName() => $baseRule];
}
}
2 changes: 1 addition & 1 deletion src/Models/Revision.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(array $attributes = [])

public function user()
{
return $this->belongsTo(User::class);
return $this->belongsTo(twillModel('user'));
}

public function getByUserAttribute()
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Role.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function scopeOnlyTrashed($query): Builder

public function users(): HasMany
{
return $this->hasMany(User::class);
return $this->hasMany(twillModel('user'));
}

public function getCreatedAtAttribute($value): string
Expand Down
4 changes: 2 additions & 2 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function sendPasswordResetByAdminNotification($password)

public function groups()
{
return $this->belongsToMany(Group::class, 'group_twill_user', 'twill_user_id', 'group_id');
return $this->belongsToMany(twillModel('group'), 'group_twill_user', 'twill_user_id', 'group_id');
}

public function publishedGroups()
Expand All @@ -254,7 +254,7 @@ public function publishedGroups()

public function role()
{
return $this->belongsTo(Role::class);
return $this->belongsTo(twillModel('role'));
}

public function allPermissions()
Expand Down
3 changes: 1 addition & 2 deletions src/Models/UserOauth.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace A17\Twill\Models;

use A17\Twill\Models\User;
use Illuminate\Database\Eloquent\Model as BaseModel;

class UserOauth extends BaseModel
Expand All @@ -24,6 +23,6 @@ public function __construct(array $attributes = [])

public function user()
{
$this->belongsTo(User::class, 'user_id');
$this->belongsTo(twillModel('user'), 'user_id');
}
}
6 changes: 3 additions & 3 deletions src/Repositories/UserRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use A17\Twill\Facades\TwillPermissions;
use A17\Twill\Models\Contracts\TwillModelContract;
use A17\Twill\Models\User;
use A17\Twill\Models\Group;
use A17\Twill\Repositories\Behaviors\HandleMedias;
use A17\Twill\Repositories\Behaviors\HandleOauth;
use A17\Twill\Repositories\Behaviors\HandleUserPermissions;
Expand Down Expand Up @@ -79,10 +78,11 @@ public function getFormFieldsForBrowser(
$browserFields = parent::getFormFieldsForBrowser($object, $relation, $routePrefix, $titleKey, $moduleName);

if (TwillPermissions::enabled()) {
$everyoneGroup = twillModel('group')::getEveryoneGroup();
foreach ($browserFields as $index => $browserField) {
if (
$browserField['id'] === Group::getEveryoneGroup()->id &&
$browserField['name'] === Group::getEveryoneGroup()->name
$browserField['id'] === $everyoneGroup->id &&
$browserField['name'] === $everyoneGroup->name
) {
$browserFields[$index]['edit'] = false;
$browserFields[$index]['deletable'] = false;
Expand Down
Loading