ref: refs/heads/backgroud_patches_bnc550934
commit f73700478f2a675896ab1c36c35ca54667b62e7b
Author: Ladislav Slezak
Date: Fri Dec 4 16:04:50 2009 +0100
cache PermissionsController#show result
---
.../app/controllers/permissions_controller.rb | 56 ++++++++++++++++++--
1 files changed, 51 insertions(+), 5 deletions(-)
diff --git a/webservice/app/controllers/permissions_controller.rb b/webservice/app/controllers/permissions_controller.rb
index 9b03ebd..3a3e7eb 100644
--- a/webservice/app/controllers/permissions_controller.rb
+++ b/webservice/app/controllers/permissions_controller.rb
@@ -6,6 +6,18 @@ class PermissionsController < ApplicationController
before_filter :login_required
+ # before filter is called even when the action is cached
+ before_filter :check_perms, :cache_valid, :only => :show
+
+ # modify the cache path so it includes also the filter and user name parameters
+ caches_action :show, :cache_path => Proc.new { |controller|
+ ret = controller.controller_path + '/' + controller.params[:user_id] + '/' + controller.params[:filter]
+ Rails.logger.info "Using cache path: #{ret}"
+ ret
+ }
+
+ CACHE_ID = 'permissions:timestamp'
+
def initialize
@permissions = []
end
@@ -18,9 +30,45 @@ class PermissionsController < ApplicationController
def user_self( params )
!params[:user_id].blank? && (params[:user_id] == self.current_account.login)
end
-
- public
+ def check_perms
+ unless user_self(params)
+ permission_check "org.opensuse.yast.permissions.read"
+ end
+ end
+
+ def get_cache_timestamp
+ lst = [
+ # the global config file
+ File.mtime('/etc/PolicyKit/PolicyKit.conf'),
+ # policies
+ File.mtime('/usr/share/PolicyKit/policy/'),
+ # explicit user authorizations
+ File.mtime('/var/lib/PolicyKit/'),
+ # default overrides
+ File.mtime('/var/lib/PolicyKit-public/'),
+ ]
+
+ lst.delete_if { |item| item.nil? }
+
+ lst.max.to_i
+ end
+
+ def cache_valid
+ cache_timestamp = Rails.cache.read(CACHE_ID)
+ current_timestamp = get_cache_timestamp
+
+ if !cache_timestamp
+ Rails.cache.write(CACHE_ID, current_timestamp)
+ elsif cache_timestamp < current_timestamp
+ Rails.logger.debug "#### Permissions cache expired"
+ # expire all cached values using a regexp (for all users/filters)
+ expire_fragment(%r{#{controller_path}/.*})
+ Rails.cache.write(CACHE_ID, current_timestamp)
+ end
+ end
+
+ public
#--------------------------------------------------------------------------------
#
# actions
@@ -31,9 +79,7 @@ class PermissionsController < ApplicationController
# GET /permissions/:user_id(.:format)
def show
- unless user_self(params)
- permission_check "org.opensuse.yast.permissions.read"
- end
+ # note: permission check is done in the check_perms before filter
permission = Permission.find(:all,params)
logger.debug permission.inspect
respond_to do |format|
--
To unsubscribe, e-mail: yast-commit+unsubscribe@opensuse.org
For additional commands, e-mail: yast-commit+help@opensuse.org