Skip to content

Commit

Permalink
Latest changes to support custom models
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed Mar 25, 2020
1 parent 5ec6c2c commit 621f5a1
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 22 deletions.
5 changes: 2 additions & 3 deletions lib/doorkeeper-mongodb/mixins/mongoid/access_grant_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ module AccessGrantMixin
inverse_of: :access_grants,
}

# Doorkeeper 5.3 has custom classes for defining OAuth roles
if DoorkeeperMongodb.doorkeeper_version?(5, 3)
belongs_to_opts[:class_name] = Doorkeeper.config.application_class
end

# optional associations added in Mongoid 6
if ::Mongoid::VERSION[0].to_i >= 6
belongs_to_opts[:optional] = true
end
belongs_to_opts[:optional] = true if ::Mongoid::VERSION[0].to_i >= 6

belongs_to :application, belongs_to_opts

Expand Down
9 changes: 3 additions & 6 deletions lib/doorkeeper-mongodb/mixins/mongoid/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@ module AccessTokenMixin
inverse_of: :access_tokens,
}

# Doorkeeper 5.3 has custom classes for defining OAuth roles
if DoorkeeperMongodb.doorkeeper_version?(5, 3)
belongs_to_opts[:class_name] = Doorkeeper.config.application_class
end

# optional associations added in Mongoid 6
if ::Mongoid::VERSION[0].to_i >= 6
belongs_to_opts[:optional] = true
end
belongs_to_opts[:optional] = true if ::Mongoid::VERSION[0].to_i >= 6

belongs_to :application, belongs_to_opts

if Doorkeeper::VERSION::MINOR > 3 && Doorkeeper.config.polymorphic_resource_owner?
opts = { polymorphic: true }

if ::Mongoid::VERSION[0].to_i >= 6
opts[:optional] = true
end
opts[:optional] = true if ::Mongoid::VERSION[0].to_i >= 6

belongs_to :resource_owner, opts
end
Expand Down
31 changes: 25 additions & 6 deletions lib/doorkeeper-mongodb/mixins/mongoid/application_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,38 @@ module ApplicationMixin
included do
has_many_options = {
dependent: :delete,

}

# Mongoid7 dropped :delete option
if ::Mongoid::VERSION[0].to_i >= 7
has_many_options[:dependent] = :delete_all
end
has_many_options[:dependent] = :delete_all if ::Mongoid::VERSION[0].to_i >= 7

# Doorkeeper 5.3 has custom classes for defining OAuth roles
access_grants_class_name = if DoorkeeperMongodb.doorkeeper_version?(5, 3)
Doorkeeper.config.access_grant_class
else
"Doorkeeper::AccessGrant"
end

access_tokens_class_name = if DoorkeeperMongodb.doorkeeper_version?(5, 3)
Doorkeeper.config.access_token_class
else
"Doorkeeper::AccessToken"
end

has_many :access_grants, has_many_options.merge(class_name: "Doorkeeper::AccessGrant")
has_many :access_tokens, has_many_options.merge(class_name: "Doorkeeper::AccessToken")
has_many :access_grants, has_many_options.merge(class_name: access_grants_class_name)
has_many :access_tokens, has_many_options.merge(class_name: access_tokens_class_name)

validates :name, :secret, :uid, presence: true
validates :uid, uniqueness: true
validates :redirect_uri, "doorkeeper/redirect_uri": true

# Before Doorkeeper 5.2.3
if defined?(::RedirectUriValidator)
validates :redirect_uri, redirect_uri: true
else
validates :redirect_uri, "doorkeeper/redirect_uri": true
end

validates :confidential, inclusion: { in: [true, false] }

validate :scopes_match_configured, if: :enforce_scopes?
Expand Down
4 changes: 1 addition & 3 deletions lib/doorkeeper/orm/concerns/mongoid/ownership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ module Ownership
included do
belongs_to_options = { polymorphic: true }

if ::Mongoid::VERSION[0].to_i >= 6
belongs_to_options[:optional] = true
end
belongs_to_options[:optional] = true if ::Mongoid::VERSION[0].to_i >= 6

belongs_to :owner, belongs_to_options
validates :owner, presence: true, if: :validate_owner?
Expand Down
1 change: 1 addition & 0 deletions lib/doorkeeper/orm/mongoid4/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Application
class_name: "Doorkeeper::AccessToken",
}

# Doorkeeper 5.3 has custom classes for defining OAuth roles
if DoorkeeperMongodb.doorkeeper_version?(5, 3)
has_many_opts[:class_name] = Doorkeeper.config.access_token_class
end
Expand Down
1 change: 1 addition & 0 deletions lib/doorkeeper/orm/mongoid5/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Application
class_name: "Doorkeeper::AccessToken",
}

# Doorkeeper 5.3 has custom classes for defining OAuth roles
if DoorkeeperMongodb.doorkeeper_version?(5, 3)
has_many_opts[:class_name] = Doorkeeper.config.access_token_class
end
Expand Down
1 change: 1 addition & 0 deletions lib/doorkeeper/orm/mongoid6/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Application
class_name: "Doorkeeper::AccessToken",
}

# Doorkeeper 5.3 has custom classes for defining OAuth roles
if DoorkeeperMongodb.doorkeeper_version?(5, 3)
has_many_opts[:class_name] = Doorkeeper.config.access_token_class
end
Expand Down
1 change: 1 addition & 0 deletions lib/doorkeeper/orm/mongoid7/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Application
class_name: "Doorkeeper::AccessToken",
}

# Doorkeeper 5.3 has custom classes for defining OAuth roles
if DoorkeeperMongodb.doorkeeper_version?(5, 3)
has_many_opts[:class_name] = Doorkeeper.config.access_token_class
end
Expand Down
6 changes: 2 additions & 4 deletions spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# frozen_string_literal: true

# [NOTE] MongoMapper support was dropped
# [NOTE] MongoMapper support was dropped, so now only Mongoid
class User
include Mongoid::Document
include Mongoid::Timestamps

field :name, type: String
field :password, type: String

if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes)
attr_accessible :name, :password
end
attr_accessible :name, :password if defined?(::ProtectedAttributes)

def self.authenticate!(name, password)
User.where(name: name, password: password).first
Expand Down

0 comments on commit 621f5a1

Please sign in to comment.