diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 7afa118..8b23d5c 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,20 +1,44 @@ class Admin::UsersController < Admin::BaseController # GET /admin/users def index @baculized_host_names = Hash.new { |h, k| h[k] = [] } @non_baculized_host_names = Hash.new { |h, k| h[k] = [] } @unverified_host_names = Hash.new { |h, k| h[k] = [] } @users = User.all.includes(:hosts) @users.each do |user| user.hosts.each do |host| if host.deployed? || host.updated? || host.dispatched? || host.for_removal? @baculized_host_names[user.id] << host.name else @non_baculized_host_names[user.id] << host.name @unverified_host_names[user.id] << host.name if !host.verified? end end end end + + # PATCH /admin/users/1/ban + def ban + @user = User.find(params[:id]) + if @user.ban + flash[:success] = 'User banned' + else + flash[:error] = 'User NOT banned' + end + + redirect_to admin_users_path + end + + # PATCH /admin/users/1/unban + def unban + @user = User.find(params[:id]) + if @user.unban + flash[:success] = 'User enabled' + else + flash[:error] = 'User NOT enabled' + end + + redirect_to admin_users_path + end end diff --git a/app/models/user.rb b/app/models/user.rb index 224d912..6091986 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,23 +1,35 @@ class User < ActiveRecord::Base has_many :ownerships has_many :hosts, through: :ownerships, inverse_of: :users enum user_type: { institutional: 0, vima: 1, okeanos: 2, admin: 3 } validates :username, :user_type, presence: true # Composes the user's display name from the user's username and email # # @return [String] def display_name "#{username} <#{email}>" end # Determines if the user must select hosts from a list or enter their # FQDN manually # # @return [Boolean] def needs_host_list? vima? || okeanos? end + + # Marks a user as not enabled + def ban + self.enabled = false + save + end + + # Marks a user as enabled + def unban + self.enabled = true + save + end end diff --git a/app/views/admin/users/_user.html.erb b/app/views/admin/users/_user.html.erb index 849d53c..20a857d 100644 --- a/app/views/admin/users/_user.html.erb +++ b/app/views/admin/users/_user.html.erb @@ -1,10 +1,25 @@ #<%= user.id %> <%= user.username %> <%= user.email %> <%= user.user_type %> <%= I18n.l(user.created_at, format: :short) %> <%= inline_list @baculized_host_names[user.id] %> <%= inline_list @unverified_host_names[user.id] %> <%= inline_list @non_baculized_host_names[user.id] %> + + <% if user.enabled? %> + <%= link_to ban_admin_user_path(user), method: :patch, class: 'btn btn-default', + data: { confirm: 'User will be banned' } do %> + + Ban + <% end %> + <% else %> + <%= link_to unban_admin_user_path(user), method: :patch, class: 'btn btn-default', + data: { confirm: 'User will be unbanned' } do %> + + Unban + <% end %> + <% end %> + diff --git a/app/views/admin/users/index.html.erb b/app/views/admin/users/index.html.erb index 367fbb0..5d1520f 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -1,22 +1,23 @@

Users

+ <%= render partial: 'user', collection: @users %>
id username email user type created at clients unverified hosts pending hostsactions
diff --git a/config/routes.rb b/config/routes.rb index c8c0de6..a948b20 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,72 +1,77 @@ Rails.application.routes.draw do root 'application#index' post 'login' => 'application#login' 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 end collection do post :index end end resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] do member do post :submit_config get :restore post :run_restore 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, :destroy] resources :schedules, only: [:show, :new, :edit, :create, :update, :destroy] end namespace :admin do match '/', to: 'base#index', via: [:get, :post] 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 end end resources :hosts, only: [:show] do collection do get :unverified end member do post :verify end end - resources :users, only: [:index] + resources :users, only: [:index] do + member do + patch :ban + patch :unban + end + end end end diff --git a/spec/routing/admin/users_routing_spec.rb b/spec/routing/admin/users_routing_spec.rb new file mode 100644 index 0000000..ee6f272 --- /dev/null +++ b/spec/routing/admin/users_routing_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe Admin::UsersController do + it 'routes GET /admin/users' do + expect(get('/admin/users')).to route_to(controller: 'admin/users', action: 'index') + end + + it 'routes PATCH /admin/users/1/ban' do + expect(patch('/admin/users/1/ban')). + to route_to(controller: 'admin/users', action: 'ban', id: '1') + end + + it 'routes PATCH /admin/users/1/unban' do + expect(patch('/admin/users/1/unban')). + to route_to(controller: 'admin/users', action: 'unban', id: '1') + end +end