myexperiment-hackers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[myexperiment-hackers] [1863] branches/event_logging/app: Work on news g


From: noreply
Subject: [myexperiment-hackers] [1863] branches/event_logging/app: Work on news generation.
Date: Wed, 15 Oct 2008 13:49:05 -0400 (EDT)

Revision
1863
Author
alekses6
Date
2008-10-15 13:49:05 -0400 (Wed, 15 Oct 2008)

Log Message

Work on news generation. Permissions now logged properly. Started generating news entries for permissions - not operational (and causing errors) at the moment.

Modified Paths

Diff

Modified: branches/event_logging/app/controllers/application.rb (1862 => 1863)


--- branches/event_logging/app/controllers/application.rb	2008-10-15 12:08:40 UTC (rev 1862)
+++ branches/event_logging/app/controllers/application.rb	2008-10-15 17:49:05 UTC (rev 1863)
@@ -189,13 +189,14 @@
     if updating_class == "5"
       if params[:updating_somefriends]
         # Delete old User permissions
-        policy.delete_all_user_permissions
+        policy.delete_all_user_permissions(current_user.id)
         
         # Now create new User permissions, if required
         params[:updating_somefriends].each do |f|
           Permission.new(:policy => policy,
               :contributor => (User.find f[1].to_i),
-              :view => 1, :download => 1, :edit => 1).save
+              :view => 1, :download => 1, :edit => 1, 
+              :userid_initiating_action => current_user.id).save
         end
       else # none of the 'some of my friends' were selected, error
         # revert changes made to policy (however any permissions updated will preserve the state)
@@ -207,7 +208,7 @@
     else
       # Delete all User permissions - as this isn't mode 5 (i.e. the mode has changed),
       # where some explicit permissions to friends are set
-      policy.delete_all_user_permissions
+      policy.delete_all_user_permissions(current_user.id)
     end
     
     
@@ -220,6 +221,7 @@
           # If a hash value with key 'id' wasn't returned then that means the checkbox was unchecked.
           unless n[1][:id]
             if p.contributor_type == 'Network' and p.contributor_id == n[0].to_i
+              p.userid_initiating_action = current_user.id
               p.destroy
             end
           end
@@ -238,10 +240,11 @@
           
           unless (perm = Permission.find(:first, :conditions => ["policy_id = ? AND contributor_type = ? AND contributor_id = ?", policy.id, 'Network', n_id]))
             # Only create new Permission if it doesn't already exist
-            p = Permission.new(:policy => policy, :contributor => (Network.find(n_id)))
+            p = Permission.new(:policy => policy, :contributor => (Network.find(n_id)), :userid_initiating_action => current_user.id)
             p.set_level!(level) if level
           else
             # Update the 'level' on the existing permission
+            perm.userid_initiating_action = current_user.id
             perm.set_level!(level) if level
           end
           
@@ -251,6 +254,7 @@
           
           # Delete permission if it exists (because this one is unchecked)
           if (perm = Permission.find(:first, :conditions => ["policy_id = ? AND contributor_type = ? AND contributor_id = ?", policy.id, 'Network', n_id]))
+            perm.userid_initiating_action = current_user.id
             perm.destroy
           end
           

Modified: branches/event_logging/app/controllers/permissions_controller.rb (1862 => 1863)


--- branches/event_logging/app/controllers/permissions_controller.rb	2008-10-15 12:08:40 UTC (rev 1862)
+++ branches/event_logging/app/controllers/permissions_controller.rb	2008-10-15 17:49:05 UTC (rev 1863)
@@ -28,7 +28,7 @@
   # GET /policies/1/permissions/new
   # GET /permissions/new
   def new
-    @permission = Permission.new
+    @permission = Permission.new(:userid_initiating_action => current_user.id)
     
     begin
       policy = Policy.find(params[:policy_id], :conditions => ["contributor_id = ? AND contributor_type = ?", current_user.id, current_user.class.to_s])
@@ -57,7 +57,7 @@
       error("Contributor ID not selected", "not selected", :contributor_id)  
     end
     
-    @permission = Permission.new(params[:permission])
+    @permission = Permission.new(params[:permission], :userid_initiating_action => current_user.id)
 
     respond_to do |format|
       if @permission.save
@@ -74,6 +74,7 @@
   # PUT /permissions/1
   def update
     respond_to do |format|
+      @permission.userid_initiating_action = current_user.id
       if @permission.update_attributes(params[:permission])
         flash[:notice] = 'Permission was successfully updated.'
         #format.html { redirect_to permission_url(@permission.policy, @permission) }
@@ -89,6 +90,7 @@
   def destroy
     policy = @permission.policy
     
+    @permission.userid_initiating_action = current_user.id
     @permission.destroy
 
     respond_to do |format|

Modified: branches/event_logging/app/helpers/application_helper.rb (1862 => 1863)


--- branches/event_logging/app/helpers/application_helper.rb	2008-10-15 12:08:40 UTC (rev 1862)
+++ branches/event_logging/app/helpers/application_helper.rb	2008-10-15 17:49:05 UTC (rev 1863)
@@ -937,6 +937,8 @@
     case model_name
       when "Blob"
         return "File"
+      when "Network"
+        return "Group"
       when "GroupAnnouncement"
         return "group announcement"
       else
@@ -1510,6 +1512,60 @@
             # do nothing, but don't display the news entry for missing group announcement / group
           end
         end
+        
+      when "Permission"
+        case action
+          when "create", "update"
+            begin
+              permission = Permission.find(log_entry.activity_loggable_id)
+              
+              # before anything else, check that this permission actually gives some sharing
+              # permissions - if not, no point in more queries to the DB 
+              access_rights = ""
+              write_news_entry = true
+              if permission.edit
+                access_rights += "(full access)"
+              elsif permission.download
+                access_rights += "(view & download only)"
+              elsif permission.view
+                access_rights += "(view only)"
+              else
+                # permission object exists, but doesn't allow any actual sharing
+                # -> so not to be shown in the news feed
+                write_news_entry = false
+              end
+              
+              if write_news_entry
+                shared_with, shared_with_path = evaluate_object_instance_and_path(log_entry.referenced_type, log_entry.referenced_id)
+                case shared_with.class.to_s
+                  when "User"
+                    shared_with_name_or_title = shared_with.name
+                  when "Network"
+                    shared_with_name_or_title = shared_with.title
+                  else
+                    shared_with_name_or_title = shared_with.to_s
+                end
+                
+                # TODO: check that if not found by sql, RecordNotFound exception is raised
+                shared_what_links_arr = []
+                items_shared_with_this_permission = Contribution.find_by_sql("SELECT contributable_type, contributable_id FROM Contributions WHERE policy_id = #{permission.policy_id}")
+                puts "============ HEHEHE ============"
+                items_shared_with_this_permission.each do |i|
+                  puts i
+                  puts i[0][0]
+                  puts i[0][1]
+                end
+                items_shared_with_this_permission.each do |item|
+                  object, path = evaluate_object_instance_and_path(item[0], item[1])
+                  shared_what_links_arr << link_to(object.title, path)
+                end
+                
+                rtn << [timestamp, "#{name(log_entry.culprit_id)} shared #{access_rights} #{shared_what_links_arr.join(", ")} with #{link_to shared_with_name_or_title, shared_with_path}"]
+              end
+            rescue ActiveRecord::RecordNotFound
+              # do nothing, but don't display the news entry for missing permission / network or user to which it refers to
+            end
+        end
       
       when "Rating"
         case action

Modified: branches/event_logging/app/models/permission.rb (1862 => 1863)


--- branches/event_logging/app/models/permission.rb	2008-10-15 12:08:40 UTC (rev 1862)
+++ branches/event_logging/app/models/permission.rb	2008-10-15 17:49:05 UTC (rev 1863)
@@ -17,8 +17,30 @@
   
   before_create :check_duplicate
   
-  acts_as_activity_logged :models => { :culprit => { :model => :contributor },
-                                       :referenced => { :model => :policy } } if USE_EVENT_LOG
+  
+  # a temporary virtual attribute - required for the "log_allowed()"
+  # callback method to work properly - for example, for "create"
+  # action with respect to permissions it is important who has done this
+  # action, so that it can be logged properly;
+  #
+  # this variable is set in the appropriate actions in the controller
+  attr_accessor :userid_initiating_action
+  
+  if USE_EVENT_LOG
+    acts_as_activity_logged(:models => { :culprit => { :model => :contributor },
+                                         :referenced => { :model => :policy } },
+                            :check_log_allowed => true)
+  end
+  
+  def log_allowed(action_name)
+    case action_name.downcase
+      when "create", "update", "destroy"
+        ActivityLog.create(:action ="" action_name, :activity_loggable => self, 
+                           :culprit_type => "User", :culprit_id => self.userid_initiating_action,
+                           :referenced => self.contributor)
+        return false
+    end
+  end
 
   
   # ==== Levels ====

Modified: branches/event_logging/app/models/policy.rb (1862 => 1863)


--- branches/event_logging/app/models/policy.rb	2008-10-15 12:08:40 UTC (rev 1862)
+++ branches/event_logging/app/models/policy.rb	2008-10-15 17:49:05 UTC (rev 1863)
@@ -298,9 +298,11 @@
   
   
   # Deletes all User permissions - used in application.rb::update_policy()
-  def delete_all_user_permissions
+  # Parameters: "user_id" -> id of the user who has initiated the action
+  def delete_all_user_permissions(user_id=nil)
     self.permissions.each do |p|
       if p.contributor_type == 'User'
+        p.userid_initiating_action = user_id
         p.destroy
       end
     end

reply via email to

[Prev in Thread] Current Thread [Next in Thread]