Page MenuHomeGRNET

No OneTemporary

File Metadata

Created
Fri, Aug 29, 7:08 PM
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 @@
<tr>
<td><%= link_to "##{user.id}", admin_user_path(user) %></td>
<td><%= link_to user.username, admin_user_path(user) %></td>
<td><%= user.email %></td>
<td><%= user.user_type %></td>
<td><%= I18n.l(user.created_at, format: :short) %></td>
<td><%= I18n.l(user.login_at, format: :short) rescue '-' %></td>
<td><%= inline_list @baculized_host_names[user.id] %></td>
<td><%= inline_list @unverified_host_names[user.id] %></td>
<td><%= inline_list @non_baculized_host_names[user.id] %></td>
+ <td><%= user.has_admin_access? ? 'yes' : 'no' %></td>
<td>
- <%= link_to admin_user_path(user) do %>
- <label class="glyphicon glyphicon-eye-open text-primary" alt="show"></label>
- <% end %>
<% if user.editable? %>
- <%= link_to edit_admin_user_path(user) do %>
+ <%= link_to edit_admin_user_path(user), class: 'btn btn-default' do %>
<label class="glyphicon glyphicon-edit text-primary" alt="show"></label>
+ Edit
<% end %>
<% end %>
</td>
<td>
<% 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 %>
<label class="glyphicon glyphicon-ban-circle text-danger"></label>
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 %>
<label class="glyphicon glyphicon-ok-circle text-success"></label>
Unban
<% end %>
<% end %>
</td>
</tr>
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 @@
<div class="row right">
<%= link_to new_admin_user_path, class: "btn btn-default", role: "button" do %>
<label class="glyphicon glyphicon-plus text-primary"></label>
New Admin
<% end %>
</div>
<h1>Users</h1>
<div class="row">
<div class="col-xs-12">
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th>id</th>
<th>Username</th>
<th>Email</th>
<th>User type</th>
<th>Created at</th>
<th>Last login at</th>
<th>Clients</th>
<th>Unverified hosts</th>
<th>Pending hosts</th>
+ <th>Admin</th>
<th>Actions</th>
<th>Ban</th>
</tr>
</thead>
<tbody>
<%= render partial: 'user', collection: @users %>
</tbody>
</table>
</div>
</div>
</div>
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' %>
<div class="col-xs-5">
<div class="panel panel-default">
<div class="panel-heading">
<h2>User details</h2>
</div>
<br/>
<div class="table-responsive">
<table class="table table-striped table-bordered table-condensed">
<tr>
<td><b>Username</b></td>
<td><%= @user.username %></td>
</tr>
<tr>
<td><b>Email</b></td>
<td><%= @user.email %></td>
</tr>
<tr>
<td><b>User Type</b></td>
<td><%= @user.user_type %></td>
</tr>
<tr>
<td><b>Created At</b></td>
<td><%= I18n.l(@user.created_at, format: :short) rescue '-' %></td>
</tr>
<tr>
<td><b>Login At</b></td>
<td><%= I18n.l(@user.login_at, format: :short) rescue '-' %></td>
</tr>
<tr>
<td><b>Clients</b></td>
<td><%= inline_list @user.baculized_hosts %></td>
</tr>
<tr>
<td><b>Pending Hosts</b></td>
<td><%= inline_list @user.non_baculized_hosts %></td>
</tr>
<% if @user.institutional? %>
<tr>
<td><b>Unverified Hosts</b></td>
<td><%= inline_list @user.unverified_hosts %></td>
</tr>
<% end %>
</table>
</div>
</div>
<% if @user.editable? %>
<%= link_to edit_admin_user_path(@user), class: 'btn btn-primary' do %>
<label class="glyphicon glyphicon-ok-edit text-success"></label>
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 %>
<label class="glyphicon glyphicon-ban-circle text-danger"></label>
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 %>
<label class="glyphicon glyphicon-ok-circle text-success"></label>
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 %>
+ <label class="glyphicon glyphicon-ban-circle text-danger"></label>
+ 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 %>
+ <label class="glyphicon glyphicon-ok-circle text-success"></label>
+ Grant Admin
+ <% end %>
+ <% end %>
+ <% end %>
+
<%= link_to 'Back to users', admin_users_path, class: 'right' %>
</div>
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

Event Timeline