implements user name change requests

This commit is contained in:
albert
2013-03-26 18:13:03 -04:00
parent 7939ce2a98
commit a9be96ce8b
12 changed files with 179 additions and 12 deletions

View File

@@ -1,6 +1,6 @@
class UserNameChangeRequestsController < ApplicationController
before_filter :member_only, :only => [:new, :create, :show]
before_filter :admin_only, :only => [:index, :approve, :reject]
before_filter :privileged_only, :only => [:new, :create, :show]
before_filter :admin_only, :only => [:index, :approve, :reject, :destroy]
def new
end
@@ -13,7 +13,12 @@ class UserNameChangeRequestsController < ApplicationController
:change_reason => params[:reason],
:desired_name => params[:desired_name]
)
redirect_to user_name_change_request_path(@change_request), :notice => "Your request has been submitted and is pending admin review"
if @change_request.errors.any?
render :action => "new"
else
redirect_to user_name_change_request_path(@change_request), :notice => "Your request has been submitted and is pending admin review"
end
end
def show
@@ -30,6 +35,12 @@ class UserNameChangeRequestsController < ApplicationController
redirect_to user_name_change_request_path(@change_request), :notice => "Name change request approved"
end
def destroy
@change_request = UserNameChangeRequest.find(params[:id])
@change_request.destroy
redirect_to user_name_change_requests_path
end
def reject
@change_request = UserNameChangeRequest.find(params[:id])
@change_request.reject!(params[:reason])

View File

@@ -464,9 +464,9 @@ class User < ActiveRecord::Base
if is_platinum?
nil
elsif is_privileged?
40_000
else
20_000
else
10_000
end
end
@@ -573,7 +573,7 @@ class User < ActiveRecord::Base
if params[:id].present?
q = q.where("id = ?", params[:id].to_i)
end
case params[:order]
when "name"
q = q.order("name")

View File

@@ -4,13 +4,27 @@ class UserNameChangeRequest < ActiveRecord::Base
belongs_to :user
belongs_to :approver, :class_name => "User"
validate :uniqueness_of_desired_name
validate :not_limited
validates_length_of :desired_name, :within => 2..100, :on => :create
validates_format_of :desired_name, :with => /\A[^\s:]+\Z/, :on => :create, :message => "cannot have whitespace or colons"
before_validation :normalize_name
after_create :notify_admins
def self.pending
where(:status => "pending")
end
def self.approved
where(:status => "approved")
end
def rejected?
status == "rejected"
end
def normalize_name
self.desired_name = desired_name.strip.gsub(/ /, "_")
end
def feedback
UserFeedback.for_user(user_id).order("id desc").all
@@ -29,6 +43,8 @@ class UserNameChangeRequest < ActiveRecord::Base
user.update_attribute(:name, desired_name)
body = "Your name change request has been approved. Be sure to log in with your new user name."
Dmail.create_split(:title => "Name change request approved", :body => body, :to_id => user_id)
UserFeedback.create(:user_id => user_id, :category => "neutral", :body => "Name changed from #{original_name} to #{desired_name}")
ModAction.create(:description => "Name changed from #{original_name} to #{desired_name}")
end
def reject!(reason)
@@ -37,6 +53,15 @@ class UserNameChangeRequest < ActiveRecord::Base
Dmail.create_split(:title => "Name change request rejected", :body => body, :to_id => user_id)
end
def not_limited
if UserNameChangeRequest.where("user_id = ? and created_at >= ?", CurrentUser.user.id, 1.week.ago).exists?
errors.add(:base, "You can only submit one name change request per week")
return false
else
return true
end
end
def uniqueness_of_desired_name
if User.find_by_name(desired_name)
errors.add(:desired_name, "already exists")

View File

@@ -130,4 +130,8 @@ class UserPresenter
user.subscriptions.select {|x| x.is_public?}
end
end
def previous_names
UserNameChangeRequest.approved.where("user_id = ?", user.id).map(&:original_name).join(", ")
end
end

View File

@@ -0,0 +1,24 @@
<div id="c-user-name-change-requests">
<h1>Name Change Requests</h1>
<table class="striped" width="100%">
<thead>
<tr>
<th>User</th>
<th>Desired</th>
<th></th>
</tr>
</thead>
<tbody>
<% @change_requests.each do |change_request| %>
<tr>
<td><%= link_to change_request.original_name, user_path(change_request.user_id) %></td>
<td><%= change_request.desired_name %></td>
<td><%= link_to "view", user_name_change_request_path(change_request) %></td>
</tr>
<% end %>
</tbody>
</table>
<%= numbered_paginator(@change_requests) %>
</div>

View File

@@ -16,6 +16,6 @@
</div>
<div class="input">
<%= submit_tag %>
<%= submit_tag "Submit" %>
</div>
<% end %>

View File

@@ -33,7 +33,11 @@
</ul>
</section>
<% if CurrentUser.user.is_admin? %>
<% if @change_request.rejected? %>
<section>
<h2>Request was rejected</h2>
</section>
<% elsif CurrentUser.user.is_admin? %>
<section>
<h2>Options</h2>
<%= form_tag(approve_user_name_change_request_path(@change_request)) do %>
@@ -52,5 +56,11 @@
<% end %>
</section>
<% end %>
<section>
<%= form_tag(user_name_change_request_path(@change_request), :method => :delete) do %>
<%= submit_tag "Delete" %>
<% end %>
</section>
</div>

View File

@@ -97,6 +97,13 @@
</td>
</tr>
<% if CurrentUser.user.is_janitor? && presenter.previous_names.present? %>
<tr>
<th>Previous Names</th>
<td><%= presenter.previous_names %></td>
</tr>
<% end %>
<% if CurrentUser.user.id == user.id %>
<tr>
<th>API Key</th>

View File

@@ -5,9 +5,15 @@
<%= simple_form_for @user do |f| %>
<fieldset>
<legend>Basic Settings</legend>
<div class="input">
<label>Name</label>
<p>Name changes are not supported. <%= link_to "Read why", name_change_path %>.</p>
<% if CurrentUser.user.is_privileged? %>
<p><%= link_to "Request a name change", new_user_name_change_request_path %></p>
<% else %>
<p>You must <%= link_to "upgrade your account", upgrade_information_users_path %> to request a name change</p>
<% end %>
</div>
<%= f.input :email, :required => Danbooru.config.enable_email_verification?, :hint => "Used for messages and for password resets", :as => :email %>
<%= f.input :time_zone, :include_blank => false %>

View File

@@ -39,8 +39,8 @@
</tr>
<tr>
<td>Favorite Limit</td>
<td>10,000</td>
<td>20,000</td>
<td>40,000</td>
<td>Unlimited</td>
</tr>
<tr>
@@ -80,7 +80,13 @@
<td>9 sec</td>
</tr>
<tr>
<td>50 or 100 Posts Per Page</td>
<td>Variable Posts Per Page</td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>
</tr>
<tr>
<td>Name Changes</td>
<td>No</td>
<td>Yes</td>
<td>Yes</td>

View File

@@ -0,0 +1,62 @@
require 'test_helper'
class UserNameChangeRequestsControllerTest < ActionController::TestCase
context "The user name change requests controller" do
setup do
@user = FactoryGirl.create(:privileged_user)
@admin = FactoryGirl.create(:admin_user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
@change_request = UserNameChangeRequest.create!(
:user_id => @user.id,
:original_name => @user.name,
:desired_name => "abc",
:change_reason => "hello"
)
end
context "new action" do
should "render" do
get :new, {}, {:user_id => @user.id}
assert_response :success
end
end
context "show action" do
should "render" do
get :show, {:id => @change_request.id}, {:user_id => @user.id}
assert_response :success
end
end
context "for actions restricted to admins" do
context "index action" do
should "render" do
get :index, {}, {:user_id => @admin.id}
assert_response :success
end
end
context "approve action" do
should "succeed" do
post :approve, {:id => @change_request.id}, {:user_id => @admin.id}
assert_redirected_to(user_name_change_request_path(@change_request))
end
end
context "reject action" do
should "succeed" do
post :reject, {:id => @change_request.id}, {:user_id => @admin.id}
assert_redirected_to(user_name_change_request_path(@change_request))
end
end
context "destroy action" do
should "destroy" do
post :destroy, {:id => @change_request.id}, {:user_id => @admin.id}
assert_redirected_to(user_name_change_requests_path)
end
end
end
end
end

View File

@@ -20,7 +20,7 @@ class UserNameChangeRequestTest < ActiveSupport::TestCase
end
should "create a dmail" do
assert_difference("Dmail.count", 2) do
assert_difference("Dmail.count", 4) do
@change_request.approve!
end
end
@@ -35,6 +35,18 @@ class UserNameChangeRequestTest < ActiveSupport::TestCase
@change_request.approve!
assert_equal("abc", Cache.get("uin:#{@requester.id}"))
end
should "create feedback" do
assert_difference("UserFeedback.count", 1) do
@change_request.approve!
end
end
should "create mod action" do
assert_difference("ModAction.count", 1) do
@change_request.approve!
end
end
end
context "rejecting a request" do