DecryptController::index()   C
last analyzed

Complexity

Conditions 14
Paths 13

Size

Total Lines 44
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 24
c 2
b 1
f 0
dl 0
loc 44
rs 6.2666
cc 14
nc 13
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace LeKoala\Encrypt;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Security\Security;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\Security\Permission;
9
use SilverStripe\Versioned\Versioned;
10
use SilverStripe\Control\HTTPResponse;
11
12
/**
13
 * Easily decrypt your files
14
 */
15
class DecryptController extends Controller
16
{
17
    /**
18
     * @return HTTPResponse|void
19
     */
20
    public function index()
21
    {
22
        $request = $this->getRequest();
23
        $ID = (int) $request->getVar("ID");
24
        $Hash = $request->getVar("Hash");
25
26
        if (!$ID || !$Hash) {
27
            return $this->httpError(404);
28
        }
29
30
        $sendDraft = $this->config()->send_draft;
31
32
        /** @var File|null $File */
33
        $File = File::get()->byID($ID);
34
        if (!$File && $sendDraft && class_exists(Versioned::class)) {
35
            /** @var File|null $File */
36
            $File = Versioned::get_one_by_stage(File::class, Versioned::DRAFT, "ID = " . $ID);
37
        }
38
        if (!$File) {
39
            return $this->httpError(404);
40
        }
41
42
        // Verify hash
43
        $FileHash = substr($File->File->Hash, 0, 10);
44
        if ($Hash != $FileHash && !Permission::check("CMS_ACCESS")) {
45
            return $this->httpError(404);
46
        }
47
48
        // Check protected
49
        $sendProtected = $this->config()->send_protected;
50
        $adminSendProtected = $this->config()->admin_send_protected;
51
        $currentUserID = Security::getCurrentUser()->ID ?? 0;
52
        $isOwner = $File->OwnerID === $currentUserID;
53
        if ($File->getVisibility() == "protected") {
54
            if (!$sendProtected && !$isOwner) {
55
                if ($adminSendProtected && Permission::check("CMS_ACCESS")) {
56
                    // We can proceed
57
                } else {
58
                    return $this->httpError(404);
59
                }
60
            }
61
        }
62
63
        EncryptHelper::sendDecryptedFile($File);
64
    }
65
}
66