Skip to content

Instantly share code, notes, and snippets.

@xperiandri
Last active February 13, 2022 21:58
Show Gist options
  • Save xperiandri/736c7e0e0d39844953eb7838542f507e to your computer and use it in GitHub Desktop.
Save xperiandri/736c7e0e0d39844953eb7838542f507e to your computer and use it in GitHub Desktop.
type AllowedGroupRequierment (groups : string seq) =
member val Groups = groups.ToImmutableHashSet ()
interface IAuthorizationRequirement
type AllowedGroupHandler (graphService : IMicrosoftGraphService, userIdResult : Lazy<Result<GraphQLUser, Errors.ErrorMessage>>) =
inherit AuthorizationHandler<AllowedGroupRequierment> ()
override _.HandleRequirementAsync (context, requirement) = task {
match userIdResult.Value with
| Result.Error _ -> return () // user not authenticated / пользователь не аутентифицирован
| Result.Ok user ->
let! userGroups = graphService.AsyncGetUserGroupNames user.Id
let requiredGroups = requirement.Groups
let allowed = not <| requiredGroups.Intersect(userGroups).IsEmpty
if allowed
then context.Succeed requirement
else ()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment