Class: OpenStax::Api::ApiUser

Inherits:
Object
  • Object
show all
Defined in:
app/models/openstax/api/api_user.rb

Instance Method Summary collapse

Constructor Details

#initialize(doorkeeper_token, non_doorkeeper_user_proc) ⇒ ApiUser

Returns a new instance of ApiUser.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/models/openstax/api/api_user.rb', line 20

def initialize(doorkeeper_token, non_doorkeeper_user_proc)
  # If we have a doorkeeper_token, derive the Application and User
  # from it.  If not, we're in case #1 above and the User should be 
  # retrieved from the alternative proc provided in arguments and 
  # there is no application.
  #
  # In both cases, don't actually retrieve any data -- just save off
  # procs that can get it for us.  This could save us some queries.

  if doorkeeper_token
    user_class = OpenStax::Api.configuration.user_class_name.classify.constantize
    @application_proc = lambda { doorkeeper_token.application }
    @user_proc = lambda {
      doorkeeper_token.resource_owner_id ? 
        user_class.find(doorkeeper_token.resource_owner_id) :
        nil
    }
  else
    @user_proc = non_doorkeeper_user_proc
    @application_proc = lambda { nil }
  end
end

Instance Method Details

#applicationObject

Returns a Doorkeeper::Application or nil



44
45
46
# File 'app/models/openstax/api/api_user.rb', line 44

def application
  @application ||= @application_proc.call
end

#can_create?(resource) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/openstax/api/api_user.rb', line 65

def can_create?(resource)
  can_do?(:create, resource)
end

#can_destroy?(resource) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
# File 'app/models/openstax/api/api_user.rb', line 73

def can_destroy?(resource)
  can_do?(:destroy, resource)
end

#can_do?(action, resource) ⇒ Boolean

Access Control Helpers #

Returns:

  • (Boolean)


57
58
59
# File 'app/models/openstax/api/api_user.rb', line 57

def can_do?(action, resource)
  OSU::AccessPolicy.action_allowed?(action, self, resource)
end

#can_read?(resource) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'app/models/openstax/api/api_user.rb', line 61

def can_read?(resource)
  can_do?(:read, resource)
end

#can_sort?(resource) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/openstax/api/api_user.rb', line 77

def can_sort?(resource)
  can_do?(:sort, resource)
end

#can_update?(resource) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'app/models/openstax/api/api_user.rb', line 69

def can_update?(resource)
  can_do?(:update, resource)
end

#human_userObject

Can return an instance of User, AnonymousUser, or nil



49
50
51
# File 'app/models/openstax/api/api_user.rb', line 49

def human_user
  @user ||= @user_proc.call
end