Passed
Pull Request — master (#18)
by Garion
01:49
created

SessionStore::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace SilverStripe\MFA\Store;
3
4
use SilverStripe\Control\HTTPRequest;
5
use SilverStripe\MFA\Extension\MemberExtension;
6
use SilverStripe\ORM\DataObject;
7
use SilverStripe\Security\Member;
8
9
/**
10
 * This class provides an interface to store data in session during an MFA process. This is implemented as a measure to
11
 * prevent bleeding state between individual MFA auth types
12
 *
13
 * @package SilverStripe\MFA
14
 */
15
class SessionStore implements StoreInterface
16
{
17
    const SESSION_KEY = 'MFASessionStore';
18
19
    /**
20
     * The member that is currently going through the MFA process
21
     *
22
     * @var Member
23
     */
24
    protected $member;
25
26
    /**
27
     * A string representing the current authentication method that is underway
28
     *
29
     * @var string
30
     */
31
    protected $method;
32
33
    /**
34
     * Any state that the current authentication method needs to retain while it is underway
35
     *
36
     * @var array
37
     */
38
    protected $state = [];
39
40
    /**
41
     * @param Member $member
42
     */
43
    public function __construct(Member $member = null)
44
    {
45
        if (!is_null($member)) {
46
            $this->setMember($member);
47
        }
48
    }
49
50
    /**
51
     * Attempt to create a store from the given request getting any existing state from the session of the request
52
     *
53
     * {@inheritdoc}
54
     */
55
    public static function create(HTTPRequest $request)
56
    {
57
        $state = $request->getSession()->get(static::SESSION_KEY);
58
59
        if ($state && $state['member']) {
60
            /** @var Member $member */
61
            $member = DataObject::get_by_id(Member::class, $state['member']);
62
63
            $new = new static($member);
64
            $new->setMethod($state['method']);
65
            $new->setState($state['state']);
66
67
            return $new;
68
        }
69
70
        return new static();
71
    }
72
73
    /**
74
     * @return Member|MemberExtension
75
     */
76
    public function getMember()
77
    {
78
        return $this->member;
79
    }
80
81
    /**
82
     * @param Member $member
83
     * @return $this
84
     */
85
    public function setMember(Member $member)
86
    {
87
        // Early return if there's no change
88
        if ($this->member && $this->member->ID === $member->ID) {
89
            return $this;
90
        }
91
92
        // If the member has changed we should null out the method that's underway and the state of it
93
        $this->resetMethod();
94
95
        $this->member = $member;
96
97
        return $this;
98
    }
99
100
    /**
101
     * @return string
102
     */
103
    public function getMethod()
104
    {
105
        return $this->method;
106
    }
107
108
    /**
109
     * @param string $method
110
     * @return $this
111
     */
112
    public function setMethod($method)
113
    {
114
        $this->method = $method;
115
116
        return $this;
117
    }
118
119
    public function getState()
120
    {
121
        return $this->state;
122
    }
123
124
    public function setState(array $state)
125
    {
126
        $this->state = $state;
127
128
        return $this;
129
    }
130
131
    /**
132
     * Save this store into the session of the given request
133
     *
134
     * {@inheritdoc}
135
     */
136
    public function save(HTTPRequest $request)
137
    {
138
        $request->getSession()->set(static::SESSION_KEY, $this->build());
139
140
        return $this;
141
    }
142
143
    /**
144
     * Clear any stored values for the given request
145
     *
146
     * {@inheritdoc}
147
     */
148
    public static function clear(HTTPRequest $request)
149
    {
150
        $request->getSession()->clear(static::SESSION_KEY);
151
    }
152
153
    protected function resetMethod()
154
    {
155
        $this->setMethod(null)->setState([]);
156
157
        return $this;
158
    }
159
160
    protected function build()
161
    {
162
        return [
163
            'member' => $this->getMember() ? $this->getMember()->ID : null,
164
            'method' => $this->getMethod(),
165
            'state' => $this->getState(),
166
        ];
167
    }
168
}
169