diff --git a/lib/devise/oauth2_providable/engine.rb b/lib/devise/oauth2_providable/engine.rb index 0869344..0140e3b 100644 --- a/lib/devise/oauth2_providable/engine.rb +++ b/lib/devise/oauth2_providable/engine.rb @@ -5,6 +5,7 @@ class Engine < Rails::Engine config.devise_oauth2_providable.access_token_expires_in = 15.minutes config.devise_oauth2_providable.refresh_token_expires_in = 1.month config.devise_oauth2_providable.authorization_code_expires_in = 1.minute + config.devise_oauth2_providable.scope_settings = {} engine_name 'oauth2' isolate_namespace Devise::Oauth2Providable diff --git a/lib/devise/oauth2_providable/mapping.rb b/lib/devise/oauth2_providable/mapping.rb index 0fb0f70..c6b21e3 100644 --- a/lib/devise/oauth2_providable/mapping.rb +++ b/lib/devise/oauth2_providable/mapping.rb @@ -17,7 +17,7 @@ module Oauth2Providable # # mapping.prefix class Mapping - attr_reader :scope_name, :path_prefix, :controllers + attr_reader :scope_name, :path_prefix, :controllers, :models class << self def default_controllers @@ -26,12 +26,22 @@ def default_controllers :tokens => "devise/oauth2_providable/tokens" } end + + def default_models + { + :access_token => 'Devise::Oauth2Providable::AccessToken', + :client => 'Devise::Oauth2Providable::Client', + :refresh_token => 'Devise::Oauth2Providable::RefreshToken', + :user => 'User', + } + end end def initialize(scope_name, options = {}) @scope_name = (options[:scope_name] || scope_name.to_s.singularize).to_sym @path_prefix = options[:path_prefix] @controllers = self.select_controllers(options) + @models = self.select_models(options) end # Returns the devise scope mapping object associated with this oauth endpoint @@ -43,6 +53,16 @@ def devise_scope def select_controllers(options) self.class.default_controllers.merge(options[:controllers] || {}) end + + def select_models(options) + models = self.class.default_models.merge(self.scope_config[:models] || {}) + + models.each { |key,value| models[key] = value.constantize } + end + + def scope_config + ::Rails.application.config.devise_oauth2_providable.scope_settings[@scope_name] || {} + end end end end diff --git a/spec/lib/devise/oauth2_providable/mapping_spec.rb b/spec/lib/devise/oauth2_providable/mapping_spec.rb index 9ad52bc..1eb15d9 100644 --- a/spec/lib/devise/oauth2_providable/mapping_spec.rb +++ b/spec/lib/devise/oauth2_providable/mapping_spec.rb @@ -32,6 +32,47 @@ end end + describe "#models" do + subject { mapping_class.new(:users) } + + context "when no model overrides exist for the scope" do + before(:each) do + ::Rails.application.config. + stub_chain(:devise_oauth2_providable, :scope_settings). + and_return({}) + end + + it "the defaults are used" do + subject.models.should eql({ + :access_token => Devise::Oauth2Providable::AccessToken, + :client => Devise::Oauth2Providable::Client, + :refresh_token => Devise::Oauth2Providable::RefreshToken, + :user => User + }) + end + end + + context "when model overrides are specified" do + class MyTotallyAwesomeUser + end + + before(:each) do + ::Rails.application.config. + stub_chain(:devise_oauth2_providable, :scope_settings). + and_return({:user => {:models => {:user => 'MyTotallyAwesomeUser'}}}) + end + + it "uses the overrides instead" do + subject.models.should eql({ + :access_token => Devise::Oauth2Providable::AccessToken, + :client => Devise::Oauth2Providable::Client, + :refresh_token => Devise::Oauth2Providable::RefreshToken, + :user => MyTotallyAwesomeUser + }) + end + end + end + describe "#path_prefix" do context "is specified" do subject { mapping = mapping_class.new(:users, {:path_prefix => "member"}) }