43 lines
883 B
PHP
43 lines
883 B
PHP
<?php
|
|
|
|
class Account {
|
|
static function trySync($db, $userName, $password = NULL) {
|
|
$isSync = $db->getValue(
|
|
'SELECT COUNT(*) > 0 FROM account.userSync WHERE name = #',
|
|
[$userName]
|
|
);
|
|
|
|
if ($isSync)
|
|
return;
|
|
|
|
self::sync($db, $userName, $password);
|
|
}
|
|
|
|
static function sync($db, $userName, $password = NULL) {
|
|
$bcryptPassword = password_hash($password, PASSWORD_BCRYPT);
|
|
|
|
$userId = $db->getValue(
|
|
'SELECT id FROM account.user WHERE `name` = #',
|
|
[$userName]
|
|
);
|
|
$db->query(
|
|
'UPDATE account.user SET
|
|
bcryptPassword = #
|
|
WHERE id = #',
|
|
[$bcryptPassword, $userId]
|
|
);
|
|
|
|
$hasAccount = $db->getValue(
|
|
'SELECT COUNT(*) > 0
|
|
FROM account.user u
|
|
JOIN account.account a ON u.id = a.id
|
|
WHERE u.name = #',
|
|
[$userName]
|
|
);
|
|
if (!$hasAccount)
|
|
$db->getValue('DELETE FROM account.userSync WHERE name = #',
|
|
[$userName]
|
|
);
|
|
}
|
|
}
|