diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index 94ab580..d687f8e 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -1,19 +1,41 @@ class InvitationsController < ApplicationController + before_action :fetch_invitation, only: [:accept] + # POST /invitations def create invitation = Invitation.new(fetch_params) if invitation.save flash[:success] = "User #{invitation.user.username} has been invited to the client" else flash[:alert] = 'Invitation not created' end redirect_to :back end + # GET /invitations/:host_id/:verification_code + def accept + return redirect_to root_path unless @invitation + host = @invitation.host + if !@invitation.host.users.include? @invitation.user + if @invitation.host.users << @invitation.user + @invitation.destroy + end + else + @invitation.destroy + end + + redirect_to client_path(host.client) + end + private def fetch_params params.require(:invitation).permit(:user_id, :host_id) end + + def fetch_invitation + @invitation = Invitation.find_by(host_id: params[:host_id], + verification_code: params[:verification_code]) + end end diff --git a/config/routes.rb b/config/routes.rb index d46cffe..2238249 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,96 +1,99 @@ Rails.application.routes.draw do root 'application#index' post 'grnet' => 'application#grnet' get 'institutional' => 'application#institutional' match 'vima', to: 'application#vima', :via => [:get, :post] get 'logout' => 'application#logout' resources :clients, only: [:index, :show] do member do get :jobs get :logs get :stats post :stats get :users get :restore post :run_restore post :restore_selected delete :remove_user end collection do post :index end end resources :clients, only: [], param: :client_id do member do get :tree end end resources :invitations, only: [:create] + get '/invitations/:host_id/:verification_code/accept' => 'invitations#accept', + as: :accept_invitation + resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] do member do post :submit_config post :disable delete :revoke end resources :jobs, only: [:new, :create, :show, :edit, :update, :destroy] do member do patch :toggle_enable post :backup_now end end resources :filesets, only: [:show, :new, :create, :edit, :update, :destroy] resources :schedules, only: [:show, :new, :edit, :create, :update, :destroy] end namespace :admin do match '/', to: 'base#index', via: [:get, :post] get '/login' => 'base#login', as: :login resources :settings, only: [:index, :new, :create, :edit, :update] do member do delete :reset end end resources :clients, only: [:index, :show] do member do get :jobs get :logs get :stats post :stats get :configuration post :disable post :block post :unblock delete :revoke end end resources :hosts, only: [:show] do collection do get :unverified end member do post :verify end end resources :users, only: [:index, :new, :create, :show, :edit, :update] do member do patch :ban patch :unban end end resources :pools, only: [:index, :new, :create, :show, :edit, :update] end end diff --git a/spec/routing/invitation_routing_spec.rb b/spec/routing/invitation_routing_spec.rb index 48b3bb9..a1776db 100644 --- a/spec/routing/invitation_routing_spec.rb +++ b/spec/routing/invitation_routing_spec.rb @@ -1,8 +1,14 @@ require 'spec_helper' describe InvitationsController do it 'routes POST /invitations' do expect(post('/invitations')).to route_to(controller: 'invitations', action: 'create') end + + it 'routes GET /invitations/1/abcdef012345689/accept' do + expect(get('/invitations/1/abcdef012345689/accept')). + to route_to(controller: 'invitations', action: 'accept', + host_id: '1', verification_code: 'abcdef012345689') + end end