Passed
Pull Request — master (#3)
by Garion
01:38
created

SessionStore::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
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\Extensions\MemberExtension;
0 ignored issues
show
Bug introduced by
The type SilverStripe\MFA\Extensions\MemberExtension was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\Security\Member;
7
8
/**
9
 * This class provides an interface to store data in session during an MFA process. This is implemented as a measure to
10
 * prevent bleeding state between individual MFA auth types
11
 *
12
 * @package SilverStripe\MFA
13
 */
14
class SessionStore
15
{
16
    const SESSION_KEY = 'MFASessionStore';
17
18
    /**
19
     * The member that is currently going through the MFA process
20
     *
21
     * @var Member
22
     */
23
    protected $member;
24
25
    /**
26
     * A string representing the current authentication method that is underway
27
     *
28
     * @var string
29
     */
30
    protected $method;
31
32
    /**
33
     * Any state that the current authentication method needs to retain while it is underway
34
     *
35
     * @var array
36
     */
37
    protected $state = [];
38
39
    /**
40
     * Create a store from the given request getting any initial state from the session of the request
41
     *
42
     * @param HTTPRequest $request
43
     * @return SessionStore
44
     */
45
    public static function create(HTTPRequest $request)
46
    {
47
        $state = $request->getSession()->get(static::SESSION_KEY);
48
49
        $new = new static;
50
51
        if ($state) {
52
            $new->setMethod($state['method']);
53
            $new->setState($state['state']);
54
            if ($state['member']) {
55
                $new->setMember(Member::get_by_id($state['member']));
56
            }
57
        }
58
59
        return $new;
60
    }
61
62
    /**
63
     * @return Member|MemberExtension
64
     */
65
    public function getMember()
66
    {
67
        return $this->member;
68
    }
69
70
    /**
71
     * @param Member $member
72
     * @return $this
73
     */
74
    public function setMember(Member $member)
75
    {
76
        // Early return if there's no change
77
        if ($this->member && $this->member->ID === $member->ID) {
78
            return $this;
79
        }
80
81
        // If the member has changed we should null out the method that's underway and the state of it
82
        $this->resetMethod();
83
84
        $this->member = $member;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @return string
91
     */
92
    public function getMethod()
93
    {
94
        return $this->method;
95
    }
96
97
    /**
98
     * @param string $method
99
     * @return $this
100
     */
101
    public function setMethod($method)
102
    {
103
        $this->method = $method;
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return array
110
     */
111
    public function getState()
112
    {
113
        return $this->state;
114
    }
115
116
    /**
117
     * @param array $state
118
     * @return $this
119
     */
120
    public function setState(array $state)
121
    {
122
        $this->state = $state;
123
124
        return $this;
125
    }
126
127
    /**
128
     * Save this store into the session of the given request
129
     *
130
     * @param HTTPRequest $request
131
     * @return $this
132
     */
133
    public function save(HTTPRequest $request)
134
    {
135
        $request->getSession()->set(static::SESSION_KEY, $this->build());
136
137
        return $this;
138
    }
139
140
    /**
141
     * Clear any stored values for the given request
142
     *
143
     * @param HTTPRequest $request
144
     * @return void
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