Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Last active April 16, 2020 12:10
Show Gist options
  • Save jeremytregunna/95830562d9b3a70e0c898d9694445eb5 to your computer and use it in GitHub Desktop.
Save jeremytregunna/95830562d9b3a70e0c898d9694445eb5 to your computer and use it in GitHub Desktop.
After refactoring...
defmodule Api.Authorize do
alias Account.Models.User
alias Tracker.OrganizationMembers, as: OM
def board?(board_id, %User{} = user, available_perms, required_perms) do
with(
{:ok, board} <- Tracker.Board.find(board_id),
true <- OM.user_in_organization?(user.id, board.organization_id)
) do
required_perms == common_permissions(required_perms, available_perms)
else
_ -> false
end
end
defp common_permissions(required, available) do
required
|> MapSet.new()
|> MapSet.intersection(MapSet.new(available))
|> Enum.to_list()
end
end
@jeremytregunna
Copy link
Author

Could be further improved I'm sure, but for now, I'm happy with it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment