easy-admin-rails 0.2.1 → 0.2.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a486a14f84c7adc080acaf93dd573f6bcd2fbe6c2118e4c37d58e3b34b254ff
4
- data.tar.gz: d7e4324d5c53c1dd23ff1c3a2cd7df00d57feecea70fa4cb5a8e8e52520bb56c
3
+ metadata.gz: 114db756f935751ce2446ada0465829ebc368580af18e11c0e4de7d736b3e57b
4
+ data.tar.gz: c12a7bcdf614b7fedc87ce6cc942a26094b6eb3a62b4bed93453cab3a79ea809
5
5
  SHA512:
6
- metadata.gz: 1325f6e7cf083a5a512bc8db44e3136c17dd4e4482fd7a4f08ec09d656108efe9767ed564a358cfbd5156644eeb9bb5e493b2b85254ac7e93121293dcb4875b7
7
- data.tar.gz: 39933c874954b69d2678bc6e2128ac07d3dafa4578cd68e394156a1c5fa0f2bb923e1fb43f0b45f3dff49199208b16603855d1cef8fc4411a8645b1c57d29a0c
6
+ metadata.gz: fe827e1adac715c9bf5db4caa5a7dc8749b3b32c65c0adc4362591c5f2845c454e4e7c7b623d3e44eb892dc561854ef5087cf4e073a036127b680957e3c44be2
7
+ data.tar.gz: 85b294f85b2d244fa8e22ab4bc0323100a54f052f7206bcd9c425881fa5ed907ee4e66ab5b575dd751164ac3f6fd36694e4e72f88afbc64d43cd5f75881cfeb0
@@ -13,7 +13,7 @@ module EasyAdmin
13
13
 
14
14
  # Get actual permissions from permissions_cache
15
15
  @user_permissions = get_user_permissions_from_cache(user)
16
- @all_permissions = EasyAdmin::Permissions::Permission.all.order(:resource_type, :action)
16
+ @available_resources = EasyAdmin::Permissions.available_resources
17
17
 
18
18
  Rails.logger.debug "UserRolePermissionsComponent: user=#{@user&.id}, role=#{@current_role&.name}, cached_permissions=#{@user_permissions.size}"
19
19
  end
@@ -59,7 +59,8 @@ module EasyAdmin
59
59
 
60
60
  div do
61
61
  span(class: "font-medium text-gray-500") { "Permissions Count:" }
62
- p(class: "text-gray-900") { @current_role.permissions.count.to_s }
62
+ enabled_count = @user_permissions.count { |name, granted| granted == "true" || granted == true }
63
+ p(class: "text-gray-900") { enabled_count.to_s }
63
64
  end
64
65
 
65
66
  div do
@@ -85,12 +86,11 @@ module EasyAdmin
85
86
  end
86
87
 
87
88
  # Get permissions that are granted (true) from cache
88
- granted_permission_names = @user_permissions.select { |name, granted| granted == "true" }.keys
89
- granted_permissions = @all_permissions.select { |p| granted_permission_names.include?(p.name) }
89
+ granted_permission_names = @user_permissions.select { |name, granted| granted == "true" || granted == true }.keys
90
90
 
91
- if granted_permissions.any?
91
+ if granted_permission_names.any?
92
92
  # Group permissions by resource type
93
- grouped_permissions = granted_permissions.group_by(&:resource_type)
93
+ grouped_permissions = group_permissions_by_resource(granted_permission_names)
94
94
 
95
95
  div(class: "space-y-6") do
96
96
  grouped_permissions.each do |resource_type, resource_permissions|
@@ -115,14 +115,16 @@ module EasyAdmin
115
115
 
116
116
  # Permissions grid
117
117
  div(class: "grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-3") do
118
- permissions.each do |permission|
119
- render_permission_card(permission)
118
+ permissions.each do |permission_name|
119
+ render_permission_card(permission_name)
120
120
  end
121
121
  end
122
122
  end
123
123
  end
124
124
 
125
- def render_permission_card(permission)
125
+ def render_permission_card(permission_name)
126
+ resource_type, action = permission_name.split(':')
127
+
126
128
  div(class: "flex items-start p-3 bg-green-50 border border-green-200 rounded-lg") do
127
129
  # Permission icon
128
130
  div(class: "flex-shrink-0 mr-3") do
@@ -132,18 +134,14 @@ module EasyAdmin
132
134
  # Permission details
133
135
  div(class: "flex-1 min-w-0") do
134
136
  div(class: "flex items-center mb-1") do
135
- span(class: "text-sm font-medium text-gray-900 capitalize") { permission.action.humanize }
137
+ span(class: "text-sm font-medium text-gray-900 capitalize") { action.humanize }
136
138
  span(class: "ml-2 inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-green-100 text-green-800") do
137
- permission.action
139
+ action
138
140
  end
139
141
  end
140
142
 
141
- if permission.description.present?
142
- p(class: "text-xs text-gray-600 leading-relaxed") { permission.description }
143
- end
144
-
145
143
  # Permission name (technical)
146
- p(class: "text-xs text-gray-500 font-mono mt-1") { permission.name }
144
+ p(class: "text-xs text-gray-500 font-mono mt-1") { permission_name }
147
145
  end
148
146
  end
149
147
  end
@@ -181,6 +179,17 @@ module EasyAdmin
181
179
  {}
182
180
  end
183
181
  end
182
+
183
+ # Group permission names by resource type
184
+ def group_permissions_by_resource(permission_names)
185
+ grouped = {}
186
+ permission_names.each do |permission_name|
187
+ resource_type, action = permission_name.split(':')
188
+ grouped[resource_type] ||= []
189
+ grouped[resource_type] << permission_name
190
+ end
191
+ grouped
192
+ end
184
193
  end
185
194
  end
186
195
  end
@@ -213,13 +213,18 @@ module EasyAdmin
213
213
  # Allow non-resource items (like Dashboard, Settings, etc.)
214
214
  return true unless item[:resource]
215
215
 
216
- # For resource items, check permission
217
- resource_name = item[:resource]
218
- # Convert complex resource names to permission format
219
- permission_resource_name = convert_resource_to_permission_name(resource_name)
220
- permission_name = "#{permission_resource_name}:read"
221
-
222
- EasyAdmin::Permissions.authorized?(@current_user, permission_name)
216
+ # For resource items, check permission only if EasyAdmin::Permissions is available
217
+ if defined?(EasyAdmin::Permissions) && EasyAdmin::Permissions.enabled?
218
+ resource_name = item[:resource]
219
+ # Convert complex resource names to permission format
220
+ permission_resource_name = convert_resource_to_permission_name(resource_name)
221
+ permission_name = "#{permission_resource_name}:read"
222
+
223
+ EasyAdmin::Permissions.authorized?(@current_user, permission_name)
224
+ else
225
+ # If permissions are not available, allow access to all resource items
226
+ true
227
+ end
223
228
  end
224
229
 
225
230
  def convert_resource_to_permission_name(resource_name)
@@ -1,7 +1,17 @@
1
1
  module EasyAdmin
2
2
  class ApplicationController < ActionController::Base
3
3
  include Pagy::Backend
4
- include ActionPolicy::Controller
4
+
5
+ # Only include ActionPolicy if it's available
6
+ if defined?(ActionPolicy)
7
+ include ActionPolicy::Controller
8
+
9
+ # ActionPolicy authorization context
10
+ authorize :user, through: :current_admin_user
11
+
12
+ # Handle ActionPolicy authorization failures
13
+ rescue_from ActionPolicy::Unauthorized, with: :handle_authorization_failure
14
+ end
5
15
 
6
16
  helper EasyAdmin::FieldsHelper
7
17
  helper EasyAdmin::PagyHelper
@@ -11,16 +21,12 @@ module EasyAdmin
11
21
  before_action :set_feature_toggles
12
22
  before_action :set_paper_trail_whodunnit
13
23
 
14
- # ActionPolicy authorization context
15
- authorize :user, through: :current_admin_user
16
-
17
- # Handle ActionPolicy authorization failures
18
- rescue_from ActionPolicy::Unauthorized, with: :handle_authorization_failure
19
-
20
- # Configure ActionPolicy to use ApplicationPolicy as default for all models
21
- def policy_for(record:, **opts)
22
- # Always use ApplicationPolicy for all models (EasyAdmin and regular models)
23
- ApplicationPolicy.new(record, **authorization_context, **opts)
24
+ # Configure ActionPolicy to use ApplicationPolicy as default for all models (only if ActionPolicy is available)
25
+ if defined?(ActionPolicy)
26
+ def policy_for(record:, **opts)
27
+ # Always use ApplicationPolicy for all models (EasyAdmin and regular models)
28
+ ApplicationPolicy.new(record, **authorization_context, **opts)
29
+ end
24
30
  end
25
31
 
26
32
  protected
@@ -104,11 +110,16 @@ module EasyAdmin
104
110
 
105
111
  respond_to do |format|
106
112
  format.html do
107
- flash[:alert] = error_message
108
-
109
- # Try to redirect back, fallback to dashboard
110
- redirect_path = request.referer&.start_with?(request.base_url) ? request.referer : easy_admin.root_path
111
- redirect_to redirect_path
113
+ # Check if this is a turbo_frame request (like inline editing)
114
+ if turbo_frame_request?
115
+ render template: 'easy_admin/application/authorization_failure', layout: false
116
+ else
117
+ flash[:alert] = error_message
118
+
119
+ # Try to redirect back, fallback to dashboard
120
+ redirect_path = request.referer&.start_with?(request.base_url) ? request.referer : easy_admin.root_path
121
+ redirect_to redirect_path
122
+ end
112
123
  end
113
124
 
114
125
  format.turbo_stream do
@@ -2,53 +2,60 @@ module EasyAdmin
2
2
  module Concerns
3
3
  # ResourceAuthorization concern handles authorization checks for resource actions
4
4
  # Provides before_action callbacks and authorization methods for all CRUD operations
5
+ # Only active when ActionPolicy is available
5
6
  module ResourceAuthorization
6
7
  extend ActiveSupport::Concern
7
8
 
8
9
  included do
9
- before_action :authorize_resource_access!, only: [:index]
10
- before_action :authorize_record_access!, only: [:show]
11
- before_action :authorize_record_creation!, only: [:new, :create]
12
- before_action :authorize_record_update!, only: [
13
- :edit, :update, :edit_field, :update_field,
14
- :belongs_to_reattach, :belongs_to_edit_attached, :update_belongs_to_attached
15
- ]
16
- before_action :authorize_record_destruction!, only: [:destroy]
17
- before_action :authorize_versioning_access!, only: [:versions, :revert_version, :version_diff]
10
+ # Only add authorization callbacks if ActionPolicy is available
11
+ if defined?(ActionPolicy)
12
+ before_action :authorize_resource_access!, only: [:index]
13
+ before_action :authorize_record_access!, only: [:show]
14
+ before_action :authorize_record_creation!, only: [:new, :create]
15
+ before_action :authorize_record_update!, only: [
16
+ :edit, :update, :edit_field, :update_field,
17
+ :belongs_to_reattach, :belongs_to_edit_attached, :update_belongs_to_attached
18
+ ]
19
+ before_action :authorize_record_destruction!, only: [:destroy]
20
+ before_action :authorize_versioning_access!, only: [:versions, :revert_version, :version_diff]
21
+ end
18
22
  end
19
23
 
20
24
  private
21
25
 
22
- # Authorize access to resource index
23
- def authorize_resource_access!
24
- authorize! @resource_class.model_class, to: :index?
25
- end
26
-
27
- # Authorize access to view a specific record
28
- def authorize_record_access!
29
- authorize! @record, to: :show?
30
- end
31
-
32
- # Authorize creation of new records
33
- def authorize_record_creation!
34
- authorize! @resource_class.model_class, to: :create?
35
- end
36
-
37
- # Authorize updating of existing records
38
- def authorize_record_update!
39
- authorize! @record, to: :update?
40
- end
41
-
42
- # Authorize destruction of existing records
43
- def authorize_record_destruction!
44
- authorize! @record, to: :destroy?
45
- end
46
-
47
- # Authorize access to versioning features (PaperTrail integration)
48
- def authorize_versioning_access!
49
- # Check if user can view record and has versioning permissions
50
- authorize! @record, to: :show?
51
- authorize! @record, to: :manage_versions?
26
+ # Authorization methods - only defined if ActionPolicy is available
27
+ if defined?(ActionPolicy)
28
+ # Authorize access to resource index
29
+ def authorize_resource_access!
30
+ authorize! @resource_class.model_class, to: :index?
31
+ end
32
+
33
+ # Authorize access to view a specific record
34
+ def authorize_record_access!
35
+ authorize! @record, to: :show?
36
+ end
37
+
38
+ # Authorize creation of new records
39
+ def authorize_record_creation!
40
+ authorize! @resource_class.model_class, to: :create?
41
+ end
42
+
43
+ # Authorize updating of existing records
44
+ def authorize_record_update!
45
+ authorize! @record, to: :update?
46
+ end
47
+
48
+ # Authorize destruction of existing records
49
+ def authorize_record_destruction!
50
+ authorize! @record, to: :destroy?
51
+ end
52
+
53
+ # Authorize access to versioning features (PaperTrail integration)
54
+ def authorize_versioning_access!
55
+ # Check if user can view record and has versioning permissions
56
+ authorize! @record, to: :show?
57
+ authorize! @record, to: :manage_versions?
58
+ end
52
59
  end
53
60
  end
54
61
  end
@@ -0,0 +1,9 @@
1
+ <turbo-frame id="modal">
2
+ <%= turbo_stream.update("notifications") do %>
3
+ <%== EasyAdmin::NotificationComponent.new(
4
+ type: :error,
5
+ message: @error_message,
6
+ title: "Access Denied"
7
+ ).call %>
8
+ <% end %>
9
+ </turbo-frame>
@@ -1,3 +1,3 @@
1
1
  module EasyAdmin
2
- VERSION = "0.2.1"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy-admin-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Slaurmagan
@@ -355,6 +355,7 @@ files:
355
355
  - app/models/easy_admin/application_record.rb
356
356
  - app/policies/admin_user_policy.rb
357
357
  - app/policies/application_policy.rb
358
+ - app/views/easy_admin/application/authorization_failure.html.erb
358
359
  - app/views/easy_admin/application/authorization_failure.turbo_stream.erb
359
360
  - app/views/easy_admin/dashboard/index.html.erb
360
361
  - app/views/easy_admin/dashboards/card.html.erb