daily pastebin goal
59%
SHARE
TWEET

Untitled

a guest Jun 21st, 2018 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.   def update_permissions
  2.     permission_edited = false
  3.     self.file_permissions.each do |p|
  4.       if p.new_record?
  5.         deleted_permission = self.file_permissions.deleted.find_by(user: p.user, access_role: p.access_role)
  6.         if deleted_permission.present? and deleted_permission != p
  7.           permission_edited = true
  8.           deleted_permission.delete
  9.         end
  10.       elsif p.changed?
  11.         permission_edited = true
  12.         p.inherted = false
  13.       elsif p.flagged_for_destroy?
  14.         permission_edited = true
  15.         self.file_permissions.build(user: p.user, access_role: p.access_role, access_type: p.access_type, inherted: p.inherted, deleted: true)
  16.       end
  17.     end
  18.  
  19.     return if children_changed? or permission_edited
  20.     parent_permissions_ids = []
  21.     self.parent.file_permissions.not_deleted.not_overwritten.each do |pfp|
  22.       permission = self.file_permissions.where(user: pfp.user, access_role: pfp.access_role).first
  23.       # if Directory doesnt have parent`s permission
  24.       if permission.blank?
  25.         unless pfp.deleted?
  26.           p = self.file_permissions.new(user: pfp.user, access_role: pfp.access_role, access_type: pfp.access_type, inherted: true)
  27.           parent_permissions_ids << p.id
  28.         end
  29.         # if Directory has parent`s permission
  30.       else
  31.         parent_permissions_ids << permission.id
  32.  
  33.         # if parent permission has different access_type. It means it must changed.
  34.         if permission.access_type != pfp.access_type
  35.           # if directory_permission is inherted from parent. It also may be deleted, so certain user can access directory.
  36.           if permission.inherted?
  37.             #inhert access_type from parent and restore if deleted
  38.             permission.access_type = pfp.access_type
  39.             permission.deleted = false
  40.             # if directory isnt inherted from parent. It must be defined by user.
  41.           else
  42.             #if directory has lower access_type than his parent
  43.             if permission.access_type == :read_only and pfp.access_type == :write
  44.               #if not arleady overwritten
  45.               unless permission.overwritten?
  46.                 #overwritte permission by creating a new inherted permission with higher access_type and overwrite flag to arleady existing. If we moved directory, inherted permission would be erased and overwritten permission would be restored.
  47.                 permission.overwritten = true
  48.                 p = self.file_permissions.new(user: pfp.user, access_role: pfp.access_role, access_type: pfp.access_type, inherted: true)
  49.                 parent_permissions_ids << p.id
  50.               end
  51.  
  52.             end
  53.  
  54.           end
  55.         end
  56.       end
  57.     end
  58.     self.file_permissions.where(:id.nin => parent_permissions_ids, inherted: true).each do |p|
  59.       overwritten_permission = self.file_permissions.overwritten.find_by(user: p.user, access_role: p.access_role)
  60.       overwritten_permission.overwritten = false if overwritten_permission.present?
  61.       p.delete
  62.     end
  63.   end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top