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

SessionStore::resetMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
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
        $this->member = $member;
46
    }
47
48
    /**
49
     * Attempt to create a store from the given request getting any existing state from the session of the request
50
     *
51
     * {@inheritdoc}
52
     */
53
    public static function create(HTTPRequest $request)
54
    {
55
        $state = $request->getSession()->get(static::SESSION_KEY);
56
57
        if ($state && $state['member']) {
58
            /** @var Member $member */
59
            $member = DataObject::get_by_id(Member::class, $state['member']);
60
61
            $new = new static($member);
62
            $new->setMethod($state['method']);
63
            $new->setState($state['state']);
64
65
            return $new;
66
        }
67
68
        return new static();
69
    }
70
71
    /**
72
     * @return Member|MemberExtension
73
     */
74
    public function getMember()
75
    {
76
        return $this->member;
77
    }
78
79
    /**
80
     * @param Member $member
81
     * @return $this
82
     */
83
    public function setMember(Member $member)
84
    {
85
        // Early return if there's no change
86
        if ($this->member && $this->member->ID === $member->ID) {
87
            return $this;
88
        }
89
90
        // If the member has changed we should null out the method that's underway and the state of it
91
        $this->resetMethod();
92
93
        $this->member = $member;
94
95
        return $this;
96
    }
97
98
    /**
99
     * @return string
100
     */
101
    public function getMethod()
102
    {
103
        return $this->method;
104
    }
105
106
    /**
107
     * @param string $method
108
     * @return $this
109
     */
110
    public function setMethod($method)
111
    {
112
        $this->method = $method;
113
114
        return $this;
115
    }
116
117
    public function getState()
118
    {
119
        return $this->state;
120
    }
121
122
    public function setState(array $state)
123
    {
124
        $this->state = $state;
125
126
        return $this;
127
    }
128
129
    /**
130
     * Save this store into the session of the given request
131
     *
132
     * {@inheritdoc}
133
     */
134
    public function save(HTTPRequest $request)
135
    {
136
        $request->getSession()->set(static::SESSION_KEY, $this->build());
137
138
        return $this;
139
    }
140
141
    /**
142
     * Clear any stored values for the given request
143
     *
144
     * {@inheritdoc}
145
     */
146
    public static function clear(HTTPRequest $request)
147
    {
148
        $request->getSession()->clear(static::SESSION_KEY);
149
    }
150
151
    protected function resetMethod()
152
    {
153
        $this->setMethod(null)->setState([]);
154
155
        return $this;
156
    }
157
158
    protected function build()
159
    {
160
        return [
161
            'member' => $this->getMember() ? $this->getMember()->ID : null,
162
            'method' => $this->getMethod(),
163
            'state' => $this->getState(),
164
        ];
165
    }
166
}
167