Completed
Push — master ( bade18...269a91 )
by Robbie
11s
created

SessionStore::setMember()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 13
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 13
c 0
b 0
f 0
rs 10
cc 3
nc 2
nop 1
1
<?php
2
namespace SilverStripe\MFA;
3
4
use SilverStripe\Control\HTTPRequest;
5
use SilverStripe\MFA\Extensions\MemberExtension;
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 = 'thing';
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
    public static function clear(HTTPRequest $request)
141
    {
142
        $request->getSession()->clear(static::SESSION_KEY);
143
    }
144
145
    protected function resetMethod()
146
    {
147
        $this->setMethod(null)->setState([]);
148
    }
149
150
    protected function build()
151
    {
152
        return [
153
            'member' => $this->getMember() ? $this->getMember()->ID : null,
154
            'method' => $this->getMethod(),
155
            'state' => $this->getState(),
156
        ];
157
    }
158
}
159