Skip to content

Commit 40ffa99

Browse files
authored
Improve acl (#80)
* Improve defaultACL * Doc improvement
1 parent 0d81cd4 commit 40ffa99

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.1.4...main)
55
* _Contributing to this repo? Add info about your change here to be included in next release_
66

7+
### 1.1.5
8+
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.1.4...1.1.5)
9+
10+
__Improvements__
11+
- `ParseACL` improvements. Only call `ParseUser.current` when necessary ([#80](https://github.com/parse-community/Parse-Swift/pull/80)), thanks to [Corey Baker](https://github.com/cbaker6).
12+
713
### 1.1.4
814
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.1.3...1.1.4)
915

Sources/ParseSwift/Types/ParseACL.swift

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,6 @@ extension ParseACL {
298298
*/
299299
public static func defaultACL() throws -> Self {
300300

301-
let currentUser = BaseParseUser.current
302301
let aclController: DefaultACL?
303302

304303
#if !os(Linux)
@@ -311,11 +310,11 @@ extension ParseACL {
311310
if !aclController!.useCurrentUser {
312311
return aclController!.defaultACL
313312
} else {
314-
guard let userObjectId = currentUser?.objectId else {
313+
guard let userObjectId = BaseParseUser.current?.objectId else {
315314
return aclController!.defaultACL
316315
}
317316

318-
guard let lastCurrentUserObjectId = aclController!.lastCurrentUser?.objectId,
317+
guard let lastCurrentUserObjectId = aclController!.lastCurrentUserObjectId,
319318
userObjectId == lastCurrentUserObjectId else {
320319
return try setDefaultACL(ParseACL(), withAccessForCurrentUser: true)
321320
}
@@ -328,10 +327,21 @@ extension ParseACL {
328327
}
329328

330329
/**
331-
Sets a default ACL that will be applied to all instances of `ParseObject` when they are created.
330+
Sets a default ACL that can later be used by `ParseObjects`.
331+
332+
To apply the default ACL to all instances of a respective `ParseObject` when they are created,
333+
you will need to add `ACL = try? ParseACL.defaultACL()`. You can also at it when
334+
conforming to `ParseObject`:
335+
336+
struct MyParseObject: ParseObject {
337+
338+
var objectId: String?
339+
var createdAt: Date?
340+
var updatedAt: Date?
341+
var ACL: ParseACL? = try? ParseACL.defaultACL()
342+
}
332343

333-
- parameter acl: The ACL to use as a template for all instances of `ParseObject` created
334-
after this method has been called.
344+
- parameter acl: The ACL to use as a template for instances of `ParseObject`.
335345

336346
This value will be copied and used as a template for the creation of new ACLs, so changes to the
337347
instance after this method has been called will not be reflected in new instance of `ParseObject`.
@@ -346,39 +356,42 @@ extension ParseACL {
346356
*/
347357
public static func setDefaultACL(_ acl: ParseACL, withAccessForCurrentUser: Bool) throws -> ParseACL {
348358

349-
let currentUser = BaseParseUser.current
359+
guard let currentUser = BaseParseUser.current,
360+
let currentUserObjectId = currentUser.objectId else {
361+
throw ParseError(code: .missingObjectId, message: "Can't set defaultACL with no current user")
362+
}
350363

351364
let modifiedACL: ParseACL?
352365
if withAccessForCurrentUser {
353-
modifiedACL = setDefaultAccess(acl)
366+
modifiedACL = setDefaultAccess(acl, user: currentUser)
354367
} else {
355368
modifiedACL = acl
356369
}
357370

358371
let aclController: DefaultACL!
359372
if modifiedACL != nil {
360373
aclController = DefaultACL(defaultACL: modifiedACL!,
361-
lastCurrentUser: currentUser, useCurrentUser: withAccessForCurrentUser)
374+
lastCurrentUserObjectId: currentUserObjectId,
375+
useCurrentUser: withAccessForCurrentUser)
362376
} else {
363377
aclController =
364-
DefaultACL(defaultACL: acl, lastCurrentUser: currentUser, useCurrentUser: withAccessForCurrentUser)
378+
DefaultACL(defaultACL: acl,
379+
lastCurrentUserObjectId: currentUserObjectId,
380+
useCurrentUser: withAccessForCurrentUser)
365381
}
366382

367383
#if !os(Linux)
368-
try? KeychainStore.shared.set(aclController, for: ParseStorage.Keys.defaultACL)
384+
try KeychainStore.shared.set(aclController, for: ParseStorage.Keys.defaultACL)
369385
#else
370-
try? ParseStorage.shared.set(aclController, for: ParseStorage.Keys.defaultACL)
386+
try ParseStorage.shared.set(aclController, for: ParseStorage.Keys.defaultACL)
371387
#endif
372388
return aclController.defaultACL
373389
}
374390

375-
private static func setDefaultAccess(_ acl: ParseACL) -> ParseACL? {
376-
guard let currentUser = BaseParseUser.current else {
377-
return nil
378-
}
391+
private static func setDefaultAccess<T>(_ acl: ParseACL, user: T) -> ParseACL? where T: ParseUser {
379392
var modifiedACL = acl
380-
modifiedACL.setReadAccess(user: currentUser, value: true)
381-
modifiedACL.setWriteAccess(user: currentUser, value: true)
393+
modifiedACL.setReadAccess(user: user, value: true)
394+
modifiedACL.setWriteAccess(user: user, value: true)
382395

383396
return modifiedACL
384397
}
@@ -432,6 +445,6 @@ extension ParseACL: CustomDebugStringConvertible {
432445

433446
struct DefaultACL: Codable {
434447
var defaultACL: ParseACL
435-
var lastCurrentUser: BaseParseUser?
448+
var lastCurrentUserObjectId: String?
436449
var useCurrentUser: Bool
437450
}

Tests/ParseSwiftTests/ParseACLTests.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,8 @@ class ParseACLTests: XCTestCase {
9696
}
9797
}
9898

99-
func testSetACLOfObjectWithDefaultACL() throws {
100-
var user = User()
101-
user.ACL = try ParseACL.defaultACL()
102-
XCTAssertNotNil(user.ACL)
99+
func testCantSetDefaultACLWhenNotLoggedIn() throws {
100+
XCTAssertThrowsError(try ParseACL.defaultACL())
103101
}
104102

105103
func testPublicAccess() {

0 commit comments

Comments
 (0)