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..4a6e178 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." + 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 + end + + def enable_invitation + target = User.where(:username => params[:username]).first + if !target + flash[:error] = "Invalid user." + 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 + end + def ban buser = User.where(:username => params[:username]).first if !buser diff --git a/app/models/user.rb b/app/models/user.rb index aa882de..7927195 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,33 @@ 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 + + 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 + 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 +281,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 +351,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/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