Issues (102)

src/RequestHandler/BaseHandlerTrait.php (2 issues)

1
<?php
2
3
namespace SilverStripe\MFA\RequestHandler;
4
5
use SilverStripe\Core\Injector\Injector;
6
use SilverStripe\MFA\Service\MethodRegistry;
7
use SilverStripe\MFA\Store\StoreInterface;
8
use SilverStripe\Security\Member;
9
use SilverStripe\SecurityExtensions\Service\SudoModeServiceInterface;
10
use SilverStripe\View\Requirements;
11
12
trait BaseHandlerTrait
13
{
14
    /**
15
     * A "session store" object that helps contain MFA specific session detail
16
     *
17
     * @var StoreInterface
18
     */
19
    protected $store;
20
21
    /**
22
     * Perform the necessary "Requirements" calls to ensure client side scripts are available in the response
23
     *
24
     * @param bool $frontEndRequirements Indicates dependencies usually provided by admin should also be required
25
     */
26
    protected function applyRequirements(bool $frontEndRequirements = true): void
27
    {
28
        // Run through requirements
29
        if ($frontEndRequirements) {
30
            Requirements::javascript('silverstripe/mfa: client/dist/js/injector.js');
31
            Requirements::javascript('silverstripe/admin: client/dist/js/i18n.js');
32
        }
33
34
        // Plugin module requirements
35
        foreach (MethodRegistry::singleton()->getMethods() as $method) {
36
            $method->applyRequirements();
37
        }
38
39
        Requirements::add_i18n_javascript('silverstripe/mfa: client/lang');
0 ignored issues
show
Deprecated Code introduced by
The function SilverStripe\View\Requir...::add_i18n_javascript() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

39
        /** @scrutinizer ignore-deprecated */ Requirements::add_i18n_javascript('silverstripe/mfa: client/lang');
Loading history...
40
41
        $suffix = $frontEndRequirements ? '' : '-cms';
42
        Requirements::javascript("silverstripe/mfa: client/dist/js/bundle{$suffix}.js");
43
        Requirements::css("silverstripe/mfa: client/dist/styles/bundle{$suffix}.css");
44
    }
45
46
    /**
47
     * @return StoreInterface|null
48
     */
49
    protected function getStore(): ?StoreInterface
50
    {
51
        if (!$this->store) {
52
            $spec = Injector::inst()->getServiceSpec(StoreInterface::class);
53
            $class = is_string($spec) ? $spec : $spec['class'];
54
            $this->store = call_user_func([$class, 'load'], $this->getRequest());
0 ignored issues
show
It seems like getRequest() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $this->store = call_user_func([$class, 'load'], $this->/** @scrutinizer ignore-call */ getRequest());
Loading history...
55
        }
56
57
        return $this->store;
58
    }
59
60
    /**
61
     * @param StoreInterface $store
62
     * @return $this
63
     */
64
    public function setStore(StoreInterface $store): self
65
    {
66
        $this->store = $store;
67
        return $this;
68
    }
69
70
    /**
71
     * @param Member $member
72
     * @return StoreInterface
73
     */
74
    protected function createStore(Member $member): StoreInterface
75
    {
76
        $store = Injector::inst()->create(StoreInterface::class, $member);
77
        // Ensure use of the getter returns the new store
78
        $this->store = $store;
79
        return $store;
80
    }
81
82
    /**
83
     * Returns a sudo mode service instance
84
     *
85
     * @return SudoModeServiceInterface
86
     */
87
    protected function getSudoModeService(): SudoModeServiceInterface
88
    {
89
        return Injector::inst()->get(SudoModeServiceInterface::class);
90
    }
91
}
92