add forum ability

This commit is contained in:
byte[] 2019-10-06 16:25:40 -04:00
parent 0d74aa9ab0
commit a37699338e
7 changed files with 34 additions and 10 deletions

View file

@ -1,20 +1,51 @@
defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
alias Philomena.Users.User
alias Philomena.Images.Image
alias Philomena.Forums.Forum
alias Philomena.Topics.Topic
# Admins can do anything
def can?(%User{role: "admin"}, _action, _model), do: true
# Users can...
#
# Moderators can...
#
# View images
def can?(%User{role: "moderator"}, :show, %Image{}), do: true
# View forums
def can?(%User{role: "moderator"}, :show, %Forum{access_level: level})
when level in ["normal", "assistant", "staff"], do: true
def can?(%User{role: "moderator"}, :show, %Topic{hidden_from_users: true}), do: true
#
# Assistants can...
#
# View images
def can?(%User{role: "assistant"}, :show, %Image{}), do: true
# View forums
def can?(%User{role: "assistant"}, :show, %Forum{access_level: level})
when level in ["normal", "assistant"], do: true
def can?(%User{role: "assistant"}, :show, %Topic{hidden_from_users: true}), do: true
#
# Users and anonymous users can...
#
# View non-deleted images
def can?(_user, action, Image)
when action in [:new, :create, :index],
do: true
def can?(_user, :show, %Image{hidden_from_users: true}), do: false
def can?(_user, :show, %Image{hidden_from_users: false}), do: true
# View forums
def can?(_user, :show, %Forum{access_level: "normal"}), do: true
def can?(_user, :show, %Topic{hidden_from_users: false}), do: true
# Otherwise...
def can?(_user, _action, _model), do: false
end

View file

@ -25,7 +25,6 @@ defmodule PhilomenaWeb do
import PhilomenaWeb.Gettext
import Canary.Plugs
alias PhilomenaWeb.Router.Helpers, as: Routes
alias PhilomenaWeb.Plugs.ImageFilter
end
end

View file

@ -5,8 +5,6 @@ defmodule PhilomenaWeb.ActivityController do
alias Philomena.Repo
import Ecto.Query
plug ImageFilter
def index(conn, _params) do
user = conn.assigns.current_user
filter = conn.assigns.compiled_filter

View file

@ -5,7 +5,6 @@ defmodule PhilomenaWeb.ImageController do
alias Philomena.Repo
import Ecto.Query
plug ImageFilter
plug :load_and_authorize_resource, model: Image, only: :show, preload: [:tags, :user]
def index(conn, _params) do

View file

@ -6,8 +6,6 @@ defmodule PhilomenaWeb.SearchController do
import Ecto.Query
plug ImageFilter
def index(conn, params) do
filter = conn.assigns[:compiled_filter]
user = conn |> Plug.current_user()

View file

@ -4,8 +4,6 @@ defmodule PhilomenaWeb.TagController do
alias Philomena.{Images.Image, Tags, Tags.Tag}
import Ecto.Query
plug ImageFilter
def index(conn, params) do
{:ok, query} = Tags.Query.compile(params["tq"] || "*")

View file

@ -8,6 +8,7 @@ defmodule PhilomenaWeb.Router do
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug PhilomenaWeb.Plugs.ImageFilter
end
pipeline :api do