Completed
Push — master ( d03bbd...580028 )
by Charles
43:02
created

YRC::getUserClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace yrc\components;
4
5
use Yii;
6
use yii\base\Object;
7
8
/**
9
 * Yii2 Rest Component 
10
 * @class YRC
11
 */
12
class YRC extends Object
13
{
14
    /**
15
     * The user class
16
     * @var string
17
     */
18
    public $userClass;
19
20
    /**
21
     * The email address that emails should be sent from
22
     * @var string
23
     */
24
    public $fromEmail;
25
    
26
    /**
27
     * The name to associate with the origin email
28
     * @var string
29
     */
30
    public $fromName;
31
32
    /**
33
     * The access header
34
     * If set access to controller actions is granted if and only if the HTTP header value
35
     * identified by this parameters equals the $accessHeaderSecret property
36
     * @return mixed
37
     */
38
    public $accessHeader;
39
40
    /**
41
     * The access header secret value
42
     * @return mixed
43
     */
44
    public $accessHeaderSecret;
45
46
    /**
47
     * Helper method to get the user class
48
     * @return string
49
     */
50
    public function getUserClass()
51
    {
52
        return $this->userClass;
53
    }
54
55
    /**
56
     * Helper method to check the access header
57
     * @return boolean
58
     */
59
    public function checkAccessHeader($request)
60
    {
61
        // Both the access header and access header secret must be set for this check to validate
62
        if ($this->accessHeader === null || $this->accessHeaderSecret === null) {
63
            return true;
64
        }
65
66
        // Fetch the access header from the request
67
        $header = $request->getHeaders()->get($this->accessHeader);
68
69
        // Allow if the header values match
70
        if (\hash_equals($this->accessHeaderSecret, $header)) {
71
            return true;
72
        }
73
        
74
        // Deny by default
75
        return false;
76
    }
77
}