diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 4d9137d..457b401 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -1,106 +1,130 @@ class Admin::UsersController < Admin::BaseController - before_action :fetch_user, only: [:show, :edit, :update, :ban, :unban] + before_action :fetch_user, only: [:show, :edit, :update, :ban, :unban, + :grant_admin, :revoke_admin] before_action :editable_users_only, only: [:edit, :update] # 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 = @users.admin if params[:type] == 'admin' @users = @users.vima if params[:type] == 'vima' @users = @users.institutional if params[:type] == 'institutional' @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 # GET /admin/users/new def new @user = User.new(user_type: :admin) end # POST /admin/users def create @user = User.new(fetch_params) @user.user_type = :admin if @user.add_password(@user.password) flash[:success] = 'User created' redirect_to admin_users_path else flash[:error] = 'User was not created' render 'new' end end # GET /admin/users/1 def show end # GET /admin/users/1/edit def edit end # PATCH /admin/users/1/update def update if @user.admin? && @user.update_attributes(fetch_params) flash[:success] = 'User updated' redirect_to admin_user_path(@user) elsif @user.admin? flash[:error] = 'User not updated' redirect_to edit_admin_user_path(@user) else flash[:error] = "User is #{@user.user_type} and thus accepts no updates" redirect_to admin_user_path(@user) end end # PATCH /admin/users/1/ban def ban 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 if @user.unban flash[:success] = 'User enabled' else flash[:error] = 'User NOT enabled' end redirect_to admin_users_path end + # PATCH /admin/users/1/revoke_admin + def revoke_admin + if @user.update_attribute(:moderator, false) + flash[:success] = 'User is no longer an admin' + else + flash[:error] = 'Admin rights were NOT revoked' + end + + redirect_to admin_users_path + end + + # PATCH /admin/users/1/grant_admin + def grant_admin + if @user.update_attribute(:moderator, true) + flash[:success] = 'User is now an admin' + else + flash[:error] = 'Admin rights were NOT granted' + end + + redirect_to admin_users_path + end + + private def fetch_params params.require(:user).permit(:username, :email, :password, :retype_password) end def fetch_user @user = User.find(params[:id]) end def editable_users_only return if @user.editable? flash[:error] = "User #{@user.username} is not editable" redirect_to admin_users_path end end diff --git a/app/views/admin/users/_user.html.erb b/app/views/admin/users/_user.html.erb index 94699ad..5fa612d 100644 --- a/app/views/admin/users/_user.html.erb +++ b/app/views/admin/users/_user.html.erb @@ -1,36 +1,35 @@ <%= link_to "##{user.id}", admin_user_path(user) %> <%= link_to user.username, admin_user_path(user) %> <%= user.email %> <%= user.user_type %> <%= I18n.l(user.created_at, format: :short) %> <%= I18n.l(user.login_at, format: :short) rescue '-' %> <%= inline_list @baculized_host_names[user.id] %> <%= inline_list @unverified_host_names[user.id] %> <%= inline_list @non_baculized_host_names[user.id] %> + <%= user.has_admin_access? ? 'yes' : 'no' %> - <%= link_to admin_user_path(user) do %> - - <% end %> <% if user.editable? %> - <%= link_to edit_admin_user_path(user) do %> + <%= link_to edit_admin_user_path(user), class: 'btn btn-default' do %> + Edit <% end %> <% end %> <% if user.enabled? %> <%= link_to ban_admin_user_path(user), method: :patch, class: 'btn btn-default', data: { confirm: "User #{user.username} will be banned" } do %> Ban <% end %> <% else %> <%= link_to unban_admin_user_path(user), method: :patch, class: 'btn btn-default', data: { confirm: "User #{user.username} 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 009c388..57f645b 100644 --- a/app/views/admin/users/index.html.erb +++ b/app/views/admin/users/index.html.erb @@ -1,36 +1,37 @@
<%= link_to new_admin_user_path, class: "btn btn-default", role: "button" do %> New Admin <% end %>

Users

+ <%= render partial: 'user', collection: @users %>
id Username Email User type Created at Last login at Clients Unverified hosts Pending hostsAdmin Actions Ban
diff --git a/app/views/admin/users/show.html.erb b/app/views/admin/users/show.html.erb index 6c637a6..1ba4b69 100644 --- a/app/views/admin/users/show.html.erb +++ b/app/views/admin/users/show.html.erb @@ -1,71 +1,87 @@ <%= render partial: 'header' %>

User details


<% if @user.institutional? %> <% end %>
Username <%= @user.username %>
Email <%= @user.email %>
User Type <%= @user.user_type %>
Created At <%= I18n.l(@user.created_at, format: :short) rescue '-' %>
Login At <%= I18n.l(@user.login_at, format: :short) rescue '-' %>
Clients <%= inline_list @user.baculized_hosts %>
Pending Hosts <%= inline_list @user.non_baculized_hosts %>
Unverified Hosts <%= inline_list @user.unverified_hosts %>
<% if @user.editable? %> <%= link_to edit_admin_user_path(@user), class: 'btn btn-primary' do %> Edit User <% end %> <% end %> <% if @user.enabled? %> <%= link_to ban_admin_user_path(@user), method: :patch, class: 'btn btn-default', data: { confirm: "User #{@user.username} will be banned" } do %> Ban User <% end %> <% else %> <%= link_to unban_admin_user_path(@user), method: :patch, class: 'btn btn-default', data: { confirm: "User #{@user.username} will be unbanned" } do %> Unban User <% end %> <% end %> + <% if !@user.admin? %> + <% if @user.has_admin_access? %> + <%= link_to revoke_admin_admin_user_path(@user), method: :patch, class: 'btn btn-default', + data: { confirm: "User #{@user.username} will be NOT an admin from now on" } do %> + + Revoke Admin + <% end %> + <% else %> + <%= link_to grant_admin_admin_user_path(@user), method: :patch, class: 'btn btn-default', + data: { confirm: "User #{@user.username} will an admin from now on" } do %> + + Grant Admin + <% end %> + <% end %> + <% end %> + <%= link_to 'Back to users', admin_users_path, class: 'right' %>
diff --git a/config/routes.rb b/config/routes.rb index d196b98..a8cd54c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,124 +1,126 @@ Rails.application.routes.draw do root 'application#index' get 'faq' => 'application#faq' 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 collection do get :fetch_vima_hosts, to: 'hosts#fetch_vima_hosts', as: :fetch_vima 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 resources :users, only: :show do member do patch :generate_token end 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: [] do collection do get :unverified end member do post :verify put :set_quota end end resources :users, only: [:index, :new, :create, :show, :edit, :update] do member do patch :ban patch :unban + patch :revoke_admin + patch :grant_admin end end resources :pools, only: [:index, :new, :create] resources :faqs end namespace :api, defaults: { format: :json } do scope module: :v1, constraints: ApiVersion.new(version: 1, default: true) do resources :clients, only: [:index, :show] do member do post :backup post :restore end end end end end