Module: Opro::Controllers::Concerns::Permissions

Extended by:
ActiveSupport::Concern
Included in:
ApplicationControllerHelper
Defined in:
lib/opro/controllers/concerns/permissions.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#add_oauth_required_permission(permission) ⇒ Object



24
25
26
27
# File 'lib/opro/controllers/concerns/permissions.rb', line 24

def add_oauth_required_permission(permission)
  @oauth_required_permissions ||= global_oauth_required_permissions
  @oauth_required_permissions << permission
end

#global_oauth_required_permissionsObject

By default :write permission is required if included in Opro.request_permissions returns Array



6
7
8
# File 'lib/opro/controllers/concerns/permissions.rb', line 6

def global_oauth_required_permissions
   [:write] & Opro.request_permissions
end

#oauth_client_can_write?Boolean

Returns boolean if client has been granted write permissions or request is a ‘GET’ returns true

Returns:

  • (Boolean)


56
57
58
59
60
61
# File 'lib/opro/controllers/concerns/permissions.rb', line 56

def oauth_client_can_write?
  return false unless oauth_access_grant.present?
  return true if env['REQUEST_METHOD'] == 'GET'
  return true if oauth_access_grant.can?(:write)
  false
end

#oauth_client_has_permission?(permission) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/opro/controllers/concerns/permissions.rb', line 44

def oauth_client_has_permission?(permission)
  oauth_permission_method = "oauth_client_can_#{permission}?".to_sym
  if respond_to?(oauth_permission_method)
    has_permission = method(oauth_permission_method).call
  else
    has_permission = oauth_access_grant.can?(permission.to_sym)
  end
  has_permission
end

#oauth_client_has_permissions?Boolean

Checks to make sure client has given permission permission checks can be extended by creating methods oauth_client_can_:method? so to over-write a default check for :write permission, you would need to define oauth_client_can_write?

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'lib/opro/controllers/concerns/permissions.rb', line 33

def oauth_client_has_permissions?
  return false unless oauth_access_grant.present?
  permissions_valid_array = []
  oauth_required_permissions.each do |permission|
    permissions_valid_array << oauth_client_has_permission?(permission)
  end

  return true unless permissions_valid_array.include?(false)
  false
end

#oauth_required_permissionsObject

returns Array of permissions required for controller action



11
12
13
# File 'lib/opro/controllers/concerns/permissions.rb', line 11

def oauth_required_permissions
  (@oauth_required_permissions || global_oauth_required_permissions) - skip_oauth_required_permissions
end

#skip_oauth_required_permission(permission) ⇒ Object



19
20
21
22
# File 'lib/opro/controllers/concerns/permissions.rb', line 19

def skip_oauth_required_permission(permission)
  @skip_oauth_required_permissions << permission
  @skip_oauth_required_permissions
end

#skip_oauth_required_permissionsObject



15
16
17
# File 'lib/opro/controllers/concerns/permissions.rb', line 15

def skip_oauth_required_permissions
  @skip_oauth_required_permissions ||= []
end