Added basic Registrant portal

This commit is contained in:
Priit Tark 2015-05-13 13:02:08 +03:00
parent 90633160c0
commit fdcad95683
28 changed files with 810 additions and 26 deletions

View file

@ -12,11 +12,14 @@ class ApplicationController < ActionController::Base
end
rescue_from CanCan::AccessDenied do |exception|
redirect_to admin_root_path, alert: exception.message if current_user.is_a?(AdminUser)
redirect_to registrar_root_path, alert: exception.message if current_user.is_a?(ApiUser)
redirect_to current_root_url, alert: exception.message
end
helper_method :registrant_request?, :registrar_request?, :admin_request?, :current_root_url
def registrant_request?
request.path.match(/^\/registrant/)
end
helper_method :registrar_request?, :admin_request?
def registrar_request?
request.path.match(/^\/registrar/)
end
@ -25,21 +28,28 @@ class ApplicationController < ActionController::Base
request.path.match(/^\/admin/)
end
def after_sign_in_path_for(_resource)
rt = session[:user_return_to].to_s.presence
login_paths = [admin_login_path, registrar_login_path, '/login']
return rt if rt && !login_paths.include?(rt)
def current_root_url
if registrar_request?
registrar_root_url
elsif registrant_request?
registrar_root_url
elsif admin_request?
admin_root_url
end
end
def after_sign_in_path_for(_resource)
rt = session[:user_return_to].to_s.presence
login_paths = [admin_login_path, registrar_login_path, '/login']
return rt if rt && !login_paths.include?(rt)
current_root_url
end
def after_sign_out_path_for(_resource)
if registrar_request?
registrar_login_url
elsif registrant_request?
registrant_login_url
elsif admin_request?
admin_login_url
end

View file

@ -0,0 +1,32 @@
class Registrant::DeppController < RegistrantController # EPP controller
helper_method :depp_current_user
rescue_from(Errno::ECONNRESET, Errno::ECONNREFUSED) do |_exception|
redirect_to registrant_login_url, alert: t(:no_connection_to_registry)
end
before_action :authenticate_user
def authenticate_user
redirect_to registrant_login_url and return unless depp_current_user
end
def depp_controller?
true
end
def depp_current_user
return nil unless current_user
@depp_current_user ||= Depp::User.new(
tag: current_user.username,
password: current_user.password
)
end
def response_ok?
@data.css('result').each do |x|
success_codes = %(1000, 1001, 1300, 1301)
return false unless success_codes.include?(x['code'])
end
true
end
end

View file

@ -0,0 +1,119 @@
class Registrant::DomainsController < Registrant::DeppController # EPP controller
before_action :init_domain, except: :new
def index
authorize! :view, Depp::Domain
limit, offset = pagination_details
res = depp_current_user.repp_request('domains', { details: true, limit: limit, offset: offset })
if res.code == '200'
@response = res.parsed_body.with_indifferent_access
@contacts = @response ? @response[:contacts] : []
@paginatable_array = Kaminari.paginate_array(
[], total_count: @response[:total_number_of_records]
).page(params[:page]).per(limit)
end
flash.now[:epp_results] = [{ 'code' => res.code, 'msg' => res.message }]
end
def info
authorize! :view, Depp::Domain
@data = @domain.info(params[:domain_name]) if params[:domain_name]
if response_ok?
render 'info'
else
flash[:alert] = t(:domain_not_found)
redirect_to registrant_domains_url and return
end
end
def check
authorize! :view, Depp::Domain
if params[:domain_name]
@data = @domain.check(params[:domain_name])
render 'check_index' and return unless response_ok?
else
render 'check_index'
end
end
def new
authorize! :create, Depp::Domain
@domain_params = Depp::Domain.default_params
end
def create
authorize! :create, Depp::Domain
@domain_params = params[:domain]
@data = @domain.create(@domain_params)
if response_ok?
redirect_to info_registrant_domains_url(domain_name: @domain_params[:name])
else
render 'new'
end
end
def edit
authorize! :update, Depp::Domain
@data = @domain.info(params[:domain_name])
@domain_params = Depp::Domain.construct_params_from_server_data(@data)
end
def update
authorize! :update, Depp::Domain
@domain_params = params[:domain]
@data = @domain.update(@domain_params)
if response_ok?
redirect_to info_registrant_domains_url(domain_name: @domain_params[:name])
else
params[:domain_name] = @domain_params[:name]
render 'new'
end
end
def delete
authorize! :delete, Depp::Domain
end
def destroy
authorize! :delete, Depp::Domain
@data = @domain.delete(params[:domain])
@results = @data.css('result')
if response_ok?
params[:domain_name] = nil
render 'info_index'
else
params[:domain_name] = params[:domain][:name]
render 'delete'
end
end
def renew
authorize! :renew, Depp::Domain
if params[:domain_name] && params[:cur_exp_date]
@data = @domain.renew(params)
render 'renew_index' and return unless response_ok?
else
render 'renew_index'
end
end
def transfer
authorize! :transfer, Depp::Domain
if params[:domain_name]
@data = @domain.transfer(params)
render 'transfer_index' and return unless response_ok?
else
render 'transfer_index'
end
end
private
def init_domain
@domain = Depp::Domain.new(current_user: depp_current_user)
end
end

View file

@ -0,0 +1,130 @@
class Registrant::SessionsController < ::SessionsController
layout 'registrant/application'
helper_method :depp_controller?
def depp_controller?
false
end
def login
@depp_user = Depp::User.new
end
# rubocop:disable Metrics/PerceivedComplexity
# rubocop:disable Metrics/CyclomaticComplexity
def create
@depp_user = Depp::User.new(params[:depp_user].merge(
pki: !Rails.env.development?
)
)
if @depp_user.pki && request.env['HTTP_SSL_CLIENT_S_DN_CN'].blank?
@depp_user.errors.add(:base, :webserver_missing_user_name_directive)
end
if @depp_user.pki && request.env['HTTP_SSL_CLIENT_S_DN_CN'] == '(null)'
@depp_user.errors.add(:base, :webserver_user_name_directive_should_be_required)
end
if @depp_user.pki && request.env['HTTP_SSL_CLIENT_S_DN_CN'] != params[:depp_user][:tag]
@depp_user.errors.add(:base, :invalid_cert)
end
if @depp_user.errors.none? && @depp_user.valid?
@api_user = ApiUser.find_by(username: params[:depp_user][:tag])
if @api_user.active?
sign_in @api_user
redirect_to registrant_root_url
else
@depp_user.errors.add(:base, :not_active)
render 'login'
end
else
render 'login'
end
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/PerceivedComplexity
def login_mid
@user = User.new
end
def mid
phone = params[:user][:phone]
client = Digidoc::Client.new
if Rails.env.test? && phone == "123"
@user = ApiUser.find_by(identity_code: "14212128025")
sign_in(@user, event: :authentication)
return redirect_to registrant_root_url
end
# country_codes = {'+372' => 'EST'}
response = client.authenticate(
phone: "+372#{phone}",
message_to_display: 'Authenticating',
service_name: 'Testing'
)
if response.faultcode
render json: { message: response.detail.message }, status: :unauthorized
return
end
@user = find_user_by_idc(response.user_id_code)
if @user.persisted?
session[:user_id_code] = response.user_id_code
session[:mid_session_code] = client.session_code
render json: { message: t(:check_your_phone_for_confirmation_code) }, status: :ok
else
render json: { message: t(:no_such_user) }, status: :unauthorized
end
end
# rubocop: disable Metrics/PerceivedComplexity
# rubocop: disable Metrics/CyclomaticComplexity
# rubocop: disable Metrics/MethodLength
def mid_status
client = Digidoc::Client.new
client.session_code = session[:mid_session_code]
auth_status = client.authentication_status
case auth_status.status
when 'OUTSTANDING_TRANSACTION'
render json: { message: t(:check_your_phone_for_confirmation_code) }, status: :ok
when 'USER_AUTHENTICATED'
@user = find_user_by_idc(session[:user_id_code])
sign_in @user
flash[:notice] = t(:welcome)
flash.keep(:notice)
render js: "window.location = '#{registrant_root_path}'"
when 'NOT_VALID'
render json: { message: t(:user_signature_is_invalid) }, status: :bad_request
when 'EXPIRED_TRANSACTION'
render json: { message: t(:session_timeout) }, status: :bad_request
when 'USER_CANCEL'
render json: { message: t(:user_cancelled) }, status: :bad_request
when 'MID_NOT_READY'
render json: { message: t(:mid_not_ready) }, status: :bad_request
when 'PHONE_ABSENT'
render json: { message: t(:phone_absent) }, status: :bad_request
when 'SENDING_ERROR'
render json: { message: t(:sending_error) }, status: :bad_request
when 'SIM_ERROR'
render json: { message: t(:sim_error) }, status: :bad_request
when 'INTERNAL_ERROR'
render json: { message: t(:internal_error) }, status: :bad_request
else
render json: { message: t(:internal_error) }, status: :bad_request
end
end
# rubocop: enable Metrics/PerceivedComplexity
# rubocop: enable Metrics/CyclomaticComplexity
# rubocop: enable Metrics/MethodLength
def find_user_by_idc(idc)
return User.new unless idc
ApiUser.find_by(identity_code: idc) || User.new
end
end

View file

@ -0,0 +1,16 @@
class RegistrantController < ApplicationController
before_action :authenticate_user!
layout 'registrant/application'
include Registrant::ApplicationHelper
helper_method :depp_controller?
def depp_controller?
false
end
helper_method :head_title_sufix
def head_title_sufix
t(:registrant_head_title_sufix)
end
end