Skip to content
This repository was archived by the owner on Feb 5, 2025. It is now read-only.

Allow login for WPCOM suspended sites #858

Merged
merged 7 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ _None._

### New Features

_None._
- Add support for logging in into WPCOM suspended sites.

### Bug Fixes

Original file line number Diff line number Diff line change
@@ -178,6 +178,10 @@ public struct WordPressAuthenticatorConfiguration {
///
let enableSiteCreationGuide: Bool

/// If enabled allows login into sites marked as suspended in WordPress.com
///
let enableSiteCredentialsLoginForWPCOMSuspendedSites: Bool

/// Designated Initializer
///
public init (wpcomClientId: String,
@@ -215,7 +219,8 @@ public struct WordPressAuthenticatorConfiguration {
enableManualErrorHandlingForSiteCredentialLogin: Bool = false,
useEnterEmailAddressAsStepValueForGetStartedVC: Bool = false,
enableSiteAddressLoginOnlyInPrologue: Bool = false,
enableSiteCreationGuide: Bool = false
enableSiteCreationGuide: Bool = false,
enableSiteCredentialsLoginForWPCOMSuspendedSites: Bool = false
) {

self.wpcomClientId = wpcomClientId
@@ -254,5 +259,6 @@ public struct WordPressAuthenticatorConfiguration {
self.useEnterEmailAddressAsStepValueForGetStartedVC = useEnterEmailAddressAsStepValueForGetStartedVC
self.enableSiteAddressLoginOnlyInPrologue = enableSiteAddressLoginOnlyInPrologue
self.enableSiteCreationGuide = enableSiteCreationGuide
self.enableSiteCredentialsLoginForWPCOMSuspendedSites = enableSiteCredentialsLoginForWPCOMSuspendedSites
}
}
13 changes: 13 additions & 0 deletions WordPressAuthenticator/Model/WordPressComSiteInfo.swift
Original file line number Diff line number Diff line change
@@ -44,6 +44,19 @@ public class WordPressComSiteInfo {
///
public let exists: Bool

public init(name: String, tagline: String, url: String, hasJetpack: Bool, isJetpackActive: Bool, isJetpackConnected: Bool, icon: String, isWPCom: Bool, isWP: Bool, exists: Bool) {
self.name = name
self.tagline = tagline
self.url = url
self.hasJetpack = hasJetpack
self.isJetpackActive = isJetpackActive
self.isJetpackConnected = isJetpackConnected
self.icon = icon
self.isWPCom = isWPCom
self.isWP = isWP
self.exists = exists
}

/// Initializes the current SiteInfo instance with a raw dictionary.
///
public init(remote: [AnyHashable: Any]) {
31 changes: 22 additions & 9 deletions WordPressAuthenticator/Services/WordPressComBlogService.swift
Original file line number Diff line number Diff line change
@@ -20,15 +20,15 @@ class WordPressComBlogService {

remote.fetchSiteInfo(forAddress: address, success: { response in
guard let response = response else {
failure(ServiceError.unknown)
failure(WordPressComBlogServiceError.unknown)
return
}

let site = WordPressComSiteInfo(remote: response)
success(site)

}, failure: { error in
let result = error ?? ServiceError.unknown
let result = error ?? WordPressComBlogServiceError.unknown
failure(result)
})
}
@@ -37,18 +37,28 @@ class WordPressComBlogService {
let remote = BlogServiceRemoteREST(wordPressComRestApi: anonymousAPI, siteID: 0)
remote.fetchUnauthenticatedSiteInfo(forAddress: address, success: { response in
guard let response = response else {
failure(ServiceError.unknown)
failure(WordPressComBlogServiceError.unknown)
return
}

let site = WordPressComSiteInfo(remote: response)
guard site.url != Constants.wordPressBlogURL else {
failure(ServiceError.invalidWordPressAddress)
failure(WordPressComBlogServiceError.invalidWordPressAddress)
return
}
success(site)
}, failure: { error in
let result = error ?? ServiceError.unknown
let result: Error = {
/// Check whether the site is suspended on WordPress.com and can't be connected using Jetpack
///
if let apiError = error as? WordPressAPIError<WordPressComRestApiEndpointError>,
case let .endpointError(endpointError) = apiError,
endpointError.apiErrorCode == "connection_disabled" {
return WordPressComBlogServiceError.wpcomSiteSuspended
}

return error ?? WordPressComBlogServiceError.unknown
}()
failure(result)
})
}
@@ -60,9 +70,12 @@ extension WordPressComBlogService {
enum Constants {
static let wordPressBlogURL = "https://wordpress.com/blog"
}
}

enum ServiceError: Error {
case unknown
case invalidWordPressAddress
}
public enum WordPressComBlogServiceError: Error {
case unknown
case invalidWordPressAddress
/// Whether the site is suspended on WordPress.com and can't be connected using Jetpack
///
case wpcomSiteSuspended
}
Original file line number Diff line number Diff line change
@@ -552,6 +552,23 @@ private extension SiteAddressViewController {
self?.navigationController?.pushViewController(customUI, animated: true)
}
} else {
// Don't display error and allow login for suspended sites
//
if configuration.enableSiteCredentialsLoginForWPCOMSuspendedSites,
let serviceError = error as? WordPressComBlogServiceError,
serviceError == .wpcomSiteSuspended {
successBlock(WordPressComSiteInfo(name: "",
tagline: "",
url: baseSiteUrl,
hasJetpack: false,
isJetpackActive: false,
isJetpackConnected: false,
icon: "",
isWPCom: false,
isWP: true,
exists: true))
return
}
self.displayError(message: Localization.invalidURL)
}
})