Context switching for client

This commit is contained in:
Martin Lensment 2014-09-26 17:32:57 +03:00
parent 5dfee40b87
commit b48b91878d
9 changed files with 44 additions and 12 deletions

View file

@ -24,7 +24,7 @@ class Client::ContactsController < ClientController
end end
end end
def destroy def destroy
if @contact.destroy_and_clean if @contact.destroy_and_clean
flash[:notice] = I18n.t('shared.contact_deleted') flash[:notice] = I18n.t('shared.contact_deleted')
redirect_to client_contacts_path redirect_to client_contacts_path

View file

@ -7,9 +7,10 @@ class Client::DomainTransfersController < ClientController
end end
def create def create
@domain_transfer = @domain.pending_transfer || @domain.domain_transfers.create(domain_transfer_params) @domain_transfer = @domain.pending_transfer || @domain.domain_transfers.build(domain_transfer_params)
if can? :read, @domain_transfer if can? :read, @domain_transfer
flash[:notice] = I18n.t('shared.domain_transfer_requested') @domain_transfer.save
flash[:notice] = I18n.t('shared.domain_transfer_requested') if @domain.registrar != current_registrar
redirect_to [:client, @domain_transfer] redirect_to [:client, @domain_transfer]
else else
flash.now[:alert] = I18n.t('shared.other_registrar_has_already_requested_to_transfer_this_domain') flash.now[:alert] = I18n.t('shared.other_registrar_has_already_requested_to_transfer_this_domain')
@ -38,7 +39,7 @@ class Client::DomainTransfersController < ClientController
ret = { ret = {
status: DomainTransfer::PENDING, status: DomainTransfer::PENDING,
transfer_requested_at: Time.zone.now, transfer_requested_at: Time.zone.now,
transfer_to: current_user.registrar, transfer_to: current_registrar,
transfer_from: @domain.registrar transfer_from: @domain.registrar
} }
@ -61,7 +62,7 @@ class Client::DomainTransfersController < ClientController
render 'new' and return render 'new' and return
end end
if @domain.registrar == current_user.registrar if @domain.registrar == current_registrar && !@domain.pending_transfer
flash.now[:alert] = I18n.t('shared.domain_already_belongs_to_the_querying_registrar') flash.now[:alert] = I18n.t('shared.domain_already_belongs_to_the_querying_registrar')
render 'new' and return render 'new' and return
end end

View file

@ -4,14 +4,13 @@ class Client::DomainsController < ClientController
before_action :verify_deletion, only: [:destroy] before_action :verify_deletion, only: [:destroy]
def index def index
@q = Domain.search(params[:q]) if current_user.admin? @q = current_registrar.domains.search(params[:q])
@q = current_user.registrar.domains.search(params[:q]) unless current_user.admin?
@domains = @q.result.page(params[:page]) @domains = @q.result.page(params[:page])
end end
def new def new
owner_contact = Contact.find(params[:owner_contact_id]) if params[:owner_contact_id] owner_contact = Contact.find(params[:owner_contact_id]) if params[:owner_contact_id]
@domain = Domain.new(owner_contact: owner_contact, registrar: current_user.registrar) @domain = Domain.new(owner_contact: owner_contact, registrar: current_registrar)
params[:domain_owner_contact] = owner_contact params[:domain_owner_contact] = owner_contact
build_associations build_associations
@ -21,7 +20,7 @@ class Client::DomainsController < ClientController
add_prefix_to_statuses add_prefix_to_statuses
@domain = Domain.new(domain_params) @domain = Domain.new(domain_params)
@domain.registrar = current_user.registrar @domain.registrar = current_registrar
if @domain.save if @domain.save
flash[:notice] = I18n.t('shared.domain_added') flash[:notice] = I18n.t('shared.domain_added')

View file

@ -1,3 +1,8 @@
class ClientController < ApplicationController class ClientController < ApplicationController
helper_method :current_registrar
def current_registrar
return Registrar.find(session[:current_user_registrar_id]) if current_user.admin?
current_user.registrar
end
end end

View file

@ -6,6 +6,8 @@ class SessionsController < Devise::SessionsController
@user = User.find_by(username: 'gitlab') if params[:gitlab] @user = User.find_by(username: 'gitlab') if params[:gitlab]
@user = User.find_by(username: 'zone') if params[:zone] @user = User.find_by(username: 'zone') if params[:zone]
session[:current_user_registrar_id] = Registrar.first.id if @user.admin?
flash[:notice] = I18n.t('shared.welcome') flash[:notice] = I18n.t('shared.welcome')
sign_in_and_redirect @user, :event => :authentication sign_in_and_redirect @user, :event => :authentication
return return
@ -15,4 +17,10 @@ class SessionsController < Devise::SessionsController
def login def login
render 'layouts/login', layout: false render 'layouts/login', layout: false
end end
def switch_registrar
authorize! :switch, :registrar
session[:current_user_registrar_id] = params[:registrar_id]
redirect_to client_root_path
end
end end

View file

@ -3,9 +3,14 @@ class Ability
def initialize(user) def initialize(user)
alias_action :create, :read, :update, :destroy, :to => :crud
user ||= User.new user ||= User.new
if user.admin? if user.admin?
can :manage, Domain can :manage, Domain
can :switch, :registrar
can :crud, DomainTransfer
can :approve_as_client, DomainTransfer, status: DomainTransfer::PENDING
elsif user.persisted? elsif user.persisted?
can :manage, Domain, registrar_id: user.registrar.id can :manage, Domain, registrar_id: user.registrar.id
can :read, DomainTransfer, transfer_to_id: user.registrar.id can :read, DomainTransfer, transfer_to_id: user.registrar.id

View file

@ -5,8 +5,7 @@
.col-sm-6 .col-sm-6
%h2.text-right.text-center-xs %h2.text-right.text-center-xs
- if can? :approve_as_client, @domain_transfer - if can? :approve_as_client, @domain_transfer
= button_to(t('shared.approve'), approve_client_domain_transfer_path, class: 'btn btn-primary') = button_to(t('shared.approve'), approve_client_domain_transfer_path, class: 'btn btn-success')
%hr %hr
.row .row
.col-md-12 .col-md-12
@ -23,6 +22,9 @@
%dt= t('shared.transfer_from') %dt= t('shared.transfer_from')
%dd= @domain_transfer.transfer_from %dd= @domain_transfer.transfer_from
%dt= t('shared.transfer_to')
%dd= @domain_transfer.transfer_to
- if @domain_transfer.transferred_at - if @domain_transfer.transferred_at
%dt= t('shared.transferred_at') %dt= t('shared.transferred_at')
%dd= l(@domain_transfer.transferred_at) %dd= l(@domain_transfer.transferred_at)

View file

@ -43,6 +43,15 @@
- if current_user.admin? - if current_user.admin?
%li %li
= link_to 'Admin', admin_root_path = link_to 'Admin', admin_root_path
%li.dropdown
%a.dropdown-toggle{"data-toggle" => "dropdown", href: "#"}
= current_registrar
%span.caret
%ul.dropdown-menu{role: "menu"}
- Registrar.all.each do |x|
- next if x == current_registrar
%li
= link_to x, switch_registrar_sessions_path(registrar_id: x)
%ul.nav.navbar-nav.navbar-right %ul.nav.navbar-nav.navbar-right
%li= link_to t('shared.log_out'), '/logout' %li= link_to t('shared.log_out'), '/logout'
/ /.nav-collapse / /.nav-collapse

View file

@ -45,7 +45,10 @@ Rails.application.routes.draw do
devise_for :users devise_for :users
devise_scope :user do devise_scope :user do
resources :sessions resources :sessions do
get :switch_registrar, on: :collection
end
get 'logout' => 'devise/sessions#destroy' get 'logout' => 'devise/sessions#destroy'
get 'login' => 'sessions#login' get 'login' => 'sessions#login'
end end