Completed
Pull Request — master (#7026)
by Damian
08:24
created

DefaultAdminService::isDefaultAdminCredentials()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 2
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Security;
4
5
use BadMethodCallException;
6
use InvalidArgumentException;
7
use SilverStripe\Core\Config\Configurable;
8
use SilverStripe\Core\Extensible;
9
use SilverStripe\Core\Injector\Injectable;
10
11
/**
12
 * Provides access to the default admin
13
 */
14
class DefaultAdminService
15
{
16
    use Extensible;
17
    use Configurable;
18
    use Injectable;
19
20
    /**
21
     * @var bool
22
     */
23
    protected static $has_default_admin = false;
24
25
    /**
26
     * @var string
27
     */
28
    protected static $default_username = null;
29
30
    /**
31
     * @var string
32
     */
33
    protected static $default_password = null;
34
35
    public function __construct()
36
    {
37
        $this->constructExtensions();
38
    }
39
40
    /**
41
     * Set the default admin credentials
42
     *
43
     * @param string $username
44
     * @param string $password
45
     */
46
    public static function setDefaultAdmin($username, $password)
47
    {
48
        // don't overwrite if already set
49
        if (static::hasDefaultAdmin()) {
50
            throw new BadMethodCallException(
51
                "Default admin already exists. Use clearDefaultAdmin() first."
52
            );
53
        }
54
55
        if (empty($username) || empty($password)) {
56
            throw new InvalidArgumentException("Default admin username / password cannot be empty");
57
        }
58
59
        static::$default_username = $username;
60
        static::$default_password = $password;
61
        static::$has_default_admin = true;
62
    }
63
64
    /**
65
     * @return string The default admin username
66
     * @throws BadMethodCallException Throws exception if there is no default admin
67
     */
68
    public static function getDefaultAdminUsername()
69
    {
70
        if (!static::hasDefaultAdmin()) {
71
            throw new BadMethodCallException(
72
                "No default admin configured. Please call hasDefaultAdmin() before getting default admin username"
73
            );
74
        }
75
        return static::$default_username;
76
    }
77
78
    /**
79
     * @return string The default admin password
80
     * @throws BadMethodCallException Throws exception if there is no default admin
81
     */
82
    public static function getDefaultAdminPassword()
83
    {
84
        if (!static::hasDefaultAdmin()) {
85
            throw new BadMethodCallException(
86
                "No default admin configured. Please call hasDefaultAdmin() before getting default admin password"
87
            );
88
        }
89
        return static::$default_password;
90
    }
91
92
    /**
93
     * Check if there is a default admin
94
     *
95
     * @return bool
96
     */
97
    public static function hasDefaultAdmin()
98
    {
99
        return static::$has_default_admin;
100
    }
101
102
    /**
103
     * Flush the default admin credentials
104
     */
105
    public static function clearDefaultAdmin()
106
    {
107
        static::$has_default_admin = false;
108
        static::$default_username = null;
109
        static::$default_password = null;
110
    }
111
112
    /**
113
     * @return Member|null
114
     */
115
    public function findOrCreateDefaultAdmin()
116
    {
117
        $this->extend('beforeFindOrCreateDefaultAdmin');
118
119
        // Check if we have default admins
120
        if (!static::hasDefaultAdmin()) {
121
            return null;
122
        }
123
124
        // Find or create ADMIN group
125
        Group::singleton()->requireDefaultRecords();
126
        $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
127
128
        if (!$adminGroup) {
129
            Group::singleton()->requireDefaultRecords();
130
            $adminGroup = Permission::get_groups_by_permission('ADMIN')->first();
131
        }
132
133
        // Find member
134
        /** @skipUpgrade */
135
        $admin = Member::get()
136
            ->filter('Email', static::getDefaultAdminUsername())
137
            ->first();
138
        // If no admin is found, create one
139
        if (!$admin) {
140
            // 'Password' is not set to avoid creating
141
            // persistent logins in the database. See Security::setDefaultAdmin().
142
            // Set 'Email' to identify this as the default admin
143
            $admin = Member::create();
144
            $admin->FirstName = _t(__CLASS__ . '.DefaultAdminFirstname', 'Default Admin');
145
            $admin->Email = static::getDefaultAdminUsername();
146
            $admin->write();
147
        }
148
149
        // Ensure this user is in the admin group
150
        if (!$admin->inGroup($adminGroup)) {
151
            // Add member to group instead of adding group to member
152
            // This bypasses the privilege escallation code in Member_GroupSet
153
            $adminGroup
154
                ->DirectMembers()
155
                ->add($admin);
156
        }
157
158
        $this->extend('afterFindOrCreateDefaultAdmin', $admin);
159
160
        return $admin;
161
    }
162
163
    /**
164
     * Check if the user is a default admin.
165
     * Returns false if there is no default admin.
166
     *
167
     * @param string $username
168
     * @return bool
169
     */
170
    public static function isDefaultAdmin($username)
171
    {
172
        return static::hasDefaultAdmin()
173
            && $username
174
            && $username === static::getDefaultAdminUsername();
175
    }
176
177
    /**
178
     * Check if the user credentials match the default admin.
179
     * Returns false if there is no default admin.
180
     *
181
     * @param string $username
182
     * @param string $password
183
     * @return bool
184
     */
185
    public static function isDefaultAdminCredentials($username, $password)
186
    {
187
        return static::isDefaultAdmin($username)
188
            && $password
189
            && $password === static::getDefaultAdminPassword();
190
    }
191
}
192