upload_d_n_d 0.9.4 → 0.10.0
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/README.md +13 -0
- data/app/components/upload_d_n_d/upload_component.html.erb +2 -4
- data/app/components/upload_d_n_d/upload_component.rb +19 -14
- data/app/controllers/upload_d_n_d/attachments_controller.rb +38 -133
- data/app/controllers/upload_d_n_d/base_attachments_controller.rb +62 -22
- data/app/controllers/upload_d_n_d/temporary_attachments_controller.rb +13 -20
- data/app/helpers/upload_d_n_d/application_helper.rb +5 -6
- data/app/javascript/upload_d_n_d/controllers/uploads_controller.js +4 -15
- data/app/models/upload_d_n_d/temporary_attachment.rb +49 -11
- data/app/views/upload_d_n_d/shared/_files_list.html.erb +1 -1
- data/docs/CHANGELOG.md +36 -0
- data/lib/generators/upload_d_n_d/install_generator.rb +10 -14
- data/lib/generators/upload_d_n_d/templates/POST_INSTALL +28 -26
- data/lib/generators/upload_d_n_d/templates/initializers/upload_d_n_d.rb +4 -3
- data/lib/upload_d_n_d/configuration.rb +12 -0
- data/lib/upload_d_n_d/file_validator.rb +8 -5
- data/lib/upload_d_n_d/js_link.rb +16 -8
- data/lib/upload_d_n_d/mime_types.rb +8 -0
- data/lib/upload_d_n_d/signed_target.rb +83 -0
- data/lib/upload_d_n_d/uploadable.rb +9 -42
- data/lib/upload_d_n_d/version.rb +1 -1
- data/lib/upload_d_n_d.rb +1 -1
- data/package.json +5 -3
- data/styles.css +4 -0
- metadata +3 -4
- data/app/helpers/upload_d_n_d/controllers/helpers.rb +0 -21
- data/lib/generators/upload_d_n_d/templates/concerns/upload_authorization.rb +0 -91
- data/lib/upload_d_n_d/field_reference.rb +0 -44
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: upload_d_n_d
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.10.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Pierre Cherbero
|
|
@@ -65,7 +65,6 @@ files:
|
|
|
65
65
|
- app/controllers/upload_d_n_d/base_attachments_controller.rb
|
|
66
66
|
- app/controllers/upload_d_n_d/temporary_attachments_controller.rb
|
|
67
67
|
- app/helpers/upload_d_n_d/application_helper.rb
|
|
68
|
-
- app/helpers/upload_d_n_d/controllers/helpers.rb
|
|
69
68
|
- app/javascript/upload_d_n_d/controllers/preview_controller.js
|
|
70
69
|
- app/javascript/upload_d_n_d/controllers/uploads_controller.js
|
|
71
70
|
- app/javascript/upload_d_n_d/index.js
|
|
@@ -92,7 +91,6 @@ files:
|
|
|
92
91
|
- lib/generators/upload_d_n_d/policy_generator.rb
|
|
93
92
|
- lib/generators/upload_d_n_d/templates/POST_INSTALL
|
|
94
93
|
- lib/generators/upload_d_n_d/templates/concerns/controllers_uploadable.rb
|
|
95
|
-
- lib/generators/upload_d_n_d/templates/concerns/upload_authorization.rb
|
|
96
94
|
- lib/generators/upload_d_n_d/templates/concerns/uploadable.rb
|
|
97
95
|
- lib/generators/upload_d_n_d/templates/controller.rb.tt
|
|
98
96
|
- lib/generators/upload_d_n_d/templates/helpers/upload_helper.rb
|
|
@@ -114,16 +112,17 @@ files:
|
|
|
114
112
|
- lib/upload_d_n_d/controllers/uploadable.rb
|
|
115
113
|
- lib/upload_d_n_d/engine.rb
|
|
116
114
|
- lib/upload_d_n_d/field_constraints.rb
|
|
117
|
-
- lib/upload_d_n_d/field_reference.rb
|
|
118
115
|
- lib/upload_d_n_d/file_validator.rb
|
|
119
116
|
- lib/upload_d_n_d/js_link.rb
|
|
120
117
|
- lib/upload_d_n_d/mime_types.rb
|
|
121
118
|
- lib/upload_d_n_d/orphan_blobs.rb
|
|
122
119
|
- lib/upload_d_n_d/rejected_blob.rb
|
|
123
120
|
- lib/upload_d_n_d/session_id.rb
|
|
121
|
+
- lib/upload_d_n_d/signed_target.rb
|
|
124
122
|
- lib/upload_d_n_d/uploadable.rb
|
|
125
123
|
- lib/upload_d_n_d/version.rb
|
|
126
124
|
- package.json
|
|
125
|
+
- styles.css
|
|
127
126
|
homepage: https://github.com/Peyochanchan/upload_d_n_d
|
|
128
127
|
licenses:
|
|
129
128
|
- MIT
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
# lib/upload_d_n_d/controllers/helpers.rb
|
|
2
|
-
module UploadDND
|
|
3
|
-
module Controllers
|
|
4
|
-
module Helpers
|
|
5
|
-
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
def attach_upload_d_n_d_files(record, attribute_name)
|
|
8
|
-
# On cherche les fichiers temporaires liés à cette session
|
|
9
|
-
temps = UploadDND::TemporaryAttachment.where(
|
|
10
|
-
session_id: UploadDND::SessionId.for(session),
|
|
11
|
-
attribute_name: attribute_name
|
|
12
|
-
)
|
|
13
|
-
|
|
14
|
-
temps.each do |temp|
|
|
15
|
-
record.send(attribute_name).attach(temp.blob)
|
|
16
|
-
temp.destroy # On nettoie après attachement
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module UploadDND
|
|
4
|
-
module UploadAuthorization
|
|
5
|
-
extend ActiveSupport::Concern
|
|
6
|
-
|
|
7
|
-
included do
|
|
8
|
-
before_action :authorize_upload!, only: [ :create ]
|
|
9
|
-
before_action :authorize_destroy!, only: [ :destroy ]
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
private
|
|
13
|
-
|
|
14
|
-
# Vérifie si l'utilisateur peut uploader sur cet attribut
|
|
15
|
-
# Peut être surchargée dans votre controller
|
|
16
|
-
def authorize_upload!
|
|
17
|
-
# Si vous utilisez Pundit
|
|
18
|
-
if defined?(Pundit)
|
|
19
|
-
authorize_with_pundit
|
|
20
|
-
# Si vous utilisez CanCanCan
|
|
21
|
-
elsif defined?(CanCan)
|
|
22
|
-
authorize_with_cancan
|
|
23
|
-
else
|
|
24
|
-
# Autorisation par défaut : l'utilisateur doit être connecté
|
|
25
|
-
authorize_basic_upload!
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
# Vérifie si l'utilisateur peut supprimer ce fichier
|
|
30
|
-
def authorize_destroy!
|
|
31
|
-
if defined?(Pundit)
|
|
32
|
-
authorize @temp_attachment || @attachment
|
|
33
|
-
elsif defined?(CanCan)
|
|
34
|
-
authorize! :destroy, @temp_attachment || @attachment
|
|
35
|
-
else
|
|
36
|
-
authorize_basic_destroy!
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# Autorisation basique : utilisateur connecté
|
|
41
|
-
def authorize_basic_upload!
|
|
42
|
-
return if current_user.present?
|
|
43
|
-
|
|
44
|
-
render json: { error: "Vous devez être connecté pour uploader des fichiers" },
|
|
45
|
-
status: :unauthorized
|
|
46
|
-
end
|
|
47
|
-
|
|
48
|
-
def authorize_basic_destroy!
|
|
49
|
-
# Vérifie que le fichier appartient à la session ou à l'utilisateur
|
|
50
|
-
return if owner_matches_current_user?
|
|
51
|
-
|
|
52
|
-
render json: { error: "Vous ne pouvez pas supprimer ce fichier" },
|
|
53
|
-
status: :forbidden
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
def authorize_with_pundit
|
|
57
|
-
record = find_or_build_record
|
|
58
|
-
authorize record, :update?
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def authorize_with_cancan
|
|
62
|
-
record = find_or_build_record
|
|
63
|
-
authorize! :update, record
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def owner_matches_current_user?
|
|
67
|
-
if @temp_attachment
|
|
68
|
-
@temp_attachment.session_id == UploadDND::SessionId.for(session) ||
|
|
69
|
-
@temp_attachment.user_id == current_user&.id
|
|
70
|
-
elsif @attachment
|
|
71
|
-
@attachment.record.user_id == current_user&.id if @attachment.record.respond_to?(:user_id)
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
def find_or_build_record
|
|
76
|
-
if params[:record_gid].present?
|
|
77
|
-
GlobalID::Locator.locate(params[:record_gid])
|
|
78
|
-
else
|
|
79
|
-
# Pour les uploads temporaires, on ne peut pas vraiment autoriser
|
|
80
|
-
# car le record n'existe pas encore
|
|
81
|
-
nil
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def current_user
|
|
86
|
-
# À adapter selon votre système d'authentification
|
|
87
|
-
# Par défaut, on suppose que vous utilisez Devise ou similaire
|
|
88
|
-
respond_to?(:current_user, true) ? super : nil
|
|
89
|
-
end
|
|
90
|
-
end
|
|
91
|
-
end
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module UploadDND
|
|
4
|
-
# Référence signée d'un champ (modèle + attribut), émise par le composant et
|
|
5
|
-
# renvoyée au dépôt : le serveur retrouve les limites du champ sans faire
|
|
6
|
-
# confiance au client. Elle ne porte pas les limites elles-mêmes : le modèle
|
|
7
|
-
# reste la seule autorité.
|
|
8
|
-
module FieldReference
|
|
9
|
-
PURPOSE = :upload_d_n_d_field
|
|
10
|
-
|
|
11
|
-
class Invalid < UploadDND::Error; end
|
|
12
|
-
|
|
13
|
-
class << self
|
|
14
|
-
def generate(model_class, attribute_name)
|
|
15
|
-
verifier.generate([ model_class.name, attribute_name.to_s ], purpose: PURPOSE)
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
# @raise [Invalid] signature fausse, autre attribut, modèle ou attachment inconnu
|
|
19
|
-
# @return [UploadDND::FieldConstraints]
|
|
20
|
-
def constraints(token, attribute_name)
|
|
21
|
-
model_name, attribute = verified(token)
|
|
22
|
-
raise Invalid unless attribute.present? && attribute == attribute_name.to_s
|
|
23
|
-
|
|
24
|
-
model_class = model_name.to_s.safe_constantize
|
|
25
|
-
raise Invalid unless model_class.respond_to?(:reflect_on_attachment) && model_class.reflect_on_attachment(attribute)
|
|
26
|
-
|
|
27
|
-
FieldConstraints.for(model_class, attribute)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
private
|
|
31
|
-
|
|
32
|
-
def verified(token)
|
|
33
|
-
payload = verifier.verified(token.to_s, purpose: PURPOSE)
|
|
34
|
-
payload.is_a?(Array) && payload.size == 2 ? payload : raise(Invalid)
|
|
35
|
-
rescue ActiveSupport::MessageVerifier::InvalidSignature, ArgumentError
|
|
36
|
-
raise Invalid
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
def verifier
|
|
40
|
-
Rails.application.message_verifier("upload_d_n_d/field_reference")
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
end
|
|
44
|
-
end
|