Class: Decidim::Authorization

Inherits:
ApplicationRecord show all
Defined in:
app/models/decidim/authorization.rb

Overview

An authorization is a record that a User has been authorized somehow. Other models in the system can use different kind of authorizations to allow a user to perform actions.

To create an authorization for a user we need to use an AuthorizationHandler that validates the user against a set of rules. An example could be a handler that validates a user email against an API and depending on the response it allows the creation of the authorization or not.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_or_update_from(handler) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/models/decidim/authorization.rb', line 25

def self.create_or_update_from(handler)
  authorization = find_or_initialize_by(
    user: handler.user,
    name: handler.handler_name
  )

  authorization.attributes = {
    unique_id: handler.unique_id,
    metadata: handler.
  }

  authorization.grant!
end

Instance Method Details

#expired?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'app/models/decidim/authorization.rb', line 60

def expired?
  expires_at.present? && expires_at < Time.current
end

#expires_atObject

Calculates at when this authorization will expire, if it needs to.

Returns nil if the authorization does not expire. Returns an ActiveSupport::TimeWithZone if it expires.



53
54
55
56
57
58
# File 'app/models/decidim/authorization.rb', line 53

def expires_at
  return unless workflow_manifest
  return if workflow_manifest.expires_in.zero?

  (granted_at || created_at) + workflow_manifest.expires_in
end

#grant!Object



39
40
41
42
43
# File 'app/models/decidim/authorization.rb', line 39

def grant!
  remove_verification_attachment!

  update!(granted_at: Time.current, verification_metadata: {})
end

#granted?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'app/models/decidim/authorization.rb', line 45

def granted?
  !granted_at.nil?
end