easy-admin-rails 0.2.6 → 0.2.7
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/app/assets/builds/easy_admin.base.js +7 -0
- data/app/assets/builds/easy_admin.base.js.map +2 -2
- data/app/assets/builds/easy_admin.css +207 -35
- data/app/components/easy_admin/fields/form/belongs_to_component.rb +0 -1
- data/app/components/easy_admin/form_layout_component.rb +553 -0
- data/app/components/easy_admin/permissions/user_role_permissions_component.rb +1 -3
- data/app/components/easy_admin/show_layout_component.rb +694 -24
- data/app/controllers/easy_admin/application_controller.rb +0 -5
- data/app/controllers/easy_admin/batch_actions_controller.rb +0 -1
- data/app/controllers/easy_admin/concerns/inline_field_editing.rb +4 -11
- data/app/controllers/easy_admin/concerns/resource_loading.rb +10 -9
- data/app/controllers/easy_admin/concerns/resource_pagination.rb +3 -0
- data/app/controllers/easy_admin/dashboards_controller.rb +0 -1
- data/app/controllers/easy_admin/resources_controller.rb +1 -5
- data/app/controllers/easy_admin/row_actions_controller.rb +1 -4
- data/app/helpers/easy_admin/fields_helper.rb +8 -22
- data/app/javascript/easy_admin/controllers/infinite_scroll_controller.js +12 -0
- data/app/views/easy_admin/resources/edit.html.erb +2 -2
- data/app/views/easy_admin/resources/new.html.erb +2 -2
- data/app/views/easy_admin/resources/show.html.erb +3 -1
- data/lib/easy_admin/field.rb +3 -2
- data/lib/easy_admin/layouts/builders/base_layout_builder.rb +245 -0
- data/lib/easy_admin/layouts/builders/form_layout_builder.rb +208 -0
- data/lib/easy_admin/layouts/builders/index_layout_builder.rb +22 -0
- data/lib/easy_admin/layouts/builders/show_layout_builder.rb +199 -0
- data/lib/easy_admin/layouts/dsl.rb +200 -0
- data/lib/easy_admin/layouts/layout_context.rb +189 -0
- data/lib/easy_admin/layouts/nodes/base_node.rb +88 -0
- data/lib/easy_admin/layouts/nodes/divider.rb +27 -0
- data/lib/easy_admin/layouts/nodes/field_node.rb +57 -0
- data/lib/easy_admin/layouts/nodes/grid.rb +60 -0
- data/lib/easy_admin/layouts/nodes/render_node.rb +41 -0
- data/lib/easy_admin/layouts/nodes/root.rb +25 -0
- data/lib/easy_admin/layouts/nodes/section.rb +46 -0
- data/lib/easy_admin/layouts/nodes/spacer.rb +17 -0
- data/lib/easy_admin/layouts/nodes/stubs.rb +109 -0
- data/lib/easy_admin/layouts/nodes/tab.rb +40 -0
- data/lib/easy_admin/layouts/nodes/tabs.rb +40 -0
- data/lib/easy_admin/layouts.rb +28 -0
- data/lib/easy_admin/permissions/resource_permissions.rb +1 -5
- data/lib/easy_admin/resource/base.rb +2 -2
- data/lib/easy_admin/resource/dsl.rb +2 -11
- data/lib/easy_admin/resource/field_registry.rb +58 -2
- data/lib/easy_admin/resource.rb +0 -9
- data/lib/easy_admin/resource_modules.rb +21 -4
- data/lib/easy_admin/version.rb +1 -1
- data/lib/generators/easy_admin/permissions/install_generator.rb +0 -10
- data/lib/generators/easy_admin/permissions/templates/migrations/create_permission_tables.rb +33 -3
- metadata +21 -9
- data/lib/easy_admin/resource/form_builder.rb +0 -123
- data/lib/easy_admin/resource/layout_builder.rb +0 -249
- data/lib/easy_admin/resource/show_builder.rb +0 -359
- data/lib/generators/easy_admin/permissions/templates/migrations/update_users_for_permissions.rb +0 -6
- data/lib/generators/easy_admin/rbac/rbac_generator.rb +0 -244
- data/lib/generators/easy_admin/rbac/templates/add_rbac_to_admin_users.rb +0 -23
- data/lib/generators/easy_admin/rbac/templates/super_admin.rb +0 -34
@@ -1,244 +0,0 @@
|
|
1
|
-
require 'rails/generators'
|
2
|
-
require 'rails/generators/migration'
|
3
|
-
|
4
|
-
module EasyAdmin
|
5
|
-
module Generators
|
6
|
-
class RbacGenerator < Rails::Generators::Base
|
7
|
-
include Rails::Generators::Migration
|
8
|
-
|
9
|
-
source_root File.expand_path('../templates', __FILE__)
|
10
|
-
|
11
|
-
desc 'Generate EasyAdmin Role-Based Access Control setup'
|
12
|
-
|
13
|
-
def self.next_migration_number(path)
|
14
|
-
Time.current.utc.strftime("%Y%m%d%H%M%S")
|
15
|
-
end
|
16
|
-
|
17
|
-
def copy_migration
|
18
|
-
migration_template(
|
19
|
-
'add_rbac_to_admin_users.rb',
|
20
|
-
'db/migrate/add_rbac_to_admin_users.rb',
|
21
|
-
migration_version: migration_version
|
22
|
-
)
|
23
|
-
end
|
24
|
-
|
25
|
-
def create_role_models
|
26
|
-
template 'super_admin.rb', 'app/models/easy_admin/super_admin.rb'
|
27
|
-
template 'admin.rb', 'app/models/easy_admin/admin.rb'
|
28
|
-
template 'editor.rb', 'app/models/easy_admin/editor.rb'
|
29
|
-
template 'viewer.rb', 'app/models/easy_admin/viewer.rb'
|
30
|
-
end
|
31
|
-
|
32
|
-
def update_admin_user_model
|
33
|
-
inject_into_file 'app/models/easy_admin/admin_user.rb', after: "self.table_name = \"easy_admin_admin_users\"\n" do
|
34
|
-
<<-RUBY
|
35
|
-
|
36
|
-
# STI configuration for role-based access
|
37
|
-
self.inheritance_column = :type
|
38
|
-
|
39
|
-
# Associations
|
40
|
-
has_many :audit_logs, class_name: 'EasyAdmin::AuditLog', foreign_key: :admin_user_id, dependent: :destroy
|
41
|
-
|
42
|
-
# Scopes
|
43
|
-
scope :by_role, ->(role) { where(type: "EasyAdmin::\#{role}") }
|
44
|
-
|
45
|
-
# Cache permissions per request
|
46
|
-
def permissions_cache
|
47
|
-
@permissions_cache ||= {}
|
48
|
-
end
|
49
|
-
|
50
|
-
# Core authorization methods (to be overridden by subclasses)
|
51
|
-
def can_access_resource?(resource_name)
|
52
|
-
accessible_resources.include?(resource_name.to_s)
|
53
|
-
end
|
54
|
-
|
55
|
-
def can_perform_action?(resource_name, action)
|
56
|
-
return false unless can_access_resource?(resource_name)
|
57
|
-
allowed_actions_for(resource_name).include?(action.to_s)
|
58
|
-
end
|
59
|
-
|
60
|
-
def can_access_field?(resource_name, field_name, action = :read)
|
61
|
-
return false unless can_access_resource?(resource_name)
|
62
|
-
!restricted_fields_for(resource_name, action).include?(field_name.to_s)
|
63
|
-
end
|
64
|
-
|
65
|
-
# Override in subclasses
|
66
|
-
def accessible_resources
|
67
|
-
[]
|
68
|
-
end
|
69
|
-
|
70
|
-
def allowed_actions_for(resource_name)
|
71
|
-
[:index, :show]
|
72
|
-
end
|
73
|
-
|
74
|
-
def restricted_fields_for(resource_name, action)
|
75
|
-
[]
|
76
|
-
end
|
77
|
-
|
78
|
-
def apply_resource_scope(resource_name, scope)
|
79
|
-
scope
|
80
|
-
end
|
81
|
-
|
82
|
-
# Helper for caching expensive operations
|
83
|
-
def cached_permission(key, &block)
|
84
|
-
permissions_cache[key] ||= block.call
|
85
|
-
end
|
86
|
-
RUBY
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def create_permission_cache
|
91
|
-
template 'permission_cache.rb', 'lib/easy_admin/permission_cache.rb'
|
92
|
-
end
|
93
|
-
|
94
|
-
def create_audit_log_model
|
95
|
-
template 'audit_log.rb', 'app/models/easy_admin/audit_log.rb'
|
96
|
-
|
97
|
-
migration_template(
|
98
|
-
'create_audit_logs.rb',
|
99
|
-
'db/migrate/create_easy_admin_audit_logs.rb',
|
100
|
-
migration_version: migration_version
|
101
|
-
)
|
102
|
-
end
|
103
|
-
|
104
|
-
def update_application_controller
|
105
|
-
inject_into_file 'app/controllers/easy_admin/application_controller.rb',
|
106
|
-
before: "include Pagy::Backend" do
|
107
|
-
<<-RUBY
|
108
|
-
class UnauthorizedError < StandardError; end
|
109
|
-
|
110
|
-
RUBY
|
111
|
-
end
|
112
|
-
|
113
|
-
inject_into_file 'app/controllers/easy_admin/application_controller.rb',
|
114
|
-
after: "before_action :authenticate_easy_admin_admin_user!\n" do
|
115
|
-
<<-RUBY
|
116
|
-
before_action :check_resource_access!, except: [:index]
|
117
|
-
|
118
|
-
rescue_from UnauthorizedError do |exception|
|
119
|
-
respond_to do |format|
|
120
|
-
format.html { redirect_to easy_admin.root_path, alert: 'You are not authorized to access this resource.' }
|
121
|
-
format.turbo_stream do
|
122
|
-
render turbo_stream: turbo_stream.replace("notifications",
|
123
|
-
EasyAdmin::NotificationComponent.new(
|
124
|
-
message: 'You are not authorized to access this resource',
|
125
|
-
type: :error
|
126
|
-
).call
|
127
|
-
)
|
128
|
-
end
|
129
|
-
end
|
130
|
-
end
|
131
|
-
RUBY
|
132
|
-
end
|
133
|
-
|
134
|
-
inject_into_file 'app/controllers/easy_admin/application_controller.rb',
|
135
|
-
before: "def authenticate_admin_user!" do
|
136
|
-
<<-RUBY
|
137
|
-
def check_resource_access!
|
138
|
-
return unless defined?(@resource_class)
|
139
|
-
|
140
|
-
unless current_admin_user.can_access_resource?(@resource_class.resource_name)
|
141
|
-
raise UnauthorizedError, "Access denied to \#{@resource_class.resource_name}"
|
142
|
-
end
|
143
|
-
|
144
|
-
check_action_permission!
|
145
|
-
end
|
146
|
-
|
147
|
-
def check_action_permission!
|
148
|
-
action_map = {
|
149
|
-
'index' => :index,
|
150
|
-
'show' => :show,
|
151
|
-
'new' => :new,
|
152
|
-
'create' => :create,
|
153
|
-
'edit' => :edit,
|
154
|
-
'update' => :update,
|
155
|
-
'destroy' => :destroy,
|
156
|
-
'execute' => :batch_action
|
157
|
-
}
|
158
|
-
|
159
|
-
mapped_action = action_map[action_name] || action_name.to_sym
|
160
|
-
|
161
|
-
unless current_admin_user.can_perform_action?(@resource_class.resource_name, mapped_action)
|
162
|
-
raise UnauthorizedError, "Cannot perform \#{mapped_action} on \#{@resource_class.resource_name}"
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
def apply_role_scoping(scope)
|
167
|
-
current_admin_user.apply_resource_scope(@resource_class.resource_name, scope)
|
168
|
-
end
|
169
|
-
|
170
|
-
def permission_cache
|
171
|
-
@permission_cache ||= EasyAdmin::PermissionCache.new(current_admin_user)
|
172
|
-
end
|
173
|
-
|
174
|
-
RUBY
|
175
|
-
end
|
176
|
-
|
177
|
-
inject_into_file 'app/controllers/easy_admin/application_controller.rb',
|
178
|
-
after: "helper_method :current_admin_user, :admin_user_signed_in?" do
|
179
|
-
", :permission_cache"
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def create_seeds
|
184
|
-
create_file 'db/seeds/easy_admin_rbac.rb', <<~RUBY
|
185
|
-
# Create default admin users with different roles
|
186
|
-
if Rails.env.development?
|
187
|
-
# Super Admin - full access
|
188
|
-
EasyAdmin::SuperAdmin.find_or_create_by(email: 'super@example.com') do |admin|
|
189
|
-
admin.password = 'password'
|
190
|
-
admin.password_confirmation = 'password'
|
191
|
-
admin.first_name = 'Super'
|
192
|
-
admin.last_name = 'Admin'
|
193
|
-
admin.confirmed_at = Time.current
|
194
|
-
end
|
195
|
-
|
196
|
-
# Standard Admin - most access except system settings
|
197
|
-
EasyAdmin::Admin.find_or_create_by(email: 'admin@example.com') do |admin|
|
198
|
-
admin.password = 'password'
|
199
|
-
admin.password_confirmation = 'password'
|
200
|
-
admin.first_name = 'Admin'
|
201
|
-
admin.last_name = 'User'
|
202
|
-
admin.confirmed_at = Time.current
|
203
|
-
end
|
204
|
-
|
205
|
-
# Editor - content management only
|
206
|
-
EasyAdmin::Editor.find_or_create_by(email: 'editor@example.com') do |admin|
|
207
|
-
admin.password = 'password'
|
208
|
-
admin.password_confirmation = 'password'
|
209
|
-
admin.first_name = 'Content'
|
210
|
-
admin.last_name = 'Editor'
|
211
|
-
admin.confirmed_at = Time.current
|
212
|
-
end
|
213
|
-
|
214
|
-
# Viewer - read-only access
|
215
|
-
EasyAdmin::Viewer.find_or_create_by(email: 'viewer@example.com') do |admin|
|
216
|
-
admin.password = 'password'
|
217
|
-
admin.password_confirmation = 'password'
|
218
|
-
admin.first_name = 'Read'
|
219
|
-
admin.last_name = 'Only'
|
220
|
-
admin.confirmed_at = Time.current
|
221
|
-
end
|
222
|
-
|
223
|
-
puts "EasyAdmin RBAC users created!"
|
224
|
-
puts "Roles available:"
|
225
|
-
puts " SuperAdmin - super@example.com / password (full access)"
|
226
|
-
puts " Admin - admin@example.com / password (standard admin)"
|
227
|
-
puts " Editor - editor@example.com / password (content only)"
|
228
|
-
puts " Viewer - viewer@example.com / password (read-only)"
|
229
|
-
end
|
230
|
-
RUBY
|
231
|
-
end
|
232
|
-
|
233
|
-
def show_readme
|
234
|
-
readme 'RBAC_README'
|
235
|
-
end
|
236
|
-
|
237
|
-
private
|
238
|
-
|
239
|
-
def migration_version
|
240
|
-
"[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|
244
|
-
end
|
@@ -1,23 +0,0 @@
|
|
1
|
-
class AddRbacToAdminUsers < ActiveRecord::Migration<%= migration_version %>
|
2
|
-
def change
|
3
|
-
# Add type column for STI
|
4
|
-
add_column :easy_admin_admin_users, :type, :string
|
5
|
-
add_column :easy_admin_admin_users, :permissions, :json, default: {}
|
6
|
-
add_column :easy_admin_admin_users, :resource_access, :json, default: {}
|
7
|
-
|
8
|
-
# Add indexes for performance
|
9
|
-
add_index :easy_admin_admin_users, :type
|
10
|
-
add_index :easy_admin_admin_users, [:type, :locked_at]
|
11
|
-
|
12
|
-
# Migrate existing users to Admin type
|
13
|
-
reversible do |dir|
|
14
|
-
dir.up do
|
15
|
-
execute <<-SQL
|
16
|
-
UPDATE easy_admin_admin_users
|
17
|
-
SET type = 'EasyAdmin::Admin'
|
18
|
-
WHERE type IS NULL
|
19
|
-
SQL
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module EasyAdmin
|
2
|
-
class SuperAdmin < AdminUser
|
3
|
-
def accessible_resources
|
4
|
-
cached_permission(:resources) do
|
5
|
-
EasyAdmin::ResourceRegistry.all_resources.map(&:resource_name)
|
6
|
-
end
|
7
|
-
end
|
8
|
-
|
9
|
-
def allowed_actions_for(resource_name)
|
10
|
-
[:index, :show, :new, :create, :edit, :update, :destroy, :batch_action, :row_action].map(&:to_s)
|
11
|
-
end
|
12
|
-
|
13
|
-
def restricted_fields_for(resource_name, action)
|
14
|
-
[] # No restrictions for super admin
|
15
|
-
end
|
16
|
-
|
17
|
-
def apply_resource_scope(resource_name, scope)
|
18
|
-
scope # No filtering for super admin
|
19
|
-
end
|
20
|
-
|
21
|
-
# Super admin specific methods
|
22
|
-
def super_admin?
|
23
|
-
true
|
24
|
-
end
|
25
|
-
|
26
|
-
def role_name
|
27
|
-
'Super Admin'
|
28
|
-
end
|
29
|
-
|
30
|
-
def role_badge_color
|
31
|
-
'red'
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|