Skip to content

Commit

Permalink
KBP-178 #time 2h - Implement basic authentication was done
Browse files Browse the repository at this point in the history
  • Loading branch information
hamdibayhan committed Dec 6, 2017
1 parent 251a5e9 commit 1b48709
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/cybele.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'cybele/helpers/error_pages'
require 'cybele/helpers/audited'
require 'cybele/helpers/routes'
require 'cybele/helpers/basic_authentication'
require 'cybele/helpers/app_files/assets_files'
require 'cybele/helpers/app_files/controller_files'
require 'cybele/helpers/app_files/model_files'
Expand Down
1 change: 1 addition & 0 deletions lib/cybele/app_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class AppBuilder < Rails::AppBuilder # rubocop:disable Metrics/ClassLength
include Cybele::Helpers::ErrorPages
include Cybele::Helpers::Audited
include Cybele::Helpers::Routes
include Cybele::Helpers::BasicAuthentication
include Cybele::Helpers::AppFiles::AssetsFiles
include Cybele::Helpers::AppFiles::ControllerFiles
include Cybele::Helpers::AppFiles::ModelFiles
Expand Down
1 change: 1 addition & 0 deletions lib/cybele/generators/app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def customize_app_files
build :customize_controller_files
build :add_devise_strong_parameter
build :add_devise_authenticate_admin
build :configure_basic_authentication
end

def setup_git_and_git_flow
Expand Down
44 changes: 44 additions & 0 deletions lib/cybele/helpers/basic_authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

module Cybele
module Helpers
module BasicAuthentication
def configure_basic_authentication
create_basic_authentication_files
add_to_settings_yml
add_to_dotenv_files
include_basic_authentication_module
configure_app_name(['config/settings.yml'])
end

private

def create_basic_authentication_files
# Initialize file
template 'basic_authentication/basic_authentication.rb',
'app/controllers/concerns/basic_authentication.rb',
force: true
end

def add_to_settings_yml
# Add basic authentication settings to the config/settings.yml file
append_file 'config/settings.yml',
template_content('basic_authentication/basic_authentication_settings.yml.erb')
end

def add_to_dotenv_files
# Add basic authentication env to the all env files
append_file('env.sample', template_content('basic_authentication/no_basic_authentication.erb'))
append_file('.env.local', template_content('basic_authentication/no_basic_authentication.erb'))
append_file('.env.staging', template_content('basic_authentication/yes_basic_authentication.erb'))
append_file('.env.production', template_content('basic_authentication/no_basic_authentication.erb'))
end

def include_basic_authentication_module
inject_into_file 'app/controllers/application_controller.rb',
template_content('basic_authentication/include_module.rb'),
after: 'class ApplicationController < ActionController::Base'
end
end
end
end
18 changes: 18 additions & 0 deletions spec/features/new_default_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,24 @@
expect(File).to exist(file_project_path('app/views/welcome/about.html.haml'))
expect(File).to exist(file_project_path('app/views/welcome/contact.html.haml'))
expect(File).to exist(file_project_path('app/views/welcome/index.html.haml'))

# Basic authentication files
expect(File).to exist(file_project_path('app/controllers/concerns/basic_authentication.rb'))

application_controller = content('app/controllers/application_controller.rb')
expect(application_controller).to match('include BasicAuthentication')

env_sample_file = content('env.sample')
expect(env_sample_file).to match('BASIC_AUTH_IS_ACTIVE=no')

env_local_file = content('.env.local')
expect(env_local_file).to match('BASIC_AUTH_IS_ACTIVE=no')

env_staging_file = content('.env.staging')
expect(env_staging_file).to match('BASIC_AUTH_IS_ACTIVE=yes')

env_production_file = content('.env.production')
expect(env_production_file).to match('BASIC_AUTH_IS_ACTIVE=no')
end

it 'uses default view files' do
Expand Down
17 changes: 17 additions & 0 deletions spec/features/new_not_default_project_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,23 @@
expect(File).not_to exist(file_project_path('app/views/devise/contact.html.haml'))
expect(File).not_to exist(file_project_path('app/views/devise/index.html.haml'))

# Basic authentication files
expect(File).not_to exist(file_project_path('app/controllers/concerns/basic_authentication.rb'))

application_controller = content('app/controllers/application_controller.rb')
expect(application_controller).not_to match('include BasicAuthentication')

env_sample_file = content('env.sample')
expect(env_sample_file).not_to match('BASIC_AUTH_IS_ACTIVE=no')

env_local_file = content('.env.local')
expect(env_local_file).not_to match('BASIC_AUTH_IS_ACTIVE=no')

env_staging_file = content('.env.staging')
expect(env_staging_file).not_to match('BASIC_AUTH_IS_ACTIVE=yes')

env_production_file = content('.env.production')
expect(env_production_file).not_to match('BASIC_AUTH_IS_ACTIVE=no')
end

it 'uses default view files' do
Expand Down
18 changes: 18 additions & 0 deletions templates/basic_authentication/basic_authentication.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module BasicAuthentication
extend ActiveSupport::Concern

included do
before_filter :authenticate
end

private

def authenticate
if ENV['BASIC_AUTH_IS_ACTIVE'] == 'yes'
authenticate_or_request_with_http_basic do |username, password|
username == Settings.basic_auth.username && password == Settings.basic_auth.password
end
end
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
basic_auth:
username: <%= app_name %>
password: <%= app_name %>
2 changes: 2 additions & 0 deletions templates/basic_authentication/include_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

include BasicAuthentication
2 changes: 2 additions & 0 deletions templates/basic_authentication/no_basic_authentication.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

BASIC_AUTH_IS_ACTIVE=no
2 changes: 2 additions & 0 deletions templates/basic_authentication/yes_basic_authentication.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

BASIC_AUTH_IS_ACTIVE=yes

0 comments on commit 1b48709

Please sign in to comment.