Completed
Push — authenticator-refactor ( 4aec3f...04850c )
by Simon
06:12
created

CMSAuthenticator::authenticateMember()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 4
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security\MemberAuthenticator;
4
5
6
use SilverStripe\Security\Authenticator;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, SilverStripe\Security\Me...enticator\Authenticator.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use SilverStripe\Security\MemberAuthenticator\Authenticator as BaseAuthenticator;
8
use SilverStripe\Security\Member;
9
10
class CMSAuthenticator extends BaseAuthenticator
11
{
12
13
    public function supportedServices()
14
    {
15
        return Authenticator::CMS_LOGIN;
16
    }
17
18
    /**
19
     * @param array $data
20
     * @param $message
21
     * @param bool $success
22
     * @return Member
23
     */
24
    protected function authenticateMember($data, &$message, &$success, $member = null)
25
    {
26
        // Attempt to identify by temporary ID
27
        if (!empty($data['tempid'])) {
28
            // Find user by tempid, in case they are re-validating an existing session
29
            $member = Member::member_from_tempid($data['tempid']);
30
            if ($member) {
31
                $data['email'] = $member->Email;
32
            }
33
        }
34
35
        return parent::authenticateMember($data, $message, $success, $member);
36
    }
37
38
    public function getLoginHandler($link)
39
    {
40
        return CMSLoginHandler::create($link, $this);
41
    }
42
43
}