diff --git a/app/controllers/emails_controller.rb b/app/controllers/emails_controller.rb index 5418289a8..d9b35a46e 100644 --- a/app/controllers/emails_controller.rb +++ b/app/controllers/emails_controller.rb @@ -21,7 +21,7 @@ class EmailsController < ApplicationController end if @user.errors.none? - flash[:notice] = "Email updated" + flash[:notice] = "Email updated. Check your email to confirm your new address" UserMailer.email_change_confirmation(@user).deliver_later respond_with(@user, location: settings_url) else @@ -31,10 +31,27 @@ class EmailsController < ApplicationController end def verify - @email_address = authorize EmailAddress.find_by_user_id!(params[:user_id]) - @email_address.update!(is_verified: true) + @user = User.find(params[:user_id]) + @email_address = @user.email_address - flash[:notice] = "Email address verified" - redirect_to @email_address.user + if @email_address.blank? + redirect_to edit_user_email_path(@user) + elsif params[:email_verification_key].present? + authorize @email_address + @email_address.update!(is_verified: true) + flash[:notice] = "Email address verified" + redirect_to @email_address.user + else + authorize @email_address + respond_with(@user) + end + end + + def send_confirmation + @user = authorize User.find(params[:user_id]), policy_class: EmailAddressPolicy + UserMailer.welcome_user(@user).deliver_later + + flash[:notice] = "Confirmation email sent to #{@user.email_address.address}. Check your email to confirm your address" + redirect_to @user end end diff --git a/app/javascript/src/javascripts/common.js b/app/javascript/src/javascripts/common.js index 03563a2f9..04e2af233 100644 --- a/app/javascript/src/javascripts/common.js +++ b/app/javascript/src/javascripts/common.js @@ -15,6 +15,12 @@ $(function() { e.preventDefault(); }); + $("#hide-verify-account-notice").on("click.danbooru", function(e) { + $("#verify-account-notice").hide(); + Cookie.put('hide_verify_account_notice', '1', 3); + e.preventDefault(); + }); + $("#close-notice-link").on("click.danbooru", function(e) { $('#notice').fadeOut("fast"); e.preventDefault(); diff --git a/app/models/user.rb b/app/models/user.rb index 89ab85e6e..be3db085d 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -267,6 +267,10 @@ class User < ApplicationRecord name.match?(/\Auser_[0-9]+~*\z/) end + def is_restricted? + requires_verification? && !is_verified? + end + def is_anonymous? level == Levels::ANONYMOUS end diff --git a/app/policies/email_address_policy.rb b/app/policies/email_address_policy.rb index 7122fa1f7..cd92232d1 100644 --- a/app/policies/email_address_policy.rb +++ b/app/policies/email_address_policy.rb @@ -9,6 +9,15 @@ class EmailAddressPolicy < ApplicationPolicy end def verify? - record.valid_key?(request.params[:email_verification_key]) + if request.params[:email_verification_key].present? + record.valid_key?(request.params[:email_verification_key]) + else + record.user_id == user.id + end + end + + def send_confirmation? + # XXX record is a user, not the email address. + record.id == user.id end end diff --git a/app/views/emails/edit.html.erb b/app/views/emails/edit.html.erb index aac9b0704..e2e0c311b 100644 --- a/app/views/emails/edit.html.erb +++ b/app/views/emails/edit.html.erb @@ -1,14 +1,26 @@ -<% page_title "Change Email" %> -
You must confirm your password in order to change your email address.
+Your current email address is <%= @user.email_address.address %>. You must re-enter your password in order to update your email address.
+ <% else %> + <% page_title "Add Email" %> +Add a new email address below. You must re-enter your password in order to update your email address.
+ <% end %> + + <% if @user.is_restricted? %> +Your account is restricted because you signed up from a VPN or proxy. + You can still use the site, but you won't be able to leave comments, edit + tags, or upload posts until you add a verified email address to your account.
+ <% end %> <%= edit_form_for(@user, url: user_email_path(@user)) do |f| %> + <%= f.input :email, as: :email, label: "New Email", input_html: { value: "" } %> <%= f.input :password %> - <%= f.input :email, as: :email, input_html: { value: "" } %> <%= f.submit "Save" %> <% end %>Your account is restricted because you signed up from a VPN or proxy. + You can still use the site, but you won't be able to leave comments, edit + tags, or upload posts until you verify your account.
+ <% end %> + +Click below to send an email to <%= @email_address.address %> + to verify your account.
+ + <%= edit_form_for(@user, method: :post, url: send_confirmation_user_email_path(@user)) do |f| %> + <%= f.submit "Send confirmation email" %> + <% end %> +<% if @user.email_address.present? %> <%= @user.email_address.address %> - <% if !@user.email_address.is_verified %> - (unverified) - <% end %> - <% else %> - blank - <% end %> - - <%= link_to "Change your email", edit_user_email_path(@user) %> + (<%= link_to "Change email", edit_user_email_path(@user) %> + <% if !@user.email_address.is_verified %> + | <%= link_to "Verify email", verify_user_email_path(@user) %> + <% end %>) + <% else %> + <%= link_to "Add email", edit_user_email_path(@user) %> + <% end %>