1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15 class EncryptAllPasswordsTask extends BuildTask {
16 protected $title = 'Encrypt all passwords tasks';
17
18 protected $description = 'Convert all plaintext passwords on the Member table to the default encryption/hashing algorithm. Note: This mainly applies to passwords in SilverStripe 2.1 or earlier, passwords in newer versions are hashed by default.';
19
20 function init() {
21 parent::init();
22
23 if(!Permission::check('ADMIN')) {
24 return Security::permissionFailure($this);
25 }
26 }
27
28 public function run($request) {
29 $algo = Security::get_password_encryption_algorithm();
30 if($algo == 'none') {
31 $this->debugMessage('Password encryption disabled');
32 return;
33 }
34
35
36 $members = DataObject::get(
37 "Member",
38 "\"PasswordEncryption\" = 'none' AND \"Password\" IS NOT NULL"
39 );
40
41 if(!$members) {
42 $this->debugMessage('No passwords to encrypt');
43 return;
44 }
45
46
47 $this->debugMessage('Encrypting all passwords');
48 $this->debugMessage(sprintf(
49 'The passwords will be encrypted using the %s algorithm',
50 $algo
51 ));
52
53 foreach($members as $member) {
54
55
56
57 $member->PasswordEncryption = $algo;
58 $member->forceChange();
59 $member->write();
60
61 $this->debugMessage(sprintf('Encrypted credentials for member #%d;', $member->ID));
62 }
63 }
64
65 66 67
68 protected function debugMessage($msg) {
69 if(!SapphireTest::is_running_test()) {
70 Debug::message($msg);
71 }
72 }
73 }
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-