inline_forms_installer 8.1.76 → 8.1.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/inline_forms_installer/installer_core.rb +104 -9
- data/lib/inline_forms_installer/version.rb +1 -1
- data/lib/installer_templates/example_app_tests/test/example_app/example_integration_test_case.rb +2 -16
- data/lib/installer_templates/example_app_tests/test/integration/example_app_single_superadmin_test.rb +77 -0
- data/lib/installer_templates/example_app_views/inline_forms/_header.html.erb +1 -1
- data/lib/installer_templates/single_superadmin/single_superadmin.rb +83 -0
- data/lib/installer_templates/single_superadmin/single_superadmin_test.rb +104 -0
- data/lib/installer_templates/single_superadmin/the_superadmin.rb +28 -0
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 1570ef1fd5fb458f1bb2528d02e44466337387f0a09528e56fc4a236f27e7b0d
|
|
4
|
+
data.tar.gz: d51d7958f540e714246c87fa85e5d7eb8da8f1723b510dc71b49d69d06002e59
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 446e5e7f783fdc8f17247dfc16dad42a16081ab671232d8eb05742cad35d68ed659cb517f3b908157a110ca7f3328b675d5ab16d6f0c392c960d94748aa120ee
|
|
7
|
+
data.tar.gz: b1e0bf6f01a7bf4c53bd5575569057182afb804ced9a7909c23ffee3c9c71d32a1e5b4c6dc411c7480dd791342da8a10d41e140ed9c59e89e4ac278246dc4edd
|
|
@@ -426,8 +426,12 @@ create_file user_cfg.model_path, <<-USER_MODEL.strip_heredoc
|
|
|
426
426
|
|
|
427
427
|
# attr_accessible :email, :password, :locale, :remember_me
|
|
428
428
|
|
|
429
|
+
# One superadmin, user 1 (app/models/concerns/single_superadmin.rb):
|
|
430
|
+
# role? and superadmin?, and the refusals in the roles callbacks.
|
|
431
|
+
include SingleSuperadmin
|
|
432
|
+
|
|
429
433
|
belongs_to :locale
|
|
430
|
-
has_and_belongs_to_many :roles
|
|
434
|
+
has_and_belongs_to_many :roles, before_add: :superadmin_role_added!, before_remove: :superadmin_role_removed!
|
|
431
435
|
|
|
432
436
|
# validations
|
|
433
437
|
validates :name, presence: true
|
|
@@ -446,10 +450,6 @@ create_file user_cfg.model_path, <<-USER_MODEL.strip_heredoc
|
|
|
446
450
|
"\#{name}"
|
|
447
451
|
end
|
|
448
452
|
|
|
449
|
-
def role?(role)
|
|
450
|
-
!!self.roles.find_by_name(role)
|
|
451
|
-
end
|
|
452
|
-
|
|
453
453
|
# Preset UI themes (inline_forms Pattern 1 theming). The integer `theme`
|
|
454
454
|
# column maps to a body class `theme-<name>`; the palettes ship in the
|
|
455
455
|
# inline_forms engine (_theme.scss). The engine layout calls
|
|
@@ -496,6 +496,46 @@ create_file user_cfg.model_path, <<-USER_MODEL.strip_heredoc
|
|
|
496
496
|
end
|
|
497
497
|
USER_MODEL
|
|
498
498
|
|
|
499
|
+
# One superadmin, user 1: the concern, its messages, and for the tests a
|
|
500
|
+
# helper that makes user 1 plus the model's own tests. The same pattern as the
|
|
501
|
+
# apps on inline_forms that had it first (has_and_belongs_to_many :roles).
|
|
502
|
+
say "- One superadmin, user 1: SingleSuperadmin concern, messages, tests..."
|
|
503
|
+
single_superadmin_root = File.join(INSTALLER_ROOT, "lib/installer_templates/single_superadmin")
|
|
504
|
+
single_superadmin_source = lambda do |name|
|
|
505
|
+
user_cfg.adapt_example_test_source(File.read(File.join(single_superadmin_root, name)))
|
|
506
|
+
.gsub(".users", ".#{user_cfg.table_name}")
|
|
507
|
+
end
|
|
508
|
+
copy_file File.join(single_superadmin_root, "single_superadmin.rb"), "app/models/concerns/single_superadmin.rb"
|
|
509
|
+
{
|
|
510
|
+
"en" => [ "This role is reserved for the one superadmin",
|
|
511
|
+
"The superadmin keeps this role and cannot be removed",
|
|
512
|
+
"This role name is reserved for the one superadmin" ],
|
|
513
|
+
"nl" => [ "Deze rol is voorbehouden aan de ene superadmin",
|
|
514
|
+
"De superadmin houdt deze rol en kan niet verwijderd worden",
|
|
515
|
+
"Deze rolnaam is voorbehouden aan de ene superadmin" ]
|
|
516
|
+
}.each do |locale, (reserved, stays, name_reserved)|
|
|
517
|
+
create_file "config/locales/single_superadmin.#{locale}.yml", <<~SUPERADMIN_LOCALE
|
|
518
|
+
# One superadmin, user 1 (app/models/concerns/single_superadmin.rb).
|
|
519
|
+
#{locale}:
|
|
520
|
+
activerecord:
|
|
521
|
+
errors:
|
|
522
|
+
models:
|
|
523
|
+
#{user_cfg.class_name.underscore}:
|
|
524
|
+
superadmin_reserved: "#{reserved}"
|
|
525
|
+
superadmin_stays: "#{stays}"
|
|
526
|
+
role:
|
|
527
|
+
superadmin_reserved: "#{name_reserved}"
|
|
528
|
+
superadmin_stays: "#{stays}"
|
|
529
|
+
SUPERADMIN_LOCALE
|
|
530
|
+
end
|
|
531
|
+
create_file "test/test_helpers/the_superadmin.rb", single_superadmin_source.call("the_superadmin.rb")
|
|
532
|
+
create_file "test/models/single_superadmin_test.rb", single_superadmin_source.call("single_superadmin_test.rb")
|
|
533
|
+
insert_into_file "test/test_helper.rb", <<~'SUPERADMIN_TEST_HELPER', after: %(require "rails/test_help"\n)
|
|
534
|
+
|
|
535
|
+
# TheSuperadmin.user / the_superadmin: user 1, the one superadmin.
|
|
536
|
+
require_relative "test_helpers/the_superadmin"
|
|
537
|
+
SUPERADMIN_TEST_HELPER
|
|
538
|
+
|
|
499
539
|
# inline_forms initializer (must exist before subsequent `rails g inline_forms`
|
|
500
540
|
# runs so the generator's `add_tab` step can inject each generated model's
|
|
501
541
|
# route token into `MODEL_TABS`). Pre-seeded with the user-model route
|
|
@@ -526,6 +566,48 @@ LOCALE_SEED
|
|
|
526
566
|
say "- Create roles"
|
|
527
567
|
generate "inline_forms", "Role name:string description:text #{user_cfg.table_name}:has_and_belongs_to_many _enabled:yes _list_order:name _presentation:\#{name}"
|
|
528
568
|
append_to_file "db/seeds.rb", "Role.create({ id: 1, name: 'superadmin', description: 'Super Admin can access all.' })\n"
|
|
569
|
+
# The superadmin role is user 1's only (SingleSuperadmin): giving it to another
|
|
570
|
+
# user, taking it from user 1, renaming or destroying it is refused, also from
|
|
571
|
+
# this side of the association.
|
|
572
|
+
gsub_file "app/models/role.rb", /^ has_and_belongs_to_many :#{user_cfg.table_name}$/, <<~ROLE_GUARDS.chomp.indent(2)
|
|
573
|
+
# The superadmin role is user 1's only (SingleSuperadmin): giving it to
|
|
574
|
+
# another #{user_cfg.class_name.underscore.humanize(capitalize: false)}, taking it from user 1, renaming or deleting it is refused,
|
|
575
|
+
# and its name is reserved: no other role is created with it or renamed to
|
|
576
|
+
# it, in any case or with spaces around it.
|
|
577
|
+
validates :name, uniqueness: { case_sensitive: false }
|
|
578
|
+
has_and_belongs_to_many :#{user_cfg.table_name}, before_add: ->(role, user) { user.superadmin_role_added!(role) },
|
|
579
|
+
#{" " * user_cfg.table_name.length}before_remove: ->(role, user) { user.superadmin_role_removed!(role) }
|
|
580
|
+
validate :superadmin_role_keeps_its_name, on: :update
|
|
581
|
+
validate :superadmin_name_is_reserved
|
|
582
|
+
before_destroy :superadmin_role_is_not_destroyed, prepend: true
|
|
583
|
+
ROLE_GUARDS
|
|
584
|
+
inject_into_file "app/models/role.rb", <<~ROLE_GUARD_METHODS.indent(2), before: /^end\s*\z/
|
|
585
|
+
|
|
586
|
+
private
|
|
587
|
+
|
|
588
|
+
def superadmin_role_keeps_its_name
|
|
589
|
+
errors.add(:base, :superadmin_stays) if name_changed? && name_was == SingleSuperadmin::SUPERADMIN_ROLE
|
|
590
|
+
end
|
|
591
|
+
|
|
592
|
+
# Refused, never rewritten. The one exception is the row itself: the
|
|
593
|
+
# existing one, or the first one, spelled exactly, when there is none yet
|
|
594
|
+
# (seeds, a fresh database).
|
|
595
|
+
def superadmin_name_is_reserved
|
|
596
|
+
return unless name.to_s.strip.casecmp?(SingleSuperadmin::SUPERADMIN_ROLE)
|
|
597
|
+
return if persisted? && name_was == SingleSuperadmin::SUPERADMIN_ROLE
|
|
598
|
+
return if new_record? && name == SingleSuperadmin::SUPERADMIN_ROLE &&
|
|
599
|
+
!Role.unscoped.exists?(name: SingleSuperadmin::SUPERADMIN_ROLE)
|
|
600
|
+
|
|
601
|
+
errors.add(:base, :superadmin_reserved)
|
|
602
|
+
end
|
|
603
|
+
|
|
604
|
+
def superadmin_role_is_not_destroyed
|
|
605
|
+
return unless name_was == SingleSuperadmin::SUPERADMIN_ROLE
|
|
606
|
+
|
|
607
|
+
errors.add(:base, :superadmin_stays)
|
|
608
|
+
throw :abort
|
|
609
|
+
end
|
|
610
|
+
ROLE_GUARD_METHODS
|
|
529
611
|
|
|
530
612
|
# Create Admin User
|
|
531
613
|
|
|
@@ -759,14 +841,27 @@ create_file "app/models/ability.rb", <<-END_ABILITY.strip_heredoc
|
|
|
759
841
|
|
|
760
842
|
user ||= #{user_cfg.class_name}.new # guest user
|
|
761
843
|
|
|
762
|
-
#
|
|
763
|
-
#
|
|
764
|
-
|
|
765
|
-
if user.role? :superadmin
|
|
844
|
+
# The superadmin is user 1 holding the role "superadmin", nobody else
|
|
845
|
+
# (SingleSuperadmin): ask superadmin?, never role?(:superadmin).
|
|
846
|
+
if user.superadmin?
|
|
766
847
|
can :manage, :all
|
|
767
848
|
else
|
|
768
849
|
# put restrictions for other users here
|
|
769
850
|
end
|
|
851
|
+
|
|
852
|
+
# At the end, so it wins over every `can` above (SingleSuperadmin).
|
|
853
|
+
unless user.superadmin?
|
|
854
|
+
# User 1 is the superadmin: nobody else changes that account (all
|
|
855
|
+
# changing actions of the inline_forms controller, its file slots and
|
|
856
|
+
# PaperTrail revert) ...
|
|
857
|
+
cannot [ :update, :edit, :destroy, :soft_delete, :soft_restore, :revert,
|
|
858
|
+
:replace_file, :remove_file, :restore_file, :purge_file ],
|
|
859
|
+
#{user_cfg.class_name}, id: SingleSuperadmin::SUPERADMIN_ID
|
|
860
|
+
# ... and nobody else sees or touches the role.
|
|
861
|
+
cannot :manage, Role, name: SingleSuperadmin::SUPERADMIN_ROLE
|
|
862
|
+
end
|
|
863
|
+
# User 1 is never destroyed or soft-deleted, also not by user 1.
|
|
864
|
+
cannot [ :destroy, :soft_delete ], #{user_cfg.class_name}, id: SingleSuperadmin::SUPERADMIN_ID
|
|
770
865
|
end
|
|
771
866
|
end
|
|
772
867
|
END_ABILITY
|
data/lib/installer_templates/example_app_tests/test/example_app/example_integration_test_case.rb
CHANGED
|
@@ -15,22 +15,8 @@ class ExampleAppIntegrationTestCase < ActionDispatch::IntegrationTest
|
|
|
15
15
|
|
|
16
16
|
private
|
|
17
17
|
|
|
18
|
+
# User 1, the one superadmin (test/test_helpers/the_superadmin.rb).
|
|
18
19
|
def example_app_admin_user
|
|
19
|
-
@example_app_admin_user ||=
|
|
20
|
-
locale = Locale.find_or_create_by!(name: "en") { |l| l.title = "English" }
|
|
21
|
-
role = Role.find_or_create_by!(name: "superadmin") { |r| r.description = "Super Admin" }
|
|
22
|
-
user = User.find_or_initialize_by(email: "admin@example.com")
|
|
23
|
-
if user.new_record?
|
|
24
|
-
user.assign_attributes(
|
|
25
|
-
name: "Admin",
|
|
26
|
-
password: "admin999",
|
|
27
|
-
password_confirmation: "admin999",
|
|
28
|
-
locale: locale
|
|
29
|
-
)
|
|
30
|
-
user.save!
|
|
31
|
-
end
|
|
32
|
-
user.roles << role unless user.roles.where(id: role.id).exists?
|
|
33
|
-
user
|
|
34
|
-
end
|
|
20
|
+
@example_app_admin_user ||= TheSuperadmin.user
|
|
35
21
|
end
|
|
36
22
|
end
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../example_app/example_integration_test_case"
|
|
4
|
+
|
|
5
|
+
# One superadmin, user 1, on the screen: the refusals of SingleSuperadmin
|
|
6
|
+
# reach the user as messages (inline_forms 8.1.77 shows a refusing
|
|
7
|
+
# association callback in the field and a refused destroy in the row).
|
|
8
|
+
class ExampleAppSingleSuperadminTest < ExampleAppIntegrationTestCase
|
|
9
|
+
setup do
|
|
10
|
+
@superadmin = example_app_admin_user
|
|
11
|
+
@role = Role.find_by!(name: SingleSuperadmin::SUPERADMIN_ROLE)
|
|
12
|
+
@other = User.create!(name: "Other", email: "other@example.com", locale: @superadmin.locale,
|
|
13
|
+
password: "other999", password_confirmation: "other999")
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def put_roles(user, role_ids)
|
|
17
|
+
frame = "user_#{user.id}_roles"
|
|
18
|
+
put user_path(user, attribute: "roles", form_element: "check_list", update: frame),
|
|
19
|
+
params: { roles: role_ids.index_with { 1 }.transform_keys(&:to_s) },
|
|
20
|
+
headers: { "Turbo-Frame" => frame, "Accept" => "text/html" }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test "the signed-in user is user 1, the superadmin" do
|
|
24
|
+
assert_equal SingleSuperadmin::SUPERADMIN_ID, @superadmin.id
|
|
25
|
+
assert @superadmin.superadmin?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
test "a second superadmin is refused with a message" do
|
|
29
|
+
put_roles(@other, [ @role.id ])
|
|
30
|
+
|
|
31
|
+
assert_response :success
|
|
32
|
+
assert_includes @response.body, "reserved for the one superadmin"
|
|
33
|
+
assert_not @other.reload.role?(:superadmin)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test "user 1 cannot drop the role, and the screen says so" do
|
|
37
|
+
put_roles(@superadmin, [])
|
|
38
|
+
|
|
39
|
+
assert_response :success
|
|
40
|
+
assert_includes @response.body, "keeps this role"
|
|
41
|
+
assert @superadmin.reload.superadmin?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test "the superadmin role is not destroyed, the row says why" do
|
|
45
|
+
frame = "role_#{@role.id}"
|
|
46
|
+
delete role_path(@role, update: frame), headers: { "Turbo-Frame" => frame, "Accept" => "text/html" }
|
|
47
|
+
|
|
48
|
+
assert_response :unprocessable_entity
|
|
49
|
+
assert_includes @response.body, "keeps this role"
|
|
50
|
+
assert Role.exists?(@role.id)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
test "user 1 is not destroyed" do
|
|
54
|
+
frame = "user_#{@superadmin.id}"
|
|
55
|
+
delete user_path(@superadmin, update: frame), headers: { "Turbo-Frame" => frame, "Accept" => "text/html" }
|
|
56
|
+
|
|
57
|
+
# The Ability refuses before the model does: CanCan's message on the page
|
|
58
|
+
# the app sends the user to.
|
|
59
|
+
assert_redirected_to root_url
|
|
60
|
+
assert_equal I18n.t("unauthorized.default", default: "You are not authorized to access this page."), flash[:notice]
|
|
61
|
+
assert User.exists?(@superadmin.id)
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
test "the model refuses destroying user 1 when the Ability lets it through" do
|
|
65
|
+
assert_equal false, @superadmin.destroy
|
|
66
|
+
assert_includes @superadmin.errors.full_messages, "The superadmin keeps this role and cannot be removed"
|
|
67
|
+
assert User.exists?(@superadmin.id)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
test "another user is destroyed as before" do
|
|
71
|
+
frame = "user_#{@other.id}"
|
|
72
|
+
delete user_path(@other, update: frame), headers: { "Turbo-Frame" => frame, "Accept" => "text/html" }
|
|
73
|
+
|
|
74
|
+
assert_response :success
|
|
75
|
+
assert_not User.exists?(@other.id)
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
</div>
|
|
10
10
|
<div class="top-bar-right">
|
|
11
11
|
<ul class="dropdown menu" data-dropdown-menu>
|
|
12
|
-
<% if current_user.role?(:admin) ||
|
|
12
|
+
<% if current_user.role?(:admin) || InlineForms.superadmin?(current_user) %>
|
|
13
13
|
<li>
|
|
14
14
|
<a href="#"><%= t 'common.more' %></a>
|
|
15
15
|
<ul class="menu">
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# One superadmin, and it is the user with id 1 (generated by inline_forms).
|
|
2
|
+
#
|
|
3
|
+
# The same rule as the apps on inline_forms that came before this one
|
|
4
|
+
# (has_and_belongs_to_many :roles, as generated):
|
|
5
|
+
#
|
|
6
|
+
# * superadmin? is true for user 1 holding the role "superadmin" and for
|
|
7
|
+
# nobody else. Ability, controllers and views ask superadmin?, never
|
|
8
|
+
# role?(:superadmin): the role on any other user is worth nothing.
|
|
9
|
+
# * Giving the role to anybody else is refused with a message. A join row is
|
|
10
|
+
# written as soon as `role_ids=` or `roles <<` runs on a saved user, before
|
|
11
|
+
# any validation, so the refusal sits in the association callbacks of both
|
|
12
|
+
# sides (the user's roles and the role's users): it raises
|
|
13
|
+
# SingleSuperadmin::Refused (an ActiveRecord::RecordInvalid with the
|
|
14
|
+
# message in the user's errors) before the row is written. The
|
|
15
|
+
# inline_forms controller shows that message in the field.
|
|
16
|
+
# * User 1 keeps the role and cannot be destroyed or soft-deleted, so the
|
|
17
|
+
# app is never left without a superadmin.
|
|
18
|
+
#
|
|
19
|
+
# Who may change user 1 and who sees the role: the end of Ability.
|
|
20
|
+
module SingleSuperadmin
|
|
21
|
+
extend ActiveSupport::Concern
|
|
22
|
+
|
|
23
|
+
SUPERADMIN_ID = 1
|
|
24
|
+
SUPERADMIN_ROLE = "superadmin"
|
|
25
|
+
|
|
26
|
+
class Refused < ActiveRecord::RecordInvalid; end
|
|
27
|
+
|
|
28
|
+
included do
|
|
29
|
+
validate :superadmin_role_is_reserved
|
|
30
|
+
validate :superadmin_is_not_soft_deleted
|
|
31
|
+
before_destroy :superadmin_is_not_destroyed, prepend: true
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# By name, and false without roles.
|
|
35
|
+
def role?(name)
|
|
36
|
+
roles.exists?(name: name.to_s)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def superadmin?
|
|
40
|
+
id == SUPERADMIN_ID && role?(SUPERADMIN_ROLE)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# before_add of the user's roles and of the role's users.
|
|
44
|
+
def superadmin_role_added!(role)
|
|
45
|
+
return unless role.name == SUPERADMIN_ROLE && id != SUPERADMIN_ID
|
|
46
|
+
|
|
47
|
+
refuse!(:base, :superadmin_reserved)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# before_remove of the user's roles and of the role's users.
|
|
51
|
+
def superadmin_role_removed!(role)
|
|
52
|
+
return unless role.name == SUPERADMIN_ROLE && id == SUPERADMIN_ID
|
|
53
|
+
|
|
54
|
+
refuse!(:base, :superadmin_stays)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
private
|
|
58
|
+
|
|
59
|
+
def refuse!(attribute, key)
|
|
60
|
+
errors.add(attribute, key)
|
|
61
|
+
raise Refused, self
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# A role given in memory (a new record) or found on a user other than 1 (old
|
|
65
|
+
# data, a database edit): the record is not saved.
|
|
66
|
+
def superadmin_role_is_reserved
|
|
67
|
+
return if id == SUPERADMIN_ID
|
|
68
|
+
|
|
69
|
+
errors.add(:base, :superadmin_reserved) if roles.any? { |role| role.name == SUPERADMIN_ROLE }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Only for a model that includes InlineForms::SoftDeletable.
|
|
73
|
+
def superadmin_is_not_soft_deleted
|
|
74
|
+
errors.add(:base, :superadmin_stays) if id == SUPERADMIN_ID && try(:deleted_at).present?
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def superadmin_is_not_destroyed
|
|
78
|
+
return unless id == SUPERADMIN_ID
|
|
79
|
+
|
|
80
|
+
errors.add(:base, :superadmin_stays)
|
|
81
|
+
throw :abort
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
# One superadmin, user 1 (app/models/concerns/single_superadmin.rb and the
|
|
6
|
+
# end of app/models/ability.rb).
|
|
7
|
+
class SingleSuperadminTest < ActiveSupport::TestCase
|
|
8
|
+
include TheSuperadmin
|
|
9
|
+
|
|
10
|
+
setup do
|
|
11
|
+
@superadmin = the_superadmin
|
|
12
|
+
@role = Role.find_by!(name: SingleSuperadmin::SUPERADMIN_ROLE)
|
|
13
|
+
@other = User.create!(name: "Other", email: "other@example.com", locale: @superadmin.locale,
|
|
14
|
+
password: "other999", password_confirmation: "other999")
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
test "only user 1 holding the role is the superadmin" do
|
|
18
|
+
assert @superadmin.superadmin?
|
|
19
|
+
assert_not @other.superadmin?
|
|
20
|
+
assert_not User.new.superadmin?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test "role? asks by name and is false without roles" do
|
|
24
|
+
assert @superadmin.role?(:superadmin)
|
|
25
|
+
assert_not @other.role?(:superadmin)
|
|
26
|
+
assert_not @other.role?(:no_such_role)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
test "giving the role to another user is refused, from either side" do
|
|
30
|
+
error = assert_raises(SingleSuperadmin::Refused) { @other.role_ids = [ @role.id ] }
|
|
31
|
+
assert_includes error.record.errors.full_messages.join, "reserved"
|
|
32
|
+
assert_raises(SingleSuperadmin::Refused) { @role.users << @other }
|
|
33
|
+
assert_not @other.reload.roles.include?(@role)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
test "a new user is refused the role when it is given" do
|
|
37
|
+
assert_raises(SingleSuperadmin::Refused) do
|
|
38
|
+
User.new(name: "New", email: "new@example.com", locale: @superadmin.locale,
|
|
39
|
+
password: "new99999", password_confirmation: "new99999", roles: [ @role ])
|
|
40
|
+
end
|
|
41
|
+
assert_not User.exists?(email: "new@example.com")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test "a user who holds the role by a database edit is not saved" do
|
|
45
|
+
give_role_by_database_edit(@other)
|
|
46
|
+
assert_not @other.reload.valid?
|
|
47
|
+
assert_includes @other.errors.full_messages.join, "reserved"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
test "user 1 keeps the role, from either side" do
|
|
51
|
+
assert_raises(SingleSuperadmin::Refused) { @superadmin.role_ids = [] }
|
|
52
|
+
assert_raises(SingleSuperadmin::Refused) { @role.users.delete(@superadmin) }
|
|
53
|
+
assert @superadmin.reload.superadmin?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
test "user 1 is not destroyed" do
|
|
57
|
+
assert_equal false, @superadmin.destroy
|
|
58
|
+
assert User.exists?(SingleSuperadmin::SUPERADMIN_ID)
|
|
59
|
+
assert_not_empty @superadmin.errors[:base]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
test "the superadmin role keeps its name and is not destroyed" do
|
|
63
|
+
assert_not @role.update(name: "boss")
|
|
64
|
+
assert_equal false, @role.reload.destroy
|
|
65
|
+
assert Role.exists?(name: SingleSuperadmin::SUPERADMIN_ROLE)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
test "no other role takes the name superadmin, in any spelling" do
|
|
69
|
+
[ "superadmin", "SuperAdmin", " superadmin " ].each do |name|
|
|
70
|
+
role = Role.new(name: name)
|
|
71
|
+
assert_not role.valid?, name
|
|
72
|
+
end
|
|
73
|
+
editor = Role.create!(name: "editor")
|
|
74
|
+
assert_not editor.update(name: "Superadmin")
|
|
75
|
+
assert_equal "editor", editor.reload.name
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
test "another holder of the role, by a database edit, gets nothing" do
|
|
79
|
+
give_role_by_database_edit(@other)
|
|
80
|
+
ability = Ability.new(@other.reload)
|
|
81
|
+
assert @other.role?(:superadmin)
|
|
82
|
+
assert_not @other.superadmin?
|
|
83
|
+
assert ability.cannot?(:update, @superadmin)
|
|
84
|
+
assert ability.cannot?(:soft_delete, @superadmin)
|
|
85
|
+
assert ability.cannot?(:read, @role)
|
|
86
|
+
assert ability.cannot?(:manage, User)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
test "the superadmin manages everything but destroying user 1" do
|
|
90
|
+
ability = Ability.new(@superadmin)
|
|
91
|
+
assert ability.can?(:update, @superadmin)
|
|
92
|
+
assert ability.can?(:update, @role)
|
|
93
|
+
assert ability.can?(:destroy, @other)
|
|
94
|
+
assert ability.cannot?(:destroy, @superadmin)
|
|
95
|
+
assert ability.cannot?(:soft_delete, @superadmin)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def give_role_by_database_edit(user)
|
|
101
|
+
roles = User.reflect_on_association(:roles)
|
|
102
|
+
User.connection.execute("INSERT INTO #{roles.join_table} (role_id, #{roles.foreign_key}) VALUES (#{@role.id}, #{user.id})")
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# THE superadmin: user 1 holding the role "superadmin" (SingleSuperadmin).
|
|
4
|
+
# Nobody else can be made superadmin, so tests sign in as this user. Made the
|
|
5
|
+
# first time it is asked for, found after; safe to call more than once in a
|
|
6
|
+
# test. Attributes given are updated on it, inside the test's transaction.
|
|
7
|
+
module TheSuperadmin
|
|
8
|
+
EMAIL = "admin@example.com"
|
|
9
|
+
PASSWORD = "admin999"
|
|
10
|
+
|
|
11
|
+
def self.user(**attributes)
|
|
12
|
+
role = Role.find_or_create_by!(name: SingleSuperadmin::SUPERADMIN_ROLE) { |r| r.description = "Super Admin can access all." }
|
|
13
|
+
user = User.find_by(id: SingleSuperadmin::SUPERADMIN_ID) || create_user_one
|
|
14
|
+
user.update!(attributes) if attributes.any?
|
|
15
|
+
user.roles << role unless user.roles.include?(role)
|
|
16
|
+
user
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.create_user_one
|
|
20
|
+
locale = Locale.find_or_create_by!(name: "en") { |l| l.title = "English" }
|
|
21
|
+
User.create!(id: SingleSuperadmin::SUPERADMIN_ID, name: "Admin", email: EMAIL, locale: locale,
|
|
22
|
+
password: PASSWORD, password_confirmation: PASSWORD)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def the_superadmin(**attributes)
|
|
26
|
+
TheSuperadmin.user(**attributes)
|
|
27
|
+
end
|
|
28
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: inline_forms_installer
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 8.1.
|
|
4
|
+
version: 8.1.78
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ace Suares
|
|
@@ -129,6 +129,7 @@ files:
|
|
|
129
129
|
- lib/installer_templates/example_app_tests/test/integration/example_app_showcase_row_turbo_test.rb
|
|
130
130
|
- lib/installer_templates/example_app_tests/test/integration/example_app_showcase_text_fields_test.rb
|
|
131
131
|
- lib/installer_templates/example_app_tests/test/integration/example_app_showcase_validation_hints_test.rb
|
|
132
|
+
- lib/installer_templates/example_app_tests/test/integration/example_app_single_superadmin_test.rb
|
|
132
133
|
- lib/installer_templates/example_app_tests/test/integration/example_app_stylesheet_test.rb
|
|
133
134
|
- lib/installer_templates/example_app_tests/test/integration/example_app_turbo_layout_test.rb
|
|
134
135
|
- lib/installer_templates/example_app_tests/test/integration/example_app_user_locale_test.rb
|
|
@@ -145,6 +146,9 @@ files:
|
|
|
145
146
|
- lib/installer_templates/example_app_views/owners/_owner_tabs.html.erb
|
|
146
147
|
- lib/installer_templates/example_app_views/owners/show_with_tabs.html.erb
|
|
147
148
|
- lib/installer_templates/example_app_views/showcase_reports/show.html.erb
|
|
149
|
+
- lib/installer_templates/single_superadmin/single_superadmin.rb
|
|
150
|
+
- lib/installer_templates/single_superadmin/single_superadmin_test.rb
|
|
151
|
+
- lib/installer_templates/single_superadmin/the_superadmin.rb
|
|
148
152
|
- lib/installer_templates/unicorn/production.rb
|
|
149
153
|
homepage: http://github.com/acesuares/inline_forms
|
|
150
154
|
licenses:
|