Skip to content

Commit

Permalink
Fixed profile item authorization issue
Browse files Browse the repository at this point in the history
  • Loading branch information
parolkar committed Apr 22, 2009
1 parent de74d8e commit a51add4
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
14 changes: 9 additions & 5 deletions app/controllers/profile_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ def show
def edit
#TODO : Security check required
@profile_item = ProfileItem.find(params[:id])

render :partial => "/profile_items/edit/#{@profile_item.itemtype.to_s}" , :object => @profile_item

if @profile_item.check(:update_permission,session) == true # Check if accessing obj has edit permission
render :partial => "/profile_items/edit/#{@profile_item.itemtype.to_s}" , :object => @profile_item
else
render :partial => "/profile_items/show/#{@profile_item.itemtype.to_s}" , :object => @profile_item
end
end

# POST /profile_items
Expand All @@ -22,7 +26,7 @@ def create

respond_to do |format|
if @profile_item.save
flash[:notice] = 'ProfileItem was successfully created.'
#flash[:notice] = 'ProfileItem was successfully created.'
format.html { redirect_to(@profile_item) }
format.xml { render :xml => @profile_item, :status => :created, :location => @profile_item }
else
Expand All @@ -36,7 +40,7 @@ def create
def update
@profile_item = ProfileItem.find(params[:id])

if @profile_item.check(:update_permission,session) == false
if @profile_item.check(:update_permission,session) != true
render :text => "Operation Not Permitted / Malicious Request"
return
end
Expand All @@ -45,7 +49,7 @@ def update
@profile_item.content = params[:content]
@profile_item.active = true
if @profile_item.save
flash[:notice] = 'ProfileItem was successfully updated.'
#flash[:notice] = 'ProfileItem was successfully updated.'
render :partial => "/profile_items/show/#{@profile_item.itemtype.to_s}" , :object => @profile_item
else
render :partial => "/profile_items/edit/#{@profile_item.itemtype.to_s}" , :object => @profile_item
Expand Down
2 changes: 1 addition & 1 deletion app/models/profile_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class ProfileItem < ActiveRecord::Base

def check(permission, app_session)

ar_obj = ProfileItem.get_obj_accessing(app_session)
ar_obj = ProfileItem.get_obj_accessing(app_session)
return self.entity_that_has_profile.profile_items_access_permitted(ar_obj,permission)
rescue
false
Expand Down
6 changes: 5 additions & 1 deletion app/views/profile_items/_profile_item.html.haml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
.profile_item{:id=> "profile-item-#{profile_item.id}"}
= render :partial => "/profile_items/#{ (profile_item.new_record? || profile_item.content == nil ) ? 'edit' : 'show' }/#{profile_item.itemtype.to_s}" , :object => profile_item
- partial_to_be_rendered = "/profile_items/show/#{profile_item.itemtype.to_s}" #this is default
- if profile_item.check(:update_permission,session) == true && (profile_item.new_record? || profile_item.content == nil )
- partial_to_be_rendered = "/profile_items/edit/#{profile_item.itemtype.to_s}"

= render :partial => partial_to_be_rendered , :object => profile_item
6 changes: 3 additions & 3 deletions lib/has_profile_items.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ def profile_items_access_permitted(ar_obj,permission = :update_permission)
permitted_ar_objects = Array.new

method_symbols.each {|method_sym|
result_obj = ar_obj.send(method_sym.to_sym)
result_obj = self.send(method_sym.to_sym)
if result_obj.is_a?(Array)
permitted_ar_objects = permitted_ar_objects | result_obj #merge with no duplicates
else
permitted_ar_objects << result_obj # single insert
end
}
}

return permitted_ar_objects.include? ar_obj
rescue
false
Expand Down

0 comments on commit a51add4

Please sign in to comment.