Skip to content

Commit

Permalink
Merge pull request #1325 from ruby-china/not_require_model_in_initial…
Browse files Browse the repository at this point in the history
…izer

Not directly require model in initialize process
  • Loading branch information
qichunren authored Feb 20, 2022
2 parents 29d06ba + 45d5184 commit 69abd60
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 170 deletions.
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Application < Rails::Application

config.to_prepare do
Devise::Mailer.layout "mailer"
end

config.after_initialize do
Doorkeeper::ApplicationController.include Homeland::UserNotificationHelper
# Only Applications list
Doorkeeper::ApplicationsController.layout "simple"
Expand Down
11 changes: 7 additions & 4 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "active_support/core_ext/integer/time"
require_relative "../../app/models/setting"

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.
Expand Down Expand Up @@ -32,8 +31,10 @@
config.assets.compile = false

# Enable serving of images, stylesheets, and JavaScripts from an asset server.
if Setting.asset_host.present?
config.asset_host = Setting.asset_host
config.to_prepare do
if Setting.asset_host.present?
Rails.application.config.asset_host = Setting.asset_host
end
end

# Specifies the header that your server uses for sending files.
Expand All @@ -47,7 +48,9 @@
# config.action_cable.mount_path = nil
# config.action_cable.url = 'wss://example.com/cable'
# config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
config.action_cable.allowed_request_origins = [Setting.cable_allowed_request_origin]
config.to_prepare do
Rails.application.config.action_cable.allowed_request_origins = [Setting.cable_allowed_request_origin]
end

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
config.force_ssl = false
Expand Down
6 changes: 3 additions & 3 deletions config/initializers/_setup_mailer.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require_relative "../../app/models/setting"

ActionMailer::Base.default_url_options = {host: Setting.domain, protocol: Setting.protocol}
Rails.application.config.to_prepare do
ActionMailer::Base.default_url_options = {host: Setting.domain, protocol: Setting.protocol}
end
64 changes: 33 additions & 31 deletions config/initializers/carrierwave.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,39 @@ def retrieve!(_identifier)
end
end

CarrierWave.configure do |config|
# http://stackoverflow.com/questions/7534341/rails-3-test-fixtures-with-carrierwave/25315883#25315883
config.storage NullStorage if Rails.env.test?

case Setting.upload_provider
when "aliyun"
config.storage = :aliyun
config.aliyun_access_key_id = Setting.upload_access_id
config.aliyun_access_key_secret = Setting.upload_access_secret
config.aliyun_bucket = Setting.upload_bucket
config.aliyun_internal = Setting.upload_aliyun_internal.to_s != "false"
config.aliyun_region = Setting.upload_aliyun_region
if Setting.upload_url.present?
config.aliyun_host = Setting.upload_url
Rails.application.config.to_prepare do
CarrierWave.configure do |config|
# http://stackoverflow.com/questions/7534341/rails-3-test-fixtures-with-carrierwave/25315883#25315883
config.storage NullStorage if Rails.env.test?

case Setting.upload_provider
when "aliyun"
config.storage = :aliyun
config.aliyun_access_key_id = Setting.upload_access_id
config.aliyun_access_key_secret = Setting.upload_access_secret
config.aliyun_bucket = Setting.upload_bucket
config.aliyun_internal = Setting.upload_aliyun_internal.to_s != "false"
config.aliyun_region = Setting.upload_aliyun_region
if Setting.upload_url.present?
config.aliyun_host = Setting.upload_url
end
when "upyun"
config.storage = :upyun
# Do not remove previously file after new file uploaded
config.remove_previously_stored_files_after_update = false
config.upyun_username = Setting.upload_access_id
config.upyun_password = Setting.upload_access_secret
config.upyun_bucket = Setting.upload_bucket
config.upyun_bucket_host = Setting.upload_url
when "qiniu"
config.storage = :qiniu
config.qiniu_access_key = Setting.upload_access_id
config.qiniu_secret_key = Setting.upload_access_secret
config.qiniu_bucket = Setting.upload_bucket
config.qiniu_protocol = Setting.upload_url.split("://")[0]
config.qiniu_bucket_domain = Setting.upload_url.split("://")[1]
else
config.storage = :file
end
when "upyun"
config.storage = :upyun
# Do not remove previously file after new file uploaded
config.remove_previously_stored_files_after_update = false
config.upyun_username = Setting.upload_access_id
config.upyun_password = Setting.upload_access_secret
config.upyun_bucket = Setting.upload_bucket
config.upyun_bucket_host = Setting.upload_url
when "qiniu"
config.storage = :qiniu
config.qiniu_access_key = Setting.upload_access_id
config.qiniu_secret_key = Setting.upload_access_secret
config.qiniu_bucket = Setting.upload_bucket
config.qiniu_protocol = Setting.upload_url.split("://")[0]
config.qiniu_bucket_domain = Setting.upload_url.split("://")[1]
else
config.storage = :file
end
end
218 changes: 110 additions & 108 deletions config/initializers/doorkeeper.rb
Original file line number Diff line number Diff line change
@@ -1,118 +1,120 @@
# frozen_string_literal: true

Doorkeeper.configure do
# Change the ORM that doorkeeper will use.
# Currently supported options are :active_record, :mongoid2, :mongoid3,
# :mongoid4, :mongo_mapper
orm :active_record

# This block will be called to check whether the resource owner is authenticated or not.
resource_owner_authenticator do
current_user || redirect_to(new_user_session_url)
end
Rails.application.config.to_prepare do
Doorkeeper.configure do
# Change the ORM that doorkeeper will use.
# Currently supported options are :active_record, :mongoid2, :mongoid3,
# :mongoid4, :mongo_mapper
orm :active_record

# This block will be called to check whether the resource owner is authenticated or not.
resource_owner_authenticator do
current_user || redirect_to(new_user_session_url)
end

resource_owner_from_credentials do
request.params[:user] = {login: request.params[:username], password: request.params[:password]}
request.env["devise.allow_params_authentication"] = true
# 清理之前的 warden 信息
request.env["warden"].logout(:user)
resource = request.env["warden"].authenticate(scope: :user)
resource
end
resource_owner_from_credentials do
request.params[:user] = {login: request.params[:username], password: request.params[:password]}
request.env["devise.allow_params_authentication"] = true
# 清理之前的 warden 信息
request.env["warden"].logout(:user)
resource = request.env["warden"].authenticate(scope: :user)
resource
end

# If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below.
admin_authenticator do
current_user
end
# If you want to restrict access to the web interface for adding oauth authorized applications, you need to declare the block below.
admin_authenticator do
current_user
end

# Authorization Code expiration time (default 10 minutes).
# authorization_code_expires_in 10.minutes

# Access token expiration time (default 2 hours).
# If you want to disable expiration, set this to nil.
access_token_expires_in 1.days

# Assign a custom TTL for implicit grants.
custom_access_token_expires_in do |context|
application = context.client.is_a?(Doorkeeper::Application) ? context.client : context.client&.application
case application&.level
when 1 then 7.days
when 2 then 14.days
when 3 then 30.days
else
1.days
# Authorization Code expiration time (default 10 minutes).
# authorization_code_expires_in 10.minutes

# Access token expiration time (default 2 hours).
# If you want to disable expiration, set this to nil.
access_token_expires_in 1.days

# Assign a custom TTL for implicit grants.
custom_access_token_expires_in do |context|
application = context.client.is_a?(Doorkeeper::Application) ? context.client : context.client&.application
case application&.level
when 1 then 7.days
when 2 then 14.days
when 3 then 30.days
else
1.days
end
end
end

# Use a custom class for generating the access token.
# https://github.com/doorkeeper-gem/doorkeeper#custom-access-token-generator
# access_token_generator "::Doorkeeper::JWT"

# Reuse access token for the same resource owner within an application (disabled by default)
# Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383
# reuse_access_token

# Issue access tokens with refresh token (disabled by default)
use_refresh_token

# Provide support for an owner to be assigned to each registered application (disabled by default)
# Optional parameter :confirmation => true (default false) if you want to enforce ownership of
# a registered application
# Note: you must also run the rails g doorkeeper:application_owner generator to provide the necessary support
enable_application_owner confirmation: true

# Define access token scopes for your provider
# For more information go to
# https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
default_scopes :all
# optional_scopes :write, :update

# Change the way client credentials are retrieved from the request object.
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
# falls back to the `:client_id` and `:client_secret` params from the `params` object.
# Check out the wiki for more information on customization
# client_credentials :from_basic, :from_params

# Change the way access token is authenticated from the request object.
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
# falls back to the `:access_token` or `:bearer_token` params from the `params` object.
# Check out the wiki for more information on customization
# access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param

# Forces the usage of the HTTPS protocol in non-native redirect uris (enabled
# by default in non-development environments). OAuth2 delegates security in
# communication to the HTTPS protocol so it is wise to keep this enabled.
#
force_ssl_in_redirect_uri false

# Specify what grant flows are enabled in array of Strings. The valid
# strings and the flows they enable are:
#
# "authorization_code" => Authorization Code Grant Flow
# "implicit" => Implicit Grant Flow
# "password" => Resource Owner Password Credentials Grant Flow
# "client_credentials" => Client Credentials Grant Flow
#
# If not specified, Doorkeeper enables authorization_code and
# client_credentials.
#
# implicit and password grant flows have risks that you should understand
# before enabling:
# http://tools.ietf.org/html/rfc6819#section-4.4.2
# http://tools.ietf.org/html/rfc6819#section-4.4.3
#
# grant_flows %w(authorization_code client_credentials)
grant_flows %w[authorization_code client_credentials password]

# Under some circumstances you might want to have applications auto-approved,
# so that the user skips the authorization step.
# For example if dealing with a trusted application.
# skip_authorization do |resource_owner, client|
# client.superapp? or resource_owner.admin?
# end

# WWW-Authenticate Realm (default "Doorkeeper").
realm Setting.app_name
# Use a custom class for generating the access token.
# https://github.com/doorkeeper-gem/doorkeeper#custom-access-token-generator
# access_token_generator "::Doorkeeper::JWT"

# Reuse access token for the same resource owner within an application (disabled by default)
# Rationale: https://github.com/doorkeeper-gem/doorkeeper/issues/383
# reuse_access_token

# Issue access tokens with refresh token (disabled by default)
use_refresh_token

# Provide support for an owner to be assigned to each registered application (disabled by default)
# Optional parameter :confirmation => true (default false) if you want to enforce ownership of
# a registered application
# Note: you must also run the rails g doorkeeper:application_owner generator to provide the necessary support
enable_application_owner confirmation: true

# Define access token scopes for your provider
# For more information go to
# https://github.com/doorkeeper-gem/doorkeeper/wiki/Using-Scopes
default_scopes :all
# optional_scopes :write, :update

# Change the way client credentials are retrieved from the request object.
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
# falls back to the `:client_id` and `:client_secret` params from the `params` object.
# Check out the wiki for more information on customization
# client_credentials :from_basic, :from_params

# Change the way access token is authenticated from the request object.
# By default it retrieves first from the `HTTP_AUTHORIZATION` header, then
# falls back to the `:access_token` or `:bearer_token` params from the `params` object.
# Check out the wiki for more information on customization
# access_token_methods :from_bearer_authorization, :from_access_token_param, :from_bearer_param

# Forces the usage of the HTTPS protocol in non-native redirect uris (enabled
# by default in non-development environments). OAuth2 delegates security in
# communication to the HTTPS protocol so it is wise to keep this enabled.
#
force_ssl_in_redirect_uri false

# Specify what grant flows are enabled in array of Strings. The valid
# strings and the flows they enable are:
#
# "authorization_code" => Authorization Code Grant Flow
# "implicit" => Implicit Grant Flow
# "password" => Resource Owner Password Credentials Grant Flow
# "client_credentials" => Client Credentials Grant Flow
#
# If not specified, Doorkeeper enables authorization_code and
# client_credentials.
#
# implicit and password grant flows have risks that you should understand
# before enabling:
# http://tools.ietf.org/html/rfc6819#section-4.4.2
# http://tools.ietf.org/html/rfc6819#section-4.4.3
#
# grant_flows %w(authorization_code client_credentials)
grant_flows %w[authorization_code client_credentials password]

# Under some circumstances you might want to have applications auto-approved,
# so that the user skips the authorization step.
# For example if dealing with a trusted application.
# skip_authorization do |resource_owner, client|
# client.superapp? or resource_owner.admin?
# end

# WWW-Authenticate Realm (default "Doorkeeper").
realm Setting.app_name
end
end

# https://github.com/doorkeeper-gem/doorkeeper/issues/1467
Expand Down
Loading

0 comments on commit 69abd60

Please # to comment.