]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PermissionsUpdater.php
eb4eb6b48581ae037fd95f911b46beea836bee83
[bookstack] / app / Entities / Tools / PermissionsUpdater.php
1 <?php
2
3 namespace BookStack\Entities\Tools;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Permissions\EntityPermission;
7 use BookStack\Auth\User;
8 use BookStack\Entities\Models\Book;
9 use BookStack\Entities\Models\Bookshelf;
10 use BookStack\Entities\Models\Entity;
11 use BookStack\Facades\Activity;
12 use Illuminate\Http\Request;
13 use Illuminate\Support\Collection;
14
15 class PermissionsUpdater
16 {
17     /**
18      * Update an entities permissions from a permission form submit request.
19      */
20     public function updateFromPermissionsForm(Entity $entity, Request $request)
21     {
22         $permissions = $request->get('permissions', null);
23         $ownerId = $request->get('owned_by', null);
24
25         $entity->permissions()->delete();
26
27         if (!is_null($permissions)) {
28             $entityPermissionData = $this->formatPermissionsFromRequestToEntityPermissions($permissions);
29             $entity->permissions()->createMany($entityPermissionData);
30         }
31
32         if (!is_null($ownerId)) {
33             $this->updateOwnerFromId($entity, intval($ownerId));
34         }
35
36         $entity->save();
37         $entity->rebuildPermissions();
38
39         Activity::add(ActivityType::PERMISSIONS_UPDATE, $entity);
40     }
41
42     /**
43      * Update the owner of the given entity.
44      * Checks the user exists in the system first.
45      * Does not save the model, just updates it.
46      */
47     protected function updateOwnerFromId(Entity $entity, int $newOwnerId)
48     {
49         $newOwner = User::query()->find($newOwnerId);
50         if (!is_null($newOwner)) {
51             $entity->owned_by = $newOwner->id;
52         }
53     }
54
55     /**
56      * Format permissions provided from a permission form to be EntityPermission data.
57      */
58     protected function formatPermissionsFromRequestToEntityPermissions(array $permissions): array
59     {
60         $formatted = [];
61
62         foreach ($permissions as $roleId => $info) {
63             $entityPermissionData = ['role_id' => $roleId];
64             foreach (EntityPermission::PERMISSIONS as $permission) {
65                 $entityPermissionData[$permission] = (($info[$permission] ?? false) === "true");
66             }
67             $formatted[] = $entityPermissionData;
68         }
69
70         return $formatted;
71     }
72
73     /**
74      * Copy down the permissions of the given shelf to all child books.
75      */
76     public function updateBookPermissionsFromShelf(Bookshelf $shelf, $checkUserPermissions = true): int
77     {
78         $shelfPermissions = $shelf->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
79         $shelfBooks = $shelf->books()->get(['id', 'owned_by']);
80         $updatedBookCount = 0;
81
82         /** @var Book $book */
83         foreach ($shelfBooks as $book) {
84             if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
85                 continue;
86             }
87             $book->permissions()->delete();
88             $book->permissions()->createMany($shelfPermissions);
89             $book->rebuildPermissions();
90             $updatedBookCount++;
91         }
92
93         return $updatedBookCount;
94     }
95 }