Class: Decidim::PrivateDownload
- Inherits:
-
Object
- Object
- Decidim::PrivateDownload
show all
- Defined in:
- app/models/decidim/private_download.rb
Defined Under Namespace
Classes: InvalidTokenError
Constant Summary
collapse
- VERIFIER_PURPOSE =
:private_download
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(record:, attachment_name:) ⇒ PrivateDownload
Returns a new instance of PrivateDownload.
28
29
30
31
|
# File 'app/models/decidim/private_download.rb', line 28
def initialize(record:, attachment_name:)
@record = record
@attachment_name = attachment_name.to_s
end
|
Class Method Details
.for(record, attachment_name:) ⇒ Object
9
10
11
|
# File 'app/models/decidim/private_download.rb', line 9
def self.for(record, attachment_name:)
new(record:, attachment_name:)
end
|
.from_token(token) ⇒ Object
13
14
15
16
17
18
19
20
21
22
|
# File 'app/models/decidim/private_download.rb', line 13
def self.from_token(token)
payload = verifier.verify(token, purpose: VERIFIER_PURPOSE).with_indifferent_access
record = GlobalID::Locator.locate(payload[:gid])
raise InvalidTokenError if record.blank?
new(record:, attachment_name: payload[:attachment_name])
rescue ActiveSupport::MessageVerifier::InvalidSignature, TypeError
raise InvalidTokenError
end
|
.verifier ⇒ Object
24
25
26
|
# File 'app/models/decidim/private_download.rb', line 24
def self.verifier
@verifier ||= ActiveSupport::MessageVerifier.new(Rails.application.secret_key_base, serializer: JSON)
end
|
Instance Method Details
#attached? ⇒ Boolean
47
48
49
|
# File 'app/models/decidim/private_download.rb', line 47
def attached?
attachment.respond_to?(:attached?) && attachment.attached?
end
|
#attachment ⇒ Object
43
44
45
|
# File 'app/models/decidim/private_download.rb', line 43
def attachment
record.public_send(attachment_name)
end
|
#authorized_for?(user) ⇒ Boolean
51
52
53
54
55
|
# File 'app/models/decidim/private_download.rb', line 51
def authorized_for?(user)
return false unless record.respond_to?(:private_download_authorized?)
record.private_download_authorized?(user, attachment_name)
end
|
#token ⇒ Object
33
34
35
36
37
38
39
40
41
|
# File 'app/models/decidim/private_download.rb', line 33
def token
self.class.verifier.generate(
{
gid: record.to_global_id.to_s,
attachment_name:
},
purpose: VERIFIER_PURPOSE
)
end
|