Skip to content

Commit

Permalink
SmrAccount: make getAccountByX consistent
Browse files Browse the repository at this point in the history
* No more return by reference allowed. See #317.

* Remove getAccountByHofName (unused).

* Add early (null) return if input is empty. This avoids an extra
  DB query, but also is an extra sanity check because none of these
  account fields should EVER be empty. And we want to be very
  careful not to accidentally give players access to accounts they
  do not own.
  • Loading branch information
hemberger committed Dec 25, 2018
1 parent 25cc37e commit cd6daed
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
26 changes: 11 additions & 15 deletions lib/Default/AbstractSmrAccount.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ abstract class AbstractSmrAccount {
return self::$CACHE_ACCOUNTS[$accountID];
}

public static function &getAccountByName($login,$forceUpdate = false) {
public static function getAccountByName($login, $forceUpdate = false) {
if (empty($login)) { return null; }
$db = new SmrMySqlDatabase();
$db->query('SELECT account_id FROM account WHERE login = '.$db->escapeString($login).' LIMIT 1');
if($db->nextRecord())
Expand All @@ -102,6 +103,7 @@ abstract class AbstractSmrAccount {
}

public static function getAccountByEmail($email, $forceUpdate=false) {
if (empty($email)) { return null; }
$db = new SmrMySqlDatabase();
$db->query('SELECT account_id FROM account WHERE email = '.$db->escapeString($email).' LIMIT 1');
if ($db->nextRecord()) {
Expand All @@ -111,16 +113,8 @@ abstract class AbstractSmrAccount {
}
}

public static function &getAccountByHofName($hofName,$forceUpdate = false) {
$db = new SmrMySqlDatabase();
$db->query('SELECT account_id FROM account WHERE hof_name = '.$db->escapeString($hofName).' LIMIT 1');
if($db->nextRecord())
return self::getAccount($db->getField('account_id'),$forceUpdate);
$return = null;
return $return;
}

public static function getAccountByDiscordId($id, $forceUpdate=false) {
if (empty($id)) { return null; }
$db = new SmrMySqlDatabase();
$db->query('SELECT account_id FROM account where discord_id = '.$db->escapeString($id).' LIMIT 1');
if ($db->nextRecord()) {
Expand All @@ -130,16 +124,18 @@ abstract class AbstractSmrAccount {
}
}

public static function &getAccountByIrcNick($nick, $forceUpdate = false) {
public static function getAccountByIrcNick($nick, $forceUpdate = false) {
if (empty($nick)) { return null; }
$db = new SmrMySqlDatabase();
$db->query('SELECT account_id FROM account WHERE irc_nick = '.$db->escapeString($nick).' LIMIT 1');
if($db->nextRecord())
if ($db->nextRecord()) {
return self::getAccount($db->getField('account_id'), $forceUpdate);
$return = null;
return $return;
} else {
return null;
}
}

public static function &createAccount($login, $password, $email, $timez, $referral) {
public static function createAccount($login, $password, $email, $timez, $referral) {
if($referral!=0) {
// Will throw if referral account doesn't exist
SmrAccount::getAccount($referral);
Expand Down
4 changes: 2 additions & 2 deletions tools/npc/npc.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,11 +454,11 @@ function changeNPCLogin() {

if(SmrAccount::getAccountByName($NPC_LOGIN['Login'])==null) {
debug('Creating account for: '.$NPC_LOGIN['Login']);
$account =& SmrAccount::createAccount($NPC_LOGIN['Login'],'','[email protected]','NPC','NPC',0,0);
$account = SmrAccount::createAccount($NPC_LOGIN['Login'],'','[email protected]','NPC','NPC',0,0);
$account->setValidated(true);
}
else {
$account =& SmrAccount::getAccountByName($NPC_LOGIN['Login']);
$account = SmrAccount::getAccountByName($NPC_LOGIN['Login']);
}

$GLOBALS['account'] =& $account;
Expand Down

0 comments on commit cd6daed

Please sign in to comment.