users: add new owner level.

Add a new Owner user level for the site owner. Highly sensitive
operations like manually changing the passwords of other users will be
restricted to the site owner.
This commit is contained in:
evazion
2020-12-13 14:55:49 -06:00
parent 35134abe8f
commit b3ad13e6e3
7 changed files with 33 additions and 3 deletions

View File

@@ -1,4 +1,8 @@
body[data-current-user-style-usernames="true"] { body[data-current-user-style-usernames="true"] {
a.user-owner {
color: var(--user-admin-color);
}
a.user-admin { a.user-admin {
color: var(--user-admin-color); color: var(--user-admin-color);
} }

View File

@@ -27,6 +27,7 @@
margin-right: 0.25em; margin-right: 0.25em;
border-radius: 3px; border-radius: 3px;
&.user-tooltip-badge-owner { background-color: var(--user-admin-color); }
&.user-tooltip-badge-admin { background-color: var(--user-admin-color); } &.user-tooltip-badge-admin { background-color: var(--user-admin-color); }
&.user-tooltip-badge-moderator { background-color: var(--user-moderator-color); } &.user-tooltip-badge-moderator { background-color: var(--user-moderator-color); }
&.user-tooltip-badge-approver { background-color: var(--user-builder-color); } &.user-tooltip-badge-approver { background-color: var(--user-builder-color); }

View File

@@ -64,7 +64,7 @@ class UserDeletion
errors.add(:base, "Password is incorrect") errors.add(:base, "Password is incorrect")
end end
if user.level >= User::Levels::ADMIN if user.is_admin?
errors.add(:base, "Admins cannot delete their account") errors.add(:base, "Admins cannot delete their account")
end end
end end

View File

@@ -12,6 +12,7 @@ class User < ApplicationRecord
BUILDER = 32 BUILDER = 32
MODERATOR = 40 MODERATOR = 40
ADMIN = 50 ADMIN = 50
OWNER = 60
end end
# Used for `before_action :<role>_only`. Must have a corresponding `is_<role>?` method. # Used for `before_action :<role>_only`. Must have a corresponding `is_<role>?` method.
@@ -191,6 +192,10 @@ class User < ApplicationRecord
extend ActiveSupport::Concern extend ActiveSupport::Concern
module ClassMethods module ClassMethods
def owner
User.find_by!(level: Levels::ADMIN)
end
def system def system
User.find_by!(name: Danbooru.config.system_user) User.find_by!(name: Danbooru.config.system_user)
end end
@@ -208,7 +213,8 @@ class User < ApplicationRecord
"Platinum" => Levels::PLATINUM, "Platinum" => Levels::PLATINUM,
"Builder" => Levels::BUILDER, "Builder" => Levels::BUILDER,
"Moderator" => Levels::MODERATOR, "Moderator" => Levels::MODERATOR,
"Admin" => Levels::ADMIN "Admin" => Levels::ADMIN,
"Owner" => Levels::OWNER
} }
end end
@@ -235,6 +241,9 @@ class User < ApplicationRecord
when Levels::ADMIN when Levels::ADMIN
"Admin" "Admin"
when Levels::OWNER
"Owner"
else else
"" ""
end end
@@ -299,6 +308,10 @@ class User < ApplicationRecord
level >= Levels::ADMIN level >= Levels::ADMIN
end end
def is_owner?
level >= Levels::OWNER
end
def is_approver? def is_approver?
can_approve_posts? can_approve_posts?
end end

View File

@@ -56,6 +56,11 @@ FactoryBot.define do
can_approve_posts {true} can_approve_posts {true}
end end
factory(:owner_user) do
level { User::Levels::OWNER }
can_approve_posts {true}
end
factory(:uploader) do factory(:uploader) do
created_at { 2.weeks.ago } created_at { 2.weeks.ago }
end end

View File

@@ -1,6 +1,6 @@
class UserMailerPreview < ActionMailer::Preview class UserMailerPreview < ActionMailer::Preview
def dmail_notice def dmail_notice
dmail = User.admins.first.dmails.first dmail = User.system.dmails.first
UserMailer.dmail_notice(dmail) UserMailer.dmail_notice(dmail)
end end

View File

@@ -45,7 +45,14 @@ class UserTest < ActiveSupport::TestCase
end end
should "normalize its level" do should "normalize its level" do
user = FactoryBot.create(:user, :level => User::Levels::OWNER)
assert(user.is_owner?)
assert(user.is_admin?)
assert(user.is_moderator?)
assert(user.is_gold?)
user = FactoryBot.create(:user, :level => User::Levels::ADMIN) user = FactoryBot.create(:user, :level => User::Levels::ADMIN)
assert(!user.is_owner?)
assert(user.is_moderator?) assert(user.is_moderator?)
assert(user.is_gold?) assert(user.is_gold?)