Allow admin/moderator to disable user from inviting

This commit is contained in:
nyanpasu 2016-05-16 02:19:47 +08:00
parent 6537e7dc36
commit aa0ffeb37b
10 changed files with 141 additions and 5 deletions

View file

@ -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]

View file

@ -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

View file

@ -0,0 +1,16 @@
class DisableInviteNotification < ActionMailer::Base
default :from => "#{Rails.application.name} " <<
"<nobody@#{Rails.application.domain}>"
def notify(user, mod, reason)
@mod = mod
@reason = reason
mail(
:from => "#{@mod.username} <nobody@#{Rails.application.domain}>",
:replyto => "#{@mod.username} <#{@mod.email}>",
:to => user.email,
:subject => "[#{Rails.application.name}] Your invite privileges have been removed"
)
end
end

View file

@ -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

View file

@ -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.

View file

@ -212,7 +212,11 @@
Invite a New User
</div>
<%= render :partial => "users/invitationform" %>
<% if @user.can_invite? %>
<%= render :partial => "users/invitationform" %>
<% else %>
You cannot send invitations.
<% end %>
<br>
<br>

View file

@ -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 %>
</span>
<br>
@ -187,11 +190,11 @@
</p>
<% end %>
<% else %>
<%= form_tag user_ban_path, :method => :post do %>
<p>
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.
</p>
<%= form_tag user_ban_path, :method => :post do %>
<p>
<div class="boxline">
@ -204,5 +207,26 @@
</p>
<% end %>
<% end %>
<% if !@showing_user.can_invite? %>
<%= form_tag user_enable_invite_path, :method => :post do %>
<p>
<%= submit_tag "Enable invitations for User" %>
</p>
<% end %>
<% else %>
<%= form_tag user_disable_invite_path, :method => :post do %>
<p>
<div class="boxline">
<%= label_tag :reason, "Reason:", :class => "required" %>
<%= text_field_tag :reason, "", :size => 40 %>
</div>
<p>
<%= submit_tag "Disable invites" %>
</p>
<% end %>
<% end %>
<% end %>
</div>

View file

@ -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"

View file

@ -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

View file

@ -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