2016-10-27 11:22:04 +00:00
|
|
|
<?php
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
class Account {
|
|
|
|
static function trySync($db, $userName, $password = NULL) {
|
|
|
|
$isSync = $db->getValue(
|
2020-10-23 10:10:41 +00:00
|
|
|
'SELECT COUNT(*) > 0 FROM account.userSync WHERE name = #',
|
2017-11-28 15:07:21 +00:00
|
|
|
[$userName]
|
2017-05-30 13:27:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($isSync)
|
|
|
|
return;
|
|
|
|
|
2018-05-23 10:14:20 +00:00
|
|
|
self::sync($db, $userName, $password);
|
2017-05-30 13:27:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-23 10:10:41 +00:00
|
|
|
static function sync($db, $userName, $password = NULL) {
|
2019-07-02 08:48:14 +00:00
|
|
|
$bcryptPassword = password_hash($password, PASSWORD_BCRYPT);
|
2020-10-23 10:10:41 +00:00
|
|
|
|
2019-06-04 12:14:48 +00:00
|
|
|
$userId = $db->getValue(
|
|
|
|
'SELECT id FROM account.user WHERE `name` = #',
|
2017-11-28 15:07:21 +00:00
|
|
|
[$userName]
|
2017-05-02 12:33:48 +00:00
|
|
|
);
|
2019-06-04 12:14:48 +00:00
|
|
|
$db->query(
|
2019-07-02 08:48:14 +00:00
|
|
|
'UPDATE account.user SET
|
|
|
|
bcryptPassword = #
|
|
|
|
WHERE id = #',
|
|
|
|
[$bcryptPassword, $userId]
|
2019-06-04 12:14:48 +00:00
|
|
|
);
|
2017-05-30 13:27:49 +00:00
|
|
|
|
2020-10-23 10:10:41 +00:00
|
|
|
$hasAccount = $db->getValue(
|
|
|
|
'SELECT COUNT(*) > 0
|
|
|
|
FROM account.user u
|
|
|
|
JOIN account.account a ON u.id = a.id
|
|
|
|
WHERE u.name = #',
|
|
|
|
[$userName]
|
2019-08-07 12:36:20 +00:00
|
|
|
);
|
2020-10-23 10:10:41 +00:00
|
|
|
if (!$hasAccount)
|
|
|
|
$db->getValue('DELETE FROM account.userSync WHERE name = #',
|
2017-12-05 11:49:05 +00:00
|
|
|
[$userName]
|
|
|
|
);
|
2016-10-27 11:22:04 +00:00
|
|
|
}
|
|
|
|
}
|