Completed
Push — master ( b97427...e235cc )
by Raffael
30:35 queued 26:08
created

Lock::validateRequest()   A

Complexity

Conditions 6
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 0
cts 14
cp 0
rs 9.0777
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 42
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * balloon
7
 *
8
 * @copyright   Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Balloon\App\Api\Hook;
13
14
use Balloon\Filesystem\Exception;
15
use Balloon\Filesystem\Node\Collection;
16
use Balloon\Filesystem\Node\File;
17
use Balloon\Filesystem\Node\NodeInterface;
18
use Balloon\Hook\AbstractHook;
19
use MongoDB\BSON\ObjectId;
20
21
class Lock extends AbstractHook
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26
    public function preCreateCollection(Collection $parent, string &$name, array &$attributes, bool $clone): void
27
    {
28
        $this->validateRequest($parent);
29
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public function preCreateFile(Collection $parent, string &$name, array &$attributes, bool $clone): void
35
    {
36
        $this->validateRequest($parent);
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function preDeleteCollection(Collection $node, bool $force, ?string $recursion, bool $recursion_first): void
43
    {
44
        $this->validateRequest($node);
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function preRestoreFile(File $node, int $version): void
51
    {
52
        $this->validateRequest($node);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function prePutFile(File $node, ObjectId $session): void
59
    {
60
        $this->validateRequest($node);
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function preDeleteFile(File $node, bool $force, ?string $recursion, bool $recursion_first): void
67
    {
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function preSaveNodeAttributes(
74
        NodeInterface $node,
75
        array &$save_attributes,
76
        array &$remove_attributes,
77
        ?string $recursion,
78
        bool $recursion_first
79
    ): void {
80
        $this->validateRequest($node);
81
    }
82
83
    /**
84
     * Validate request.
85
     */
86
    protected function validateRequest(NodeInterface $node): bool
87
    {
88
        if (isset($_SERVER['ORIG_SCRIPT_NAME']) && preg_match('#^/index.php/api/#', $_SERVER['ORIG_SCRIPT_NAME']) && $node->isLocked()) {
89
            $token = $_SERVER['HTTP_LOCK_TOKEN'] ?? null;
90
            $lock = $node->getLock();
91
92
            if ($token === null) {
93
                throw new Exception\Locked('node is temporarily locked and can not be changed');
94
            }
95
96
            if ($lock['id'] !== $token) {
97
                throw new Exception\LockIdMissmatch('node is temporarily locked and requested id does not match the lock token');
98
            }
99
        }
100
101
        return true;
102
    }
103
}
104