Sha256: 490bf8288efebf1abfdb23073768354211ae51060dccf706fa5f95becd2197eb

Contents?: true

Size: 1.17 KB

Versions: 1

Compression:

Stored size: 1.17 KB

Contents

module Doorkeeper
  module AccessGrantMixin
    extend ActiveSupport::Concern

    include OAuth::Helpers
    include Models::Expirable
    include Models::Revocable
    include Models::Accessible
    include Models::Scopes
    include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes)

    included do
      belongs_to_options = {
        class_name: 'Doorkeeper::Application',
        inverse_of: :access_grants
      }
      if defined?(ActiveRecord::Base) && ActiveRecord::VERSION::MAJOR >= 5
        belongs_to_options.merge!(optional: true)
      end

      belongs_to :application, belongs_to_options

      if respond_to?(:attr_accessible)
        attr_accessible :resource_owner_id, :application_id, :expires_in, :redirect_uri, :scopes
      end

      validates :resource_owner_id, :application_id, :token, :expires_in, :redirect_uri, presence: true
      validates :token, uniqueness: true

      before_validation :generate_token, on: :create
    end

    module ClassMethods
      def by_token(token)
        where(token: token.to_s).limit(1).to_a.first
      end
    end

    private

    def generate_token
      self.token = UniqueToken.generate
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
doorkeeper-4.0.0.rc2 lib/doorkeeper/models/access_grant_mixin.rb