Skip to content

Commit

Permalink
make formatter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
mabiede committed Oct 18, 2023
1 parent d40eb78 commit 95e0e71
Show file tree
Hide file tree
Showing 8 changed files with 190 additions and 195 deletions.
34 changes: 17 additions & 17 deletions lib/guardian.mli
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ module Contract : sig
end

module Make : functor
(ActorModel : RoleSig)
(Role : RoleSig)
(TargetModel : RoleSig)
-> sig
(ActorModel : RoleSig)
(Role : RoleSig)
(TargetModel : RoleSig)
-> sig
module Uuid = Uuid
module Permission = Permission

Expand Down Expand Up @@ -224,20 +224,20 @@ module Make : functor

module type PersistenceSig =
Persistence.Contract
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_permission = ActorPermission.t
and type actor_role = ActorRole.t
and type permission_on_target = PermissionOnTarget.t
and type role = Role.t
and type role_permission = RolePermission.t
and type target = Target.t
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_permission = ActorPermission.t
and type actor_role = ActorRole.t
and type permission_on_target = PermissionOnTarget.t
and type role = Role.t
and type role_permission = RolePermission.t
and type target = Target.t
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t

module MakePersistence : functor
(Backend : Persistence.Backend
(Backend : Persistence.Backend
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_permission = ActorPermission.t
Expand All @@ -249,5 +249,5 @@ module Make : functor
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t)
-> PersistenceSig
-> PersistenceSig
end [@warning "-67"]
186 changes: 91 additions & 95 deletions lib/guardian_entity.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ struct

let filter_permission_on_model filter_permission filter_model =
CCList.filter (fun { permission; model; _ } ->
Permission.(
equal filter_permission permission || equal Manage permission)
&& TargetModel.equal filter_model model)
Permission.(
equal filter_permission permission || equal Manage permission)
&& TargetModel.equal filter_model model)
;;

let remove_duplicates (perms : t list) : t list =
Expand Down Expand Up @@ -214,32 +214,32 @@ struct

module type PersistenceSig =
Persistence.Contract
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_role = ActorRole.t
and type actor_permission = ActorPermission.t
and type permission_on_target = PermissionOnTarget.t
and type role = Role.t
and type role_permission = RolePermission.t
and type target = Target.t
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_role = ActorRole.t
and type actor_permission = ActorPermission.t
and type permission_on_target = PermissionOnTarget.t
and type role = Role.t
and type role_permission = RolePermission.t
and type target = Target.t
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t

module MakePersistence
(Backend : Persistence.Backend
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_role = ActorRole.t
and type actor_permission = ActorPermission.t
and type permission_on_target = PermissionOnTarget.t
and type role = Role.t
and type role_permission = RolePermission.t
and type target = Target.t
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t) : PersistenceSig =
struct
with type actor = Actor.t
and type actor_model = ActorModel.t
and type actor_role = ActorRole.t
and type actor_permission = ActorPermission.t
and type permission_on_target = PermissionOnTarget.t
and type role = Role.t
and type role_permission = RolePermission.t
and type target = Target.t
and type target_entity = TargetEntity.t
and type target_model = TargetModel.t
and type validation_set = ValidationSet.t) :
PersistenceSig = struct
include Backend

let clear_cache () = Repo.clear_cache ()
Expand Down Expand Up @@ -282,9 +282,9 @@ struct
the authorizable's roles and ownership * are consistent in both
spaces. *)
let decorate ?ctx (to_actor : 'a -> actor)
: 'a -> (actor, string) Lwt_result.t
: 'a -> (actor, string) Lwt_result.t
=
fun x ->
fun x ->
let open Lwt_result.Syntax in
let ({ Actor.uuid; _ } as entity : actor) = to_actor x in
let* mem = mem ?ctx uuid in
Expand All @@ -293,7 +293,7 @@ struct
else
let* () = insert ?ctx entity in
Lwt.return_ok entity
;;
;;
end

module ActorRole = struct
Expand All @@ -311,9 +311,9 @@ struct
the authorizable's roles and ownership * are consistent in both
spaces. *)
let decorate ?ctx (to_target : 'a -> target)
: 'a -> (target, string) Lwt_result.t
: 'a -> (target, string) Lwt_result.t
=
fun x ->
fun x ->
let open Lwt_result.Syntax in
let ({ Target.uuid; _ } as entity : target) = to_target x in
let* mem = mem ?ctx uuid in
Expand All @@ -322,57 +322,55 @@ struct
else
let* () = insert ?ctx entity in
Lwt.return_ok entity
;;
;;
end

module PermissionOnTarget = struct
include PermissionOnTarget

let validate_set
?any_id
perms
(error : string -> 'etyp)
(validation_set : ValidationSet.t)
actor
?any_id
perms
(error : string -> 'etyp)
(validation_set : ValidationSet.t)
actor
=
let open CCFun in
let rec find_checker : validation_set -> bool =
let open ValidationSet in
function
| One { PermissionOnTarget.permission; model; target_uuid } ->
(match target_uuid with
| Some target_uuid ->
validate
?any_id
(PermissionOnTarget.create ~target_uuid permission model)
perms
| None ->
validate
?any_id
(PermissionOnTarget.create permission model)
perms)
| Some target_uuid ->
validate
?any_id
(PermissionOnTarget.create ~target_uuid permission model)
perms
| None ->
validate
?any_id
(PermissionOnTarget.create permission model)
perms)
| Or (rule :: rules) ->
(match find_checker rule with
| true -> true
| false ->
CCList.fold_left
(flip (fun rule ->
function
| true -> true
| false -> find_checker rule))
false
rules)
| true -> true
| false ->
CCList.fold_left
(flip (fun rule -> function
| true -> true
| false -> find_checker rule))
false
rules)
| And (rule :: rules) ->
(match find_checker rule with
| false -> false
| true ->
CCList.fold_left
(flip (fun rule ->
function
| true -> find_checker rule
| false -> false))
true
rules)
| false -> false
| true ->
CCList.fold_left
(flip (fun rule -> function
| true -> find_checker rule
| false -> false))
true
rules)
| Or [] | And [] -> true
in
let validate = function
Expand Down Expand Up @@ -400,12 +398,12 @@ struct
[actor] actor object who'd like to perform the action *)
let validate
?ctx
?any_id
(error : string -> 'etyp)
(validation_set : ValidationSet.t)
actor
: (unit, 'etyp) Lwt_result.t
?ctx
?any_id
(error : string -> 'etyp)
(validation_set : ValidationSet.t)
actor
: (unit, 'etyp) Lwt_result.t
=
let open CCFun in
let ( |>> ) = flip Lwt.map in
Expand All @@ -416,26 +414,24 @@ struct
Repo.validate ?ctx ?any_id ?target_uuid ~model permission actor
| Or (rule :: rules) ->
(match%lwt find_checker rule with
| true -> Lwt.return_true
| false ->
Lwt_list.fold_left_s
(flip (fun rule ->
function
| true -> Lwt.return_true
| false -> find_checker rule))
false
rules)
| true -> Lwt.return_true
| false ->
Lwt_list.fold_left_s
(flip (fun rule -> function
| true -> Lwt.return_true
| false -> find_checker rule))
false
rules)
| And (rule :: rules) ->
(match%lwt find_checker rule with
| false -> Lwt.return_false
| true ->
Lwt_list.fold_left_s
(flip (fun rule ->
function
| true -> find_checker rule
| false -> Lwt.return_false))
true
rules)
| false -> Lwt.return_false
| true ->
Lwt_list.fold_left_s
(flip (fun rule -> function
| true -> find_checker rule
| false -> Lwt.return_false))
true
rules)
| Or [] | And [] -> Lwt.return_true
in
let validate = function
Expand All @@ -458,16 +454,16 @@ struct
[validation_set] effect set to check the permissions against *)
let wrap_function
?ctx
(error : string -> 'etyp)
(validation_set : ValidationSet.t)
(fcn : 'param -> ('rval, 'etyp) Lwt_result.t)
?ctx
(error : string -> 'etyp)
(validation_set : ValidationSet.t)
(fcn : 'param -> ('rval, 'etyp) Lwt_result.t)
=
let open Lwt_result.Syntax in
let can = validate ?ctx error validation_set in
Lwt.return_ok (fun actor param ->
let* () = can actor in
fcn param)
let* () = can actor in
fcn param)
;;
end
end
2 changes: 1 addition & 1 deletion lib/utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let decompose_variant_string s =
let fmt = format_of_string "`%s (%s@)" in
try
Scanf.sscanf s fmt (fun name params ->
lowercase_ascii name, CCList.map trim (split_on_char ',' params))
lowercase_ascii name, CCList.map trim (split_on_char ',' params))
with
| End_of_file ->
let fmt = format_of_string "`%s" in
Expand Down
10 changes: 5 additions & 5 deletions test/article.ml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ module Make (Backend : Guard.PersistenceSig) = struct
;;

let update_author
?ctx
(actor : Actor.t)
({ id; _ } as article)
old_author
new_author
?ctx
(actor : Actor.t)
({ id; _ } as article)
old_author
new_author
=
let open Lwt_result.Syntax in
let fcn (old_author, new_author) =
Expand Down
2 changes: 1 addition & 1 deletion test/hacker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ module Make (Backend : Guard.PersistenceSig) = struct

let to_authorizable ?ctx =
Backend.Actor.decorate ?ctx (fun (t : t) : Actor.t ->
Actor.create `Hacker (snd t))
Actor.create `Hacker (snd t))
;;
end
Loading

0 comments on commit 95e0e71

Please sign in to comment.