From 1b4870908dda19395d3c9bd81c0a18805c9564bf Mon Sep 17 00:00:00 2001 From: hamdibayhan Date: Wed, 6 Dec 2017 18:19:39 +0300 Subject: [PATCH] KBP-178 #time 2h - Implement basic authentication was done --- lib/cybele.rb | 1 + lib/cybele/app_builder.rb | 1 + lib/cybele/generators/app_generator.rb | 1 + lib/cybele/helpers/basic_authentication.rb | 44 +++++++++++++++++++ spec/features/new_default_project_spec.rb | 18 ++++++++ spec/features/new_not_default_project_spec.rb | 17 +++++++ .../basic_authentication.rb | 18 ++++++++ .../basic_authentication_settings.yml.erb | 3 ++ .../basic_authentication/include_module.rb | 2 + .../no_basic_authentication.erb | 2 + .../yes_basic_authentication.erb | 2 + 11 files changed, 109 insertions(+) create mode 100644 lib/cybele/helpers/basic_authentication.rb create mode 100644 templates/basic_authentication/basic_authentication.rb create mode 100644 templates/basic_authentication/basic_authentication_settings.yml.erb create mode 100644 templates/basic_authentication/include_module.rb create mode 100644 templates/basic_authentication/no_basic_authentication.erb create mode 100644 templates/basic_authentication/yes_basic_authentication.erb diff --git a/lib/cybele.rb b/lib/cybele.rb index 8596d9c..acad501 100644 --- a/lib/cybele.rb +++ b/lib/cybele.rb @@ -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' diff --git a/lib/cybele/app_builder.rb b/lib/cybele/app_builder.rb index 8ae2794..ca6c3a3 100644 --- a/lib/cybele/app_builder.rb +++ b/lib/cybele/app_builder.rb @@ -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 diff --git a/lib/cybele/generators/app_generator.rb b/lib/cybele/generators/app_generator.rb index 604a694..7df5bba 100644 --- a/lib/cybele/generators/app_generator.rb +++ b/lib/cybele/generators/app_generator.rb @@ -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 diff --git a/lib/cybele/helpers/basic_authentication.rb b/lib/cybele/helpers/basic_authentication.rb new file mode 100644 index 0000000..c2d81fb --- /dev/null +++ b/lib/cybele/helpers/basic_authentication.rb @@ -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 diff --git a/spec/features/new_default_project_spec.rb b/spec/features/new_default_project_spec.rb index 9e10521..c42ce2a 100644 --- a/spec/features/new_default_project_spec.rb +++ b/spec/features/new_default_project_spec.rb @@ -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 diff --git a/spec/features/new_not_default_project_spec.rb b/spec/features/new_not_default_project_spec.rb index 32e7407..db2b262 100644 --- a/spec/features/new_not_default_project_spec.rb +++ b/spec/features/new_not_default_project_spec.rb @@ -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 diff --git a/templates/basic_authentication/basic_authentication.rb b/templates/basic_authentication/basic_authentication.rb new file mode 100644 index 0000000..28fe8ae --- /dev/null +++ b/templates/basic_authentication/basic_authentication.rb @@ -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 \ No newline at end of file diff --git a/templates/basic_authentication/basic_authentication_settings.yml.erb b/templates/basic_authentication/basic_authentication_settings.yml.erb new file mode 100644 index 0000000..25e4259 --- /dev/null +++ b/templates/basic_authentication/basic_authentication_settings.yml.erb @@ -0,0 +1,3 @@ +basic_auth: + username: <%= app_name %> + password: <%= app_name %> \ No newline at end of file diff --git a/templates/basic_authentication/include_module.rb b/templates/basic_authentication/include_module.rb new file mode 100644 index 0000000..6b8d107 --- /dev/null +++ b/templates/basic_authentication/include_module.rb @@ -0,0 +1,2 @@ + + include BasicAuthentication \ No newline at end of file diff --git a/templates/basic_authentication/no_basic_authentication.erb b/templates/basic_authentication/no_basic_authentication.erb new file mode 100644 index 0000000..6a650cf --- /dev/null +++ b/templates/basic_authentication/no_basic_authentication.erb @@ -0,0 +1,2 @@ + +BASIC_AUTH_IS_ACTIVE=no \ No newline at end of file diff --git a/templates/basic_authentication/yes_basic_authentication.erb b/templates/basic_authentication/yes_basic_authentication.erb new file mode 100644 index 0000000..b38be1c --- /dev/null +++ b/templates/basic_authentication/yes_basic_authentication.erb @@ -0,0 +1,2 @@ + +BASIC_AUTH_IS_ACTIVE=yes \ No newline at end of file