Passed
Pull Request — 4 (#10130)
by Nicolaas
09:49
created

Member::canWithObject()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 13
rs 10
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use IntlDateFormatter;
6
use InvalidArgumentException;
7
use SilverStripe\Admin\LeftAndMain;
0 ignored issues
show
Bug introduced by
The type SilverStripe\Admin\LeftAndMain 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...
8
use SilverStripe\CMS\Controllers\CMSMain;
0 ignored issues
show
Bug introduced by
The type SilverStripe\CMS\Controllers\CMSMain 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...
9
use SilverStripe\Control\Controller;
10
use SilverStripe\Control\Director;
11
use SilverStripe\Control\Email\Email;
12
use SilverStripe\Control\Email\Mailer;
13
use SilverStripe\Control\HTTPRequest;
14
use SilverStripe\Core\Config\Config;
15
use SilverStripe\Core\Convert;
16
use SilverStripe\Core\Injector\Injector;
17
use SilverStripe\Dev\Deprecation;
18
use SilverStripe\Dev\TestMailer;
19
use SilverStripe\Forms\ConfirmedPasswordField;
20
use SilverStripe\Forms\DropdownField;
21
use SilverStripe\Forms\FieldList;
22
use SilverStripe\Forms\HTMLEditor\HTMLEditorConfig;
23
use SilverStripe\Forms\ListboxField;
24
use SilverStripe\Forms\Tab;
25
use SilverStripe\Forms\TabSet;
26
use SilverStripe\i18n\i18n;
27
use SilverStripe\ORM\ArrayList;
28
use SilverStripe\ORM\DataList;
29
use SilverStripe\ORM\DataObject;
30
use SilverStripe\ORM\DB;
31
use SilverStripe\ORM\FieldType\DBDatetime;
32
use SilverStripe\ORM\HasManyList;
33
use SilverStripe\ORM\ManyManyList;
34
use SilverStripe\ORM\Map;
35
use SilverStripe\ORM\SS_List;
36
use SilverStripe\ORM\UnsavedRelationList;
37
use SilverStripe\ORM\ValidationException;
38
use SilverStripe\ORM\ValidationResult;
39
40
/**
41
 * The member class which represents the users of the system
42
 *
43
 * @method HasManyList LoggedPasswords()
44
 * @method HasManyList RememberLoginHashes()
45
 * @property string $FirstName
46
 * @property string $Surname
47
 * @property string $Email
48
 * @property string $Password
49
 * @property string $TempIDHash
50
 * @property string $TempIDExpired
51
 * @property string $AutoLoginHash
52
 * @property string $AutoLoginExpired
53
 * @property string $PasswordEncryption
54
 * @property string $Salt
55
 * @property string $PasswordExpiry
56
 * @property string $LockedOutUntil
57
 * @property string $Locale
58
 * @property int $FailedLoginCount
59
 * @property string $DateFormat
60
 * @property string $TimeFormat
61
 */
62
class Member extends DataObject
63
{
64
    private static $db = [
65
        'FirstName' => 'Varchar',
66
        'Surname' => 'Varchar',
67
        'Email' => 'Varchar(254)', // See RFC 5321, Section 4.5.3.1.3. (256 minus the < and > character)
68
        'TempIDHash' => 'Varchar(160)', // Temporary id used for cms re-authentication
69
        'TempIDExpired' => 'Datetime', // Expiry of temp login
70
        'Password' => 'Varchar(160)',
71
        'AutoLoginHash' => 'Varchar(160)', // Used to auto-login the user on password reset
72
        'AutoLoginExpired' => 'Datetime',
73
        // This is an arbitrary code pointing to a PasswordEncryptor instance,
74
        // not an actual encryption algorithm.
75
        // Warning: Never change this field after its the first password hashing without
76
        // providing a new cleartext password as well.
77
        'PasswordEncryption' => "Varchar(50)",
78
        'Salt' => 'Varchar(50)',
79
        'PasswordExpiry' => 'Date',
80
        'LockedOutUntil' => 'Datetime',
81
        'Locale' => 'Varchar(6)',
82
        // handled in registerFailedLogin(), only used if $lock_out_after_incorrect_logins is set
83
        'FailedLoginCount' => 'Int',
84
    ];
85
86
    private static $belongs_many_many = [
87
        'Groups' => Group::class,
88
    ];
89
90
    private static $has_many = [
91
        'LoggedPasswords' => MemberPassword::class,
92
        'RememberLoginHashes' => RememberLoginHash::class,
93
    ];
94
95
    private static $table_name = "Member";
96
97
    private static $default_sort = '"Surname", "FirstName"';
98
99
    private static $indexes = [
100
        'Email' => true,
101
        //Removed due to duplicate null values causing MSSQL problems
102
        //'AutoLoginHash' => Array('type'=>'unique', 'value'=>'AutoLoginHash', 'ignoreNulls'=>true)
103
    ];
104
105
    /**
106
     * @config
107
     * @var boolean
108
     */
109
    private static $notify_password_change = false;
110
111
    /**
112
     * All searchable database columns
113
     * in this object, currently queried
114
     * with a "column LIKE '%keywords%'
115
     * statement.
116
     *
117
     * @var array
118
     * @todo Generic implementation of $searchable_fields on DataObject,
119
     * with definition for different searching algorithms
120
     * (LIKE, FULLTEXT) and default FormFields to construct a searchform.
121
     */
122
    private static $searchable_fields = [
123
        'FirstName',
124
        'Surname',
125
        'Email',
126
    ];
127
128
    /**
129
     * @config
130
     * @var array
131
     */
132
    private static $summary_fields = [
133
        'FirstName',
134
        'Surname',
135
        'Email',
136
    ];
137
138
    /**
139
     * @config
140
     * @var array
141
     */
142
    private static $casting = [
143
        'Name' => 'Varchar',
144
    ];
145
146
    /**
147
     * Internal-use only fields
148
     *
149
     * @config
150
     * @var array
151
     */
152
    private static $hidden_fields = [
153
        'AutoLoginHash',
154
        'AutoLoginExpired',
155
        'PasswordEncryption',
156
        'PasswordExpiry',
157
        'LockedOutUntil',
158
        'TempIDHash',
159
        'TempIDExpired',
160
        'Salt',
161
    ];
162
163
    /**
164
     * @config
165
     * @var array See {@link set_title_columns()}
166
     */
167
    private static $title_format = null;
168
169
    /**
170
     * The unique field used to identify this member.
171
     * By default, it's "Email", but another common
172
     * field could be Username.
173
     *
174
     * @config
175
     * @var string
176
     * @skipUpgrade
177
     */
178
    private static $unique_identifier_field = 'Email';
179
180
    /**
181
     * @config
182
     * The number of days that a password should be valid for.
183
     * By default, this is null, which means that passwords never expire
184
     */
185
    private static $password_expiry_days = null;
186
187
    /**
188
     * @config
189
     * @var bool enable or disable logging of previously used passwords. See {@link onAfterWrite}
190
     */
191
    private static $password_logging_enabled = true;
192
193
    /**
194
     * @config
195
     * @var Int Number of incorrect logins after which
196
     * the user is blocked from further attempts for the timespan
197
     * defined in {@link $lock_out_delay_mins}.
198
     */
199
    private static $lock_out_after_incorrect_logins = 10;
200
201
    /**
202
     * @config
203
     * @var integer Minutes of enforced lockout after incorrect password attempts.
204
     * Only applies if {@link $lock_out_after_incorrect_logins} greater than 0.
205
     */
206
    private static $lock_out_delay_mins = 15;
207
208
    /**
209
     * @config
210
     * @var String If this is set, then a session cookie with the given name will be set on log-in,
211
     * and cleared on logout.
212
     */
213
    private static $login_marker_cookie = null;
214
215
    /**
216
     * Indicates that when a {@link Member} logs in, Member:session_regenerate_id()
217
     * should be called as a security precaution.
218
     *
219
     * This doesn't always work, especially if you're trying to set session cookies
220
     * across an entire site using the domain parameter to session_set_cookie_params()
221
     *
222
     * @config
223
     * @var boolean
224
     */
225
    private static $session_regenerate_id = true;
226
227
228
    /**
229
     * Default lifetime of temporary ids.
230
     *
231
     * This is the period within which a user can be re-authenticated within the CMS by entering only their password
232
     * and without losing their workspace.
233
     *
234
     * Any session expiration outside of this time will require them to login from the frontend using their full
235
     * username and password.
236
     *
237
     * Defaults to 72 hours. Set to zero to disable expiration.
238
     *
239
     * @config
240
     * @var int Lifetime in seconds
241
     */
242
    private static $temp_id_lifetime = 259200;
243
244
    /**
245
     * Default lifetime of auto login token.
246
     *
247
     * This is the maximum allowed period between a user requesting a password reset link and using it to reset
248
     * their password.
249
     *
250
     * Defaults to 2 days.
251
     *
252
     * @config
253
     * @var int Lifetime in seconds
254
     */
255
    private static $auto_login_token_lifetime = 172800;
256
257
    /**
258
     * Used to track whether {@link Member::changePassword} has made changed that need to be written. Used to prevent
259
     * the write from calling changePassword again.
260
     *
261
     * @var bool
262
     */
263
    protected $passwordChangesToWrite = false;
264
265
    /**
266
     * Ensure the locale is set to something sensible by default.
267
     */
268
    public function populateDefaults()
269
    {
270
        parent::populateDefaults();
271
        $this->Locale = i18n::config()->get('default_locale');
272
    }
273
274
    public function requireDefaultRecords()
275
    {
276
        parent::requireDefaultRecords();
277
        // Default groups should've been built by Group->requireDefaultRecords() already
278
        $service = DefaultAdminService::singleton();
279
        $service->findOrCreateDefaultAdmin();
280
    }
281
282
    /**
283
     * Get the default admin record if it exists, or creates it otherwise if enabled
284
     *
285
     * @deprecated 4.0.0:5.0.0 Use DefaultAdminService::findOrCreateDefaultAdmin() instead
286
     * @return Member
287
     */
288
    public static function default_admin()
289
    {
290
        Deprecation::notice('5.0', 'Use DefaultAdminService::findOrCreateDefaultAdmin() instead');
291
        return DefaultAdminService::singleton()->findOrCreateDefaultAdmin();
292
    }
293
294
    /**
295
     * Check if the passed password matches the stored one (if the member is not locked out).
296
     *
297
     * @deprecated 4.0.0:5.0.0 Use Authenticator::checkPassword() instead
298
     *
299
     * @param  string $password
300
     * @return ValidationResult
301
     */
302
    public function checkPassword($password)
303
    {
304
        Deprecation::notice('5.0', 'Use Authenticator::checkPassword() instead');
305
306
        // With a valid user and password, check the password is correct
307
        $result = ValidationResult::create();
308
        $authenticators = Security::singleton()->getApplicableAuthenticators(Authenticator::CHECK_PASSWORD);
309
        foreach ($authenticators as $authenticator) {
310
            $authenticator->checkPassword($this, $password, $result);
311
            if (!$result->isValid()) {
312
                break;
313
            }
314
        }
315
        return $result;
316
    }
317
318
    /**
319
     * Check if this user is the currently configured default admin
320
     *
321
     * @return bool
322
     */
323
    public function isDefaultAdmin()
324
    {
325
        return DefaultAdminService::isDefaultAdmin($this->Email);
326
    }
327
328
    /**
329
     * Check if this user can login
330
     *
331
     * @return bool
332
     */
333
    public function canLogin()
334
    {
335
        return $this->validateCanLogin()->isValid();
336
    }
337
338
    /**
339
     * Returns a valid {@link ValidationResult} if this member can currently log in, or an invalid
340
     * one with error messages to display if the member is locked out.
341
     *
342
     * You can hook into this with a "canLogIn" method on an attached extension.
343
     *
344
     * @param ValidationResult $result Optional result to add errors to
345
     * @return ValidationResult
346
     */
347
    public function validateCanLogin(ValidationResult &$result = null)
348
    {
349
        $result = $result ?: ValidationResult::create();
350
        if ($this->isLockedOut()) {
351
            $result->addError(
352
                _t(
353
                    __CLASS__ . '.ERRORLOCKEDOUT2',
354
                    'Your account has been temporarily disabled because of too many failed attempts at ' . 'logging in. Please try again in {count} minutes.',
355
                    null,
356
                    ['count' => static::config()->get('lock_out_delay_mins')]
357
                )
358
            );
359
        }
360
361
        $this->extend('canLogIn', $result);
362
363
        return $result;
364
    }
365
366
    /**
367
     * Returns true if this user is locked out
368
     *
369
     * @skipUpgrade
370
     * @return bool
371
     */
372
    public function isLockedOut()
373
    {
374
        /** @var DBDatetime $lockedOutUntilObj */
375
        $lockedOutUntilObj = $this->dbObject('LockedOutUntil');
376
        if ($lockedOutUntilObj->InFuture()) {
377
            return true;
378
        }
379
380
        $maxAttempts = $this->config()->get('lock_out_after_incorrect_logins');
381
        if ($maxAttempts <= 0) {
382
            return false;
383
        }
384
385
        $attempts = LoginAttempt::getByEmail($this->Email)
386
            ->sort('Created', 'DESC')
387
            ->limit($maxAttempts);
388
389
        if ($attempts->count() < $maxAttempts) {
390
            return false;
391
        }
392
393
        foreach ($attempts as $attempt) {
394
            if ($attempt->Status === 'Success') {
395
                return false;
396
            }
397
        }
398
399
        // Calculate effective LockedOutUntil
400
        /** @var DBDatetime $firstFailureDate */
401
        $firstFailureDate = $attempts->first()->dbObject('Created');
402
        $maxAgeSeconds = $this->config()->get('lock_out_delay_mins') * 60;
403
        $lockedOutUntil = $firstFailureDate->getTimestamp() + $maxAgeSeconds;
404
        $now = DBDatetime::now()->getTimestamp();
405
        if ($now < $lockedOutUntil) {
406
            return true;
407
        }
408
409
        return false;
410
    }
411
412
    /**
413
     * Set a {@link PasswordValidator} object to use to validate member's passwords.
414
     *
415
     * @param PasswordValidator $validator
416
     */
417
    public static function set_password_validator(PasswordValidator $validator = null)
418
    {
419
        // Override existing config
420
        Config::modify()->remove(Injector::class, PasswordValidator::class);
421
        if ($validator) {
422
            Injector::inst()->registerService($validator, PasswordValidator::class);
423
        } else {
424
            Injector::inst()->unregisterNamedObject(PasswordValidator::class);
425
        }
426
    }
427
428
    /**
429
     * Returns the default {@link PasswordValidator}
430
     *
431
     * @return PasswordValidator
432
     */
433
    public static function password_validator()
434
    {
435
        if (Injector::inst()->has(PasswordValidator::class)) {
436
            return Injector::inst()->get(PasswordValidator::class);
437
        }
438
        return null;
439
    }
440
441
    public function isPasswordExpired()
442
    {
443
        if (!$this->PasswordExpiry) {
444
            return false;
445
        }
446
447
        return strtotime(date('Y-m-d')) >= strtotime($this->PasswordExpiry);
448
    }
449
450
    /**
451
     * @deprecated 5.0.0 Use Security::setCurrentUser() or IdentityStore::logIn()
452
     *
453
     */
454
    public function logIn()
455
    {
456
        Deprecation::notice(
457
            '5.0.0',
458
            'This method is deprecated and only logs in for the current request. Please use Security::setCurrentUser($user) or an IdentityStore'
459
        );
460
        Security::setCurrentUser($this);
461
    }
462
463
    /**
464
     * Called before a member is logged in via session/cookie/etc
465
     */
466
    public function beforeMemberLoggedIn()
467
    {
468
        // @todo Move to middleware on the AuthenticationMiddleware IdentityStore
469
        $this->extend('beforeMemberLoggedIn');
470
    }
471
472
    /**
473
     * Called after a member is logged in via session/cookie/etc
474
     */
475
    public function afterMemberLoggedIn()
476
    {
477
        // Clear the incorrect log-in count
478
        $this->registerSuccessfulLogin();
479
480
        $this->LockedOutUntil = null;
481
482
        $this->regenerateTempID();
483
484
        $this->write();
485
486
        // Audit logging hook
487
        $this->extend('afterMemberLoggedIn');
488
    }
489
490
    /**
491
     * Trigger regeneration of TempID.
492
     *
493
     * This should be performed any time the user presents their normal identification (normally Email)
494
     * and is successfully authenticated.
495
     */
496
    public function regenerateTempID()
497
    {
498
        $generator = new RandomGenerator();
499
        $lifetime = self::config()->get('temp_id_lifetime');
500
        $this->TempIDHash = $generator->randomToken('sha1');
501
        $this->TempIDExpired = $lifetime
502
            ? date('Y-m-d H:i:s', strtotime(DBDatetime::now()->getValue()) + $lifetime)
503
            : null;
504
        $this->write();
505
    }
506
507
    /**
508
     * Check if the member ID logged in session actually
509
     * has a database record of the same ID. If there is
510
     * no logged in user, FALSE is returned anyway.
511
     *
512
     * @deprecated Not needed anymore, as it returns Security::getCurrentUser();
513
     *
514
     * @return boolean TRUE record found FALSE no record found
515
     */
516
    public static function logged_in_session_exists()
517
    {
518
        Deprecation::notice(
519
            '5.0.0',
520
            'This method is deprecated and now does not add value. Please use Security::getCurrentUser()'
521
        );
522
523
        $member = Security::getCurrentUser();
524
        if ($member && $member->exists()) {
525
            return true;
526
        }
527
528
        return false;
529
    }
530
531
    /**
532
     * @deprecated Use Security::setCurrentUser(null) or an IdentityStore
533
     * Logs this member out.
534
     */
535
    public function logOut()
536
    {
537
        Deprecation::notice(
538
            '5.0.0',
539
            'This method is deprecated and now does not persist. Please use Security::setCurrentUser(null) or an IdentityStore'
540
        );
541
542
        Injector::inst()->get(IdentityStore::class)->logOut(Controller::curr()->getRequest());
543
    }
544
545
    /**
546
     * Audit logging hook, called before a member is logged out
547
     *
548
     * @param HTTPRequest|null $request
549
     */
550
    public function beforeMemberLoggedOut(HTTPRequest $request = null)
551
    {
552
        $this->extend('beforeMemberLoggedOut', $request);
553
    }
554
555
    /**
556
     * Audit logging hook, called after a member is logged out
557
     *
558
     * @param HTTPRequest|null $request
559
     */
560
    public function afterMemberLoggedOut(HTTPRequest $request = null)
561
    {
562
        $this->extend('afterMemberLoggedOut', $request);
563
    }
564
565
    /**
566
     * Utility for generating secure password hashes for this member.
567
     *
568
     * @param string $string
569
     * @return string
570
     * @throws PasswordEncryptor_NotFoundException
571
     */
572
    public function encryptWithUserSettings($string)
573
    {
574
        if (!$string) {
575
            return null;
576
        }
577
578
        // If the algorithm or salt is not available, it means we are operating
579
        // on legacy account with unhashed password. Do not hash the string.
580
        if (!$this->PasswordEncryption) {
581
            return $string;
582
        }
583
584
        // We assume we have PasswordEncryption and Salt available here.
585
        $e = PasswordEncryptor::create_for_algorithm($this->PasswordEncryption);
586
587
        return $e->encrypt($string, $this->Salt);
588
    }
589
590
    /**
591
     * Generate an auto login token which can be used to reset the password,
592
     * at the same time hashing it and storing in the database.
593
     *
594
     * @param int|null $lifetime DEPRECATED: The lifetime of the auto login hash in days. Overrides
595
     *                           the Member.auto_login_token_lifetime config value
596
     * @return string Token that should be passed to the client (but NOT persisted).
597
     */
598
    public function generateAutologinTokenAndStoreHash($lifetime = null)
599
    {
600
        if ($lifetime !== null) {
601
            Deprecation::notice(
602
                '5.0',
603
                'Passing a $lifetime to Member::generateAutologinTokenAndStoreHash() is deprecated,
604
                    use the Member.auto_login_token_lifetime config setting instead',
605
                Deprecation::SCOPE_GLOBAL
606
            );
607
            $lifetime = (86400 * $lifetime); // Method argument is days, convert to seconds
608
        } else {
609
            $lifetime = $this->config()->auto_login_token_lifetime;
0 ignored issues
show
Bug Best Practice introduced by
The property auto_login_token_lifetime does not exist on SilverStripe\Core\Config\Config_ForClass. Since you implemented __get, consider adding a @property annotation.
Loading history...
610
        }
611
612
        do {
613
            $generator = new RandomGenerator();
614
            $token = $generator->randomToken();
615
            $hash = $this->encryptWithUserSettings($token);
616
        } while (DataObject::get_one(Member::class, [
617
            '"Member"."AutoLoginHash"' => $hash
618
        ]));
619
620
        $this->AutoLoginHash = $hash;
621
        $this->AutoLoginExpired = date('Y-m-d H:i:s', time() + $lifetime);
622
623
        $this->write();
624
625
        return $token;
626
    }
627
628
    /**
629
     * Check the token against the member.
630
     *
631
     * @param string $autologinToken
632
     *
633
     * @returns bool Is token valid?
634
     */
635
    public function validateAutoLoginToken($autologinToken)
636
    {
637
        $hash = $this->encryptWithUserSettings($autologinToken);
638
        $member = self::member_from_autologinhash($hash, false);
639
640
        return (bool)$member;
641
    }
642
643
    /**
644
     * Return the member for the auto login hash
645
     *
646
     * @param string $hash The hash key
647
     * @param bool $login Should the member be logged in?
648
     *
649
     * @return Member the matching member, if valid
650
     * @return Member
651
     */
652
    public static function member_from_autologinhash($hash, $login = false)
653
    {
654
        /** @var Member $member */
655
        $member = static::get()->filter([
656
            'AutoLoginHash' => $hash,
657
            'AutoLoginExpired:GreaterThan' => DBDatetime::now()->getValue(),
658
        ])->first();
659
660
        if ($login && $member) {
661
            Injector::inst()->get(IdentityStore::class)->logIn($member);
662
        }
663
664
        return $member;
665
    }
666
667
    /**
668
     * Find a member record with the given TempIDHash value
669
     *
670
     * @param string $tempid
671
     * @return Member
672
     */
673
    public static function member_from_tempid($tempid)
674
    {
675
        $members = static::get()
676
            ->filter('TempIDHash', $tempid);
677
678
        // Exclude expired
679
        if (static::config()->get('temp_id_lifetime')) {
680
            /** @var DataList|Member[] $members */
681
            $members = $members->filter('TempIDExpired:GreaterThan', DBDatetime::now()->getValue());
682
        }
683
684
        return $members->first();
685
    }
686
687
    /**
688
     * Returns the fields for the member form - used in the registration/profile module.
689
     * It should return fields that are editable by the admin and the logged-in user.
690
     *
691
     * @todo possibly move this to an extension
692
     *
693
     * @return FieldList Returns a {@link FieldList} containing the fields for
694
     *                   the member form.
695
     */
696
    public function getMemberFormFields()
697
    {
698
        $fields = parent::getFrontEndFields();
699
700
        $fields->replaceField('Password', $this->getMemberPasswordField());
701
702
        $fields->replaceField('Locale', new DropdownField(
703
            'Locale',
704
            $this->fieldLabel('Locale'),
705
            i18n::getSources()->getKnownLocales()
706
        ));
707
708
        $fields->removeByName(static::config()->get('hidden_fields'));
709
        $fields->removeByName('FailedLoginCount');
710
711
712
        $this->extend('updateMemberFormFields', $fields);
713
714
        return $fields;
715
    }
716
717
    /**
718
     * Builds "Change / Create Password" field for this member
719
     *
720
     * @return ConfirmedPasswordField
721
     */
722
    public function getMemberPasswordField()
723
    {
724
        $editingPassword = $this->isInDB();
725
        $label = $editingPassword
726
            ? _t(__CLASS__ . '.EDIT_PASSWORD', 'New Password')
727
            : $this->fieldLabel('Password');
728
        /** @var ConfirmedPasswordField $password */
729
        $password = ConfirmedPasswordField::create(
730
            'Password',
731
            $label,
732
            null,
733
            null,
734
            $editingPassword
735
        );
736
737
        // If editing own password, require confirmation of existing
738
        if ($editingPassword && $this->ID == Security::getCurrentUser()->ID) {
739
            $password->setRequireExistingPassword(true);
740
        }
741
742
        $password->setCanBeEmpty(true);
743
        $this->extend('updateMemberPasswordField', $password);
744
745
        return $password;
746
    }
747
748
749
    /**
750
     * Returns the {@link RequiredFields} instance for the Member object. This
751
     * Validator is used when saving a {@link CMSProfileController} or added to
752
     * any form responsible for saving a users data.
753
     *
754
     * To customize the required fields, add a {@link DataExtension} to member
755
     * calling the `updateValidator()` method.
756
     *
757
     * @return Member_Validator
758
     */
759
    public function getValidator()
760
    {
761
        $validator = Member_Validator::create();
762
        $validator->setForMember($this);
763
        $this->extend('updateValidator', $validator);
764
765
        return $validator;
766
    }
767
768
769
    /**
770
     * Returns the current logged in user
771
     *
772
     * @deprecated 5.0.0 use Security::getCurrentUser()
773
     *
774
     * @return Member
775
     */
776
    public static function currentUser()
777
    {
778
        Deprecation::notice(
779
            '5.0.0',
780
            'This method is deprecated. Please use Security::getCurrentUser() or an IdentityStore'
781
        );
782
783
        return Security::getCurrentUser();
784
    }
785
786
    /**
787
     * Temporarily act as the specified user, limited to a $callback, but
788
     * without logging in as that user.
789
     *
790
     * E.g.
791
     * <code>
792
     * Member::actAs(Security::findAnAdministrator(), function() {
793
     *     $record->write();
794
     * });
795
     * </code>
796
     *
797
     * @param Member|null|int $member Member or member ID to log in as.
798
     * Set to null or 0 to act as a logged out user.
799
     * @param callable $callback
800
     * @return mixed Result of $callback
801
     */
802
    public static function actAs($member, $callback)
803
    {
804
        $previousUser = Security::getCurrentUser();
805
806
        // Transform ID to member
807
        if (is_numeric($member)) {
808
            $member = DataObject::get_by_id(Member::class, $member);
809
        }
810
        Security::setCurrentUser($member);
811
812
        try {
813
            return $callback();
814
        } finally {
815
            Security::setCurrentUser($previousUser);
816
        }
817
    }
818
819
    /**
820
     * Get the ID of the current logged in user
821
     *
822
     * @deprecated 5.0.0 use Security::getCurrentUser()
823
     *
824
     * @return int Returns the ID of the current logged in user or 0.
825
     */
826
    public static function currentUserID()
827
    {
828
        Deprecation::notice(
829
            '5.0.0',
830
            'This method is deprecated. Please use Security::getCurrentUser() or an IdentityStore'
831
        );
832
833
        $member = Security::getCurrentUser();
834
        if ($member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
835
            return $member->ID;
836
        }
837
        return 0;
838
    }
839
840
    /**
841
     * Generate a random password, with randomiser to kick in if there's no words file on the
842
     * filesystem.
843
     *
844
     * @return string Returns a random password.
845
     */
846
    public static function create_new_password()
847
    {
848
        $words = Security::config()->uninherited('word_list');
849
850
        if ($words && file_exists($words)) {
851
            $words = file($words);
852
853
            list($usec, $sec) = explode(' ', microtime());
854
            mt_srand($sec + ((float)$usec * 100000));
0 ignored issues
show
Bug introduced by
$sec + (double)$usec * 100000 of type double is incompatible with the type integer expected by parameter $seed of mt_srand(). ( Ignorable by Annotation )

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

854
            mt_srand(/** @scrutinizer ignore-type */ $sec + ((float)$usec * 100000));
Loading history...
855
856
            $word = trim($words[random_int(0, count($words) - 1)]);
857
            $number = random_int(10, 999);
858
859
            return $word . $number;
860
        } else {
861
            $random = mt_rand();
862
            $string = md5($random);
863
            $output = substr($string, 0, 8);
864
865
            return $output;
866
        }
867
    }
868
869
    /**
870
     * Event handler called before writing to the database.
871
     */
872
    public function onBeforeWrite()
873
    {
874
        // Remove any line-break or space characters accidentally added during a copy-paste operation
875
        if ($this->Email) {
876
            $this->Email = trim($this->Email);
877
        }
878
879
        // If a member with the same "unique identifier" already exists with a different ID, don't allow merging.
880
        // Note: This does not a full replacement for safeguards in the controller layer (e.g. in a registration form),
881
        // but rather a last line of defense against data inconsistencies.
882
        $identifierField = Member::config()->get('unique_identifier_field');
883
        if ($this->$identifierField) {
884
            // Note: Same logic as Member_Validator class
885
            $filter = [
886
                "\"Member\".\"$identifierField\"" => $this->$identifierField
887
            ];
888
            if ($this->ID) {
889
                $filter[] = ['"Member"."ID" <> ?' => $this->ID];
890
            }
891
            $existingRecord = DataObject::get_one(Member::class, $filter);
892
893
            if ($existingRecord) {
894
                throw new ValidationException(_t(
895
                    __CLASS__ . '.ValidationIdentifierFailed',
896
                    'Can\'t overwrite existing member #{id} with identical identifier ({name} = {value}))',
897
                    'Values in brackets show "fieldname = value", usually denoting an existing email address',
898
                    [
899
                        'id' => $existingRecord->ID,
900
                        'name' => $identifierField,
901
                        'value' => $this->$identifierField
902
                    ]
903
                ));
904
            }
905
        }
906
907
        // We don't send emails out on dev/tests sites to prevent accidentally spamming users.
908
        // However, if TestMailer is in use this isn't a risk.
909
        // @todo some developers use external tools, so emailing might be a good idea anyway
910
        if ((Director::isLive() || Injector::inst()->get(Mailer::class) instanceof TestMailer)
911
            && $this->isChanged('Password')
912
            && $this->record['Password']
913
            && $this->Email
914
            && static::config()->get('notify_password_change')
915
            && $this->isInDB()
916
        ) {
917
            Email::create()
918
                ->setHTMLTemplate('SilverStripe\\Control\\Email\\ChangePasswordEmail')
919
                ->setData($this)
920
                ->setTo($this->Email)
921
                ->setSubject(_t(
922
                    __CLASS__ . '.SUBJECTPASSWORDCHANGED',
923
                    "Your password has been changed",
924
                    'Email subject'
925
                ))
926
                ->send();
927
        }
928
929
        // The test on $this->ID is used for when records are initially created. Note that this only works with
930
        // cleartext passwords, as we can't rehash existing passwords.
931
        if (!$this->ID || $this->isChanged('Password')) {
932
            $this->encryptPassword();
933
        }
934
935
        // save locale
936
        if (!$this->Locale) {
937
            $this->Locale = i18n::config()->get('default_locale');
938
        }
939
940
        parent::onBeforeWrite();
941
    }
942
943
    public function onAfterWrite()
944
    {
945
        parent::onAfterWrite();
946
947
        Permission::reset();
948
949
        if ($this->isChanged('Password') && static::config()->get('password_logging_enabled')) {
950
            MemberPassword::log($this);
951
        }
952
    }
953
954
    public function onAfterDelete()
955
    {
956
        parent::onAfterDelete();
957
958
        // prevent orphaned records remaining in the DB
959
        $this->deletePasswordLogs();
960
        $this->Groups()->removeAll();
961
    }
962
963
    /**
964
     * Delete the MemberPassword objects that are associated to this user
965
     *
966
     * @return $this
967
     */
968
    protected function deletePasswordLogs()
969
    {
970
        foreach ($this->LoggedPasswords() as $password) {
971
            $password->delete();
972
            $password->destroy();
973
        }
974
975
        return $this;
976
    }
977
978
    /**
979
     * Filter out admin groups to avoid privilege escalation,
980
     * If any admin groups are requested, deny the whole save operation.
981
     *
982
     * @param array $ids Database IDs of Group records
983
     * @return bool True if the change can be accepted
984
     */
985
    public function onChangeGroups($ids)
986
    {
987
        // Ensure none of these match disallowed list
988
        $disallowedGroupIDs = $this->disallowedGroups();
989
        return count(array_intersect($ids, $disallowedGroupIDs)) == 0;
990
    }
991
992
    /**
993
     * List of group IDs this user is disallowed from
994
     *
995
     * @return int[] List of group IDs
996
     */
997
    protected function disallowedGroups()
998
    {
999
        // unless the current user is an admin already OR the logged in user is an admin
1000
        if (Permission::check('ADMIN') || Permission::checkMember($this, 'ADMIN')) {
1001
            return [];
1002
        }
1003
1004
        // Non-admins may not belong to admin groups
1005
        return Permission::get_groups_by_permission('ADMIN')->column('ID');
1006
    }
1007
1008
1009
    /**
1010
     * Check if the member is in one of the given groups.
1011
     *
1012
     * @param array|SS_List $groups Collection of {@link Group} DataObjects to check
1013
     * @param boolean $strict Only determine direct group membership if set to true (Default: false)
1014
     * @return bool Returns TRUE if the member is in one of the given groups, otherwise FALSE.
1015
     */
1016
    public function inGroups($groups, $strict = false)
1017
    {
1018
        if ($groups) {
1019
            foreach ($groups as $group) {
1020
                if ($this->inGroup($group, $strict)) {
1021
                    return true;
1022
                }
1023
            }
1024
        }
1025
1026
        return false;
1027
    }
1028
1029
1030
    /**
1031
     * Check if the member is in the given group or any parent groups.
1032
     *
1033
     * @param int|Group|string $group Group instance, Group Code or ID
1034
     * @param boolean $strict Only determine direct group membership if set to TRUE (Default: FALSE)
1035
     * @return bool Returns TRUE if the member is in the given group, otherwise FALSE.
1036
     */
1037
    public function inGroup($group, $strict = false)
1038
    {
1039
        if (is_numeric($group)) {
1040
            $groupCheckObj = DataObject::get_by_id(Group::class, $group);
0 ignored issues
show
Bug introduced by
It seems like $group can also be of type string; however, parameter $idOrCache of SilverStripe\ORM\DataObject::get_by_id() does only seem to accept boolean|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

1040
            $groupCheckObj = DataObject::get_by_id(Group::class, /** @scrutinizer ignore-type */ $group);
Loading history...
1041
        } elseif (is_string($group)) {
1042
            $groupCheckObj = DataObject::get_one(Group::class, [
1043
                '"Group"."Code"' => $group
1044
            ]);
1045
        } elseif ($group instanceof Group) {
0 ignored issues
show
introduced by
$group is always a sub-type of SilverStripe\Security\Group.
Loading history...
1046
            $groupCheckObj = $group;
1047
        } else {
1048
            throw new InvalidArgumentException('Member::inGroup(): Wrong format for $group parameter');
1049
        }
1050
1051
        if (!$groupCheckObj) {
1052
            return false;
1053
        }
1054
1055
        $groupCandidateObjs = ($strict) ? $this->getManyManyComponents("Groups") : $this->Groups();
1056
        if ($groupCandidateObjs) {
1057
            foreach ($groupCandidateObjs as $groupCandidateObj) {
1058
                if ($groupCandidateObj->ID == $groupCheckObj->ID) {
1059
                    return true;
1060
                }
1061
            }
1062
        }
1063
1064
        return false;
1065
    }
1066
1067
    /**
1068
     * Adds the member to a group. This will create the group if the given
1069
     * group code does not return a valid group object.
1070
     *
1071
     * @param string $groupcode
1072
     * @param string $title Title of the group
1073
     */
1074
    public function addToGroupByCode($groupcode, $title = "")
1075
    {
1076
        $group = DataObject::get_one(Group::class, [
1077
            '"Group"."Code"' => $groupcode
1078
        ]);
1079
1080
        if ($group) {
1081
            $this->Groups()->add($group);
1082
        } else {
1083
            if (!$title) {
1084
                $title = $groupcode;
1085
            }
1086
1087
            $group = new Group();
1088
            $group->Code = $groupcode;
1089
            $group->Title = $title;
1090
            $group->write();
1091
1092
            $this->Groups()->add($group);
1093
        }
1094
    }
1095
1096
    /**
1097
     * Removes a member from a group.
1098
     *
1099
     * @param string $groupcode
1100
     */
1101
    public function removeFromGroupByCode($groupcode)
1102
    {
1103
        $group = Group::get()->filter(['Code' => $groupcode])->first();
1104
1105
        if ($group) {
1106
            $this->Groups()->remove($group);
1107
        }
1108
    }
1109
1110
    /**
1111
     * @param array $columns Column names on the Member record to show in {@link getTitle()}.
1112
     * @param string $sep Separator
1113
     */
1114
    public static function set_title_columns($columns, $sep = ' ')
1115
    {
1116
        Deprecation::notice('5.0', 'Use Member.title_format config instead');
1117
        if (!is_array($columns)) {
0 ignored issues
show
introduced by
The condition is_array($columns) is always true.
Loading history...
1118
            $columns = [$columns];
1119
        }
1120
        self::config()->set(
1121
            'title_format',
1122
            [
1123
                'columns' => $columns,
1124
                'sep' => $sep
1125
            ]
1126
        );
1127
    }
1128
1129
    //------------------- HELPER METHODS -----------------------------------//
1130
1131
    /**
1132
     * Simple proxy method to get the Surname property of the member
1133
     *
1134
     * @return string
1135
     */
1136
    public function getLastName()
1137
    {
1138
        return $this->Surname;
1139
    }
1140
1141
    /**
1142
     * Get the complete name of the member, by default in the format "<Surname>, <FirstName>".
1143
     * Falls back to showing either field on its own.
1144
     *
1145
     * You can overload this getter with {@link set_title_format()}
1146
     * and {@link set_title_sql()}.
1147
     *
1148
     * @return string Returns the first- and surname of the member. If the ID
1149
     *  of the member is equal 0, only the surname is returned.
1150
     */
1151
    public function getTitle()
1152
    {
1153
        $format = static::config()->get('title_format');
1154
        if ($format) {
1155
            $values = [];
1156
            foreach ($format['columns'] as $col) {
1157
                $values[] = $this->getField($col);
1158
            }
1159
1160
            return implode($format['sep'], $values);
1161
        }
1162
        if ($this->getField('ID') === 0) {
1163
            return $this->getField('Surname');
1164
        } else {
1165
            if ($this->getField('Surname') && $this->getField('FirstName')) {
1166
                return $this->getField('Surname') . ', ' . $this->getField('FirstName');
1167
            } elseif ($this->getField('Surname')) {
1168
                return $this->getField('Surname');
1169
            } elseif ($this->getField('FirstName')) {
1170
                return $this->getField('FirstName');
1171
            } else {
1172
                return null;
1173
            }
1174
        }
1175
    }
1176
1177
    /**
1178
     * Return a SQL CONCAT() fragment suitable for a SELECT statement.
1179
     * Useful for custom queries which assume a certain member title format.
1180
     *
1181
     * @return String SQL
1182
     */
1183
    public static function get_title_sql()
1184
    {
1185
1186
        // Get title_format with fallback to default
1187
        $format = static::config()->get('title_format');
1188
        if (!$format) {
1189
            $format = [
1190
                'columns' => ['Surname', 'FirstName'],
1191
                'sep' => ' ',
1192
            ];
1193
        }
1194
1195
        $columnsWithTablename = [];
1196
        foreach ($format['columns'] as $column) {
1197
            $columnsWithTablename[] = static::getSchema()->sqlColumnForField(__CLASS__, $column);
1198
        }
1199
1200
        $sepSQL = Convert::raw2sql($format['sep'], true);
1201
        $op = DB::get_conn()->concatOperator();
1202
        return "(" . join(" $op $sepSQL $op ", $columnsWithTablename) . ")";
1203
    }
1204
1205
1206
    /**
1207
     * Get the complete name of the member
1208
     *
1209
     * @return string Returns the first- and surname of the member.
1210
     */
1211
    public function getName()
1212
    {
1213
        return ($this->Surname) ? trim($this->FirstName . ' ' . $this->Surname) : $this->FirstName;
1214
    }
1215
1216
1217
    /**
1218
     * Set first- and surname
1219
     *
1220
     * This method assumes that the last part of the name is the surname, e.g.
1221
     * <i>A B C</i> will result in firstname <i>A B</i> and surname <i>C</i>
1222
     *
1223
     * @param string $name The name
1224
     */
1225
    public function setName($name)
1226
    {
1227
        $nameParts = explode(' ', $name);
1228
        $this->Surname = array_pop($nameParts);
1229
        $this->FirstName = join(' ', $nameParts);
1230
    }
1231
1232
1233
    /**
1234
     * Alias for {@link setName}
1235
     *
1236
     * @param string $name The name
1237
     * @see setName()
1238
     */
1239
    public function splitName($name)
1240
    {
1241
        return $this->setName($name);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->setName($name) targeting SilverStripe\Security\Member::setName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1242
    }
1243
1244
    /**
1245
     * Return the date format based on the user's chosen locale,
1246
     * falling back to the default format defined by the i18n::config()->get('default_locale') config setting.
1247
     *
1248
     * @return string ISO date format
1249
     */
1250
    public function getDateFormat()
1251
    {
1252
        $formatter = new IntlDateFormatter(
1253
            $this->getLocale(),
1254
            IntlDateFormatter::MEDIUM,
1255
            IntlDateFormatter::NONE
1256
        );
1257
        $format = $formatter->getPattern();
1258
1259
        $this->extend('updateDateFormat', $format);
1260
1261
        return $format;
1262
    }
1263
1264
    /**
1265
     * Get user locale, falling back to the configured default locale
1266
     */
1267
    public function getLocale()
1268
    {
1269
        $locale = $this->getField('Locale');
1270
        if ($locale) {
1271
            return $locale;
1272
        }
1273
1274
        return i18n::config()->get('default_locale');
1275
    }
1276
1277
    /**
1278
     * Return the time format based on the user's chosen locale,
1279
     * falling back to the default format defined by the i18n::config()->get('default_locale') config setting.
1280
     *
1281
     * @return string ISO date format
1282
     */
1283
    public function getTimeFormat()
1284
    {
1285
        $formatter = new IntlDateFormatter(
1286
            $this->getLocale(),
1287
            IntlDateFormatter::NONE,
1288
            IntlDateFormatter::MEDIUM
1289
        );
1290
        $format = $formatter->getPattern();
1291
1292
        $this->extend('updateTimeFormat', $format);
1293
1294
        return $format;
1295
    }
1296
1297
    //---------------------------------------------------------------------//
1298
1299
1300
    /**
1301
     * Get a "many-to-many" map that holds for all members their group memberships,
1302
     * including any parent groups where membership is implied.
1303
     * Use {@link DirectGroups()} to only retrieve the group relations without inheritance.
1304
     *
1305
     * @todo Push all this logic into Member_GroupSet's getIterator()?
1306
     * @return Member_Groupset
1307
     */
1308
    public function Groups()
1309
    {
1310
        $groups = Member_GroupSet::create(Group::class, 'Group_Members', 'GroupID', 'MemberID');
1311
        $groups = $groups->forForeignID($this->ID);
1312
1313
        $this->extend('updateGroups', $groups);
1314
1315
        return $groups;
1316
    }
1317
1318
    /**
1319
     * @return ManyManyList|UnsavedRelationList
1320
     */
1321
    public function DirectGroups()
1322
    {
1323
        return $this->getManyManyComponents('Groups');
1324
    }
1325
1326
    /**
1327
     * Get a member SQLMap of members in specific groups
1328
     *
1329
     * If no $groups is passed, all members will be returned
1330
     *
1331
     * @param mixed $groups - takes a SS_List, an array or a single Group.ID
1332
     * @return Map Returns an Map that returns all Member data.
1333
     */
1334
    public static function map_in_groups($groups = null)
1335
    {
1336
        $groupIDList = [];
1337
1338
        if ($groups instanceof SS_List) {
1339
            foreach ($groups as $group) {
1340
                $groupIDList[] = $group->ID;
1341
            }
1342
        } elseif (is_array($groups)) {
1343
            $groupIDList = $groups;
1344
        } elseif ($groups) {
1345
            $groupIDList[] = $groups;
1346
        }
1347
1348
        // No groups, return all Members
1349
        if (!$groupIDList) {
1350
            return static::get()->sort(['Surname' => 'ASC', 'FirstName' => 'ASC'])->map();
1351
        }
1352
1353
        $membersList = new ArrayList();
1354
        // This is a bit ineffective, but follow the ORM style
1355
        /** @var Group $group */
1356
        foreach (Group::get()->byIDs($groupIDList) as $group) {
1357
            $membersList->merge($group->Members());
1358
        }
1359
1360
        $membersList->removeDuplicates('ID');
1361
1362
        return $membersList->map();
1363
    }
1364
1365
1366
    /**
1367
     * Get a map of all members in the groups given that have CMS permissions
1368
     *
1369
     * If no groups are passed, all groups with CMS permissions will be used.
1370
     *
1371
     * @param array $groups Groups to consider or NULL to use all groups with
1372
     *                      CMS permissions.
1373
     * @return Map Returns a map of all members in the groups given that
1374
     *                have CMS permissions.
1375
     */
1376
    public static function mapInCMSGroups($groups = null)
1377
    {
1378
        // non-countable $groups will issue a warning when using count() in PHP 7.2+
1379
        if (!$groups) {
1380
            $groups = [];
1381
        }
1382
1383
        // Check CMS module exists
1384
        if (!class_exists(LeftAndMain::class)) {
1385
            return ArrayList::create()->map();
1386
        }
1387
1388
        if (count($groups) == 0) {
1389
            $perms = ['ADMIN', 'CMS_ACCESS_AssetAdmin'];
1390
1391
            if (class_exists(CMSMain::class)) {
1392
                $cmsPerms = CMSMain::singleton()->providePermissions();
1393
            } else {
1394
                $cmsPerms = LeftAndMain::singleton()->providePermissions();
1395
            }
1396
1397
            if (!empty($cmsPerms)) {
1398
                $perms = array_unique(array_merge($perms, array_keys($cmsPerms)));
1399
            }
1400
1401
            $permsClause = DB::placeholders($perms);
1402
            /** @skipUpgrade */
1403
            $groups = Group::get()
1404
                ->innerJoin("Permission", '"Permission"."GroupID" = "Group"."ID"')
1405
                ->where([
1406
                    "\"Permission\".\"Code\" IN ($permsClause)" => $perms
1407
                ]);
1408
        }
1409
1410
        $groupIDList = [];
1411
1412
        if ($groups instanceof SS_List) {
1413
            foreach ($groups as $group) {
1414
                $groupIDList[] = $group->ID;
1415
            }
1416
        } elseif (is_array($groups)) {
0 ignored issues
show
introduced by
The condition is_array($groups) is always true.
Loading history...
1417
            $groupIDList = $groups;
1418
        }
1419
1420
        /** @skipUpgrade */
1421
        $members = static::get()
1422
            ->innerJoin("Group_Members", '"Group_Members"."MemberID" = "Member"."ID"')
1423
            ->innerJoin("Group", '"Group"."ID" = "Group_Members"."GroupID"');
1424
        if ($groupIDList) {
1425
            $groupClause = DB::placeholders($groupIDList);
1426
            $members = $members->where([
1427
                "\"Group\".\"ID\" IN ($groupClause)" => $groupIDList
1428
            ]);
1429
        }
1430
1431
        return $members->sort('"Member"."Surname", "Member"."FirstName"')->map();
1432
    }
1433
1434
1435
    /**
1436
     * Get the groups in which the member is NOT in
1437
     *
1438
     * When passed an array of groups, and a component set of groups, this
1439
     * function will return the array of groups the member is NOT in.
1440
     *
1441
     * @param array $groupList An array of group code names.
1442
     * @param array $memberGroups A component set of groups (if set to NULL,
1443
     *                            $this->groups() will be used)
1444
     * @return array Groups in which the member is NOT in.
1445
     */
1446
    public function memberNotInGroups($groupList, $memberGroups = null)
1447
    {
1448
        if (!$memberGroups) {
1449
            $memberGroups = $this->Groups();
1450
        }
1451
1452
        foreach ($memberGroups as $group) {
1453
            if (in_array($group->Code, $groupList)) {
1454
                $index = array_search($group->Code, $groupList);
1455
                unset($groupList[$index]);
1456
            }
1457
        }
1458
1459
        return $groupList;
1460
    }
1461
1462
1463
    /**
1464
     * Return a {@link FieldList} of fields that would appropriate for editing
1465
     * this member.
1466
     *
1467
     * @skipUpgrade
1468
     * @return FieldList Return a FieldList of fields that would appropriate for
1469
     *                   editing this member.
1470
     */
1471
    public function getCMSFields()
1472
    {
1473
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
1474
            /** @var TabSet $rootTabSet */
1475
            $rootTabSet = $fields->fieldByName("Root");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $rootTabSet is correct as $fields->fieldByName('Root') targeting SilverStripe\Forms\FieldList::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1476
            /** @var Tab $mainTab */
1477
            $mainTab = $rootTabSet->fieldByName("Main");
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $mainTab is correct as $rootTabSet->fieldByName('Main') targeting SilverStripe\Forms\CompositeField::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1478
            /** @var FieldList $mainFields */
1479
            $mainFields = $mainTab->getChildren();
1480
1481
            // Build change password field
1482
            $mainFields->replaceField('Password', $this->getMemberPasswordField());
1483
1484
            $mainFields->replaceField('Locale', new DropdownField(
1485
                "Locale",
1486
                _t(__CLASS__ . '.INTERFACELANG', "Interface Language", 'Language of the CMS'),
1487
                i18n::getSources()->getKnownLocales()
1488
            ));
1489
            $mainFields->removeByName(static::config()->get('hidden_fields'));
1490
1491
            if (!static::config()->get('lock_out_after_incorrect_logins')) {
1492
                $mainFields->removeByName('FailedLoginCount');
1493
            }
1494
1495
            // Groups relation will get us into logical conflicts because
1496
            // Members are displayed within  group edit form in SecurityAdmin
1497
            $fields->removeByName('Groups');
1498
1499
            // Members shouldn't be able to directly view/edit logged passwords
1500
            $fields->removeByName('LoggedPasswords');
1501
1502
            $fields->removeByName('RememberLoginHashes');
1503
1504
            if (Permission::check('EDIT_PERMISSIONS')) {
1505
                // Filter allowed groups
1506
                $groups = Group::get();
1507
                $disallowedGroupIDs = $this->disallowedGroups();
1508
                if ($disallowedGroupIDs) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $disallowedGroupIDs of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
1509
                    $groups = $groups->exclude('ID', $disallowedGroupIDs);
1510
                }
1511
                $groupsMap = [];
1512
                foreach ($groups as $group) {
1513
                    // Listboxfield values are escaped, use ASCII char instead of &raquo;
1514
                    $groupsMap[$group->ID] = $group->getBreadcrumbs(' > ');
1515
                }
1516
                asort($groupsMap);
1517
                $fields->addFieldToTab(
1518
                    'Root.Main',
1519
                    ListboxField::create('DirectGroups', Group::singleton()->i18n_plural_name())
1520
                        ->setSource($groupsMap)
1521
                        ->setAttribute(
1522
                            'data-placeholder',
1523
                            _t(__CLASS__ . '.ADDGROUP', 'Add group', 'Placeholder text for a dropdown')
1524
                        )
1525
                );
1526
1527
1528
                // Add permission field (readonly to avoid complicated group assignment logic).
1529
                // This should only be available for existing records, as new records start
1530
                // with no permissions until they have a group assignment anyway.
1531
                if ($this->ID) {
1532
                    $permissionsField = new PermissionCheckboxSetField_Readonly(
1533
                        'Permissions',
1534
                        false,
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $title of SilverStripe\Security\Pe...Readonly::__construct(). ( Ignorable by Annotation )

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

1534
                        /** @scrutinizer ignore-type */ false,
Loading history...
1535
                        Permission::class,
1536
                        'GroupID',
1537
                        // we don't want parent relationships, they're automatically resolved in the field
1538
                        $this->getManyManyComponents('Groups')
1539
                    );
1540
                    $fields->findOrMakeTab('Root.Permissions', Permission::singleton()->i18n_plural_name());
1541
                    $fields->addFieldToTab('Root.Permissions', $permissionsField);
1542
                }
1543
            }
1544
1545
            $permissionsTab = $rootTabSet->fieldByName('Permissions');
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $permissionsTab is correct as $rootTabSet->fieldByName('Permissions') targeting SilverStripe\Forms\CompositeField::fieldByName() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1546
            if ($permissionsTab) {
0 ignored issues
show
introduced by
$permissionsTab is of type null, thus it always evaluated to false.
Loading history...
1547
                $permissionsTab->addExtraClass('readonly');
1548
            }
1549
        });
1550
1551
        return parent::getCMSFields();
1552
    }
1553
1554
    /**
1555
     * @param bool $includerelations Indicate if the labels returned include relation fields
1556
     * @return array
1557
     */
1558
    public function fieldLabels($includerelations = true)
1559
    {
1560
        $labels = parent::fieldLabels($includerelations);
1561
1562
        $labels['FirstName'] = _t(__CLASS__ . '.FIRSTNAME', 'First Name');
1563
        $labels['Surname'] = _t(__CLASS__ . '.SURNAME', 'Surname');
1564
        /** @skipUpgrade */
1565
        $labels['Email'] = _t(__CLASS__ . '.EMAIL', 'Email');
1566
        $labels['Password'] = _t(__CLASS__ . '.db_Password', 'Password');
1567
        $labels['PasswordExpiry'] = _t(
1568
            __CLASS__ . '.db_PasswordExpiry',
1569
            'Password Expiry Date',
1570
            'Password expiry date'
1571
        );
1572
        $labels['LockedOutUntil'] = _t(__CLASS__ . '.db_LockedOutUntil', 'Locked out until', 'Security related date');
1573
        $labels['Locale'] = _t(__CLASS__ . '.db_Locale', 'Interface Locale');
1574
        if ($includerelations) {
1575
            $labels['Groups'] = _t(
1576
                __CLASS__ . '.belongs_many_many_Groups',
1577
                'Groups',
1578
                'Security Groups this member belongs to'
1579
            );
1580
        }
1581
1582
        return $labels;
1583
    }
1584
1585
    /**
1586
     * Users can view their own record.
1587
     * Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions.
1588
     * This is likely to be customized for social sites etc. with a looser permission model.
1589
     *
1590
     * @param Member $member
1591
     * @return bool
1592
     */
1593
    public function canView($member = null)
1594
    {
1595
        //get member
1596
        if (!$member) {
1597
            $member = Security::getCurrentUser();
1598
        }
1599
        //check for extensions, we do this first as they can overrule everything
1600
        $extended = $this->extendedCan(__FUNCTION__, $member);
1601
        if ($extended !== null) {
1602
            return $extended;
1603
        }
1604
1605
        //need to be logged in and/or most checks below rely on $member being a Member
1606
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
1607
            return false;
1608
        }
1609
        // members can usually view their own record
1610
        if ($this->ID == $member->ID) {
1611
            return true;
1612
        }
1613
1614
        //standard check
1615
        return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
1616
    }
1617
1618
    /**
1619
     * Users can edit their own record.
1620
     * Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
1621
     *
1622
     * @param Member $member
1623
     * @return bool
1624
     */
1625
    public function canEdit($member = null)
1626
    {
1627
        //get member
1628
        if (!$member) {
1629
            $member = Security::getCurrentUser();
1630
        }
1631
        //check for extensions, we do this first as they can overrule everything
1632
        $extended = $this->extendedCan(__FUNCTION__, $member);
1633
        if ($extended !== null) {
1634
            return $extended;
1635
        }
1636
1637
        //need to be logged in and/or most checks below rely on $member being a Member
1638
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
1639
            return false;
1640
        }
1641
1642
        // HACK: we should not allow for an non-Admin to edit an Admin
1643
        if (!Permission::checkMember($member, 'ADMIN') && Permission::checkMember($this, 'ADMIN')) {
1644
            return false;
1645
        }
1646
        // members can usually edit their own record
1647
        if ($this->ID == $member->ID) {
1648
            return true;
1649
        }
1650
1651
        //standard check
1652
        return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
1653
    }
1654
1655
    /**
1656
     * Users can edit their own record.
1657
     * Otherwise they'll need ADMIN or CMS_ACCESS_SecurityAdmin permissions
1658
     *
1659
     * @param Member $member
1660
     * @return bool
1661
     */
1662
    public function canDelete($member = null)
1663
    {
1664
        if (!$member) {
1665
            $member = Security::getCurrentUser();
1666
        }
1667
        //check for extensions, we do this first as they can overrule everything
1668
        $extended = $this->extendedCan(__FUNCTION__, $member);
1669
        if ($extended !== null) {
1670
            return $extended;
1671
        }
1672
1673
        //need to be logged in and/or most checks below rely on $member being a Member
1674
        if (!$member) {
0 ignored issues
show
introduced by
$member is of type SilverStripe\Security\Member, thus it always evaluated to true.
Loading history...
1675
            return false;
1676
        }
1677
        // Members are not allowed to remove themselves,
1678
        // since it would create inconsistencies in the admin UIs.
1679
        if ($this->ID && $member->ID == $this->ID) {
1680
            return false;
1681
        }
1682
1683
        // HACK: if you want to delete a member, you have to be a member yourself.
1684
        // this is a hack because what this should do is to stop a user
1685
        // deleting a member who has more privileges (e.g. a non-Admin deleting an Admin)
1686
        if (Permission::checkMember($this, 'ADMIN')) {
1687
            if (!Permission::checkMember($member, 'ADMIN')) {
1688
                return false;
1689
            }
1690
        }
1691
1692
        //standard check
1693
        return Permission::checkMember($member, 'CMS_ACCESS_SecurityAdmin');
1694
    }
1695
1696
    /**
1697
     * Validate this member object.
1698
     */
1699
    public function validate()
1700
    {
1701
        // If validation is disabled, skip this step
1702
        if (!DataObject::config()->uninherited('validation_enabled')) {
1703
            return ValidationResult::create();
1704
        }
1705
1706
        $valid = parent::validate();
1707
        $validator = static::password_validator();
1708
1709
        if ($validator) {
0 ignored issues
show
introduced by
$validator is of type SilverStripe\Security\PasswordValidator, thus it always evaluated to true.
Loading history...
1710
            if ((!$this->ID && $this->Password) || $this->isChanged('Password')) {
1711
                $userValid = $validator->validate($this->Password, $this);
1712
                $valid->combineAnd($userValid);
1713
            }
1714
        }
1715
1716
        return $valid;
1717
    }
1718
    
1719
    /**
1720
     * check if user can do something with a dataobject. 
1721
     * this is useful in templates and other places where short-hand is nice. 
1722
     * e.g. 
1723
     * ```php
1724
     *     $member.canWithObject('view', MyBoat::class);
1725
     *     $member.canWithObject('edit', $MyCarObject);
1726
     * ```
1727
     * @param  string              $method (Create, View, Edit, or Delete)
1728
     * @param  string|DataObject   $objectOrClassName
1729
     * @return bool
1730
     */
1731
    public function canWithObject(string $method, $objectOrClassName) : bool
1732
    {
1733
        if (is_string($objectOrClassName)) {
1734
            $objectOrClassName = Injector::inst()->get($objectOrClassName);
1735
        }
1736
        $method = ucfirst($method);
1737
        $methods =  ['Create', 'View', 'Edit', 'Delete'];
1738
        if(! in_array($method, $methods)) {
1739
            user_error('Method supplied must be one of four key ones: '.implode(', ', $methods) . '; supplied: '.$method);
1740
        }
1741
        $method = 'can'.$method;
1742
        
1743
        return (bool) $objectOrClassName->{$method}($this);
1744
    }
1745
1746
    /**
1747
     * Change password. This will cause rehashing according to the `PasswordEncryption` property via the
1748
     * `onBeforeWrite()` method. This method will allow extensions to perform actions and augment the validation
1749
     * result if required before the password is written and can check it after the write also.
1750
     *
1751
     * `onBeforeWrite()` will encrypt the password prior to writing.
1752
     *
1753
     * @param string $password Cleartext password
1754
     * @param bool $write Whether to write the member afterwards
1755
     * @return ValidationResult
1756
     */
1757
    public function changePassword($password, $write = true)
1758
    {
1759
        $this->Password = $password;
1760
        $result = $this->validate();
1761
1762
        $this->extend('onBeforeChangePassword', $password, $result);
1763
1764
        if ($result->isValid()) {
1765
            $this->AutoLoginHash = null;
1766
1767
            if ($write) {
1768
                $this->write();
1769
            }
1770
        }
1771
1772
        $this->extend('onAfterChangePassword', $password, $result);
1773
1774
        return $result;
1775
    }
1776
1777
    /**
1778
     * Takes a plaintext password (on the Member object) and encrypts it
1779
     *
1780
     * @return $this
1781
     */
1782
    protected function encryptPassword()
1783
    {
1784
        // reset salt so that it gets regenerated - this will invalidate any persistent login cookies
1785
        // or other information encrypted with this Member's settings (see self::encryptWithUserSettings)
1786
        $this->Salt = '';
1787
1788
        // Password was changed: encrypt the password according the settings
1789
        $encryption_details = Security::encrypt_password(
1790
            $this->Password,
1791
            $this->Salt,
1792
            $this->isChanged('PasswordEncryption') ? $this->PasswordEncryption : null,
1793
            $this
1794
        );
1795
1796
        // Overwrite the Password property with the hashed value
1797
        $this->Password = $encryption_details['password'];
1798
        $this->Salt = $encryption_details['salt'];
1799
        $this->PasswordEncryption = $encryption_details['algorithm'];
1800
1801
        // If we haven't manually set a password expiry
1802
        if (!$this->isChanged('PasswordExpiry')) {
1803
            // then set it for us
1804
            if (static::config()->get('password_expiry_days')) {
1805
                $this->PasswordExpiry = date('Y-m-d', time() + 86400 * static::config()->get('password_expiry_days'));
1806
            } else {
1807
                $this->PasswordExpiry = null;
1808
            }
1809
        }
1810
1811
        return $this;
1812
    }
1813
1814
    /**
1815
     * Tell this member that someone made a failed attempt at logging in as them.
1816
     * This can be used to lock the user out temporarily if too many failed attempts are made.
1817
     */
1818
    public function registerFailedLogin()
1819
    {
1820
        $lockOutAfterCount = self::config()->get('lock_out_after_incorrect_logins');
1821
        if ($lockOutAfterCount) {
1822
            // Keep a tally of the number of failed log-ins so that we can lock people out
1823
            ++$this->FailedLoginCount;
1824
1825
            if ($this->FailedLoginCount >= $lockOutAfterCount) {
1826
                $lockoutMins = self::config()->get('lock_out_delay_mins');
1827
                $this->LockedOutUntil = date('Y-m-d H:i:s', DBDatetime::now()->getTimestamp() + $lockoutMins * 60);
1828
                $this->FailedLoginCount = 0;
1829
            }
1830
        }
1831
        $this->extend('registerFailedLogin');
1832
        $this->write();
1833
    }
1834
1835
    /**
1836
     * Tell this member that a successful login has been made
1837
     */
1838
    public function registerSuccessfulLogin()
1839
    {
1840
        if (self::config()->get('lock_out_after_incorrect_logins')) {
1841
            // Forgive all past login failures
1842
            $this->FailedLoginCount = 0;
1843
            $this->LockedOutUntil = null;
1844
            $this->write();
1845
        }
1846
    }
1847
1848
    /**
1849
     * Get the HtmlEditorConfig for this user to be used in the CMS.
1850
     * This is set by the group. If multiple configurations are set,
1851
     * the one with the highest priority wins.
1852
     *
1853
     * @return string
1854
     */
1855
    public function getHtmlEditorConfigForCMS()
1856
    {
1857
        $currentName = '';
1858
        $currentPriority = 0;
1859
1860
        foreach ($this->Groups() as $group) {
1861
            $configName = $group->HtmlEditorConfig;
1862
            if ($configName) {
1863
                $config = HTMLEditorConfig::get($group->HtmlEditorConfig);
1864
                if ($config && $config->getOption('priority') > $currentPriority) {
1865
                    $currentName = $configName;
1866
                    $currentPriority = $config->getOption('priority');
1867
                }
1868
            }
1869
        }
1870
1871
        // If can't find a suitable editor, just default to cms
1872
        return $currentName ? $currentName : 'cms';
1873
    }
1874
}
1875