Use standard login form

This commit is contained in:
Artur Beljajev 2018-08-09 13:43:11 +03:00
parent 080defa228
commit 1d95c1ccbc
6 changed files with 45 additions and 38 deletions

View file

@ -3,12 +3,8 @@ class Registrar
before_action :check_ip_restriction
helper_method :depp_controller?
def new
@depp_user = Depp::User.new
end
def create
@depp_user = Depp::User.new(params[:depp_user].merge(pki: !(Rails.env.development? || Rails.env.test?)))
@depp_user = Depp::User.new(depp_user_params)
if @depp_user.pki && request.env['HTTP_SSL_CLIENT_S_DN_CN'].blank?
@depp_user.errors.add(:base, :webserver_missing_user_name_directive)
@ -26,12 +22,12 @@ class Registrar
@depp_user.errors.add(:base, :webserver_client_cert_directive_should_be_required)
end
@api_user = ApiUser.find_by(username: params[:depp_user][:tag],
plain_text_password: params[:depp_user][:password])
@api_user = ApiUser.find_by(username: sign_in_params[:username],
plain_text_password: sign_in_params[:password])
unless @api_user
@depp_user.errors.add(:base, t(:no_such_user))
render :new and return
show_error and return
end
if @depp_user.pki
@ -45,10 +41,10 @@ class Registrar
sign_in_and_redirect(:registrar_user, @api_user)
else
@depp_user.errors.add(:base, :not_active)
render :new
show_error and return
end
else
render :new
show_error and return
end
end
@ -190,5 +186,16 @@ class Registrar
def user_for_paper_trail
current_registrar_user ? current_registrar_user.id_role_username : 'anonymous'
end
def depp_user_params
params = sign_in_params
params[:tag] = params.delete(:username)
params.merge!(pki: !(Rails.env.development? || Rails.env.test?))
params
end
def show_error
redirect_to new_registrar_user_session_url, alert: @depp_user.errors.full_messages.first
end
end
end

View file

@ -2,7 +2,7 @@ require 'open3'
class ApiUser < User
include EppErrors
devise :database_authenticatable, :trackable, :timeoutable
devise :database_authenticatable, :trackable, :timeoutable, authentication_keys: [:username]
def epp_code_map
{

View file

@ -1,24 +1,29 @@
<div class="row">
<div class="form-signin col-md-6 center-block text-center">
<h2 class="form-signin-heading text-center">
<%= t(:log_in) %>
</h2>
<hr/>
<%= form_for @depp_user, url: registrar_user_session_path, html: { class: 'form-signin' } do |f| %>
<%= render 'registrar/shared/errors', object: f.object %>
<% error_class = f.object.errors.any? ? 'has-error' : '' %>
<div class="<%= error_class %>">
<%= f.text_field :tag, class: 'form-control', placeholder: t(:username), required: true %>
<%= f.password_field :password, class: 'form-control', autocomplete: 'off', placeholder: t(:password), required: true %>
</div>
<button class="btn btn-lg btn-primary btn-block" type="submit">
<%= t '.login_btn' %>
</button>
<h1 class="form-signin-heading text-center"><%= t(:log_in) %></h1>
<hr>
<%= form_for resource, as: resource_name, url: session_path(resource_name) do |f| %>
<%= f.text_field :username, placeholder: ApiUser.human_attribute_name(:username),
autofocus: true,
required: true,
class: 'form-control' %>
<%= f.password_field :password,
autocomplete: 'off',
placeholder: ApiUser.human_attribute_name(:password),
required: true,
class: 'form-control' %>
<%= f.submit t('.submit_btn'), class: 'btn btn-lg btn-primary btn-block' %>
<% end %>
<hr/>
<hr>
<%= link_to '/registrar/login/mid', id: 'login-with-mobile-id-btn' do %>
<%= image_tag 'mid.gif' %>
<% end %>
<%= link_to '/registrar/id', method: :post do %>
<%= image_tag 'id_card.gif' %>
<% end %>

View file

@ -1,5 +0,0 @@
- if object.errors.any?
%p.text-danger
- object.errors.each do |attr, err|
= err
%br

View file

@ -2,6 +2,6 @@ en:
registrar:
sessions:
new:
login_btn: Login
submit_btn: Login
login_mid:
login_btn: Login

View file

@ -7,8 +7,8 @@ class RegistrarAreaPasswordSignInTest < ApplicationSystemTestCase
def test_correct_username_and_password
visit new_registrar_user_session_url
fill_in 'depp_user_tag', with: @user.username
fill_in 'depp_user_password', with: 'testtest'
fill_in 'registrar_user_username', with: @user.username
fill_in 'registrar_user_password', with: 'testtest'
click_button 'Login'
assert_text 'Log out'
@ -17,8 +17,8 @@ class RegistrarAreaPasswordSignInTest < ApplicationSystemTestCase
def test_wrong_password
visit new_registrar_user_session_url
fill_in 'depp_user_tag', with: @user.username
fill_in 'depp_user_password', with: 'wrong'
fill_in 'registrar_user_username', with: @user.username
fill_in 'registrar_user_password', with: 'wrong'
click_button 'Login'
assert_text 'No such user'
@ -29,8 +29,8 @@ class RegistrarAreaPasswordSignInTest < ApplicationSystemTestCase
@user.update!(active: false)
visit new_registrar_user_session_url
fill_in 'depp_user_tag', with: @user.username
fill_in 'depp_user_password', with: 'testtest'
fill_in 'registrar_user_username', with: @user.username
fill_in 'registrar_user_password', with: 'testtest'
click_button 'Login'
assert_text 'User is not active'