[Ovirt-devel] [PATCH server] converted the task controller to use the service layer.

Scott Seago sseago at redhat.com
Mon May 11 18:49:57 UTC 2009


Signed-off-by: Scott Seago <sseago at redhat.com>
---
 src/app/controllers/task_controller.rb             |   15 ++-------
 src/app/models/storage_volume.rb                   |    4 ++
 .../task_service.rb}                               |   35 +++++++++++---------
 3 files changed, 26 insertions(+), 28 deletions(-)
 copy src/app/{controllers/task_controller.rb => services/task_service.rb} (57%)

diff --git a/src/app/controllers/task_controller.rb b/src/app/controllers/task_controller.rb
index 9756f79..ba31e0c 100644
--- a/src/app/controllers/task_controller.rb
+++ b/src/app/controllers/task_controller.rb
@@ -18,19 +18,10 @@
 # also available at http://www.gnu.org/copyleft/gpl.html.
 
 class TaskController < ApplicationController
-  def show
-    @task = Task.find(params[:id])
-    if @task[:type] == VmTask.name
-      set_perms(@task.vm.vm_resource_pool)
-    elsif @task[:type] == StorageTask.name 
-      set_perms(@task.storage_pool.hardware_pool)
-    elsif @task[:type] == StorageVolumeTask.name
-      set_perms(@task.storage_volume.storage_pool.hardware_pool)
-    elsif @task[:type] == HostTask.name 
-      set_perms(@task.host.hardware_pool)
-    end
-    authorize_view
+  include TaskService
 
+  def show
+    svc_show(params[:id]
   end
 
 end
diff --git a/src/app/models/storage_volume.rb b/src/app/models/storage_volume.rb
index 59d166e..45cd5c6 100644
--- a/src/app/models/storage_volume.rb
+++ b/src/app/models/storage_volume.rb
@@ -166,6 +166,10 @@ class StorageVolume < ActiveRecord::Base
     return_hash
   end
 
+  def permission_obj
+    storage_pool.hardware_pool
+  end
+
   def movable?
      if vms.size > 0 or
          (not lvm_storage_pool.nil? and not lvm_storage_pool.movable?)
diff --git a/src/app/controllers/task_controller.rb b/src/app/services/task_service.rb
similarity index 57%
copy from src/app/controllers/task_controller.rb
copy to src/app/services/task_service.rb
index 9756f79..9e5b5b4 100644
--- a/src/app/controllers/task_controller.rb
+++ b/src/app/services/task_service.rb
@@ -1,6 +1,6 @@
-# 
-# Copyright (C) 2008 Red Hat, Inc.
-# Written by Scott Seago <sseago at redhat.com>
+#
+# Copyright (C) 2009 Red Hat, Inc.
+# Written by Scott Seago <sseago at redhat.com>,
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -16,21 +16,24 @@
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 # MA  02110-1301, USA.  A copy of the GNU General Public License is
 # also available at http://www.gnu.org/copyleft/gpl.html.
+# Mid-level API: Business logic around tasks
+module TaskService
+  include ApplicationService
 
-class TaskController < ApplicationController
-  def show
-    @task = Task.find(params[:id])
-    if @task[:type] == VmTask.name
-      set_perms(@task.vm.vm_resource_pool)
-    elsif @task[:type] == StorageTask.name 
-      set_perms(@task.storage_pool.hardware_pool)
-    elsif @task[:type] == StorageVolumeTask.name
-      set_perms(@task.storage_volume.storage_pool.hardware_pool)
-    elsif @task[:type] == HostTask.name 
-      set_perms(@task.host.hardware_pool)
-    end
-    authorize_view
+  # Load the Task with +id+ for viewing
+  #
+  # === Instance variables
+  # [<tt>@task</tt>] stores the Task with +id+
+  # === Required permissions
+  # [<tt>Privilege::VIEW</tt>] on task target's Pool
+  def svc_show(id)
+    lookup(id,Privilege::VIEW)
+  end
 
+  private
+  def lookup(id, priv)
+    @task = Task.find(id)
+    authorized!(priv, at task.task_target.permission_obj)
   end
 
 end
-- 
1.6.0.6




More information about the ovirt-devel mailing list