-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add authorization to the Product Settings LiveView
- Loading branch information
Showing
7 changed files
with
76 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule NervesHub.Helpers.Authorization do | ||
alias NervesHub.Accounts.OrgUser | ||
alias NervesHub.Accounts.User | ||
|
||
def authorized!(org_user, permission) do | ||
true = authorized?(org_user, permission) | ||
end | ||
|
||
def authorized?(:update_product, %OrgUser{role: user_role}), do: role_check(:write, user_role) | ||
def authorized?(:delete_product, %OrgUser{role: user_role}), do: role_check(:admin, user_role) | ||
|
||
defp role_check(required_role, user_role) do | ||
required_role | ||
|> User.role_or_higher() | ||
|> Enum.any?(&(&1 == user_role)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule NervesHubWeb.Mounts.FetchOrgUser do | ||
import Phoenix.Component | ||
|
||
alias NervesHub.Accounts | ||
|
||
def on_mount(_, _, _, socket) do | ||
{:ok, org_user} = Accounts.get_org_user(socket.assigns.org, socket.assigns.user) | ||
|
||
{:cont, assign(socket, :org_user, org_user)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters