From a502e2295b28dd655ba543c0584342bb3e4ea0b3 Mon Sep 17 00:00:00 2001 From: Christoph Haas Date: Sun, 23 Apr 2017 13:38:12 +0200 Subject: [PATCH] Users are connected to screenshots now --- app/models/screenshot.rb | 66 +++++++++++++++++++++++++----- app/views/devise/sessions/new.slim | 1 + 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/app/models/screenshot.rb b/app/models/screenshot.rb index 024e731..6d2969c 100644 --- a/app/models/screenshot.rb +++ b/app/models/screenshot.rb @@ -1,5 +1,6 @@ class Screenshot < ApplicationRecord - belongs_to :package, :inverse_of=>:screenshots + belongs_to :package, inverse_of: :screenshots + belongs_to :user, inverse_of: :screenshots has_attached_file :image, styles: { :large => '800x600>', :thumb => '160x120>' }, @@ -8,7 +9,7 @@ class Screenshot < ApplicationRecord url: '/screenshots/:id_partition/:style.png' validates_attachment_content_type :image, :content_type => 'image/png' validates_with AttachmentSizeValidator, :attributes => :image, :less_than => 5.megabytes -# validate :validate_image_is_unique + # validate :validate_image_is_unique validates :delete_reason, length: { in: 5..100 }, allow_nil: true # Validator that checks if the image has already been uploaded. @@ -17,8 +18,6 @@ class Screenshot < ApplicationRecord # Currently this check makes sure that the same screenshot is not uploaded # twice for the same package. However it allows the screenshot to be uploaded # for two different packages. Restricting that further may happen later. - - # TODO: will fail if screenshot is updated because it finds its own image def validate_image_is_unique # Look for images with the same checksum / image_fingerprint if screenshot=Screenshot.find_by(image_fingerprint: image_fingerprint, package_id: self.package.id) @@ -51,8 +50,11 @@ class Screenshot < ApplicationRecord end def uploader - # TODO: Implement the ownership of images - 'Anonymous' + if self.user + self.user.name + else + 'Anonymous' + end end # Return Debshots 1.x path to allow migration of images into Paperclip filesystem schema @@ -88,13 +90,33 @@ class Screenshot < ApplicationRecord end end - # Return the number of screenshots that need to be approved - # def self.unapproved - # self.where(approved: false) - # end + # Check if the user is allowed to change this screenshot + def user_can_alter? + # - Is this the user's own screenshot? (anonymous) + if self.uploaderhash == session[:token] + logger.debug "Screenshot belongs to current anonymous user" + return true + end + + # - Is this the user's own screenshot? (anonymous) + if self.user = current_user + logger.debug "Screenshot belongs to user who is currently logged in" + return true + end + + # - Is the user an admin (=logged in)? + if current_user.can_admin? + logger.debug "User is an administrator and has super powers" + return true + end + + return false + end # Publish a screenshot from the moderation queue def approve_screenshot! + + self.delete_reason = nil self.markedfordelete = false self.approved = true @@ -118,4 +140,28 @@ class Screenshot < ApplicationRecord def can_admin? self.admin == 1 end + + # Check whether an upload would require moderation + def can_upload_without_moderation? + # Admins can + true if self.can_admin + + # Authenticated users with at least one approved screenshot + true if self.screenshots.where(approved: true).count >= 1 + + # Other visitors require moderation + false + end + + def can_delete? + # Admins can + true if self.can_admin + + # Authenticated users with at least one approved screenshot + true if self.screenshots.where(approved: true).count >= 1 + + # Other visitors require moderation + false + end + end diff --git a/app/views/devise/sessions/new.slim b/app/views/devise/sessions/new.slim index d9efc4e..57566af 100644 --- a/app/views/devise/sessions/new.slim +++ b/app/views/devise/sessions/new.slim @@ -40,6 +40,7 @@ .row = form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| + = f.hidden_field :provider, value: 'local' .small-2.columns b Admin only… .small-5.columns