philomena/lib/philomena/users/ability.ex

432 lines
14 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-12-20 01:00:09 +01:00
alias Philomena.Roles.Role
2019-12-15 21:02:13 +01:00
alias Philomena.Badges.Award
2019-12-20 01:00:09 +01:00
alias Philomena.Badges.Badge
2019-12-18 18:01:33 +01:00
alias Philomena.Channels.Channel
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-15 23:52:51 +01:00
alias Philomena.ModNotes.ModNote
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
alias Philomena.ArtistLinks.ArtistLink
2019-12-08 02:49:28 +01:00
alias Philomena.Tags.Tag
alias Philomena.TagChanges.TagChange
2019-12-08 18:45:37 +01:00
alias Philomena.Reports.Report
2019-12-15 18:33:37 +01:00
alias Philomena.StaticPages.StaticPage
2019-12-20 01:00:09 +01:00
alias Philomena.Adverts.Advert
alias Philomena.SiteNotices.SiteNotice
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-12-20 01:00:09 +01:00
# Show details of profiles and view user list
2019-12-17 03:26:43 +01:00
def can?(%User{role: "moderator"}, :show_details, %User{}), do: true
2020-03-05 08:04:21 +01:00
def can?(%User{role: "moderator"}, :edit_description, %User{}), do: true
2019-12-20 01:00:09 +01:00
def can?(%User{role: "moderator"}, :index, User), do: true
2019-12-17 03:26:43 +01:00
2019-10-09 02:45:04 +02:00
# View filters
def can?(%User{role: "moderator"}, :show, %Filter{}), do: true
2019-12-20 19:29:00 +01:00
# Manage images
def can?(%User{role: "moderator"}, :destroy, %Image{}), do: false
def can?(%User{role: "moderator"}, _action, Image), do: true
def can?(%User{role: "moderator"}, _action, %Image{}), do: true
2019-10-06 22:25:40 +02:00
2020-09-06 05:04:04 +02:00
# Manage channels
def can?(%User{role: "moderator"}, _action, Channel), do: true
def can?(%User{role: "moderator"}, _action, %Channel{}), 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
2020-03-03 21:38:59 +01:00
def can?(%User{role: "moderator"}, :show, %Forum{}), do: true
2020-01-11 05:20:19 +01:00
2019-10-06 22:25:40 +02:00
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
# Manage artist links
2019-12-10 02:21:49 +01:00
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"}, _action, ArtistLink), do: true
def can?(%User{role: "moderator"}, _action, %ArtistLink{}), do: true
2019-12-10 02:21:49 +01:00
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-14 22:26:05 +01:00
# Edit posts and comments
def can?(%User{role: "moderator"}, :edit, %Post{}), do: true
2019-12-14 23:07:43 +01:00
def can?(%User{role: "moderator"}, :hide, %Post{}), do: true
def can?(%User{role: "moderator"}, :delete, %Post{}), do: true
2019-12-14 22:26:05 +01:00
def can?(%User{role: "moderator"}, :edit, %Comment{}), do: true
2019-12-14 23:07:43 +01:00
def can?(%User{role: "moderator"}, :hide, %Comment{}), do: true
def can?(%User{role: "moderator"}, :delete, %Comment{}), do: true
2019-12-10 17:57:01 +01:00
2019-12-12 22:44:50 +01:00
# Show the DNP list
2019-12-21 15:36:30 +01:00
def can?(%User{role: "moderator"}, _action, DnpEntry), do: true
def can?(%User{role: "moderator"}, _action, %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
2019-12-14 23:07:43 +01:00
def can?(%User{role: "moderator"}, :show, %Topic{}), do: true
2019-12-14 20:46:50 +01:00
def can?(%User{role: "moderator"}, :hide, %Topic{}), do: true
def can?(%User{role: "moderator"}, :edit, %Topic{}), do: true
def can?(%User{role: "moderator"}, :create_post, %Topic{}), do: true
2019-12-14 20:46:50 +01:00
2019-12-20 01:00:09 +01:00
# Edit tags
def can?(%User{role: "moderator"}, :edit, %Tag{}), do: true
2019-12-15 21:02:13 +01:00
# Award badges
2019-12-25 22:10:56 +01:00
def can?(%User{role: "moderator"}, _action, %Award{}), do: true
def can?(%User{role: "moderator"}, _action, Award), do: true
2019-12-15 21:02:13 +01:00
2019-12-15 23:52:51 +01:00
# Create mod notes
def can?(%User{role: "moderator"}, :index, ModNote), do: true
# Revert tag changes
def can?(%User{role: "moderator"}, :revert, TagChange), do: true
# Manage commissions
def can?(%User{role: "moderator"}, _action, %Commission{}), do: true
# Manage galleries
def can?(%User{role: "moderator"}, _action, %Gallery{}), do: true
2019-12-20 01:00:09 +01:00
# And some privileged moderators can...
# Manage site notices
2020-01-11 05:20:19 +01:00
def can?(%User{role: "moderator", role_map: %{"SiteNotice" => "admin"}}, _action, SiteNotice),
do: true
def can?(%User{role: "moderator", role_map: %{"SiteNotice" => "admin"}}, _action, %SiteNotice{}),
do: true
2019-12-20 01:00:09 +01:00
# Manage badges
def can?(%User{role: "moderator", role_map: %{"Badge" => "admin"}}, _action, Badge), do: true
def can?(%User{role: "moderator", role_map: %{"Badge" => "admin"}}, _action, %Badge{}), do: true
# Manage tags
def can?(%User{role: "moderator", role_map: %{"Tag" => "admin"}}, _action, Tag), do: true
def can?(%User{role: "moderator", role_map: %{"Tag" => "admin"}}, _action, %Tag{}), do: true
# Manage user roles
def can?(%User{role: "moderator", role_map: %{"Role" => "admin"}}, _action, %Role{}), do: true
# Manage users
def can?(%User{role: "moderator", role_map: %{"User" => "moderator"}}, _action, User), do: true
2020-01-11 05:20:19 +01:00
def can?(%User{role: "moderator", role_map: %{"User" => "moderator"}}, _action, %User{}),
do: true
2019-12-20 01:00:09 +01:00
# Manage advertisements
def can?(%User{role: "moderator", role_map: %{"Advert" => "admin"}}, _action, Advert), do: true
2020-01-11 05:20:19 +01:00
def can?(%User{role: "moderator", role_map: %{"Advert" => "admin"}}, _action, %Advert{}),
do: true
2019-12-20 01:00:09 +01:00
# Manage static pages
2020-01-11 05:20:19 +01:00
def can?(%User{role: "moderator", role_map: %{"StaticPage" => "admin"}}, _action, StaticPage),
do: true
def can?(%User{role: "moderator", role_map: %{"StaticPage" => "admin"}}, _action, %StaticPage{}),
do: true
2019-12-20 01:00:09 +01:00
2019-10-06 22:25:40 +02:00
#
# Assistants can...
#
2019-12-08 02:49:28 +01:00
# Image assistant actions
2020-01-11 05:20:19 +01:00
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
2020-10-26 22:50:40 +01:00
def can?(
%User{role: "assistant", role_map: %{"Image" => "moderator"}},
:edit_metadata,
%Image{}
),
do: true
2020-01-11 05:20:19 +01:00
def can?(
%User{role: "assistant", role_map: %{"Image" => "moderator"}},
:edit_description,
%Image{}
),
do: true
2019-12-08 02:49:28 +01:00
# Dupe assistant actions
2020-01-11 05:20:19 +01:00
def can?(
%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}},
:index,
DuplicateReport
),
do: true
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
2019-12-08 02:49:28 +01:00
# Comment assistant actions
2020-01-11 05:20:19 +01:00
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
2019-12-08 02:49:28 +01:00
# Topic assistant actions
2020-01-11 05:20:19 +01:00
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
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Topic{}),
do: true
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
2019-12-08 02:49:28 +01:00
# Tag assistant actions
def can?(%User{role: "assistant", role_map: %{"Tag" => "moderator"}}, :edit, %Tag{}), do: true
2020-01-11 05:20:19 +01:00
def can?(%User{role: "assistant", role_map: %{"Tag" => "moderator"}}, :batch_update, Tag),
do: true
2019-12-08 02:49:28 +01:00
# Artist link assistant actions
2020-12-02 20:18:25 +01:00
def can?(
%User{role: "assistant", role_map: %{"ArtistLink" => "moderator"}},
_action,
%ArtistLink{}
),
do: true
2020-01-11 05:20:19 +01:00
2020-03-03 11:43:54 +01:00
def can?(
%User{role: "assistant", role_map: %{"ArtistLink" => "moderator"}},
2020-03-03 11:43:54 +01:00
:create_links,
%User{}
),
do: true
2020-12-02 20:18:25 +01:00
def can?(
%User{role: "assistant", role_map: %{"ArtistLink" => "moderator"}},
:edit,
%ArtistLink{}
),
do: true
2019-10-06 22:25:40 +02:00
2020-12-02 20:18:25 +01:00
def can?(
%User{role: "assistant", role_map: %{"ArtistLink" => "moderator"}},
:edit_links,
%User{}
),
do: true
2020-12-02 20:18:25 +01:00
def can?(
%User{role: "assistant", role_map: %{"ArtistLink" => "moderator"}},
:index,
%ArtistLink{}
),
do: true
2019-10-06 22:25:40 +02:00
# View forums
def can?(%User{role: "assistant"}, :show, %Forum{access_level: level})
2020-01-11 05:20:19 +01:00
when level in ["normal", "assistant"],
do: true
2019-10-06 22:25:40 +02:00
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
2019-12-16 23:11:16 +01:00
# Batch tag
def can?(%User{role_map: %{"Tag" => "batch_update"}}, :batch_update, Tag), do: true
# 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
# Edit their username
def can?(%User{id: id}, :change_username, %User{id: id} = user) do
time_ago = NaiveDateTime.utc_now() |> NaiveDateTime.add(-1 * 60 * 60 * 24 * 90)
NaiveDateTime.diff(user.last_renamed_at, time_ago) < 0
end
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-12-22 22:47:16 +01:00
# View filters they own and public/system filters
2019-10-09 02:45:04 +02:00
def can?(_user, :show, %Filter{system: true}), do: true
2019-12-22 22:47:16 +01:00
def can?(_user, :show, %Filter{public: true}), do: true
def can?(%User{}, action, Filter) when action in [:index, :new, :create], do: true
2020-01-11 05:20:19 +01:00
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
2020-01-11 05:20:19 +01:00
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:edit, :update, :delete],
do: true
2019-12-01 03:25:42 +01:00
# View artist links they've created
2019-12-10 02:21:49 +01:00
def can?(%User{id: id}, :create_links, %User{id: id}), do: true
def can?(%User{id: id}, :show, %ArtistLink{user_id: id}), do: true
2019-12-04 15:04:25 +01:00
2019-12-04 17:35:06 +01:00
# Edit their commissions
2020-01-11 05:20:19 +01:00
def can?(%User{id: id}, action, %Commission{user_id: id})
when action in [:edit, :update, :delete],
do: true
2019-12-04 17:35:06 +01:00
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
2019-12-14 22:23:35 +01:00
def can?(_user, :show, %Tag{}), do: true
# Comment on images where that is allowed
2020-01-11 05:20:19 +01:00
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}, action, %Comment{hidden_from_users: false, user_id: id})
when action in [:edit, :update],
do: true
2019-12-06 15:43:01 +01:00
# Edit metadata on images where that is allowed
2020-01-11 05:20:19 +01:00
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
2020-01-11 05:20:19 +01:00
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
2020-01-11 05:20:19 +01:00
def can?(%User{id: id}, action, %Post{hidden_from_users: false, user_id: id})
when action in [:edit, :update],
do: true
2019-12-06 15:43:01 +01:00
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
2020-01-11 05:20:19 +01:00
def can?(%User{id: id}, action, %Gallery{creator_id: id})
when action in [:edit, :update, :delete],
do: true
2019-12-05 01:11:31 +01:00
2019-12-15 18:33:37 +01:00
# Show static pages
def can?(_user, :show, %StaticPage{}), do: true
2019-12-18 18:01:33 +01:00
# Show channels
def can?(_user, :show, %Channel{}), do: true
2019-10-01 03:12:38 +02:00
# Otherwise...
def can?(_user, _action, _model), do: false
end