Redirect users back to app from confirmation if they were created with an app

This commit is contained in:
Eugen Rochko 2018-12-20 01:28:26 +01:00
parent 05ef749a0f
commit 322869a685
8 changed files with 58 additions and 6 deletions

View File

@ -6,9 +6,9 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController
before_action :set_body_classes before_action :set_body_classes
before_action :set_user, only: [:finish_signup] before_action :set_user, only: [:finish_signup]
# GET/PATCH /users/:id/finish_signup
def finish_signup def finish_signup
return unless request.patch? && params[:user] return unless request.patch? && params[:user]
if @user.update(user_params) if @user.update(user_params)
@user.skip_reconfirmation! @user.skip_reconfirmation!
bypass_sign_in(@user) bypass_sign_in(@user)
@ -31,4 +31,12 @@ class Auth::ConfirmationsController < Devise::ConfirmationsController
def user_params def user_params
params.require(:user).permit(:email) params.require(:user).permit(:email)
end end
def after_confirmation_path_for(_resource_name, user)
if user.created_by_application && truthy_param?(:redirect_to_app)
user.created_by_application.redirect_uri
else
super
end
end
end end

View File

@ -36,6 +36,7 @@
# invite_id :bigint(8) # invite_id :bigint(8)
# remember_token :string # remember_token :string
# chosen_languages :string is an Array # chosen_languages :string is an Array
# created_by_application_id :bigint(8)
# #
class User < ApplicationRecord class User < ApplicationRecord
@ -66,6 +67,7 @@ class User < ApplicationRecord
belongs_to :account, inverse_of: :user belongs_to :account, inverse_of: :user
belongs_to :invite, counter_cache: :uses, optional: true belongs_to :invite, counter_cache: :uses, optional: true
belongs_to :created_by_application, class_name: 'Doorkeeper::Application', optional: true
accepts_nested_attributes_for :account accepts_nested_attributes_for :account
has_many :applications, class_name: 'Doorkeeper::Application', as: :owner has_many :applications, class_name: 'Doorkeeper::Application', as: :owner

View File

@ -6,7 +6,7 @@ class AppSignUpService < BaseService
user_params = params.slice(:email, :password) user_params = params.slice(:email, :password)
account_params = params.slice(:username) account_params = params.slice(:username)
user = User.create!(user_params.merge(password_confirmation: user_params[:password], account_attributes: account_params)) user = User.create!(user_params.merge(created_by_application: app, password_confirmation: user_params[:password], account_attributes: account_params))
Doorkeeper::AccessToken.create!(application: app, Doorkeeper::AccessToken.create!(application: app,
resource_owner_id: user.id, resource_owner_id: user.id,

View File

@ -55,6 +55,10 @@
%tbody %tbody
%tr %tr
%td.button-primary %td.button-primary
- if @resource.created_by_application
= link_to confirmation_url(@resource, confirmation_token: @token, redirect_to_app: 'true') do
%span= t 'devise.mailer.confirmation_instructions.action_with_app', app: @resource.created_by_application.name
- else
= link_to confirmation_url(@resource, confirmation_token: @token) do = link_to confirmation_url(@resource, confirmation_token: @token) do
%span= t 'devise.mailer.confirmation_instructions.action' %span= t 'devise.mailer.confirmation_instructions.action'

View File

@ -4,7 +4,7 @@
<%= t 'devise.mailer.confirmation_instructions.explanation', host: site_hostname %> <%= t 'devise.mailer.confirmation_instructions.explanation', host: site_hostname %>
=> <%= confirmation_url(@resource, confirmation_token: @token) %> => <%= confirmation_url(@resource, confirmation_token: @token, redirect_to_app: @resource.created_by_application ? 'true' : nil) %>
<%= strip_tags(t('devise.mailer.confirmation_instructions.extra_html', terms_path: about_more_url, policy_path: terms_url)) %> <%= strip_tags(t('devise.mailer.confirmation_instructions.extra_html', terms_path: about_more_url, policy_path: terms_url)) %>

View File

@ -18,6 +18,7 @@ en:
mailer: mailer:
confirmation_instructions: confirmation_instructions:
action: Verify email address action: Verify email address
action_with_app: Confirm and return to %{app}
explanation: You have created an account on %{host} with this email address. You are one click away from activating it. If this wasn't you, please ignore this email. explanation: You have created an account on %{host} with this email address. You are one click away from activating it. If this wasn't you, please ignore this email.
extra_html: Please also check out <a href="%{terms_path}">the rules of the instance</a> and <a href="%{policy_path}">our terms of service</a>. extra_html: Please also check out <a href="%{terms_path}">the rules of the instance</a> and <a href="%{policy_path}">our terms of service</a>.
subject: 'Mastodon: Confirmation instructions for %{instance}' subject: 'Mastodon: Confirmation instructions for %{instance}'

View File

@ -0,0 +1,8 @@
class AddCreatedByApplicationIdToUsers < ActiveRecord::Migration[5.2]
disable_ddl_transaction!
def change
add_reference :users, :created_by_application, foreign_key: { to_table: 'oauth_applications', on_delete: :nullify }, index: false
add_index :users, :created_by_application_id, algorithm: :concurrently
end
end

View File

@ -10,7 +10,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2018_12_07_011115) do ActiveRecord::Schema.define(version: 2018_12_19_235220) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -76,6 +76,23 @@ ActiveRecord::Schema.define(version: 2018_12_07_011115) do
t.index ["tag_id"], name: "index_account_tag_stats_on_tag_id", unique: true t.index ["tag_id"], name: "index_account_tag_stats_on_tag_id", unique: true
end end
create_table "account_warning_presets", force: :cascade do |t|
t.text "text", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "account_warnings", force: :cascade do |t|
t.bigint "account_id"
t.bigint "target_account_id"
t.integer "action", default: 0, null: false
t.text "text", default: "", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id"], name: "index_account_warnings_on_account_id"
t.index ["target_account_id"], name: "index_account_warnings_on_target_account_id"
end
create_table "accounts", force: :cascade do |t| create_table "accounts", force: :cascade do |t|
t.string "username", default: "", null: false t.string "username", default: "", null: false
t.string "domain" t.string "domain"
@ -333,6 +350,15 @@ ActiveRecord::Schema.define(version: 2018_12_07_011115) do
t.index ["status_id"], name: "index_mentions_on_status_id" t.index ["status_id"], name: "index_mentions_on_status_id"
end end
create_table "moves", force: :cascade do |t|
t.bigint "account_id"
t.bigint "target_account_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["account_id"], name: "index_moves_on_account_id"
t.index ["target_account_id"], name: "index_moves_on_target_account_id"
end
create_table "mutes", force: :cascade do |t| create_table "mutes", force: :cascade do |t|
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
@ -620,8 +646,10 @@ ActiveRecord::Schema.define(version: 2018_12_07_011115) do
t.bigint "invite_id" t.bigint "invite_id"
t.string "remember_token" t.string "remember_token"
t.string "chosen_languages", array: true t.string "chosen_languages", array: true
t.bigint "created_by_application_id"
t.index ["account_id"], name: "index_users_on_account_id" t.index ["account_id"], name: "index_users_on_account_id"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["created_by_application_id"], name: "index_users_on_created_by_application_id"
t.index ["email"], name: "index_users_on_email", unique: true t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end end
@ -711,6 +739,7 @@ ActiveRecord::Schema.define(version: 2018_12_07_011115) do
add_foreign_key "subscriptions", "accounts", name: "fk_9847d1cbb5", on_delete: :cascade add_foreign_key "subscriptions", "accounts", name: "fk_9847d1cbb5", on_delete: :cascade
add_foreign_key "users", "accounts", name: "fk_50500f500d", on_delete: :cascade add_foreign_key "users", "accounts", name: "fk_50500f500d", on_delete: :cascade
add_foreign_key "users", "invites", on_delete: :nullify add_foreign_key "users", "invites", on_delete: :nullify
add_foreign_key "users", "oauth_applications", column: "created_by_application_id", on_delete: :nullify
add_foreign_key "web_push_subscriptions", "oauth_access_tokens", column: "access_token_id", on_delete: :cascade add_foreign_key "web_push_subscriptions", "oauth_access_tokens", column: "access_token_id", on_delete: :cascade
add_foreign_key "web_push_subscriptions", "users", on_delete: :cascade add_foreign_key "web_push_subscriptions", "users", on_delete: :cascade
add_foreign_key "web_settings", "users", name: "fk_11910667b2", on_delete: :cascade add_foreign_key "web_settings", "users", name: "fk_11910667b2", on_delete: :cascade