Skip to content

Commit 8a25353

Browse files
author
Hatim EL OUFIR
committed
Spatie permissions integration (finalization)
1 parent 1fcf417 commit 8a25353

22 files changed

Lines changed: 378 additions & 132 deletions

app/Core/LogsActivity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function getActivitylogOptions(): LogOptions
2222
->logOnly($this->getFillable())
2323
->setDescriptionForEvent(fn(string $eventName) => new HtmlString(
2424
'<div class="flex flex-col gap-1">'
25-
. auth()->user()->name . " " . $eventName . " " . $this->fromCamelCase((new \ReflectionClass($this))->getShortName()) . " " . $this
25+
. (auth()->user()->name ?? '') . " " . $eventName . " " . $this->fromCamelCase((new \ReflectionClass($this))->getShortName()) . " " . $this
2626
. ' <a class="text-primary-500 hover:underline hover:cursor-pointer" target="_blank" href="' . $this->activityLogLink() . '">' . __('See details') . '</a>'
2727
. '</div>'
2828
));

app/Http/Livewire/Administration/Companies.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Filament\Tables\Actions\Action;
88
use Filament\Tables\Columns\BooleanColumn;
99
use Filament\Tables\Columns\ImageColumn;
10+
use Filament\Tables\Columns\TagsColumn;
1011
use Filament\Tables\Columns\TextColumn;
1112
use Filament\Tables\Concerns\InteractsWithTable;
1213
use Filament\Tables\Contracts\HasTable;
@@ -35,7 +36,14 @@ public function render()
3536
*/
3637
protected function getTableQuery(): Builder|Relation
3738
{
38-
return Company::query();
39+
$query = Company::query();
40+
if (auth()->user()->can('View own companies') && !auth()->user()->can('View all companies')) {
41+
$query->where('responsible_id', auth()->user()->id);
42+
} elseif (!auth()->user()->can('View all companies')) {
43+
// Get empty list
44+
$query->whereNull('id');
45+
}
46+
return $query;
3947
}
4048

4149
/**
@@ -69,6 +77,12 @@ protected function getTableColumns(): array
6977
->searchable()
7078
->sortable(),
7179

80+
TagsColumn::make('users.name')
81+
->label(__('Company users'))
82+
->limit(1)
83+
->searchable()
84+
->sortable(),
85+
7286
TextColumn::make('created_at')
7387
->label(__('Created at'))
7488
->sortable()
@@ -89,6 +103,7 @@ protected function getTableActions(): array
89103
->icon('heroicon-o-pencil')
90104
->link()
91105
->label(__('Edit company'))
106+
->visible(fn () => auth()->user()->can('Update companies'))
92107
->action(fn(Company $record) => $this->updateCompany($record->id))
93108
];
94109
}

app/Http/Livewire/Administration/CompaniesDialog.php

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Models\CompanyUser;
56
use App\Models\Icon;
67
use App\Models\Company;
78
use App\Models\User;
9+
use Filament\Forms\Components\CheckboxList;
810
use Filament\Forms\Components\ColorPicker;
911
use Filament\Forms\Components\FileUpload;
1012
use Filament\Forms\Components\Grid;
13+
use Filament\Forms\Components\MultiSelect;
1114
use Filament\Forms\Components\RichEditor;
1215
use Filament\Forms\Components\Select;
1316
use Filament\Forms\Components\TextInput;
@@ -39,6 +42,7 @@ public function mount(): void
3942
'description' => $this->company->description,
4043
'is_disabled' => $this->company->is_disabled,
4144
'responsible_id' => $this->company->responsible_id,
45+
'users' => $this->company->users->pluck('id')->toArray()
4246
]);
4347
}
4448

@@ -60,33 +64,33 @@ protected function getFormSchema(): array
6064
Grid::make(5)
6165
->schema([
6266

63-
Grid::make(1)
64-
->columnSpan(2)
65-
->schema([
66-
FileUpload::make('logo')
67-
->image()
68-
->maxSize(10240)
69-
->label(__('Logo')),
70-
]),
71-
72-
Grid::make(1)
73-
->columnSpan(3)
74-
->schema([
75-
76-
TextInput::make('name')
77-
->label(__('Company name'))
78-
->maxLength(255)
79-
->unique(table: Company::class, column: 'name', ignorable: fn () => $this->company, callback: function (Unique $rule) {
80-
return $rule->withoutTrashed();
81-
})
82-
->required(),
83-
84-
Select::make('responsible_id')
85-
->label(__('Responsible'))
86-
->searchable()
87-
->required()
88-
->options(User::all()->pluck('name', 'id')->toArray()),
89-
]),
67+
Grid::make(1)
68+
->columnSpan(2)
69+
->schema([
70+
FileUpload::make('logo')
71+
->image()
72+
->maxSize(10240)
73+
->label(__('Logo')),
74+
]),
75+
76+
Grid::make(1)
77+
->columnSpan(3)
78+
->schema([
79+
80+
TextInput::make('name')
81+
->label(__('Company name'))
82+
->maxLength(255)
83+
->unique(table: Company::class, column: 'name', ignorable: fn() => $this->company, callback: function (Unique $rule) {
84+
return $rule->withoutTrashed();
85+
})
86+
->required(),
87+
88+
Select::make('responsible_id')
89+
->label(__('Responsible'))
90+
->searchable()
91+
->required()
92+
->options(User::all()->pluck('name', 'id')->toArray()),
93+
]),
9094

9195
]),
9296

@@ -98,6 +102,10 @@ protected function getFormSchema(): array
98102

99103
Toggle::make('is_disabled')
100104
->label(__('Disable access to this company')),
105+
106+
MultiSelect::make('users')
107+
->label(__('Company users'))
108+
->options(User::all()->pluck('name', 'id')->toArray())
101109
];
102110
}
103111

@@ -117,6 +125,12 @@ public function save(): void
117125
'is_disabled' => $data['is_disabled'] ?? false,
118126
'responsible_id' => $data['responsible_id'],
119127
]);
128+
foreach ($data['users'] as $user) {
129+
CompanyUser::create([
130+
'company_id' => $company->id,
131+
'user_id' => $user
132+
]);
133+
}
120134
Notification::make()
121135
->success()
122136
->title(__('Company created'))
@@ -129,6 +143,13 @@ public function save(): void
129143
$this->company->is_disabled = $data['is_disabled'];
130144
$this->company->responsible_id = $data['responsible_id'];
131145
$this->company->save();
146+
CompanyUser::where('company_id', $this->company->id)->delete();
147+
foreach ($data['users'] as $user) {
148+
CompanyUser::create([
149+
'company_id' => $this->company->id,
150+
'user_id' => $user
151+
]);
152+
}
132153
Notification::make()
133154
->success()
134155
->title(__('Company updated'))

app/Http/Livewire/Administration/Users.php

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Livewire\Administration;
44

5+
use App\Models\Company;
56
use App\Models\User;
67
use App\Notifications\UserCreatedNotification;
78
use Filament\Forms\Components\TagsInput;
@@ -13,6 +14,7 @@
1314
use Filament\Tables\Columns\TextColumn;
1415
use Filament\Tables\Concerns\InteractsWithTable;
1516
use Filament\Tables\Contracts\HasTable;
17+
use Filament\Tables\Filters\SelectFilter;
1618
use Illuminate\Database\Eloquent\Builder;
1719
use Illuminate\Database\Eloquent\Relations\Relation;
1820
use Livewire\Component;
@@ -37,7 +39,18 @@ public function render()
3739
*/
3840
protected function getTableQuery(): Builder|Relation
3941
{
40-
return User::query();
42+
$query = User::query();
43+
if (auth()->user()->can('View company users') && !auth()->user()->can('View all users')) {
44+
$query->whereHas('companies', fn ($query) =>
45+
$query->whereIn('companies.id',
46+
auth()->user()->ownCompanies->pluck('id')->toArray()
47+
)
48+
);
49+
} elseif (!auth()->user()->can('View all users')) {
50+
// Get empty list
51+
$query->whereNull('id');
52+
}
53+
return $query;
4154
}
4255

4356
/**
@@ -61,6 +74,13 @@ protected function getTableColumns(): array
6174
TagsColumn::make('permissions.name')
6275
->label(__('Permissions'))
6376
->limit(1)
77+
->visible(fn () => auth()->user()->can('Assign permissions'))
78+
->searchable()
79+
->sortable(),
80+
81+
TagsColumn::make('companies.name')
82+
->label(__('Companies'))
83+
->limit(1)
6484
->searchable()
6585
->sortable(),
6686

@@ -85,13 +105,14 @@ protected function getTableActions(): array
85105
->link()
86106
->color('warning')
87107
->label(__('Resend activation email'))
88-
->visible(fn(User $record) => $record->register_token)
108+
->visible(fn(User $record) => $record->register_token && auth()->user()->can('Update users'))
89109
->action(fn(User $record) => $this->resendActivationEmail($record->id)),
90110

91111
Action::make('edit')
92112
->icon('heroicon-o-pencil')
93113
->link()
94114
->label(__('Edit user'))
115+
->visible(fn () => auth()->user()->can('Update users'))
95116
->action(fn(User $record) => $this->updateUser($record->id))
96117
];
97118
}
@@ -116,6 +137,32 @@ protected function getDefaultTableSortDirection(): ?string
116137
return 'desc';
117138
}
118139

140+
/**
141+
* Table filters definition
142+
*
143+
* @return array
144+
*/
145+
protected function getTableFilters(): array
146+
{
147+
return [
148+
SelectFilter::make('isAccountActivated')
149+
->label(__('Account activated'))
150+
->placeholder(__('All users'))
151+
->options([
152+
'yes' => __('Yes'),
153+
'no' => __('No'),
154+
])
155+
->query(function ($state, $query) {
156+
if ($state['value'] === 'yes') {
157+
$query->whereNull('register_token');
158+
}
159+
if ($state['value'] === 'no') {
160+
$query->whereNotNull('register_token');
161+
}
162+
})
163+
];
164+
}
165+
119166
/**
120167
* Show update user dialog
121168
*

0 commit comments

Comments
 (0)