Passed
Push — master ( 22b305...33bedb )
by Thomas
02:16
created

DecryptController::index()   C

Complexity

Conditions 12
Paths 13

Size

Total Lines 41
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 41
rs 6.9666
cc 12
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\Control\Controller;
7
use SilverStripe\Security\Permission;
8
use SilverStripe\Security\Security;
9
use SilverStripe\Versioned\Versioned;
10
11
/**
12
 * Easily decrypt your files
13
 */
14
class DecryptController extends Controller
15
{
16
    public function index()
17
    {
18
        $request = $this->getRequest();
19
        $ID = $request->getVar("ID");
20
        $Hash = $request->getVar("Hash");
21
22
        if (!$ID || !$Hash) {
23
            return $this->httpError(404);
24
        }
25
26
        /** @var File|EncryptedDBFile $File  */
27
        $File = File::get()->byID($ID);
28
        if (!$File) {
0 ignored issues
show
introduced by
$File is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
29
            $File = Versioned::get_latest_version(File::class, $ID);
30
        }
31
        if (!$File) {
0 ignored issues
show
introduced by
$File is of type SilverStripe\Assets\File, thus it always evaluated to true.
Loading history...
32
            return $this->httpError(404);
33
        }
34
35
        // Verify hash
36
        $FileHash = substr($File->File->Hash, 0, 10);
37
        if ($Hash != $FileHash && !Permission::check("CMS_ACCESS")) {
38
            return $this->httpError(404);
39
        }
40
41
        // Check protected
42
        $sendProtected = $this->config()->send_protected;
43
        $adminSendProtected = $this->config()->admin_send_protected;
44
        $currentUserID = Security::getCurrentUser()->ID ?? 0;
45
        $isOwner = $File->OwnerID === $currentUserID;
46
        if ($File->getVisibility() == "protected") {
47
            if (!$sendProtected && !$isOwner) {
48
                if ($adminSendProtected && Permission::check("CMS_ACCESS")) {
49
                    // We can proceed
50
                } else {
51
                    return $this->httpError(404);
52
                }
53
            }
54
        }
55
56
        $File->sendDecryptedFile();
57
    }
58
}
59