[Users] Don't allow mods to use restricted tag categories

Also prevent them locking tags
This commit is contained in:
Earlopain 2023-03-29 18:18:40 +02:00
parent cbc0fd3aa1
commit 36712c7872
No known key found for this signature in database
GPG Key ID: 6CFB948E15246897
5 changed files with 13 additions and 18 deletions

View File

@ -59,7 +59,7 @@ class TagsController < ApplicationController
def tag_params
permitted_params = [:category]
permitted_params << :is_locked if CurrentUser.is_moderator?
permitted_params << :is_locked if CurrentUser.is_admin?
params.require(:tag).permit(permitted_params)
end

View File

@ -34,8 +34,8 @@ class TagCategory
@@header_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["header"]]}]
end
def mod_only_mapping
@@mod_only_mapping ||= Hash[Danbooru.config.full_tag_config_info.map {|k,v| [k,v["mod_only"] || false]}]
def admin_only_mapping
@@admin_only_mapping ||= Hash[Danbooru.config.full_tag_config_info.map { |k,v| [k, v["admin_only"] || false] }]
end
end

View File

@ -187,8 +187,8 @@ class Tag < ApplicationRecord
def user_can_change_category?
cat = TagCategory.reverse_mapping[category]
if !CurrentUser.is_moderator? && TagCategory.mod_only_mapping[cat]
errors.add(:category, "can only used by moderators")
if !CurrentUser.is_admin? && TagCategory.admin_only_mapping[cat]
errors.add(:category, "can only used by admins")
return false
end
if cat == "lore"
@ -1092,16 +1092,16 @@ class Tag < ApplicationRecord
end
def category_editable_by?(user)
return true if user.is_moderator?
return true if user.is_admin?
return false if is_locked?
return false if TagCategory.mod_only_mapping[TagCategory.reverse_mapping[category]]
return false if TagCategory.admin_only_mapping[TagCategory.reverse_mapping[category]]
return true if post_count < Danbooru.config.tag_type_change_cutoff
false
end
def user_can_create_tag?
if name =~ /\A.*_\(lore\)\z/ && !CurrentUser.user.is_moderator?
errors.add(:base, "Can not create lore tags unless moderator")
if name =~ /\A.*_\(lore\)\z/ && !CurrentUser.user.is_admin?
errors.add(:base, "Can not create lore tags unless admin")
errors.add(:name, "is invalid")
return false
end

View File

@ -9,7 +9,7 @@
<%= f.input :category, :collection => TagCategory.canonical_mapping.to_a, :include_blank => false %>
<% end %>
<% if CurrentUser.is_moderator? %>
<% if CurrentUser.is_admin? %>
<%= f.input :is_locked, :collection => [["No", "false"], ["Yes", "true"]], :include_blank => false %>
<% end %>

View File

@ -428,7 +428,6 @@ module Danbooru
"extra" => [],
"header" => 'General',
"humanized" => nil,
"mod_only" => false,
},
"species" => {
"category" => 5,
@ -436,7 +435,6 @@ module Danbooru
"extra" => [],
"header" => 'Species',
"humanized" => nil,
"mod_only" => false,
},
"character" => {
"category" => 4,
@ -449,7 +447,6 @@ module Danbooru
"regexmap" => /^(.+?)(?:_\(.+\))?$/,
"formatstr" => "%s"
},
"mod_only" => false,
},
"copyright" => {
"category" => 3,
@ -462,7 +459,6 @@ module Danbooru
"regexmap" => //,
"formatstr" => "(%s)"
},
"mod_only" => false,
},
"artist" => {
"category" => 1,
@ -475,7 +471,6 @@ module Danbooru
"regexmap" => //,
"formatstr" => "created by %s"
},
"mod_only" => false,
},
"invalid" => {
"category" => 6,
@ -483,7 +478,7 @@ module Danbooru
"extra" => [],
"header" => 'Invalid',
"humanized" => nil,
"mod_only" => true,
"admin_only" => true,
},
"lore" => {
"category" => 8,
@ -491,7 +486,7 @@ module Danbooru
'extra' => [],
'header' => 'Lore',
'humanized' => nil,
'mod_only' => true,
"admin_only" => true,
},
"meta" => {
"category" => 7,
@ -499,7 +494,7 @@ module Danbooru
"extra" => [],
"header" => 'Meta',
"humanized" => nil,
"mod_only" => true,
"admin_only" => true,
}
}
end