Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update DefaultAdminService.php #9487

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 71 additions & 28 deletions src/Security/DefaultAdminService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use SilverStripe\Core\Environment;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\Security\Member;

/**
* Provides access to the default admin
Expand All @@ -29,7 +30,12 @@ class DefaultAdminService
/**
* @var string
*/
protected static $default_username = null;
protected static $default_uniqueIdentifier = null;

/**
* @var string
*/
protected static $default_email = null;

/**
* @var string
Expand All @@ -43,10 +49,11 @@ public function __construct()
/**
* Set the default admin credentials
*
* @param string $username
* @param string $email
* @param string $password
* @param string $uniqueIdentifier
*/
public static function setDefaultAdmin($username, $password)
public static function setDefaultAdmin($email, $password, $uniqueIdentifier = null)
{
// don't overwrite if already set
if (static::hasDefaultAdmin()) {
Expand All @@ -55,29 +62,52 @@ public static function setDefaultAdmin($username, $password)
);
}

if (empty($username) || empty($password)) {
throw new InvalidArgumentException("Default admin username / password cannot be empty");
$uniqueIdentifierFieldName = Member::config()->get('unique_identifier_field');

if (empty($email) || empty($password) || (empty($uniqueIdentifier) && $uniqueIdentifierFieldName != 'Email')) {
throw new InvalidArgumentException("Default admin ". ($uniqueIdentifierFieldName != 'Email' ? strtolower($uniqueIdentifierFieldName)." / " : "") ."email / password cannot be empty");
}

static::$default_username = $username;
static::$default_uniqueIdentifier = $uniqueIdentifier;
static::$default_email = $email;
static::$default_password = $password;
static::$has_default_admin = true;
}

/**
* @return string The default admin username
* @return string The default admin uniqueIdentifier with fallback to the default admin email
* @throws BadMethodCallException Throws exception if there is no default admin
*/
public static function getDefaultAdminUsername()
public static function getDefaultAdminUniqueIdentifier()
{
$uniqueIdentifierFieldName = Member::config()->get('unique_identifier_field');

if($uniqueIdentifierFieldName == 'Email')
return static::getDefaultAdminEmail();

if (!static::hasDefaultAdmin()) {
throw new BadMethodCallException(
"No default admin configured. Please call hasDefaultAdmin() before getting default admin username"
"No default admin configured. Please call hasDefaultAdmin() before getting default admin " . strtolower($uniqueIdentifierFieldName)
);
}
return static::$default_username ?: Environment::getEnv('SS_DEFAULT_ADMIN_USERNAME');
return static::$default_uniqueIdentifier ?: Environment::getEnv('SS_DEFAULT_ADMIN_' . strtoupper($uniqueIdentifierFieldName));
}

/**
* @return string The default admin email
* @throws BadMethodCallException Throws exception if there is no default admin
*/
public static function getDefaultAdminEmail()
{
if (!static::hasDefaultAdmin()) {
throw new BadMethodCallException(
"No default admin configured. Please call hasDefaultAdmin() before getting default admin email"
);
}
return static::$default_email ?: Environment::getEnv('SS_DEFAULT_ADMIN_EMAIL');
}


/**
* @return string The default admin password
* @throws BadMethodCallException Throws exception if there is no default admin
Expand All @@ -101,7 +131,11 @@ public static function hasDefaultAdmin()
{
// Check environment if not explicitly set
if (!isset(static::$has_default_admin)) {
return !empty(Environment::getEnv('SS_DEFAULT_ADMIN_USERNAME'))

$uniqueIdentifierFieldName = Member::config()->get('unique_identifier_field');

return ($uniqueIdentifierFieldName == 'Email' || ($uniqueIdentifierFieldName != 'Email' && !empty(Environment::getEnv('SS_DEFAULT_ADMIN_' . strtoupper($uniqueIdentifierFieldName)))))
&& !empty(Environment::getEnv('SS_DEFAULT_ADMIN_EMAIL'))
&& !empty(Environment::getEnv('SS_DEFAULT_ADMIN_PASSWORD'));
}
return static::$has_default_admin;
Expand All @@ -113,7 +147,8 @@ public static function hasDefaultAdmin()
public static function clearDefaultAdmin()
{
static::$has_default_admin = false;
static::$default_username = null;
static::$default_uniqueIdentifier = null;
static::$default_email = null;
static::$default_password = null;
}

Expand All @@ -129,9 +164,9 @@ public function findOrCreateDefaultAdmin()
return null;
}

// Create admin with default admin username
// Create admin with default admin uniqueIdentifier
$admin = $this->findOrCreateAdmin(
static::getDefaultAdminUsername(),
static::getDefaultAdminUniqueIdentifier(),
_t(__CLASS__ . '.DefaultAdminFirstname', 'Default Admin')
);

Expand All @@ -144,18 +179,21 @@ public function findOrCreateDefaultAdmin()
* Find or create a Member with admin permissions
*
* @skipUpgrade
* @param string $email
* @param string $uniqueIdentifier
* @param string $name
* @return Member
*/
public function findOrCreateAdmin($email, $name = null)
public function findOrCreateAdmin($uniqueIdentifier, $name = null)
{
$this->extend('beforeFindOrCreateAdmin', $email, $name);
$this->extend('beforeFindOrCreateAdmin', $uniqueIdentifier, $name);

// Find member
/** @var Member $admin */

$uniqueIdentifierFieldName = Member::config()->get('unique_identifier_field');

$admin = Member::get()
->filter('Email', $email)
->filter($uniqueIdentifierFieldName, $uniqueIdentifier)
->first();

// Find or create admin group
Expand All @@ -166,11 +204,16 @@ public function findOrCreateAdmin($email, $name = null)
$inGroup = $admin->inGroup($adminGroup);
} else {
// Note: This user won't be able to login until a password is set
// Set 'Email' to identify this as the default admin
// Set 'uniqueIdentifierFieldName' to identify this as the default admin

$inGroup = false;
$admin = Member::create();
$admin->FirstName = $name ?: $email;
$admin->Email = $email;
$admin->FirstName = $name ?: static::getDefaultAdminUniqueIdentifier();

if($uniqueIdentifierFieldName != 'Email')
$admin->$uniqueIdentifierFieldName = $uniqueIdentifier;

$admin->Email = static::getDefaultAdminEmail();
$admin->PasswordEncryption = 'none';
$admin->write();
}
Expand Down Expand Up @@ -223,27 +266,27 @@ protected function findOrCreateAdminGroup()
* Check if the user is a default admin.
* Returns false if there is no default admin.
*
* @param string $username
* @param string $uniqueIdentifier
* @return bool
*/
public static function isDefaultAdmin($username)
public static function isDefaultAdmin($uniqueIdentifier)
{
return static::hasDefaultAdmin()
&& $username
&& $username === static::getDefaultAdminUsername();
&& $uniqueIdentifier
&& $uniqueIdentifier === static::getDefaultAdminUniqueIdentifier();
}

/**
* Check if the user credentials match the default admin.
* Returns false if there is no default admin.
*
* @param string $username
* @param string $uniqueIdentifier
* @param string $password
* @return bool
*/
public static function isDefaultAdminCredentials($username, $password)
public static function isDefaultAdminCredentials($uniqueIdentifier, $password)
{
return static::isDefaultAdmin($username)
return static::isDefaultAdmin($uniqueIdentifier)
&& $password
&& $password === static::getDefaultAdminPassword();
}
Expand Down