Skip to content

Commit 345d348

Browse files
committed
Check if the current user can manage plugins
1 parent 0878c22 commit 345d348

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

Modules/Sources/WordPressCore/WordPressClient.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,35 @@ public actor WordPressClient {
1414
public let rootUrl: String
1515

1616
private var apiRoot: WpApiDetails?
17+
private var currentUser: UserWithEditContext?
1718

1819
public init(api: WordPressAPI, rootUrl: ParsedUrl) {
1920
self.api = api
2021
self.rootUrl = rootUrl.url()
2122
}
2223

2324
public func refreshCachedSiteInfo() async throws {
24-
let apiRoot = try await self.api.apiRoot.get()
25-
self.apiRoot = apiRoot.data
25+
async let apiRootTask = try await self.api.apiRoot.get().data
26+
async let currentUserTask = try await self.api.users.retrieveMeWithEditContext().data
27+
28+
let (apiRoot, currentUser) = try await (apiRootTask, currentUserTask)
29+
30+
self.apiRoot = apiRoot
31+
self.currentUser = currentUser
2632
}
2733

28-
public func currentUserCan(_ capability: String) async throws -> Bool {
29-
false
34+
public func currentUserCan(_ capability: UserCapability) async throws -> Bool {
35+
try await fetchCurrentUser().capabilities.keys.contains(capability)
36+
}
37+
38+
private func fetchCurrentUser() async throws -> UserWithEditContext {
39+
if let currentUser = self.currentUser {
40+
return currentUser
41+
}
42+
43+
let currentUser = try await self.api.users.retrieveMeWithEditContext().data
44+
self.currentUser = currentUser
45+
return currentUser
3046
}
3147

3248
public func supports(_ feature: Feature, forSiteId siteId: Int? = nil) async throws -> Bool {

WordPress/Classes/Services/CommentServiceRemoteFactory.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import WordPressData
33
import WordPressKit
4+
import WordPressCore
45

56
/// Provides service remote instances for CommentService
67
@objc public class CommentServiceRemoteFactory: NSObject {
@@ -18,7 +19,7 @@ import WordPressKit
1819

1920
// The REST API does not have information about comment "likes". We'll continue to use WordPress.com API for now.
2021
if let site = try? WordPressSite(blog: blog) {
21-
return CommentServiceRemoteCoreRESTAPI(client: .init(site: site))
22+
return CommentServiceRemoteCoreRESTAPI(client: WordPressClient(site: site))
2223
}
2324

2425
if let api = blog.xmlrpcApi,

WordPress/Classes/ViewRelated/NewGutenberg/NewGutenbergViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ class NewGutenbergViewController: UIViewController, PostEditor, PublishingEditor
538538

539539
private func fetchPluginRecommendation(client: WordPressClient) async throws -> RecommendedPlugin? {
540540

541-
guard try await client.supports(.managePlugins) else {
541+
guard try await client.supports(.managePlugins), try await client.currentUserCan(.installPlugins) else {
542542
return nil
543543
}
544544

0 commit comments

Comments
 (0)