From cba7f20dc8614d12e3eeda6e14f454aeb22b9b54 Mon Sep 17 00:00:00 2001
From: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
Date: Thu, 6 Aug 2015 14:03:27 +0200
Subject: [PATCH] Allow users to send abuse reports

Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
---
 CHANGELOG                                     |  1 +
 app/controllers/abuse_reports_controller.rb   | 23 +++++++++++++++
 app/models/abuse_report.rb                    |  9 ++++++
 app/views/abuse_reports/new.html.haml         | 29 +++++++++++++++++++
 app/views/users/show.html.haml                | 10 +++++++
 config/routes.rb                              |  3 ++
 .../20150806104937_create_abuse_reports.rb    | 11 +++++++
 db/schema.rb                                  | 10 ++++++-
 spec/factories/abuse_reports.rb               |  9 ++++++
 spec/models/abuse_report_spec.rb              |  7 +++++
 10 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 app/controllers/abuse_reports_controller.rb
 create mode 100644 app/models/abuse_report.rb
 create mode 100644 app/views/abuse_reports/new.html.haml
 create mode 100644 db/migrate/20150806104937_create_abuse_reports.rb
 create mode 100644 spec/factories/abuse_reports.rb
 create mode 100644 spec/models/abuse_report_spec.rb

diff --git a/CHANGELOG b/CHANGELOG
index 943e4e8c586..0ee85090fdf 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -34,6 +34,7 @@ v 7.14.0 (unreleased)
   - Add support for CI skipped status
   - Fetch code from forks to refs/merge-requests/:id/head when merge request created
   - Remove satellites 
+  - Allow users to send abuse reports
 
 v 7.13.2
   - Fix randomly failed spec
diff --git a/app/controllers/abuse_reports_controller.rb b/app/controllers/abuse_reports_controller.rb
new file mode 100644
index 00000000000..757be5ef727
--- /dev/null
+++ b/app/controllers/abuse_reports_controller.rb
@@ -0,0 +1,23 @@
+class AbuseReportsController < ApplicationController
+  def new
+    @abuse_report = AbuseReport.new
+    @abuse_report.user_id = params[:user_id]
+  end
+
+  def create
+    @abuse_report = AbuseReport.new(report_params)
+    @abuse_report.reporter = current_user
+
+    if @abuse_report.save
+      redirect_to root_path, notice: 'Thank you for report. GitLab administrator will be able to see it'
+    else
+      render :new
+    end
+  end
+
+  private
+
+  def report_params
+    params.require(:abuse_report).permit(:user_id, :message)
+  end
+end
diff --git a/app/models/abuse_report.rb b/app/models/abuse_report.rb
new file mode 100644
index 00000000000..c8c39db11bc
--- /dev/null
+++ b/app/models/abuse_report.rb
@@ -0,0 +1,9 @@
+class AbuseReport < ActiveRecord::Base
+  belongs_to :reporter, class_name: "User"
+  belongs_to :user
+
+  validates :reporter, presence: true
+  validates :user, presence: true
+  validates :message, presence: true
+  validates :user_id, uniqueness: { scope: :reporter_id }
+end
diff --git a/app/views/abuse_reports/new.html.haml b/app/views/abuse_reports/new.html.haml
new file mode 100644
index 00000000000..736456b67ba
--- /dev/null
+++ b/app/views/abuse_reports/new.html.haml
@@ -0,0 +1,29 @@
+- page_title "Report abuse"
+%h3.page-title Report abuse
+%p Please use this form if user makes spam or inappropriate content
+%hr
+= form_for @abuse_report, html: { class: 'form-horizontal'} do |f|
+  = f.hidden_field :user_id
+  - if @abuse_report.errors.any?
+    .alert.alert-danger
+      - @abuse_report.errors.full_messages.each do |msg|
+        %p= msg
+  .form-group
+    = f.label :user_id, class: 'control-label'
+    .col-sm-10
+      = users_select_tag("abuse_reports[user_id]", placeholder: 'Select user to report abuse',
+        class: 'custom-form-control js-select2', selected: @abuse_report.user_id, scope: :all)
+  .form-group
+    = f.label :message, class: 'control-label'
+    .col-sm-10
+      = f.text_area :message, class: "form-control", rows: 2, required: true
+      .help-block
+        Explain the problem with this account.
+        %br
+        If user sends spam please provide a link to spam issue or comment
+
+  .form-actions
+    = f.submit "Send report", class: "btn btn-create"
+
+:coffeescript
+  new UsersSelect()
diff --git a/app/views/users/show.html.haml b/app/views/users/show.html.haml
index 43d847831d6..64b7f25ad37 100644
--- a/app/views/users/show.html.haml
+++ b/app/views/users/show.html.haml
@@ -18,6 +18,16 @@
             = link_to profile_path, class: 'btn btn-sm' do
               %i.fa.fa-pencil-square-o
               Edit Profile settings
+        - elsif current_user
+          .pull-right
+            %span.dropdown
+              %a.light.dropdown-toggle.btn.btn-sm{href: '#', "data-toggle" => "dropdown"}
+                = icon('exclamation-circle')
+              %ul.dropdown-menu.dropdown-menu-right
+                %li
+                  = link_to new_abuse_report_path(user_id: @user.id) do
+                    Report abuse
+
       .username
         @#{@user.username}
       .description
diff --git a/config/routes.rb b/config/routes.rb
index f252a6fcc91..4e90a89535e 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -65,6 +65,9 @@ Gitlab::Application.routes.draw do
     end
   end
 
+  # Spam reports
+  resources :abuse_reports, only: [:new, :create]
+
   #
   # Import
   #
diff --git a/db/migrate/20150806104937_create_abuse_reports.rb b/db/migrate/20150806104937_create_abuse_reports.rb
new file mode 100644
index 00000000000..e97dc4cf04c
--- /dev/null
+++ b/db/migrate/20150806104937_create_abuse_reports.rb
@@ -0,0 +1,11 @@
+class CreateAbuseReports < ActiveRecord::Migration
+  def change
+    create_table :abuse_reports do |t|
+      t.integer :reporter_id
+      t.integer :user_id
+      t.text :message
+
+      t.timestamps
+    end
+  end
+end
diff --git a/db/schema.rb b/db/schema.rb
index a63c2d05821..af10a2ff7cd 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,11 +11,19 @@
 #
 # It's strongly recommended that you check this file into your version control system.
 
-ActiveRecord::Schema.define(version: 20150717130904) do
+ActiveRecord::Schema.define(version: 20150806104937) do
 
   # These are extensions that must be enabled in order to support this database
   enable_extension "plpgsql"
 
+  create_table "abuse_reports", force: true do |t|
+    t.integer  "reporter_id"
+    t.integer  "user_id"
+    t.text     "message"
+    t.datetime "created_at"
+    t.datetime "updated_at"
+  end
+
   create_table "application_settings", force: true do |t|
     t.integer  "default_projects_limit"
     t.boolean  "signup_enabled"
diff --git a/spec/factories/abuse_reports.rb b/spec/factories/abuse_reports.rb
new file mode 100644
index 00000000000..29fcbc5e197
--- /dev/null
+++ b/spec/factories/abuse_reports.rb
@@ -0,0 +1,9 @@
+# Read about factories at https://github.com/thoughtbot/factory_girl
+
+FactoryGirl.define do
+  factory :abuse_report do
+    reporter factory: :user
+    user
+    message 'User sends spam'
+  end
+end
diff --git a/spec/models/abuse_report_spec.rb b/spec/models/abuse_report_spec.rb
new file mode 100644
index 00000000000..d83004a8388
--- /dev/null
+++ b/spec/models/abuse_report_spec.rb
@@ -0,0 +1,7 @@
+require 'rails_helper'
+
+RSpec.describe AbuseReport, type: :model do
+  subject { create(:abuse_report) }
+
+  it { expect(subject).to be_valid }
+end
-- 
GitLab