Skip to content

Commit c4c7511

Browse files
authored
feat(conditional-approval): users want conditional triggers of conditional approval (#612)
Co-authored-by: Nicolas Alexandre <[email protected]> BREAKING CHANGE: Introduction of a new permission module call Ability. The previous permission system PermissionChecker doesn't exist anymore.
1 parent 6311398 commit c4c7511

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1591
-2709
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/spec/dummy/tmp/
1515
/tmp/
1616
.byebug_history
17+
/out
1718

1819
## Specific to RubyMotion:
1920
.dat*

CHANGELOG.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,74 @@
1+
# [8.0.0-beta.1](https://github.com/ForestAdmin/forest-rails/compare/v7.8.1...v8.0.0-beta.1) (2023-03-14)
2+
3+
4+
### Bug Fixes
5+
6+
* raise error when an action is call on a smartcollection ([#605](https://github.com/ForestAdmin/forest-rails/issues/605)) ([675821e](https://github.com/ForestAdmin/forest-rails/commit/675821e086137cf7413a1e328b71a9c836d8c21a))
7+
* **apimap:** format apimap liana version ([#603](https://github.com/ForestAdmin/forest-rails/issues/603)) ([b8a83bb](https://github.com/ForestAdmin/forest-rails/commit/b8a83bb6a4edf229ca788227cac2a917dc4fd204))
8+
* **conditional-approval:** update condition to check if user can trigger the action ([#604](https://github.com/ForestAdmin/forest-rails/issues/604)) ([36f55b3](https://github.com/ForestAdmin/forest-rails/commit/36f55b3bfeb992831c10238264af2931448ccae8))
9+
10+
11+
### Features
12+
13+
* **conditional-approval:** users want conditional triggers of conditional approval ([#600](https://github.com/ForestAdmin/forest-rails/issues/600)) ([062bcac](https://github.com/ForestAdmin/forest-rails/commit/062bcace329f302f238324fb0f1db2f00331978e))
14+
15+
16+
### BREAKING CHANGES
17+
18+
* **conditional-approval:** Introduction of a new permission module call Ability. The previous permission system PermissionChecker doesn't exist anymore.
19+
20+
## [7.8.2-beta.1](https://github.com/ForestAdmin/forest-rails/compare/v7.8.1...v7.8.2-beta.1) (2023-03-14)
21+
22+
23+
### Bug Fixes
24+
25+
* raise error when an action is call on a smartcollection ([#605](https://github.com/ForestAdmin/forest-rails/issues/605)) ([675821e](https://github.com/ForestAdmin/forest-rails/commit/675821e086137cf7413a1e328b71a9c836d8c21a))
26+
* **apimap:** format apimap liana version ([#603](https://github.com/ForestAdmin/forest-rails/issues/603)) ([b8a83bb](https://github.com/ForestAdmin/forest-rails/commit/b8a83bb6a4edf229ca788227cac2a917dc4fd204))
27+
* **conditional-approval:** update condition to check if user can trigger the action ([#604](https://github.com/ForestAdmin/forest-rails/issues/604)) ([36f55b3](https://github.com/ForestAdmin/forest-rails/commit/36f55b3bfeb992831c10238264af2931448ccae8))
28+
29+
30+
### Features
31+
32+
* **conditional-approval:** users want conditional triggers of conditional approval ([#600](https://github.com/ForestAdmin/forest-rails/issues/600)) ([062bcac](https://github.com/ForestAdmin/forest-rails/commit/062bcace329f302f238324fb0f1db2f00331978e))
33+
34+
35+
### BREAKING CHANGES
36+
37+
* **conditional-approval:** Introduction of a new permission module call Ability. The previous permission system PermissionChecker doesn't exist anymore.
38+
39+
# [8.0.0-beta.4](https://github.com/ForestAdmin/forest-rails/compare/v8.0.0-beta.3...v8.0.0-beta.4) (2023-02-23)
40+
41+
42+
### Bug Fixes
43+
44+
* raise error when an action is call on a smartcollection ([#605](https://github.com/ForestAdmin/forest-rails/issues/605)) ([c98ac23](https://github.com/ForestAdmin/forest-rails/commit/c98ac238d55bef201945b1cd3cfcea806d4c2d26))
45+
46+
# [8.0.0-beta.3](https://github.com/ForestAdmin/forest-rails/compare/v8.0.0-beta.2...v8.0.0-beta.3) (2023-02-22)
47+
48+
49+
### Bug Fixes
50+
51+
* **conditional-approval:** update condition to check if user can trigger the action ([#604](https://github.com/ForestAdmin/forest-rails/issues/604)) ([d55db18](https://github.com/ForestAdmin/forest-rails/commit/d55db187ea6c12d6aa24e10a509983b0940bc21e))
52+
53+
# [8.0.0-beta.2](https://github.com/ForestAdmin/forest-rails/compare/v8.0.0-beta.1...v8.0.0-beta.2) (2023-02-21)
54+
55+
56+
### Bug Fixes
57+
58+
* **apimap:** format apimap liana version ([#603](https://github.com/ForestAdmin/forest-rails/issues/603)) ([37fc823](https://github.com/ForestAdmin/forest-rails/commit/37fc82346870ab367a130c750256d5e37f1de4fd))
59+
60+
# [8.0.0-beta.1](https://github.com/ForestAdmin/forest-rails/compare/v7.8.0...v8.0.0-beta.1) (2023-02-17)
61+
62+
63+
### Features
64+
65+
* **conditional-approval:** users want conditional triggers of conditional approval ([#600](https://github.com/ForestAdmin/forest-rails/issues/600)) ([ca99939](https://github.com/ForestAdmin/forest-rails/commit/ca99939647d20c0a223f9f4f1d1d75bede1128b1))
66+
67+
68+
### BREAKING CHANGES
69+
70+
* **conditional-approval:** Introduction of a new permission module call Ability. The previous permission system PermissionChecker doesn't exist anymore.
71+
172
## [7.8.1](https://github.com/ForestAdmin/forest-rails/compare/v7.8.0...v7.8.1) (2023-02-28)
273

374
# [7.8.0](https://github.com/ForestAdmin/forest-rails/compare/v7.7.3...v7.8.0) (2023-01-24)

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,4 @@ gem 'ipaddress', '0.8.3'
3535
gem 'openid_connect', '1.4.2'
3636
gem 'json'
3737
gem 'json-jwt', '1.15.0'
38+
gem 'deepsort'

Gemfile.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
PATH
22
remote: .
33
specs:
4-
forest_liana (7.8.1)
4+
forest_liana (8.0.0-beta.1)
55
arel-helpers
66
bcrypt
7+
deepsort
78
forestadmin-jsonapi-serializers (>= 0.14.0)
89
groupdate (>= 5.0.0)
910
httparty
@@ -89,6 +90,7 @@ GEM
8990
concurrent-ruby (1.1.10)
9091
crass (1.0.6)
9192
date (3.3.3)
93+
deepsort (0.4.5)
9294
diff-lcs (1.5.0)
9395
docile (1.4.0)
9496
erubi (1.12.0)
@@ -254,6 +256,7 @@ DEPENDENCIES
254256
arel-helpers (= 2.14.0)
255257
bcrypt
256258
byebug
259+
deepsort
257260
forest_liana!
258261
forestadmin-jsonapi-serializers
259262
groupdate (= 5.2.2)

app/controllers/forest_liana/actions_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ module ForestLiana
22
class ActionsController < ApplicationController
33

44
def get_smart_action_hook_request
5-
begin
5+
if params[:data] && params[:data][:attributes] && params[:data][:attributes][:collection_name]
66
params[:data][:attributes]
7-
rescue => error
7+
else
8+
error = 'parameters data attributes missing'
89
FOREST_REPORTER.report error
910
FOREST_LOGGER.error "Smart Action hook request error: #{error}"
10-
{}
11+
12+
raise ForestLiana::Errors::HTTP422Error.new("Error in smart action load hook: cannot retrieve action from collection")
1113
end
1214
end
1315

app/controllers/forest_liana/application_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
module ForestLiana
55
class ApplicationController < ForestLiana::BaseController
6+
rescue_from ForestLiana::Ability::Exceptions::AccessDenied, with: :render_error
7+
rescue_from ForestLiana::Errors::HTTP422Error, with: :render_error
8+
69
def self.papertrail?
710
Object.const_get('PaperTrail::Version').is_a?(Class) rescue false
811
end
@@ -96,6 +99,18 @@ def deactivate_count_response
9699

97100
private
98101

102+
def render_error(exception)
103+
errors = {
104+
status: exception.error_code,
105+
detail: exception.message,
106+
}
107+
108+
errors['name'] = exception.name if exception.try(:name)
109+
errors['data'] = exception.data if exception.try(:data)
110+
111+
render json: { errors: [errors] }, status: exception.status
112+
end
113+
99114
def force_utf8_encoding(json)
100115
if json['data'].class == Array
101116
# NOTICE: Collection of records case

app/controllers/forest_liana/resources_controller.rb

Lines changed: 31 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module ForestLiana
22
class ResourcesController < ForestLiana::ApplicationController
3+
include ForestLiana::Ability
34
begin
45
prepend ResourcesExtensions
56
rescue NameError
@@ -14,24 +15,11 @@ class ResourcesController < ForestLiana::ApplicationController
1415
end
1516

1617
def index
18+
action = request.format == 'csv' ? 'export' : 'browse'
19+
forest_authorize!(action, forest_user, @resource)
1720
begin
18-
if request.format == 'csv'
19-
checker = ForestLiana::PermissionsChecker.new(@resource, 'exportEnabled', @rendering_id, user: forest_user)
20-
return head :forbidden unless checker.is_authorized?
21-
else
22-
checker = ForestLiana::PermissionsChecker.new(
23-
@resource,
24-
'browseEnabled',
25-
@rendering_id,
26-
user: forest_user,
27-
collection_list_parameters: get_collection_list_permission_info(forest_user, request)
28-
)
29-
return head :forbidden unless checker.is_authorized?
30-
end
31-
3221
getter = ForestLiana::ResourcesGetter.new(@resource, params, forest_user)
3322
getter.perform
34-
3523
respond_to do |format|
3624
format.json { render_jsonapi(getter) }
3725
format.csv { render_csv(getter, @resource) }
@@ -55,16 +43,8 @@ def index
5543

5644
def count
5745
find_resource
46+
forest_authorize!('browse', forest_user, @resource)
5847
begin
59-
checker = ForestLiana::PermissionsChecker.new(
60-
@resource,
61-
'browseEnabled',
62-
@rendering_id,
63-
user: forest_user,
64-
collection_list_parameters: get_collection_list_permission_info(forest_user, request)
65-
)
66-
return head :forbidden unless checker.is_authorized?
67-
6848
getter = ForestLiana::ResourcesGetter.new(@resource, params, forest_user)
6949
getter.count
7050

@@ -88,10 +68,8 @@ def count
8868
end
8969

9070
def show
71+
forest_authorize!('read', forest_user, @resource)
9172
begin
92-
checker = ForestLiana::PermissionsChecker.new(@resource, 'readEnabled', @rendering_id, user: forest_user)
93-
return head :forbidden unless checker.is_authorized?
94-
9573
getter = ForestLiana::ResourceGetter.new(@resource, params, forest_user)
9674
getter.perform
9775

@@ -106,10 +84,8 @@ def show
10684
end
10785

10886
def create
87+
forest_authorize!('add', forest_user, @resource)
10988
begin
110-
checker = ForestLiana::PermissionsChecker.new(@resource, 'addEnabled', @rendering_id, user: forest_user)
111-
return head :forbidden unless checker.is_authorized?
112-
11389
creator = ForestLiana::ResourceCreator.new(@resource, params)
11490
creator.perform
11591

@@ -130,10 +106,8 @@ def create
130106
end
131107

132108
def update
109+
forest_authorize!('edit', forest_user, @resource)
133110
begin
134-
checker = ForestLiana::PermissionsChecker.new(@resource, 'editEnabled', @rendering_id, user: forest_user)
135-
return head :forbidden unless checker.is_authorized?
136-
137111
updater = ForestLiana::ResourceUpdater.new(@resource, params, forest_user)
138112
updater.perform
139113

@@ -154,37 +128,37 @@ def update
154128
end
155129

156130
def destroy
157-
checker = ForestLiana::PermissionsChecker.new(@resource, 'deleteEnabled', @rendering_id, user: forest_user)
158-
return head :forbidden unless checker.is_authorized?
131+
forest_authorize!('delete', forest_user, @resource)
132+
begin
133+
collection_name = ForestLiana.name_for(@resource)
134+
scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, forest_user, collection_name, params[:timezone])
159135

160-
collection_name = ForestLiana.name_for(@resource)
161-
scoped_records = ForestLiana::ScopeManager.apply_scopes_on_records(@resource, forest_user, collection_name, params[:timezone])
162-
163-
unless scoped_records.exists?(params[:id])
164-
return render serializer: nil, json: { status: 404 }, status: :not_found
165-
end
136+
unless scoped_records.exists?(params[:id])
137+
return render serializer: nil, json: { status: 404 }, status: :not_found
138+
end
166139

167-
scoped_records.destroy(params[:id])
140+
scoped_records.destroy(params[:id])
168141

169-
head :no_content
170-
rescue => error
171-
FOREST_REPORTER.report error
172-
FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
173-
internal_server_error
142+
head :no_content
143+
rescue => error
144+
FOREST_REPORTER.report error
145+
FOREST_LOGGER.error "Record Destroy error: #{error}\n#{format_stacktrace(error)}"
146+
internal_server_error
147+
end
174148
end
175149

176150
def destroy_bulk
177-
checker = ForestLiana::PermissionsChecker.new(@resource, 'deleteEnabled', @rendering_id, user: forest_user)
178-
return head :forbidden unless checker.is_authorized?
179-
180-
ids = ForestLiana::ResourcesGetter.get_ids_from_request(params, forest_user)
181-
@resource.destroy(ids) if ids&.any?
151+
forest_authorize!('delete', forest_user, @resource)
152+
begin
153+
ids = ForestLiana::ResourcesGetter.get_ids_from_request(params, forest_user)
154+
@resource.destroy(ids) if ids&.any?
182155

183-
head :no_content
184-
rescue => error
185-
FOREST_REPORTER.report error
186-
FOREST_LOGGER.error "Records Destroy error: #{error}\n#{format_stacktrace(error)}"
187-
internal_server_error
156+
head :no_content
157+
rescue => error
158+
FOREST_REPORTER.report error
159+
FOREST_LOGGER.error "Records Destroy error: #{error}\n#{format_stacktrace(error)}"
160+
internal_server_error
161+
end
188162
end
189163

190164
private

0 commit comments

Comments
 (0)