Skip to content

Instantly share code, notes, and snippets.

@vukanac
Forked from jeremytregunna/authorize.ex
Created April 16, 2020 12:10
Show Gist options
  • Save vukanac/7cd684dbe41be790ce6fb279827b3a4d to your computer and use it in GitHub Desktop.
Save vukanac/7cd684dbe41be790ce6fb279827b3a4d 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment