public async Task RestoreServicesFromRepositoryAsync() { var gmailDbContextWorker = new GmailDbContextWorker(); var users = await gmailDbContextWorker.GetAllUsersAsync(); if (users == null) { return; } await Task.Run(() => { users.ForEach(userModel => { var userSettingsModel = gmailDbContextWorker.FindUserSettings(userModel.UserId); try { Instance_AuthorizationRegistredEvent(userModel, userSettingsModel); } catch (Exception ex) { gmailDbContextWorker.RemoveUserRecord(userModel); LogMaker.Log(Logger, ex); } }); }); }
public void RestoreServicesFromRepository() { var gmailDbContextWorker = new GmailDbContextWorker(); var users = gmailDbContextWorker.GetAllUsers(); users?.ForEach(userModel => { var userSettingsModel = gmailDbContextWorker.FindUserSettings(userModel.UserId); try { Instance_AuthorizationRegistredEvent(userModel, userSettingsModel); } catch (Exception ex) { gmailDbContextWorker.RemoveUserRecord(userModel); LogMaker.Log(Logger, ex); } }); }
#pragma warning disable 4014 private void Authorizer_AuthorizeRequestEvent(string code, string state, string error) { int id; string access; if (!RestoreState(state, out id, out access)) { return; } UserModel userModel = null; PendingUserModel pendingUserModel = null; var gmailDbContextWorker = new GmailDbContextWorker(); try { pendingUserModel = gmailDbContextWorker.FindPendingUser(id); if (!string.IsNullOrEmpty(error)) { _botActions.AuthorizationFailedMessage(id.ToString()); LogMaker.Log(Logger, error == "access_denied" ? $"User with id:{id} user declined the authorization request." : $"Authorization user with id:{id} has faulted with error={error}", false); return; } if (pendingUserModel == null) { _botActions.AuthorizationTimeExpiredMessage(id.ToString()); return; } if (DateTime.Now.Subtract(pendingUserModel.JoinTimeUtc).Minutes > MaxPendingMinutes) { _botActions.AuthorizationTimeExpiredMessage(id.ToString()); LogMaker.Log(Logger, $"Authorization attempt from user with id:{id} when authorization time has expired.", false); return; } if (string.IsNullOrEmpty(code)) { LogMaker.Log(Logger, $"Server returned empty authorization code for user with id:{id}.", false); return; } userModel = gmailDbContextWorker.FindUser(id); if (userModel == null) { return; } RevokeToken(userModel); userModel.RefreshToken = null; ExchangeCodeForToken(code, userModel); GetTokenInfo(userModel); var userSettings = gmailDbContextWorker.FindUserSettings(id) ?? gmailDbContextWorker.AddNewUserSettings(id); userSettings.Access = access; gmailDbContextWorker.UpdateUserSettingsRecord(userSettings); AuthorizationRegistredEvent?.Invoke(userModel, userSettings); _botActions.AuthorizationSuccessfulMessage(id.ToString()); } catch (Exception ex) { gmailDbContextWorker.RemoveUserRecord(userModel); _botActions.AuthorizationFailedMessage(id.ToString()); throw new AuthorizeException("An error occurred while attempting to authorize the user.", ex); } finally { if (pendingUserModel != null) { gmailDbContextWorker.RemoveFromQueue(pendingUserModel); } if (userModel != null) { gmailDbContextWorker.UpdateUserRecord(userModel); } } }