From aa0ffeb37b353f0736a71d06157cdf66872bad17 Mon Sep 17 00:00:00 2001
From: nyanpasu
Date: Mon, 16 May 2016 02:19:47 +0800
Subject: [PATCH 1/4] Allow admin/moderator to disable user from inviting
---
app/controllers/invitations_controller.rb | 6 +++
app/controllers/users_controller.rb | 30 +++++++++++++-
app/mailers/disable_invite_notification.rb | 16 ++++++++
app/models/user.rb | 40 +++++++++++++++++++
.../notify.text.erb | 6 +++
app/views/settings/index.html.erb | 6 ++-
app/views/users/show.html.erb | 28 ++++++++++++-
config/routes.rb | 2 +
...515162433_add_disabled_invites_to_users.rb | 7 ++++
db/schema.rb | 5 ++-
10 files changed, 141 insertions(+), 5 deletions(-)
create mode 100644 app/mailers/disable_invite_notification.rb
create mode 100644 app/views/disable_invite_notification/notify.text.erb
create mode 100644 db/migrate/20160515162433_add_disabled_invites_to_users.rb
diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb
index e750fb1..8485cb3 100644
--- a/app/controllers/invitations_controller.rb
+++ b/app/controllers/invitations_controller.rb
@@ -30,6 +30,12 @@ class InvitationsController < ApplicationController
end
def create
+ if !@user.can_invite?
+ flash[:error] = "Your account cannot send invitations"
+ redirect_to "/settings"
+ return
+ end
+
i = Invitation.new
i.user_id = @user.id
i.email = params[:email]
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index c7db870..818b822 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -1,5 +1,7 @@
class UsersController < ApplicationController
- before_filter :require_logged_in_moderator, :only => [ :ban, :unban ]
+ before_filter :require_logged_in_moderator, :only => [ :enable_invitation,
+ :disable_invitation,
+ :ban, :unban ]
def show
@showing_user = User.where(:username => params[:username]).first!
@@ -37,6 +39,32 @@ class UsersController < ApplicationController
@title = "Pass Along an Invitation"
end
+ def disable_invitation
+ target = User.where(:username => params[:username]).first
+ if !target
+ flash[:error] = "Invalid user."
+ return redirect_to "/"
+ end
+
+ target.disable_invite_by_user_for_reason!(@user, params[:reason])
+
+ flash[:success] = "User has had invite capability disabled."
+ return redirect_to user_path(:user => target.username)
+ end
+
+ def enable_invitation
+ target = User.where(:username => params[:username]).first
+ if !target
+ flash[:error] = "Invalid user."
+ return redirect_to "/"
+ end
+
+ target.enable_invite_by_user!(@user)
+
+ flash[:success] = "User has had invite capability enabled."
+ return redirect_to user_path(:user => target.username)
+ end
+
def ban
buser = User.where(:username => params[:username]).first
if !buser
diff --git a/app/mailers/disable_invite_notification.rb b/app/mailers/disable_invite_notification.rb
new file mode 100644
index 0000000..1f4d4bc
--- /dev/null
+++ b/app/mailers/disable_invite_notification.rb
@@ -0,0 +1,16 @@
+class DisableInviteNotification < ActionMailer::Base
+ default :from => "#{Rails.application.name} " <<
+ ""
+
+ def notify(user, mod, reason)
+ @mod = mod
+ @reason = reason
+
+ mail(
+ :from => "#{@mod.username} ",
+ :replyto => "#{@mod.username} <#{@mod.email}>",
+ :to => user.email,
+ :subject => "[#{Rails.application.name}] Your invite privileges have been removed"
+ )
+ end
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index aa882de..d4aaeeb 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -18,6 +18,8 @@ class User < ActiveRecord::Base
:class_name => "User"
belongs_to :banned_by_user,
:class_name => "User"
+ belongs_to :disabled_invite_by_user,
+ :class_name => "User"
has_many :invitations
has_many :votes
has_many :voted_stories, -> { where('votes.comment_id' => nil) },
@@ -108,6 +110,25 @@ class User < ActiveRecord::Base
end
end
+ def disable_invite_by_user_for_reason!(disabler, reason)
+ self.disabled_invite_at = Time.now
+ self.disabled_invite_by_user_id = disabler.id
+ self.disabled_invite_reason = reason
+
+ self.delete!
+
+ DisableInviteNotification.notify(self, disabler, reason)
+
+ m = Moderation.new
+ m.moderator_user_id = disabler.id
+ m.user_id = self.id
+ m.action = "Disabled invitations"
+ m.reason = reason
+ m.save!
+
+ true
+ end
+
def ban_by_user_for_reason!(banner, reason)
self.banned_at = Time.now
self.banned_by_user_id = banner.id
@@ -252,6 +273,10 @@ class User < ActiveRecord::Base
banned_at?
end
+ def can_invite?
+ !disabled_invite_at?
+ end
+
def is_new?
Time.now - self.created_at <= NEW_USER_DAYS.days
end
@@ -318,6 +343,21 @@ class User < ActiveRecord::Base
true
end
+ def enable_invite_by_user!(mod)
+ self.disabled_invite_at = nil
+ self.disabled_invite_by_user_id = nil
+ self.disabled_invite_reason = nil
+ self.save!
+
+ m = Moderation.new
+ m.moderator_user_id = mod.id
+ m.user_id = self.id
+ m.action = "Enabled invitations"
+ m.save!
+
+ true
+ end
+
def undeleted_received_messages
received_messages.where(:deleted_by_recipient => false)
end
diff --git a/app/views/disable_invite_notification/notify.text.erb b/app/views/disable_invite_notification/notify.text.erb
new file mode 100644
index 0000000..df7634e
--- /dev/null
+++ b/app/views/disable_invite_notification/notify.text.erb
@@ -0,0 +1,6 @@
+Invitations have been disabled for you on <%= Rails.application.name %> by <%= @mod.username %> for:
+
+ <%= word_wrap(@reason, :line_width => 72).gsub(/\n/, "\n ") %>
+
+You can no longer allowed send invites. If you wish, you can discuss this with
+the moderator by replying to this e-mail.
diff --git a/app/views/settings/index.html.erb b/app/views/settings/index.html.erb
index c737a79..2d11ef0 100644
--- a/app/views/settings/index.html.erb
+++ b/app/views/settings/index.html.erb
@@ -212,7 +212,11 @@
Invite a New User
- <%= render :partial => "users/invitationform" %>
+ <% if @user.can_invite? %>
+ <%= render :partial => "users/invitationform" %>
+ <% else %>
+ You cannot send invitations.
+ <% end %>
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb
index ce2ad6a..258dd40 100644
--- a/app/views/users/show.html.erb
+++ b/app/views/users/show.html.erb
@@ -35,6 +35,9 @@
<% end %>
<%= @showing_user.is_admin? ? "administrator" :
(@showing_user.is_moderator? ? "moderator" : "user") %>
+ <% if !@showing_user.can_invite? %>
+ with invites disabled
+ <% end %>
@@ -187,11 +190,11 @@
<% end %>
<% else %>
- <%= form_tag user_ban_path, :method => :post do %>
- Banning a user will send an e-mail to the user with the reason below,
+ Banning or disabling invites for a user will send an e-mail to the user with the reason below,
with your e-mail address as the Reply-To so the user can respond.
+ <%= form_tag user_ban_path, :method => :post do %>
@@ -204,5 +207,26 @@
<% end %>
<% end %>
+
+ <% if !@showing_user.can_invite? %>
+ <%= form_tag user_enable_invite_path, :method => :post do %>
+
+ <%= submit_tag "Enable invitations for User" %>
+
+ <% end %>
+ <% else %>
+ <%= form_tag user_disable_invite_path, :method => :post do %>
+
+
+
+ <%= label_tag :reason, "Reason:", :class => "required" %>
+ <%= text_field_tag :reason, "", :size => 40 %>
+
+
+
+ <%= submit_tag "Disable invites" %>
+
+ <% end %>
+ <% end %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 0550610..e2320d3 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -95,6 +95,8 @@ Lobsters::Application.routes.draw do
post "/users/:username/ban" => "users#ban", :as => "user_ban"
post "/users/:username/unban" => "users#unban", :as => "user_unban"
+ post "/users/:username/disable_invitation" => "users#disable_invitation", :as => "user_disable_invite"
+ post "/users/:username/enable_invitation" => "users#enable_invitation", :as => "user_enable_invite"
get "/settings" => "settings#index"
post "/settings" => "settings#update"
diff --git a/db/migrate/20160515162433_add_disabled_invites_to_users.rb b/db/migrate/20160515162433_add_disabled_invites_to_users.rb
new file mode 100644
index 0000000..7a48da5
--- /dev/null
+++ b/db/migrate/20160515162433_add_disabled_invites_to_users.rb
@@ -0,0 +1,7 @@
+class AddDisabledInvitesToUsers < ActiveRecord::Migration
+ def change
+ add_column :users, :disabled_invite_at, :datetime
+ add_column :users, :disabled_invite_by_user_id, :integer
+ add_column :users, :disabled_invite_reason, :string, {limit: 200}
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index dbb50de..ccd3e31 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20160406160019) do
+ActiveRecord::Schema.define(version: 20160515162433) do
create_table "comments", force: true do |t|
t.datetime "created_at", null: false
@@ -212,6 +212,9 @@ ActiveRecord::Schema.define(version: 20160406160019) do
t.boolean "show_avatars", default: false
t.boolean "show_story_previews", default: false
t.boolean "show_submitted_story_threads", default: true
+ t.datetime disabled_invite_at
+ t.integer disabled_invite_by_user_id
+ t.string disabled_invite_reason, limit: 200
end
add_index "users", ["mailing_list_mode"], name: "mailing_list_enabled", using: :btree
From 39b911963e23fe0bfc93e1bf9bf01da951fd5b09 Mon Sep 17 00:00:00 2001
From: nyanpasu
Date: Mon, 16 May 2016 11:54:45 +0800
Subject: [PATCH 2/4] Remove redundant return
---
app/controllers/users_controller.rb | 24 ++++++++++++------------
1 file changed, 12 insertions(+), 12 deletions(-)
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb
index 818b822..4a6e178 100644
--- a/app/controllers/users_controller.rb
+++ b/app/controllers/users_controller.rb
@@ -43,26 +43,26 @@ class UsersController < ApplicationController
target = User.where(:username => params[:username]).first
if !target
flash[:error] = "Invalid user."
- return redirect_to "/"
+ redirect_to "/"
+ else
+ target.disable_invite_by_user_for_reason!(@user, params[:reason])
+
+ flash[:success] = "User has had invite capability disabled."
+ redirect_to user_path(:user => target.username)
end
-
- target.disable_invite_by_user_for_reason!(@user, params[:reason])
-
- flash[:success] = "User has had invite capability disabled."
- return redirect_to user_path(:user => target.username)
end
def enable_invitation
target = User.where(:username => params[:username]).first
if !target
flash[:error] = "Invalid user."
- return redirect_to "/"
+ redirect_to "/"
+ else
+ target.enable_invite_by_user!(@user)
+
+ flash[:success] = "User has had invite capability enabled."
+ redirect_to user_path(:user => target.username)
end
-
- target.enable_invite_by_user!(@user)
-
- flash[:success] = "User has had invite capability enabled."
- return redirect_to user_path(:user => target.username)
end
def ban
From 19526c2d3ff7d98a99cc4f365dee1020954c3ead Mon Sep 17 00:00:00 2001
From: nyanpasu
Date: Thu, 19 May 2016 08:41:39 +0800
Subject: [PATCH 3/4] Don't delete user after disabling invites
---
app/models/user.rb | 2 --
1 file changed, 2 deletions(-)
diff --git a/app/models/user.rb b/app/models/user.rb
index d4aaeeb..653a008 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -115,8 +115,6 @@ class User < ActiveRecord::Base
self.disabled_invite_by_user_id = disabler.id
self.disabled_invite_reason = reason
- self.delete!
-
DisableInviteNotification.notify(self, disabler, reason)
m = Moderation.new
From 99f78ad10c163a64c588868003fb3dff75aea799 Mon Sep 17 00:00:00 2001
From: nyanpasu
Date: Thu, 19 May 2016 08:59:59 +0800
Subject: [PATCH 4/4] Send pm instead of email on invite disable
---
app/mailers/disable_invite_notification.rb | 16 ----------------
app/models/user.rb | 12 +++++++++++-
.../disable_invite_notification/notify.text.erb | 6 ------
3 files changed, 11 insertions(+), 23 deletions(-)
delete mode 100644 app/mailers/disable_invite_notification.rb
delete mode 100644 app/views/disable_invite_notification/notify.text.erb
diff --git a/app/mailers/disable_invite_notification.rb b/app/mailers/disable_invite_notification.rb
deleted file mode 100644
index 1f4d4bc..0000000
--- a/app/mailers/disable_invite_notification.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-class DisableInviteNotification < ActionMailer::Base
- default :from => "#{Rails.application.name} " <<
- ""
-
- def notify(user, mod, reason)
- @mod = mod
- @reason = reason
-
- mail(
- :from => "#{@mod.username} ",
- :replyto => "#{@mod.username} <#{@mod.email}>",
- :to => user.email,
- :subject => "[#{Rails.application.name}] Your invite privileges have been removed"
- )
- end
-end
diff --git a/app/models/user.rb b/app/models/user.rb
index 653a008..7927195 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -115,7 +115,17 @@ class User < ActiveRecord::Base
self.disabled_invite_by_user_id = disabler.id
self.disabled_invite_reason = reason
- DisableInviteNotification.notify(self, disabler, reason)
+ msg = Message.new
+ msg.deleted_by_author = true
+ msg.author_user_id = disabler.id
+ msg.recipient_user_id = self.id
+ msg.subject = "Your invite privileges have been revoked"
+ msg.body = "The reason given:\n" <<
+ "\n" <<
+ "> *#{reason}*\n" <<
+ "\n" <<
+ "*This is an automated message.*"
+ msg.save
m = Moderation.new
m.moderator_user_id = disabler.id
diff --git a/app/views/disable_invite_notification/notify.text.erb b/app/views/disable_invite_notification/notify.text.erb
deleted file mode 100644
index df7634e..0000000
--- a/app/views/disable_invite_notification/notify.text.erb
+++ /dev/null
@@ -1,6 +0,0 @@
-Invitations have been disabled for you on <%= Rails.application.name %> by <%= @mod.username %> for:
-
- <%= word_wrap(@reason, :line_width => 72).gsub(/\n/, "\n ") %>
-
-You can no longer allowed send invites. If you wish, you can discuss this with
-the moderator by replying to this e-mail.