Skip to content

Commit

Permalink
add observer
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed Aug 14, 2024
1 parent 81637a1 commit 063774c
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Staff/GroupMemberController.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function update(Group $group, User $member, Request $request)
'level' => new Enum(GroupUserLevel::class),
]);

$requestMember = $group->users()->find($request->user()->id)->pivot;
$requestMember = $group->users()->find($member->id)->pivot;
$this->authorize("update", $requestMember);

$pivot = $group->users()->find($member->id)->pivot;
Expand Down
7 changes: 7 additions & 0 deletions app/Http/Controllers/Staff/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function index(Request $request)

$departments = Group::where('type', GroupTypeEnum::Department)
->withCount('users')->get();

$departmentsSortedByMembershipAndUserCount = $departments->sortByDesc(fn($department) => [
$myDepartments->contains($department->id),
$department->users_count
Expand Down Expand Up @@ -51,6 +52,12 @@ public function show(Group $group, Request $request)
public function update(GroupUpdateRequest $request, Group $group)
{
Gate::authorize('update', $group);
// if name isset then it needs to be not null
if (isset($request->validated()['name'])) {
$request->validate([
'name' => ['required', 'string', 'max:255'],
]);
}
$group->update($request->validated());
return redirect()->back();

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/Groups/GroupUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function rules(): array
{
return [
'description' => ['nullable','string','max:10000'],
'name' => ['required','string','max:255'],
'name' => ['nullable','string','max:255'],
];
}

Expand Down
6 changes: 5 additions & 1 deletion app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function getHashidsConnection()
public function setNameAttribute($value)
{
$this->attributes['name'] = $value;
$this->attributes['slug'] = Str::slug($value);
$prefix = '';
if ($this->parent) {
$prefix = $this->parent->name.' / ';
}
$this->attributes['slug'] = $prefix.Str::slug($value);
}

public function getLogoUrlAttribute()
Expand Down
13 changes: 13 additions & 0 deletions app/Observers/GroupObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@
use App\Enums\GroupUserLevel;
use App\Models\Group;
use App\Services\NextcloudService;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Auth;

class GroupObserver
{
public function created(Group $group)
{
if (App::isLocal()) {
return;
}

if ($group->type === GroupTypeEnum::Team && $group->parent->nextcloud_folder_id) {
NextcloudService::createGroup($group->hashid);
// Parent->name / Group->name
Expand All @@ -29,6 +34,10 @@ public function created(Group $group)

public function updated(Group $group): void
{
if (App::isLocal()) {
return;
}

if ($group->isDirty('nextcloud_folder_name') && !app()->runningUnitTests()) {
// Update or create the folder via nextcloud
if ($group->nextcloud_folder_id) {
Expand Down Expand Up @@ -61,6 +70,10 @@ public function updated(Group $group): void

public function deleted(Group $group)
{
if (App::isLocal()) {
return;
}

NextcloudService::deleteGroup($group->hashid);
}
}
10 changes: 10 additions & 0 deletions app/Observers/GroupUserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Jobs\CheckStaffGroupMembershipJob;
use App\Models\GroupUser;
use App\Services\NextcloudService;
use Illuminate\Support\Facades\App;

class GroupUserObserver
{
Expand All @@ -15,6 +16,9 @@ public function created(GroupUser $groupUser): void
if ($groupUser->group->type === GroupTypeEnum::Department) {
CheckStaffGroupMembershipJob::dispatch($groupUser->user);
}
if (App::isLocal()) {
return;
}
if (($groupUser->group->nextcloud_folder_name || $groupUser->group->parent?->nextcloud_folder_name) && !app()->runningUnitTests()) {
NextcloudService::addUserToGroup($groupUser->group, $groupUser->user);
$allowAclManagement = in_array($groupUser->level, [GroupUserLevel::Admin, GroupUserLevel::Owner]);
Expand All @@ -26,6 +30,9 @@ public function created(GroupUser $groupUser): void

public function updated(GroupUser $groupUser): void
{
if (App::isLocal()) {
return;
}
if ($groupUser->group->nextcloud_folder_name && !app()->runningUnitTests()) {
if ($groupUser->isDirty('level')) {
$allowAclManagement = in_array($groupUser->level, [GroupUserLevel::Admin, GroupUserLevel::Owner]);
Expand All @@ -39,6 +46,9 @@ public function deleted(GroupUser $groupUser): void
if ($groupUser->group->type === GroupTypeEnum::Department) {
CheckStaffGroupMembershipJob::dispatch($groupUser->user);
}
if (App::isLocal()) {
return;
}
if ($groupUser->group->nextcloud_folder_name && !app()->runningUnitTests()) {
NextcloudService::removeUserFromGroup($groupUser->group, $groupUser->user);
if ($groupUser->group->type !== GroupTypeEnum::Team) {
Expand Down

0 comments on commit 063774c

Please sign in to comment.