New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add REST API for creating an account #9572
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9aa1247
Add REST API for creating an account
Gargron 00b8a4b
Redirect users back to app from confirmation if they were created wit…
Gargron 541479d
Add tests
Gargron 232e3dc
Return 403 on the method if registrations are not open
Gargron 2bf7bb5
Require agreement param to be true in the API when creating an account
Gargron File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
class Api::V1::AccountsController < Api::BaseController | ||
before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:follow, :unfollow, :block, :unblock, :mute, :unmute] | ||
before_action -> { authorize_if_got_token! :read, :'read:accounts' }, except: [:create, :follow, :unfollow, :block, :unblock, :mute, :unmute] | ||
before_action -> { doorkeeper_authorize! :follow, :'write:follows' }, only: [:follow, :unfollow] | ||
before_action -> { doorkeeper_authorize! :follow, :'write:mutes' }, only: [:mute, :unmute] | ||
before_action -> { doorkeeper_authorize! :follow, :'write:blocks' }, only: [:block, :unblock] | ||
before_action -> { doorkeeper_authorize! :write, :'write:accounts' }, only: [:create] | ||
|
||
before_action :require_user!, except: [:show] | ||
before_action :set_account | ||
before_action :require_user!, except: [:show, :create] | ||
before_action :set_account, except: [:create] | ||
before_action :check_account_suspension, only: [:show] | ||
before_action :check_enabled_registrations, only: [:create] | ||
|
||
respond_to :json | ||
|
||
def show | ||
render json: @account, serializer: REST::AccountSerializer | ||
end | ||
|
||
def create | ||
token = AppSignUpService.new.call(doorkeeper_token.application, account_params) | ||
response = Doorkeeper::OAuth::TokenResponse.new(token) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mirrors the usage of this class by Doorkeeper controllers. |
||
|
||
headers.merge!(response.headers) | ||
|
||
self.response_body = Oj.dump(response.body) | ||
self.status = response.status | ||
end | ||
|
||
def follow | ||
FollowService.new.call(current_user.account, @account, reblogs: truthy_param?(:reblogs)) | ||
|
||
|
@@ -62,4 +74,12 @@ def relationships(**options) | |
def check_account_suspension | ||
gone if @account.suspended? | ||
end | ||
|
||
def account_params | ||
params.permit(:username, :email, :password, :agreement) | ||
end | ||
|
||
def check_enabled_registrations | ||
forbidden if single_user_mode? || !Setting.open_registrations | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
class AppSignUpService < BaseService | ||
def call(app, params) | ||
return unless allowed_registrations? | ||
|
||
user_params = params.slice(:email, :password, :agreement) | ||
account_params = params.slice(:username) | ||
user = User.create!(user_params.merge(created_by_application: app, password_confirmation: user_params[:password], account_attributes: account_params)) | ||
|
||
Doorkeeper::AccessToken.create!(application: app, | ||
resource_owner_id: user.id, | ||
scopes: app.scopes, | ||
expires_in: Doorkeeper.configuration.access_token_expires_in, | ||
use_refresh_token: Doorkeeper.configuration.refresh_token_enabled?) | ||
end | ||
|
||
private | ||
|
||
def allowed_registrations? | ||
Setting.open_registrations && !Rails.configuration.x.single_user_mode | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
db/migrate/20181219235220_add_created_by_application_id_to_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe AppSignUpService, type: :service do | ||
let(:app) { Fabricate(:application, scopes: 'read write') } | ||
let(:good_params) { { username: 'alice', password: '12345678', email: 'good@email.com', agreement: true } } | ||
|
||
subject { described_class.new } | ||
|
||
describe '#call' do | ||
it 'returns nil when registrations are closed' do | ||
Setting.open_registrations = false | ||
expect(subject.call(app, good_params)).to be_nil | ||
end | ||
|
||
it 'raises an error when params are missing' do | ||
expect { subject.call(app, {}) }.to raise_error ActiveRecord::RecordInvalid | ||
end | ||
|
||
it 'creates an unconfirmed user with access token' do | ||
access_token = subject.call(app, good_params) | ||
expect(access_token).to_not be_nil | ||
user = User.find_by(id: access_token.resource_owner_id) | ||
expect(user).to_not be_nil | ||
expect(user.confirmed?).to be false | ||
end | ||
|
||
it 'creates access token with the app\'s scopes' do | ||
access_token = subject.call(app, good_params) | ||
expect(access_token).to_not be_nil | ||
expect(access_token.scopes.to_s).to eq 'read write' | ||
end | ||
|
||
it 'creates an account' do | ||
access_token = subject.call(app, good_params) | ||
expect(access_token).to_not be_nil | ||
user = User.find_by(id: access_token.resource_owner_id) | ||
expect(user).to_not be_nil | ||
expect(user.account).to_not be_nil | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition was unneccessary before because unconfirmed users had no way to obtain an access token.