From: martyntaylor <[email protected]>

---
 src/app/controllers/users_controller.rb |   42 +++++++++++++++--
 src/app/models/quota.rb                 |   11 ++++
 src/app/views/users/index.haml          |   78 +++++++++++++++++++++++++------
 3 files changed, 112 insertions(+), 19 deletions(-)

diff --git a/src/app/controllers/users_controller.rb 
b/src/app/controllers/users_controller.rb
index 6d3102a..cd0bc00 100644
--- a/src/app/controllers/users_controller.rb
+++ b/src/app/controllers/users_controller.rb
@@ -20,8 +20,8 @@
 # Likewise, all the methods added will be available for all controllers.
 
 class UsersController < ApplicationController
-  before_filter :require_user, :only => [:show, :edit, :update]
-  before_filter :current_user, :only => [:new, :index]
+  before_filter :require_user, :only => [:show, :edit, :update, :index, 
:destroy]
+  before_filter :current_user, :only => [:new, :index, :destroy]
 
   def new
     @user = User.new
@@ -81,6 +81,40 @@ class UsersController < ApplicationController
   end
 
   def index
-    @users = User.all
+    if @current_user.permissions.collect { |p| p.role }.find { |r| r.name == 
"Administrator" }
+      @users = User.all
+      sort_order = params[:sort_by].nil? ? "login" : params[:sort_by]
+      if sort_order == "percentage_quota_used"
+        @users = User.all
+        @users.sort! {|x,y| y.quota.percentage_used <=> 
x.quota.percentage_used }
+      elsif sort_order == "quota"
+        @users = User.all
+        @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.find(:all, :order => sort_order)
+      end
+    else
+      flash[:notice] = "Invalid Permission to perform this operation"
+      redirect_to :dashboard
+    end
   end
-end
\ No newline at end of file
+
+  def destroy
+    if @current_user.permissions.collect { |p| p.role }.find { |r| r.name == 
"Administrator" }
+      if request.post? || request.delete?
+        @user = User.find(params[:id])
+        if @user.destroy
+          flash[:notice] = "User Deleted"
+        else
+          flash[:error] = {
+            :summary => "Failed to delete User",
+            :failures => @user.errors.full_messages,
+          }
+        end
+      end
+    else
+      flash[:notice] = "Invalid Permission to perform this operation"
+    end
+    redirect_to :dashboard
+   end
+end
diff --git a/src/app/models/quota.rb b/src/app/models/quota.rb
index 3bb05b8..6087ee1 100644
--- a/src/app/models/quota.rb
+++ b/src/app/models/quota.rb
@@ -77,6 +77,17 @@ class Quota < ActiveRecord::Base
     return quota_resources
   end
 
+  def percentage_used
+    if Quota.no_limit(maximum_running_instances) || running_instances == 0
+      return 0
+    elsif maximum_running_instances == 0
+      return 100
+    else
+      percentage_used = (running_instances.to_f / 
maximum_running_instances.to_f) * 100
+      return percentage_used
+    end
+  end
+
   def self.no_limit(resource)
     if resource.to_s == NO_LIMIT.to_s
       return true
diff --git a/src/app/views/users/index.haml b/src/app/views/users/index.haml
index 529cb5c..f5b47b9 100644
--- a/src/app/views/users/index.haml
+++ b/src/app/views/users/index.haml
@@ -1,16 +1,64 @@
-%table
-  %tr
-    %td login
-    %td email
-    %td first_name
-    %td last_name
-    %td current_login_at
-    %td last_login_at
-  [email protected] do |user|
+%div
+  %dl
+    %dt
+      Users
+      %dd.edit
+        %span
+        = link_to "edit", edit_user_url(@users.first.id), :id => "edit_link"
+      %dd.delete
+        %span
+        = link_to "delete", user_url(@users.first.id), :id => "delete_link", 
:method => "delete"
+      %dd.create
+        %span
+        = link_to "create", new_user_url
+
+%div
+  %table
     %tr
-      %td= user.login
-      %td= user.email
-      %td= user.first_name
-      %td= user.last_name
-      %td= user.current_login_at
-      %td= user.last_login_at
+      %th
+      %th= link_to "User ID", users_url(:sort_by => "login")
+      %th= link_to "Last Name", users_url(:sort_by => "last_name")
+      %th= link_to "First Name", users_url(:sort_by => "first_name")
+      %th= link_to "% Quota Used", users_url(:sort_by => 
"percentage_quota_used")
+      %th
+        =link_to "Quota", users_url(:sort_by => "quota")
+        %span (Instances)
+      %th= link_to "e-mail", users_url(:sort_by => "email")
+    [email protected] do |user|
+      %tr
+        %td
+          - if user == @users.first
+            %input{:checked => true, :name => "user_checkbox", :type => 
"checkbox", :onchange => "update_link(#{user.id})", :id => 
"user_checkbox_#{user.id}" }
+          - else
+            %input{:checked => false, :name => "user_checkbox", :type => 
"checkbox", :onchange => "update_link(#{user.id})", :id => 
"user_checkbox_#{user.id}" }
+        %td= link_to user.login, user, :id => user.id
+        %td= user.first_name
+        %td= user.last_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
+
+:javascript
+  function update_link(id)
+  {
+    var checkbox = document.getElementById('user_checkbox_' + id)
+    var edit_link = document.getElementById('edit_link')
+    var delete_link = document.getElementById('delete_link')
+
+    if(checkbox.checked)
+    {
+      edit_link.href = "/users/" + id + "/edit"
+      delete_link.href = "/users/" + id
+
+      var checkboxes = document.getElementsByName("user_checkbox")
+      for(var i = 0; i < checkboxes.length; i++)
+      {
+         checkboxes[i].checked = false
+      }
+      checkbox.checked = true
+    }
+    else
+    {
+      checkbox.checked = true
+    }
+  }
\ No newline at end of file
-- 
1.7.2.2

_______________________________________________
deltacloud-devel mailing list
[email protected]
https://fedorahosted.org/mailman/listinfo/deltacloud-devel

Reply via email to