]> BookStack Code Mirror - bookstack/blob - app/Entities/Tools/PermissionsUpdater.php
Added users to permission form interface
[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
14 class PermissionsUpdater
15 {
16     /**
17      * Update an entities permissions from a permission form submit request.
18      */
19     public function updateFromPermissionsForm(Entity $entity, Request $request)
20     {
21         $permissions = $request->get('permissions', null);
22         $ownerId = $request->get('owned_by', null);
23
24         $entity->permissions()->delete();
25
26         if (!is_null($permissions)) {
27             $entityPermissionData = $this->formatPermissionsFromRequestToEntityPermissions($permissions);
28             $entity->permissions()->createMany($entityPermissionData);
29         }
30
31         if (!is_null($ownerId)) {
32             $this->updateOwnerFromId($entity, intval($ownerId));
33         }
34
35         $entity->save();
36         $entity->rebuildPermissions();
37
38         Activity::add(ActivityType::PERMISSIONS_UPDATE, $entity);
39     }
40
41     /**
42      * Update the owner of the given entity.
43      * Checks the user exists in the system first.
44      * Does not save the model, just updates it.
45      */
46     protected function updateOwnerFromId(Entity $entity, int $newOwnerId)
47     {
48         $newOwner = User::query()->find($newOwnerId);
49         if (!is_null($newOwner)) {
50             $entity->owned_by = $newOwner->id;
51         }
52     }
53
54     /**
55      * Format permissions provided from a permission form to be EntityPermission data.
56      */
57     protected function formatPermissionsFromRequestToEntityPermissions(array $permissions): array
58     {
59         $formatted = [];
60         $columnsByType = [
61             'role' => 'role_id',
62             'user' => 'user_id',
63             'fallback' => '',
64         ];
65
66         foreach ($permissions as $type => $byId) {
67             $column  = $columnsByType[$type] ?? null;
68             if (is_null($column)) {
69                 continue;
70             }
71
72             foreach ($byId as $id => $info) {
73                 $entityPermissionData = [];
74
75                 if (!empty($column)) {
76                     $entityPermissionData[$column] = $id;
77                 }
78
79                 foreach (EntityPermission::PERMISSIONS as $permission) {
80                     $entityPermissionData[$permission] = (($info[$permission] ?? false) === "true");
81                 }
82                 $formatted[] = $entityPermissionData;
83             }
84         }
85
86         return $formatted;
87     }
88
89     /**
90      * Copy down the permissions of the given shelf to all child books.
91      */
92     public function updateBookPermissionsFromShelf(Bookshelf $shelf, $checkUserPermissions = true): int
93     {
94         $shelfPermissions = $shelf->permissions()->get(['role_id', 'view', 'create', 'update', 'delete'])->toArray();
95         $shelfBooks = $shelf->books()->get(['id', 'owned_by']);
96         $updatedBookCount = 0;
97
98         /** @var Book $book */
99         foreach ($shelfBooks as $book) {
100             if ($checkUserPermissions && !userCan('restrictions-manage', $book)) {
101                 continue;
102             }
103             $book->permissions()->delete();
104             $book->permissions()->createMany($shelfPermissions);
105             $book->rebuildPermissions();
106             $updatedBookCount++;
107         }
108
109         return $updatedBookCount;
110     }
111 }