philomena/lib/philomena/users/ability.ex

212 lines
9.1 KiB
Elixir
Raw Normal View History

2019-10-01 03:12:38 +02:00
defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
alias Philomena.Users.User
2019-11-12 03:38:51 +01:00
alias Philomena.Comments.Comment
2019-12-04 17:35:06 +01:00
alias Philomena.Commissions.Commission
2019-11-16 05:38:42 +01:00
alias Philomena.Conversations.Conversation
2019-12-08 02:49:28 +01:00
alias Philomena.DuplicateReports.DuplicateReport
2019-12-12 22:44:50 +01:00
alias Philomena.DnpEntries.DnpEntry
2019-10-01 03:12:38 +02:00
alias Philomena.Images.Image
2019-10-06 22:25:40 +02:00
alias Philomena.Forums.Forum
alias Philomena.Topics.Topic
2019-12-04 14:13:10 +01:00
alias Philomena.Posts.Post
2019-10-09 02:45:04 +02:00
alias Philomena.Filters.Filter
2019-12-05 01:11:31 +01:00
alias Philomena.Galleries.Gallery
2019-11-29 22:47:41 +01:00
alias Philomena.DnpEntries.DnpEntry
2019-12-04 15:04:25 +01:00
alias Philomena.UserLinks.UserLink
2019-12-08 02:49:28 +01:00
alias Philomena.Tags.Tag
2019-12-08 18:45:37 +01:00
alias Philomena.Reports.Report
2019-10-01 03:12:38 +02:00
2019-12-13 18:14:34 +01:00
alias Philomena.Bans.User, as: UserBan
alias Philomena.Bans.Subnet, as: SubnetBan
alias Philomena.Bans.Fingerprint, as: FingerprintBan
2019-10-01 03:12:38 +02:00
# Admins can do anything
def can?(%User{role: "admin"}, _action, _model), do: true
2019-10-06 22:25:40 +02:00
#
# Moderators can...
#
2019-10-09 02:45:04 +02:00
# View filters
def can?(%User{role: "moderator"}, :show, %Filter{}), do: true
2019-10-06 22:25:40 +02:00
# View images
def can?(%User{role: "moderator"}, :show, %Image{}), do: true
2019-11-12 03:38:51 +01:00
# View comments
def can?(%User{role: "moderator"}, :show, %Comment{}), do: true
2019-10-06 22:25:40 +02:00
# 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
2019-11-16 05:38:42 +01:00
# View conversations
def can?(%User{role: "moderator"}, :show, %Conversation{}), do: true
2019-12-08 02:49:28 +01:00
# View IP addresses and fingerprints
def can?(%User{role: "moderator"}, :show, :ip_address), do: true
# Manage duplicate reports
def can?(%User{role: "moderator"}, :index, DuplicateReport), do: true
2019-12-10 02:38:55 +01:00
def can?(%User{role: "moderator"}, :edit, %DuplicateReport{}), do: true
# Manage reports
2019-12-08 18:45:37 +01:00
def can?(%User{role: "moderator"}, :index, Report), do: true
def can?(%User{role: "moderator"}, :show, %Report{}), do: true
def can?(%User{role: "moderator"}, :edit, %Report{}), do: true
2019-12-10 02:21:49 +01:00
# Manage user links
def can?(%User{role: "moderator"}, :create_links, %User{}), do: true
def can?(%User{role: "moderator"}, :edit_links, %User{}), do: true
def can?(%User{role: "moderator"}, :edit, %UserLink{}), do: true
def can?(%User{role: "moderator"}, :index, UserLink), do: true
2019-12-10 17:57:01 +01:00
# Reveal anon users
2019-12-10 18:17:13 +01:00
def can?(%User{role: "moderator"}, :reveal_anon, _object), do: true
2019-12-10 17:57:01 +01:00
2019-12-12 22:44:50 +01:00
# Show the DNP list
def can?(%User{role: "moderator"}, :index, DnpEntry), do: true
def can?(%User{role: "moderator"}, :edit, %DnpEntry{}), do: true
def can?(%User{role: "moderator"}, :update, %DnpEntry{}), do: true
2019-12-12 23:15:59 +01:00
def can?(%User{role: "moderator"}, :show_reason, %DnpEntry{}), do: true
def can?(%User{role: "moderator"}, :show_feedback, %DnpEntry{}), do: true
2019-12-12 22:44:50 +01:00
2019-12-13 18:14:34 +01:00
# Create bans
def can?(%User{role: "moderator"}, _action, UserBan), do: true
def can?(%User{role: "moderator"}, _action, SubnetBan), do: true
def can?(%User{role: "moderator"}, _action, FingerprintBan), do: true
2019-12-14 20:46:50 +01:00
# Hide topics
def can?(%User{role: "moderator"}, :hide, %Topic{}), do: true
2019-10-06 22:25:40 +02:00
#
# Assistants can...
#
2019-12-08 02:49:28 +01:00
# Image assistant actions
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :show, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :hide, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :edit, %Image{}), do: true
# Dupe assistant actions
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :index, DuplicateReport), do: true
2019-12-08 02:49:28 +01:00
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :edit, %DuplicateReport{}), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :show, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :edit, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :hide, %Comment{}), do: true
# Comment assistant actions
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :show, %Comment{}), do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :edit, %Comment{}), do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :hide, %Comment{}), do: true
# Topic assistant actions
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :show, %Topic{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :edit, %Topic{}), do: true
2019-12-14 20:46:50 +01:00
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Topic{}), do: true
2019-12-08 02:49:28 +01:00
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :show, %Post{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :edit, %Post{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Post{}), do: true
# Tag assistant actions
def can?(%User{role: "assistant", role_map: %{"Tag" => "moderator"}}, :edit, %Tag{}), do: true
# User link assistant actions
def can?(%User{role: "assistant", role_map: %{"UserLink" => "moderator"}}, :show, %UserLink{}), do: true
def can?(%User{role: "assistant", role_map: %{"UserLink" => "moderator"}}, :edit, %UserLink{}), do: true
2019-10-06 22:25:40 +02:00
# 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...
#
2019-10-01 03:12:38 +02:00
# Edit their description and personal title
def can?(%User{id: id}, :edit_description, %User{id: id}), do: true
def can?(%User{id: id}, :edit_title, %User{id: id}), do: true
2019-11-16 05:38:42 +01:00
# View conversations they are involved in
def can?(%User{id: id}, :show, %Conversation{to_id: id}), do: true
def can?(%User{id: id}, :show, %Conversation{from_id: id}), do: true
2019-10-09 02:45:04 +02:00
# View filters they own and system filters
def can?(_user, :show, %Filter{system: true}), do: true
def can?(%User{}, action, Filter) when action in [:index, :new, :create], do: true
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:show, :edit, :update], do: true
2019-10-09 02:45:04 +02:00
2019-12-01 03:25:42 +01:00
# Edit filters they own
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:edit, :update], do: true
2019-12-04 15:04:25 +01:00
# View user links they've created
2019-12-10 02:21:49 +01:00
def can?(%User{id: id}, :create_links, %User{id: id}), do: true
2019-12-04 15:04:25 +01:00
def can?(%User{id: id}, :show, %UserLink{user_id: id}), do: true
2019-12-04 17:35:06 +01:00
# Edit their commissions
def can?(%User{id: id}, action, %Commission{user_id: id}) when action in [:edit, :update, :delete], do: true
2019-10-01 03:12:38 +02:00
# View non-deleted images
def can?(_user, action, Image)
when action in [:new, :create, :index],
do: true
2019-11-12 03:38:51 +01:00
def can?(_user, action, %Image{hidden_from_users: false})
when action in [:show, :index],
do: true
# Comment on images where that is allowed
def can?(_user, :create_comment, %Image{hidden_from_users: false, commenting_allowed: true}), do: true
2019-12-06 15:43:01 +01:00
# Edit comments on images
def can?(%User{id: id}, :edit, %Comment{hidden_from_users: false, user_id: id} = comment) do
# comment must have been made no later than 15 minutes ago
2019-12-06 23:37:45 +01:00
time_ago = NaiveDateTime.utc_now() |> NaiveDateTime.add(-15 * 60)
2019-12-06 15:43:01 +01:00
2019-12-06 23:41:56 +01:00
NaiveDateTime.diff(comment.created_at, time_ago) > 0
2019-12-06 15:43:01 +01:00
end
# Edit metadata on images where that is allowed
def can?(_user, :edit_metadata, %Image{hidden_from_users: false, tag_editing_allowed: true}), do: true
def can?(%User{id: id}, :edit_description, %Image{user_id: id, hidden_from_users: false, description_editing_allowed: true}), do: true
2019-11-17 03:53:41 +01:00
# Vote on images they can see
def can?(user, :vote, image), do: can?(user, :show, image)
2019-11-12 03:38:51 +01:00
# View non-deleted comments
def can?(_user, :show, %Comment{hidden_from_users: false}), do: true
2019-10-01 03:12:38 +02:00
2019-10-06 22:25:40 +02:00
# View forums
2019-12-04 13:38:57 +01:00
def can?(_user, :index, Forum), do: true
2019-10-06 22:25:40 +02:00
def can?(_user, :show, %Forum{access_level: "normal"}), do: true
def can?(_user, :show, %Topic{hidden_from_users: false}), do: true
2019-12-04 14:13:10 +01:00
def can?(_user, :show, %Post{hidden_from_users: false}), do: true
2019-12-07 00:25:05 +01:00
# Create and edit posts
def can?(_user, :create_post, %Topic{locked_at: nil, hidden_from_users: false}), do: true
2019-12-06 15:43:01 +01:00
def can?(%User{id: id}, :edit, %Post{hidden_from_users: false, user_id: id}), do: true
2019-11-12 02:27:09 +01:00
# View profile pages
def can?(_user, :show, %User{}), do: true
2019-11-29 22:47:41 +01:00
# View and create DNP entries
2019-12-12 22:44:50 +01:00
def can?(%User{}, action, DnpEntry) when action in [:new, :create], do: true
2019-11-29 22:47:41 +01:00
def can?(%User{id: id}, :show, %DnpEntry{requesting_user_id: id}), do: true
def can?(%User{id: id}, :show_reason, %DnpEntry{requesting_user_id: id}), do: true
def can?(%User{id: id}, :show_feedback, %DnpEntry{requesting_user_id: id}), do: true
def can?(_user, :show, %DnpEntry{aasm_state: "listed"}), do: true
def can?(_user, :show_reason, %DnpEntry{aasm_state: "listed", hide_reason: false}), do: true
2019-12-05 01:11:31 +01:00
# Create and edit galleries
def can?(_user, :show, %Gallery{}), do: true
def can?(%User{}, action, Gallery) when action in [:new, :create], do: true
def can?(%User{id: id}, action, %Gallery{creator_id: id}) when action in [:edit, :update, :delete], do: true
2019-10-01 03:12:38 +02:00
# Otherwise...
def can?(_user, _action, _model), do: false
end