mirror of
https://github.com/AyuGram/AyuGramDesktop.git
synced 2025-06-06 23:24:01 +02:00
Don't allow two same accounts being logged in.
This commit is contained in:
parent
d8a2b391a3
commit
0bc2bfe630
6 changed files with 61 additions and 16 deletions
|
@ -16,7 +16,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "ui/wrap/fade_wrap.h"
|
#include "ui/wrap/fade_wrap.h"
|
||||||
#include "ui/special_fields.h"
|
#include "ui/special_fields.h"
|
||||||
#include "main/main_account.h"
|
#include "main/main_account.h"
|
||||||
|
#include "main/main_domain.h"
|
||||||
#include "main/main_app_config.h"
|
#include "main/main_app_config.h"
|
||||||
|
#include "main/main_session.h"
|
||||||
|
#include "data/data_user.h"
|
||||||
#include "boxes/confirm_phone_box.h"
|
#include "boxes/confirm_phone_box.h"
|
||||||
#include "boxes/confirm_box.h"
|
#include "boxes/confirm_box.h"
|
||||||
#include "core/application.h"
|
#include "core/application.h"
|
||||||
|
@ -138,6 +141,24 @@ void PhoneWidget::submit() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if such account is authorized already.
|
||||||
|
const auto digitsOnly = [](QString value) {
|
||||||
|
return value.replace(QRegularExpression("[^0-9]"), QString());
|
||||||
|
};
|
||||||
|
const auto phoneDigits = digitsOnly(phone);
|
||||||
|
for (const auto &[index, existing] : Core::App().domain().accounts()) {
|
||||||
|
const auto raw = existing.get();
|
||||||
|
if (const auto session = raw->maybeSession()) {
|
||||||
|
if (raw->mtp().environment() == account().mtp().environment()
|
||||||
|
&& digitsOnly(session->user()->phone()) == phoneDigits) {
|
||||||
|
crl::on_main(raw, [=] {
|
||||||
|
Core::App().domain().activate(raw);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
hidePhoneError();
|
hidePhoneError();
|
||||||
|
|
||||||
_checkRequestTimer.callEach(1000);
|
_checkRequestTimer.callEach(1000);
|
||||||
|
|
|
@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||||
#include "lang/lang_keys.h"
|
#include "lang/lang_keys.h"
|
||||||
#include "lang/lang_cloud_manager.h"
|
#include "lang/lang_cloud_manager.h"
|
||||||
#include "main/main_account.h"
|
#include "main/main_account.h"
|
||||||
|
#include "main/main_domain.h"
|
||||||
#include "main/main_session.h"
|
#include "main/main_session.h"
|
||||||
#include "main/main_session_settings.h"
|
#include "main/main_session_settings.h"
|
||||||
#include "core/application.h"
|
#include "core/application.h"
|
||||||
|
@ -144,6 +145,21 @@ void Step::finish(const MTPUser &user, QImage &&photo) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if such account is authorized already.
|
||||||
|
for (const auto &[index, existing] : Core::App().domain().accounts()) {
|
||||||
|
const auto raw = existing.get();
|
||||||
|
if (const auto session = raw->maybeSession()) {
|
||||||
|
if (raw->mtp().environment() == _account->mtp().environment()
|
||||||
|
&& user.c_user().vid().v == session->userId()) {
|
||||||
|
_account->logOut();
|
||||||
|
crl::on_main(raw, [=] {
|
||||||
|
Core::App().domain().activate(raw);
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Save the default language if we've suggested some other and user ignored it.
|
// Save the default language if we've suggested some other and user ignored it.
|
||||||
const auto currentId = Lang::Current().id();
|
const auto currentId = Lang::Current().id();
|
||||||
const auto defaultId = Lang::DefaultLanguageId();
|
const auto defaultId = Lang::DefaultLanguageId();
|
||||||
|
@ -152,7 +168,6 @@ void Step::finish(const MTPUser &user, QImage &&photo) {
|
||||||
Lang::Current().switchToId(Lang::DefaultLanguage());
|
Lang::Current().switchToId(Lang::DefaultLanguage());
|
||||||
Local::writeLangPack();
|
Local::writeLangPack();
|
||||||
}
|
}
|
||||||
|
|
||||||
const auto account = _account;
|
const auto account = _account;
|
||||||
account->createSession(user);
|
account->createSession(user);
|
||||||
|
|
||||||
|
|
|
@ -126,8 +126,13 @@ void Account::watchSessionChanges() {
|
||||||
}, _lifetime);
|
}, _lifetime);
|
||||||
}
|
}
|
||||||
|
|
||||||
UserId Account::willHaveUserId() const {
|
uint64 Account::willHaveSessionUniqueId(MTP::Config *config) const {
|
||||||
return _sessionUserId;
|
// See also Session::uniqueId.
|
||||||
|
if (!_sessionUserId) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return uint64(uint32(_sessionUserId))
|
||||||
|
| (config && config->isTestMode() ? 0x0100'0000'0000'0000ULL : 0ULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Account::createSession(const MTPUser &user) {
|
void Account::createSession(const MTPUser &user) {
|
||||||
|
|
|
@ -48,7 +48,7 @@ public:
|
||||||
std::shared_ptr<MTP::AuthKey> localKey);
|
std::shared_ptr<MTP::AuthKey> localKey);
|
||||||
void start(std::unique_ptr<MTP::Config> config);
|
void start(std::unique_ptr<MTP::Config> config);
|
||||||
|
|
||||||
[[nodiscard]] UserId willHaveUserId() const;
|
[[nodiscard]] uint64 willHaveSessionUniqueId(MTP::Config *config) const;
|
||||||
void createSession(const MTPUser &user);
|
void createSession(const MTPUser &user);
|
||||||
void createSession(
|
void createSession(
|
||||||
UserId id,
|
UserId id,
|
||||||
|
|
|
@ -166,11 +166,9 @@ base::Observable<void> &Session::downloaderTaskFinished() {
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64 Session::uniqueId() const {
|
uint64 Session::uniqueId() const {
|
||||||
auto result = uint64(uint32(userId()));
|
// See also Account::willHaveSessionUniqueId.
|
||||||
if (mtp().isTestMode()) {
|
return uint64(uint32(userId()))
|
||||||
result |= 0x0100'0000'0000'0000ULL;
|
| (mtp().isTestMode() ? 0x0100'0000'0000'0000ULL : 0ULL);
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UserId Session::userId() const {
|
UserId Session::userId() const {
|
||||||
|
|
|
@ -169,7 +169,7 @@ Domain::StartModernResult Domain::startModern(
|
||||||
_oldVersion = keyData.version;
|
_oldVersion = keyData.version;
|
||||||
|
|
||||||
auto tried = base::flat_set<int>();
|
auto tried = base::flat_set<int>();
|
||||||
auto users = base::flat_set<UserId>();
|
auto sessions = base::flat_set<uint64>();
|
||||||
auto active = 0;
|
auto active = 0;
|
||||||
for (auto i = 0; i != count; ++i) {
|
for (auto i = 0; i != count; ++i) {
|
||||||
auto index = qint32();
|
auto index = qint32();
|
||||||
|
@ -182,10 +182,11 @@ Domain::StartModernResult Domain::startModern(
|
||||||
_dataName,
|
_dataName,
|
||||||
index);
|
index);
|
||||||
auto config = account->prepareToStart(_localKey);
|
auto config = account->prepareToStart(_localKey);
|
||||||
const auto userId = account->willHaveUserId();
|
const auto sessionId = account->willHaveSessionUniqueId(
|
||||||
if (!users.contains(userId)
|
config.get());
|
||||||
&& (userId != 0 || (users.empty() && i + 1 == count))) {
|
if (!sessions.contains(sessionId)
|
||||||
if (users.empty()) {
|
&& (sessionId != 0 || (sessions.empty() && i + 1 == count))) {
|
||||||
|
if (sessions.empty()) {
|
||||||
active = index;
|
active = index;
|
||||||
}
|
}
|
||||||
account->start(std::move(config));
|
account->start(std::move(config));
|
||||||
|
@ -193,16 +194,21 @@ Domain::StartModernResult Domain::startModern(
|
||||||
.index = index,
|
.index = index,
|
||||||
.account = std::move(account)
|
.account = std::move(account)
|
||||||
});
|
});
|
||||||
users.emplace(userId);
|
sessions.emplace(sessionId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (sessions.empty()) {
|
||||||
|
LOG(("App Error: no accounts read."));
|
||||||
|
return StartModernResult::Failed;
|
||||||
|
}
|
||||||
|
|
||||||
if (!info.stream.atEnd()) {
|
if (!info.stream.atEnd()) {
|
||||||
info.stream >> active;
|
info.stream >> active;
|
||||||
}
|
}
|
||||||
_owner->activateFromStorage(active);
|
_owner->activateFromStorage(active);
|
||||||
|
|
||||||
Ensures(!users.empty());
|
Ensures(!sessions.empty());
|
||||||
return StartModernResult::Success;
|
return StartModernResult::Success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue