From 8e2e0d5da48d81bf5d340231caa37526ae0f689a Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 29 Jul 2021 11:30:30 +0300 Subject: [PATCH 1/5] implemented authorization --- Gemfile | 2 + Gemfile.lock | 23 ++++++++ app/controllers/application_controller.rb | 11 ++++ app/controllers/home_controller.rb | 6 ++ app/controllers/users/sessions_controller.rb | 56 ++++++++++++++----- app/models/user.rb | 18 +++++- app/services/api_connector.rb | 32 +++++++++++ app/views/home/index.html.erb | 5 +- app/views/shared/_navbar.html.erb | 2 +- app/views/users/confirmations/new.html.erb | 16 ------ .../mailer/confirmation_instructions.html.erb | 5 -- app/views/users/mailer/email_changed.html.erb | 7 --- .../users/mailer/password_change.html.erb | 3 - .../reset_password_instructions.html.erb | 8 --- .../users/mailer/unlock_instructions.html.erb | 7 --- app/views/users/passwords/edit.html.erb | 25 --------- app/views/users/passwords/new.html.erb | 16 ------ app/views/users/registrations/edit.html.erb | 43 -------------- app/views/users/registrations/new.html.erb | 29 ---------- app/views/users/sessions/new.html.erb | 4 +- app/views/users/shared/_links.html.erb | 2 +- app/views/users/unlocks/new.html.erb | 16 ------ config/database.yml | 11 ++-- config/environment.rb | 1 + config/environments/development.rb | 2 + config/initializers/devise.rb | 2 +- config/routes.rb | 4 ++ .../20210728115257_rename_column_in_user.rb | 5 ++ .../20210728121505_add_username_to_users.rb | 6 ++ db/schema.rb | 4 +- db/seeds.rb | 9 ++- dig | 0 32 files changed, 173 insertions(+), 207 deletions(-) create mode 100644 app/services/api_connector.rb delete mode 100644 app/views/users/confirmations/new.html.erb delete mode 100644 app/views/users/mailer/confirmation_instructions.html.erb delete mode 100644 app/views/users/mailer/email_changed.html.erb delete mode 100644 app/views/users/mailer/password_change.html.erb delete mode 100644 app/views/users/mailer/reset_password_instructions.html.erb delete mode 100644 app/views/users/mailer/unlock_instructions.html.erb delete mode 100644 app/views/users/passwords/edit.html.erb delete mode 100644 app/views/users/passwords/new.html.erb delete mode 100644 app/views/users/registrations/edit.html.erb delete mode 100644 app/views/users/registrations/new.html.erb delete mode 100644 app/views/users/unlocks/new.html.erb create mode 100644 db/migrate/20210728115257_rename_column_in_user.rb create mode 100644 db/migrate/20210728121505_add_username_to_users.rb create mode 100644 dig diff --git a/Gemfile b/Gemfile index cd631cf2..9dd0e704 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,8 @@ gem 'devise' gem 'rails_admin', '~> 2.2' gem 'cancancan' gem 'rexml', '~> 3.2', '>= 3.2.4' +gem 'faraday' +gem 'faraday_middleware' group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console diff --git a/Gemfile.lock b/Gemfile.lock index 6f683959..5da7d779 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -107,6 +107,25 @@ GEM railties (>= 5.0.0) faker (2.18.0) i18n (>= 1.6, < 2) + faraday (1.5.1) + faraday-em_http (~> 1.0) + faraday-em_synchrony (~> 1.0) + faraday-excon (~> 1.1) + faraday-httpclient (~> 1.0.1) + faraday-net_http (~> 1.0) + faraday-net_http_persistent (~> 1.1) + faraday-patron (~> 1.0) + multipart-post (>= 1.2, < 3) + ruby2_keywords (>= 0.0.4) + faraday-em_http (1.0.0) + faraday-em_synchrony (1.0.0) + faraday-excon (1.1.0) + faraday-httpclient (1.0.1) + faraday-net_http (1.0.1) + faraday-net_http_persistent (1.2.0) + faraday-patron (1.0.0) + faraday_middleware (1.0.0) + faraday (~> 1.0) ffi (1.15.3) globalid (0.4.2) activesupport (>= 4.2.0) @@ -149,6 +168,7 @@ GEM mini_mime (1.1.0) minitest (5.14.4) msgpack (1.4.2) + multipart-post (2.1.1) nested_form (0.3.2) nio4r (2.5.7) nokogiri (1.11.7-x86_64-linux) @@ -241,6 +261,7 @@ GEM rspec-mocks (~> 3.10) rspec-support (~> 3.10) rspec-support (3.10.2) + ruby2_keywords (0.0.5) rubyzip (2.3.2) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) @@ -312,6 +333,8 @@ DEPENDENCIES devise factory_bot_rails faker + faraday + faraday_middleware jbuilder (~> 2.7) listen (~> 3.6) pg diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c797c7cb..91fca5a3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,6 +4,17 @@ class ApplicationController < ActionController::Base before_action :authenticate_user! before_action :set_user_answer + before_action :configure_permitted_parameters, if: :devise_controller? + + protected + + def configure_permitted_parameters + added_attrs = [:username, :email, :password, :password_confirmation, :remember_me] + devise_parameter_sanitizer.permit :sign_up, keys: added_attrs + devise_parameter_sanitizer.permit :sign_in, keys: [:login, :password] + devise_parameter_sanitizer.permit :account_update, keys: added_attrs + end + private def set_user_answer diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index be0d71ac..88bd266a 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,13 @@ +require_relative '../services/api_connector.rb' + + class HomeController < ApplicationController skip_before_action :authenticate_user!, :only => [:index] def index @quizzes = Quiz.all + + # test_request = ApiConnector.new(username: "oleghasjanov", password: "123456") + # @result = test_request.get_pull_message(method: :get) end end diff --git a/app/controllers/users/sessions_controller.rb b/app/controllers/users/sessions_controller.rb index a0f9b48e..f4dbe74b 100644 --- a/app/controllers/users/sessions_controller.rb +++ b/app/controllers/users/sessions_controller.rb @@ -1,27 +1,55 @@ # frozen_string_literal: true class Users::SessionsController < Devise::SessionsController - # before_action :configure_sign_in_params, only: [:create] + before_action :configure_sign_in_params, only: [:create] + before_action :login_request, only: [:create] # GET /resource/sign_in - # def new - # super - # end + def new + super + end # POST /resource/sign_in - # def create - # super - # end + def create + super + end # DELETE /resource/sign_out - # def destroy - # super - # end + def destroy + super + end - # protected + protected # If you have extra params to permit, append them to the sanitizer. - # def configure_sign_in_params - # devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) - # end + def configure_sign_in_params + devise_parameter_sanitizer.permit(:sign_in, keys: [:attribute]) + end + + def login_request + user_request = ApiConnector.new(username: params[:user][:username], password: params[:user][:password]) + result = user_request.sign_in + + checking_username(result) + end + + def checking_username(result) + if result["code"] == 1000 + username = result["data"]["username"] + user = User.find_by(username: username) + + if user.present? + sign_in user + else + new_user = User.create!( + username: username, + superadmin_role: false) + + sign_in new_user + end + + else + Rails.logger.info "Fails to sign in" + end + end end diff --git a/app/models/user.rb b/app/models/user.rb index 3eafb4d5..0b4e08e0 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,8 +2,24 @@ class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, - :recoverable, :rememberable, :validatable + :recoverable, :rememberable, :validatable, authentication_keys: [:username] has_many :quizzes has_many :user_answers + + validates :email, presence: false + + attr_writer :login + + def email_required? + false + end + + def password_required? + false + end + + def login + @login || self.username || self.email + end end diff --git a/app/services/api_connector.rb b/app/services/api_connector.rb new file mode 100644 index 00000000..ab96e99c --- /dev/null +++ b/app/services/api_connector.rb @@ -0,0 +1,32 @@ +require 'faraday' + +class ApiConnector + attr_reader :auth_token + + POLL_MESSAGE_ENDPOINT = "http://registry:3000/repp/v1/registrar/login" + + def initialize(username:, password:) + @auth_token = generate_token(username: username, password: password) + end + + def sign_in(params: nil) + request = faraday_request(url: POLL_MESSAGE_ENDPOINT, params: params) + response = request.send(:get) + JSON.parse(response.body) + end + + private + + def generate_token(username:, password:) + Base64.urlsafe_encode64("#{username}:#{password}") + end + + def faraday_request(url:, params: {}) + Faraday.new( + url: url, + headers: { 'Authorization' => "Basic #{@auth_token}" }, + params: params, + ssl: { verify: false} + ) + end +end \ No newline at end of file diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index c1a27794..3e3212b0 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,5 +1,8 @@ <% if user_signed_in? %>
+ + <%= @result %> +

Your quizzes:

<% if @quizzes.any? %> @@ -36,7 +39,7 @@