From: Ladislav Martincik <lmartinc(a)redhat.com>
---
src/app/controllers/admin/users_controller.rb | 98 ++++++++++++++++++++++++-
src/app/services/application_service.rb | 6 +-
src/app/stylesheets/newui.scss | 1 +
src/app/views/admin/users/_form.haml | 44 +++++++++++
src/app/views/admin/users/_list.haml | 42 +++++++++++
src/app/views/admin/users/edit.haml | 7 ++
src/app/views/admin/users/index.haml | 3 +-
src/app/views/admin/users/new.haml | 6 ++
src/app/views/admin/users/show.haml | 99 +++++++++++++++++++++++++
src/config/routes.rb | 3 +-
src/features/step_definitions/user_steps.rb | 2 +-
src/features/support/paths.rb | 2 +-
src/features/user.feature | 11 ++-
13 files changed, 312 insertions(+), 12 deletions(-)
create mode 100644 src/app/views/admin/users/_form.haml
create mode 100644 src/app/views/admin/users/_list.haml
create mode 100644 src/app/views/admin/users/edit.haml
create mode 100644 src/app/views/admin/users/new.haml
create mode 100644 src/app/views/admin/users/show.haml
diff --git a/src/app/controllers/admin/users_controller.rb
b/src/app/controllers/admin/users_controller.rb
index 591edad..65b1ce6 100644
--- a/src/app/controllers/admin/users_controller.rb
+++ b/src/app/controllers/admin/users_controller.rb
@@ -1,6 +1,102 @@
class Admin::UsersController < ApplicationController
before_filter :require_user
+ before_filter :only_admin, :only => [:index, :multi_destroy]
+ before_filter :load_users, :only => [:index, :show]
- def index
+ def new
+ @user = User.new
+ @user.quota = Quota.new
end
+
+ def create
+ if params[:commit] == "Reset"
+ redirect_to :action => 'new' and return
+ end
+
+ # TODO: Shouldn't it be if current_user.nil? instead?
+ require_privilege(Privilege::USER_MODIFY) unless current_user.nil?
+ @user = User.new(params[:user])
+
+ @registration = RegistrationService.new(@user)
+ unless @registration.save
+ flash.now[:warning] = "User registration failed: #{(a)registration.error}"
+ render :action => 'new' and return
+ end
+
+ if current_user
+ flash[:notice] = "User registered!"
+ redirect_to admin_users_url
+ else
+ flash[:notice] = "You have successfully registered!"
+ redirect_to dashboard_url
+ end
+ end
+
+ def show
+ @user = User.find_by_id(params[:id]) || current_user
+ @quota_resources = @user.quota.quota_resources
+ end
+
+ def edit
+ @user = User.find_by_id(params[:id]) || current_user
+
+ if cannot_modify_different_user?(@user)
+ flash[:notice] = "Invalid Permission to perform this operation"
+ redirect_to dashboard_url and return
+ end
+ end
+
+ def update
+ @user = User.find_by_id(params[:id]) || current_user
+
+ if params[:commit] == "Reset"
+ redirect_to edit_admin_user_url(@user) and return
+ end
+
+ redirect_to dashboard_url and return unless @user
+
+ if cannot_modify_different_user = cannot_modify_different_user?(@user)
+ flash[:notice] = "Invalid Permission to perform this operation"
+ redirect_to dashboard_url and return
+ end
+
+ unless @user.update_attributes(params[:user])
+ render :action => 'edit' and return
+ else
+ flash[:notice] = "User updated!"
+ redirect_to cannot_modify_different_user ? dashboard_url : admin_users_url
+ end
+ end
+
+ def multi_destroy
+ User.destroy(params[:user_selected])
+ redirect_to admin_users_url
+ end
+
+ protected
+
+ def load_users
+ @users = User.all
+ sort_order = params[:sort_by].nil? ? "login" : params[:sort_by]
+ # TODO: (lmartinc) Optimize this sort! hell!
+ if sort_order == "percentage_quota_used"
+ @users.sort! {|x,y| y.quota.percentage_used <=> x.quota.percentage_used }
+ elsif sort_order == "quota"
+ @users.sort! {|x,y| (x.quota.maximum_running_instances and
y.quota.maximum_running_instances) ? x.quota.maximum_running_instances <=>
y.quota.maximum_running_instances : (x ? 1 : -1) }
+ else
+ @users = User.all(:order => sort_order)
+ end
+ end
+
+ def only_admin
+ unless current_user.permissions.collect { |p| p.role }.find { |r| r.name ==
"Administrator" }
+ flash[:notice] = "Invalid Permission to perform this operation"
+ redirect_to dashboard_url
+ end
+ end
+
+ def cannot_modify_different_user?(user)
+ user && user != current_user &&
!BasePermissionObject.general_permission_scope.can_modify_users(current_user)
+ end
+
end
diff --git a/src/app/services/application_service.rb
b/src/app/services/application_service.rb
index 3315fb1..8161718 100644
--- a/src/app/services/application_service.rb
+++ b/src/app/services/application_service.rb
@@ -41,14 +41,16 @@ module ApplicationService
# @current_user must be defined
def check_privilege(privilege, perm_obj)
- ((perm_obj and perm_obj.has_privilege(@current_user, privilege)) or
- BasePermissionObject.general_permission_scope.has_privilege(@current_user,
+ ((perm_obj and perm_obj.has_privilege(current_user, privilege)) or
+ BasePermissionObject.general_permission_scope.has_privilege(current_user,
privilege))
end
+
def authorized?(privilege, perm_obj=nil)
@perm_obj = perm_obj
check_privilege(privilege,@perm_obj)
end
+
def require_privilege(privilege, perm_obj=nil)
unless authorized?(privilege, perm_obj)
raise PermissionError.new(
diff --git a/src/app/stylesheets/newui.scss b/src/app/stylesheets/newui.scss
index 933165d..e397564 100644
--- a/src/app/stylesheets/newui.scss
+++ b/src/app/stylesheets/newui.scss
@@ -1357,6 +1357,7 @@ $content-left: 180px;
height: $details-height;
bottom: 0;
padding: 0;
+ overflow-y:scroll;
}
/* DEBUG GRID */
diff --git a/src/app/views/admin/users/_form.haml b/src/app/views/admin/users/_form.haml
new file mode 100644
index 0000000..3fdf33f
--- /dev/null
+++ b/src/app/views/admin/users/_form.haml
@@ -0,0 +1,44 @@
+= form.error_messages
+%fieldset.clear
+ = form.label :login, t(:choose_name), :class => "grid_3 alpha"
+ = form.text_field :login, :class => "grid_5"
+%fieldset.clear
+ = form.label :password, form.object.new_record? ? t(:choose_password) :
t(:change_password), :class => "grid_3 alpha"
+ = form.password_field :password, :class => "grid_5"
+%fieldset
+ = form.label :password_confirmation, t(:confirm_password), :class => "grid_3
alpha"
+ = form.password_field :password_confirmation, :class => "grid_5"
+-if has_user_modify?
+ %fieldset.clear
+ = form.label :user_status, "User Status:", :class => "alpha
grid_3"
+ .grid_5
+ = radio_button_tag "user_status","Active",true
+ = label_tag "user_status_active", "Active"
+ = radio_button_tag "user_status","Inactive", false, :disabled
=> true
+ = label_tag "user_status_inactive", "Inactive"
+%fieldset.clear
+ = form.label :first_name, t(:first_name), :class => "alpha grid_3"
+ = form.text_field :first_name, :class => "grid_5"
+%fieldset.clear
+ = form.label :last_name, t(:last_name), :class => "alpha grid_3"
+ = form.text_field :last_name, :class => "grid_5"
+%fieldset.clear
+ = form.label :email, t(:email), :class => "alpha grid_3"
+ = form.text_field :email, :class => "grid_5"
+-if has_user_modify?
+ %h3.grid_16 User Treatment
+ %fieldset.clearfix
+ = label_tag 'apply_treatment', t(:apply_treatment), :class => "alpha
grid_3"
+ = select_tag 'user_treatment', options_for_select([t(:choose_treatment)]),
:class => "grid_5"
+ = submit_tag t(:apply), :disabled => true, :class => "grid_2"
+
+ - form.fields_for :quota do |quota_form|
+ %fieldset.clear
+ = quota_form.label :maximum_running_instances, t(:maximum_running_instances),
:class => "alpha grid_3"
+ = quota_form.text_field :maximum_running_instances, :class =>
"grid_5"
+ (instances)
+ = quota_form.error_message_on :maximum_running_instances, 'Maximum Running
Instances '
+%fieldset.clearfix
+ = form.submit "Save", :class => "submit formbutton"
+ = form.submit "Reset", :class => "submit formbutton"
+ = link_to t(:cancel), cancel_path, :class => 'button formbutton'
diff --git a/src/app/views/admin/users/_list.haml b/src/app/views/admin/users/_list.haml
new file mode 100644
index 0000000..ff62782
--- /dev/null
+++ b/src/app/views/admin/users/_list.haml
@@ -0,0 +1,42 @@
+- form_tag do
+ = link_to "Create", new_admin_user_url, :class => 'button'
+ = restful_submit_tag "Delete", 'destroy',
multi_destroy_admin_users_path, 'DELETE', :id => 'delete_button'
+
+ %table#users_table
+ %thead
+ %tr
+ %th
+ %th= link_to "User ID", admin_users_url(:sort_by =>
"login")
+ %th= link_to "Last Name", admin_users_url(:sort_by =>
"last_name")
+ %th= link_to "First Name", admin_users_url(:sort_by =>
"first_name")
+ %th= link_to "% Quota Used", admin_users_url(:sort_by =>
"percentage_quota_used")
+ %th
+ =link_to "Quota", admin_users_url(:sort_by => "quota")
+ %span (Instances)
+ %th= link_to "e-mail", admin_users_url(:sort_by =>
"email")
+ %th
+ -(a)users.each do |user|
+ %tr
+ %td
+ %input{:name => "user_selected[]", :type =>
"checkbox", :value => user.id, :id => "user_checkbox_#{user.id}"
}
+ %td= link_to user.login, admin_user_path(user)
+ %td= user.last_name
+ %td= user.first_name
+ %td= sprintf("%.2f", user.quota.percentage_used)
+ %td= Quota.no_limit(user.quota.maximum_running_instances) ? "Unlimited"
: user.quota.maximum_running_instances
+ %td= user.email
+ %td= link_to "edit", edit_admin_user_path(user)
+
+:javascript
+ $(document).ready(function () {
+ $('#delete_button').click(function(e) {
+ if ($("#users_table input[@type=radio]:checked").length == 0) {
+ alert('Please select any user to be deleted before clicking Delete
button.');
+ e.preventDefault();
+ } else {
+ if (!confirm("Are you sure you want to delete this user?")) {
+ e.preventDefault();
+ }
+ }
+ });
+ });
diff --git a/src/app/views/admin/users/edit.haml b/src/app/views/admin/users/edit.haml
new file mode 100644
index 0000000..a367fec
--- /dev/null
+++ b/src/app/views/admin/users/edit.haml
@@ -0,0 +1,7 @@
+- if has_user_modify?
+ %h2 Editing User: #{(a)user.first_name} #{(a)user.last_name}
+- else
+ %h2 Editing Account
+
+- form_for @user, :url => admin_user_path(@user), :html => { :method => :put }
do |f|
+ = render :partial => "form", :locals => { :form => f, :cancel_path
=> admin_users_path }
diff --git a/src/app/views/admin/users/index.haml b/src/app/views/admin/users/index.haml
index 606d747..62ccbc6 100644
--- a/src/app/views/admin/users/index.haml
+++ b/src/app/views/admin/users/index.haml
@@ -1 +1,2 @@
-admin/users/index.haml
+- content_for :list do
+ = render :partial => 'list'
diff --git a/src/app/views/admin/users/new.haml b/src/app/views/admin/users/new.haml
new file mode 100644
index 0000000..0958f12
--- /dev/null
+++ b/src/app/views/admin/users/new.haml
@@ -0,0 +1,6 @@
+%h2 New Account
+- form_for @user, :url => admin_users_path do |f|
+ -if current_user
+ = render :partial => "form", :locals => { :form => f, :cancel_path
=> admin_users_path }
+ -else
+ = render :partial => "form", :locals => { :form => f, :cancel_path
=> login_path }
diff --git a/src/app/views/admin/users/show.haml b/src/app/views/admin/users/show.haml
new file mode 100644
index 0000000..c5cca96
--- /dev/null
+++ b/src/app/views/admin/users/show.haml
@@ -0,0 +1,99 @@
+-content_for :list do
+ = render :partial => 'list'
+-content_for :details do
+ .grid_13
+ %h2 #{(a)user.first_name} #{(a)user.last_name} (#{(a)user.login})
+
+ %table
+ %thead
+ %tr
+ %th.show
+ Personal Details
+ %th.show
+ %tbody
+ %tr
+ %td
+ %label Username
+ %td
+ = @user.login
+ %tr
+ %td
+ %label First Name
+ %td
+ = @user.first_name
+ %tr
+ %td
+ %label Last Name
+ %td
+ = @user.last_name
+ %tr
+ %td
+ %label e-mail
+ %td
+ = @user.email
+
+ %table
+ %thead
+ %tr
+ %th.show
+ Quota Status
+ %th.show
+ %tbody
+ %tr
+ %td
+ %label Maximum
+ %td
+ = @quota_resources["running_instances"].max
+ %tr
+ %td
+ %label Used
+ %td
+ = @quota_resources["running_instances"].used
+ %tr
+ %td
+ %label Availabe
+ %td
+ = @quota_resources["running_instances"].available
+
+ %table
+ %thead
+ %tr
+ %th.show
+ Login Information
+ %th.show
+ %tbody
+ %tr
+ %td
+ %label Current Login IP
+ %td
+ = @user.current_login_ip
+ %tr
+ %td
+ %label Current Login at
+ %td
+ = @user.current_login_at
+ %tr
+ %td
+ %label Last Login IP
+ %td
+ = @user.last_login_ip
+ %tr
+ %td
+ %label Last Login at
+ %td
+ = @user.last_login_at
+ %tr
+ %td
+ %label Login Count
+ %td
+ = @user.login_count
+ %tr
+ %td
+ %label Failed Login Count
+ %td
+ = @user.login_count
+ %tr
+ %td
+ %label Last Request at
+ %td
+ = @user.last_request_at
diff --git a/src/config/routes.rb b/src/config/routes.rb
index c1964ec..5dd6560 100644
--- a/src/config/routes.rb
+++ b/src/config/routes.rb
@@ -43,7 +43,8 @@ ActionController::Routing::Routes.draw do |map|
map.connect '/set_layout', :controller => 'application', :action
=> 'set_layout'
map.namespace 'admin' do |r|
- r.resources :hardware_profiles, :pool_families, :providers, :provider_accounts,
:realms, :roles, :settings, :users
+ r.resources :hardware_profiles, :pool_families, :providers, :provider_accounts,
:realms, :roles, :settings
+ r.resources :users, :collection => { :multi_destroy => :delete }
end
map.resources :pools
diff --git a/src/features/step_definitions/user_steps.rb
b/src/features/step_definitions/user_steps.rb
index 19d63c2..c6467e8 100644
--- a/src/features/step_definitions/user_steps.rb
+++ b/src/features/step_definitions/user_steps.rb
@@ -1,5 +1,5 @@
Given /^there is a user "([^"]*)"$/ do |name|
- User.find_by_login(name).should_not == nil
+ User.find_by_login(name).should_not == nil
end
Given /^there are (\d+) users$/ do |number|
diff --git a/src/features/support/paths.rb b/src/features/support/paths.rb
index d55f1f3..c60b940 100644
--- a/src/features/support/paths.rb
+++ b/src/features/support/paths.rb
@@ -18,7 +18,7 @@ module NavigationHelpers
login_path
when /^(.*)'s user page$/i
- user_path(User.find_by_login($1))
+ admin_user_path(User.find_by_login($1))
when /the account page/
account_path
diff --git a/src/features/user.feature b/src/features/user.feature
index a99cb20..2160ed1 100644
--- a/src/features/user.feature
+++ b/src/features/user.feature
@@ -7,9 +7,10 @@ Feature: Manage Users
Given I am an authorised user
And I am logged in
And a user "testuser" exists
+ And I am using new UI
Scenario: Change the password
- Given I am on the users page
+ Given I am on the admin users page
And there is a user "testuser"
When I follow "edit"
Then I should see "Editing User:"
@@ -27,16 +28,16 @@ Feature: Manage Users
Then I should see "User updated!"
Scenario: Show user detials
- Given I am on the users page
+ Given I am on the admin users page
And there is a user "testuser"
When I follow "testuser"
Then I should be on testuser's user page
Scenario: Administrator cancels the creation of a user account
- Given I am on the users page
+ Given I am on the admin users page
And there are 2 users
When I follow "create"
- Then I should be on the new user page
+ Then I should be on the new admin user page
When I follow "cancel"
Then there should only be 2 users
- And I should be on the users page
+ And I should be on the admin users page
--
1.7.3.2