Skip to content

Commit

Permalink
Bugfix and code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Evertt committed May 13, 2018
1 parent fdb0e81 commit e9df2a0
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
36 changes: 22 additions & 14 deletions Sources/Gate/Gate.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
public class Gate<Ability: AbilitySet> {
public class Gate<Ability> where Ability: AbilitySet {
public enum Mode {
case giveRights
case takeRights

var defaultPolicy: Bool {
return self == .takeRights
}
}

let mode: Mode
Expand Down Expand Up @@ -43,33 +47,37 @@ extension Gate {
}

extension Gate {
public func check<User, Object>(_ user: User?, can ability: Ability, _ object: Object?) -> Bool {
if let generalAbilities = getAbilities(
public func check<User,Object>(_ user: User?, can ability: Ability, _ object: Object?) -> Bool {
let generalAbilities = getAbilities(
from: policies[User.self, Any.self],
user: user, object: object
), !checkAllPolicies {
)

if !checkAllPolicies, let generalAbilities = generalAbilities {
return hasPermission(for: ability, given: generalAbilities)
}

if let specificAbilities = getAbilities(
let specificAbilities = getAbilities(
from: policies[User.self, Object.self],
user: user, object: object
) {
return hasPermission(for: ability, given: specificAbilities)
)

if let specificAbilities = specificAbilities {
return hasPermission(for: ability, given: specificAbilities.union(generalAbilities ?? []))
}

return mode == .takeRights
return mode.defaultPolicy
}

public func check<User, Object>(_ user: User?, cannot ability: Ability, _ object: Object?) -> Bool {
public func check<User,Object>(_ user: User?, cannot ability: Ability, _ object: Object?) -> Bool {
return !check(user, can: ability, object)
}

public func check<User, Object>(_ user: User?, can ability: Ability, _ type: Object.Type) -> Bool {
public func check<User,Object>(_ user: User?, can ability: Ability, _ type: Object.Type) -> Bool {
return check(user, can: ability, Object?.none)
}

public func check<User, Object>(_ user: User?, cannot ability: Ability, _ type: Object.Type) -> Bool {
public func check<User,Object>(_ user: User?, cannot ability: Ability, _ type: Object.Type) -> Bool {
return !check(user, can: ability, type)
}

Expand All @@ -95,13 +103,13 @@ extension Gate {
}

extension Gate {
public func ensure<User, Object>(_ user: User?, can ability: Ability, _ object: Object?) throws {
public func ensure<User,Object>(_ user: User?, can ability: Ability, _ object: Object?) throws {
guard check(user, can: ability, object) else {
throw Unauthorized(user: user, ability: ability, object: object)
}
}

public func ensure<User, Object>(_ user: User?, can ability: Ability, _ type: Object.Type) throws {
public func ensure<User,Object>(_ user: User?, can ability: Ability, _ type: Object.Type) throws {
try ensure(user, can: ability, Object?.none)
}
}
12 changes: 9 additions & 3 deletions Sources/Gate/Policy.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
struct Policy<User, Object, Ability: AbilitySet> {
struct Policy<User,Object,Ability> where Ability: AbilitySet {
let getAbilities: (User?, Object?) -> Ability?

init(_ policy: @escaping (User?, Object?) -> Ability?) {
getAbilities = policy
}

}

// MARK: User only policies, AKA before policies
extension Policy {
init(_ policy: @escaping (User?) -> Ability?) {
self.init { user, _ in policy(user) }
}
Expand All @@ -15,7 +18,10 @@ struct Policy<User, Object, Ability: AbilitySet> {
return policy(user)
}
}

}

// MARK: Policies with a non-optional user and/or object
extension Policy {
init(_ policy: @escaping (User?, Object) -> Ability?) {
self.init { user, object in
guard let object = object else { return nil }
Expand Down
12 changes: 11 additions & 1 deletion Sources/Gate/TypeTuple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ struct TypeTuple: Hashable {
}

extension Dictionary where Key == TypeTuple, Value == [Any] {
subscript<User,Object,Ability:AbilitySet>(_ userType: User.Type,_ objectType: Object.Type) -> [Policy<User,Object,Ability>] {
subscript<User,Object,Ability>(_ userType: User.Type, _ objectType: Object.Type, _: Ability.Type) -> [Policy<User,Object,Ability>] {
get {
return self[userType, objectType]
}

set(newValue) {
self[userType, objectType] = newValue
}
}

subscript<User,Object,Ability>(_ userType: User.Type, _ objectType: Object.Type) -> [Policy<User,Object,Ability>] {
get {
let hash = TypeTuple(User.self, Object.self)
return self[hash] as? [Policy<User,Object,Ability>] ?? []
Expand Down

0 comments on commit e9df2a0

Please sign in to comment.