Completed
Push — master ( 197387...875417 )
by Damian
11s
created

LDAPMigrateExistingMembersTask::log()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\ActiveDirectory\Tasks;
4
5
use SilverStripe\Dev\BuildTask;
6
use SilverStripe\Core\Convert;
7
use SilverStripe\Control\Director;
8
use SilverStripe\Security\Member;
9
10
/**
11
 * Class LDAPMigrateExistingMembersTask
12
 *
13
 * Migrate existing Member records in SilverStripe into "LDAP Members"
14
 * by matching existing emails with ones that exist in AD.
15
 *
16
 * @package activedirectory
17
 */
18
class LDAPMigrateExistingMembersTask extends BuildTask
19
{
20
    /**
21
     * {@inheritDoc}
22
     * @var string
23
     */
24
    private static $segment = 'LDAPMigrateExistingMembersTask';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $segment is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
25
26
    /**
27
     * @var array
28
     */
29
    private static $dependencies = [
0 ignored issues
show
Unused Code introduced by
The property $dependencies is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
        'ldapService' => '%$SilverStripe\\ActiveDirectory\\Services\\LDAPService'
31
    ];
32
33
    /**
34
     * {@inheritDoc}
35
     * @param HTTPRequest $request
36
     */
37
    public function run($request)
38
    {
39
        $users = $this->ldapService->getUsers(['objectguid', 'mail']);
0 ignored issues
show
Bug introduced by
The property ldapService does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
40
        $start = time();
41
        $count = 0;
42
43
        foreach ($users as $user) {
44
            // Empty mail attribute for the user, nothing we can do. Skip!
45
            if (empty($user['mail'])) {
46
                continue;
47
            }
48
49
            $member = Member::get()->where(
50
                sprintf('"Email" = \'%s\' AND "GUID" IS NULL', Convert::raw2sql($user['mail']))
51
            )->first();
52
53
            if (!($member && $member->exists())) {
54
                continue;
55
            }
56
57
            // Member was found, migrate them by setting the GUID field
58
            $member->GUID = $user['objectguid'];
59
            $member->write();
60
61
            $count++;
62
63
            $this->log(sprintf(
64
                'Migrated Member %s (ID: %s, Email: %s)',
65
                $member->getName(),
66
                $member->ID,
67
                $member->Email
68
            ));
69
        }
70
71
        $end = time() - $start;
72
73
        $this->log(sprintf('Done. Migrated %s Member records. Duration: %s seconds', $count, round($end, 0)));
74
    }
75
76
    /**
77
     * Sends a message, formatted either for the CLI or browser
78
     *
79
     * @param string $message
80
     */
81
    protected function log($message)
82
    {
83
        $message = sprintf('[%s] ', date('Y-m-d H:i:s')) . $message;
84
        echo Director::is_cli() ? ($message . PHP_EOL) : ($message . '<br>');
85
    }
86
}
87