Skip to content

Commit

Permalink
applications.scopes upgrade notice and rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
tute committed Dec 9, 2014
1 parent 97ddf50 commit 8dfc57b
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 44 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
`before_action :doorkeeper_authorize!`.
- [#469] Allow client applications to restrict the set of allowable scopes.
Fixes #317. `oauth_applications` relation needs a new `scopes` string column,
non nullable, which defaults to an empty string:
non nullable, which defaults to an empty string. Run `rails generate
doorkeeper:application_scopes` to add the column. If you’d rather do it by
hand, your ActiveRecord migration should contain:

```ruby
add_column :oauth_applications, :scopes, :string, null: false, default: ‘’
Expand Down
12 changes: 12 additions & 0 deletions lib/doorkeeper/models/application_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def by_uid(uid)
end
end

alias_method :original_scopes, :scopes
def scopes
if Application.new.attributes.include?("scopes")
original_scopes
else
fail NameError, "Missing column: `applications.scopes`.", <<-MSG.squish
If you are using ActiveRecord run `rails generate doorkeeper:application_scopes
&& rake db:migrate` to add it.
MSG
end
end

private

def generate_uid
Expand Down
5 changes: 4 additions & 1 deletion lib/generators/doorkeeper/application_owner_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ class Doorkeeper::ApplicationOwnerGenerator < Rails::Generators::Base
desc 'Provide support for client application ownership.'

def application_owner
migration_template 'add_owner_to_application_migration.rb', 'db/migrate/add_owner_to_application.rb'
migration_template(
'add_owner_to_application_migration.rb',
'db/migrate/add_owner_to_application.rb'
)
end

def self.next_migration_number(dirname)
Expand Down
34 changes: 34 additions & 0 deletions lib/generators/doorkeeper/application_scopes_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require 'rails/generators/active_record'

class Doorkeeper::ApplicationScopesGenerator < Rails::Generators::Base
include Rails::Generators::Migration
source_root File.expand_path('../templates', __FILE__)
desc 'Copies ActiveRecord migrations to handle upgrade to doorkeeper 2'

def self.next_migration_number(path)
ActiveRecord::Generators::Base.next_migration_number(path)
end

def application_scopes
if oauth_applications_exists? && !scopes_column_exists?
migration_template(
'add_scopes_to_oauth_applications.rb',
'db/migrate/add_scopes_to_oauth_applications.rb'
)
end
end

private

def scopes_column_exists?
ActiveRecord::Base.connection.column_exists?(
:oauth_applications,
:scopes
)
end

# Might be running this before install
def oauth_applications_exists?
ActiveRecord::Base.connection.table_exists? :oauth_applications
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AddScopesToApplication < ActiveRecord::Migration
class AddScopesToOauthApplications < ActiveRecord::Migration
def change
add_column :oauth_applications, :scopes, :string, null: false, default: ''
end
Expand Down
Binary file modified spec/dummy/db/development.sqlite3
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddScopesToOauthApplications < ActiveRecord::Migration
def change
add_column :oauth_applications, :scopes, :string, null: false, default: ''
end
end
82 changes: 41 additions & 41 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,58 @@
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to 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: 20130902175349) do
ActiveRecord::Schema.define(version: 20141209001746) do

create_table 'oauth_access_grants', force: true do |t|
t.integer 'resource_owner_id', null: false
t.integer 'application_id', null: false
t.string 'token', null: false
t.integer 'expires_in', null: false
t.text 'redirect_uri', null: false
t.datetime 'created_at', null: false
t.datetime 'revoked_at'
t.string 'scopes'
create_table "oauth_access_grants", force: true do |t|
t.integer "resource_owner_id", null: false
t.integer "application_id", null: false
t.string "token", null: false
t.integer "expires_in", null: false
t.string "redirect_uri", limit: 2048, null: false
t.datetime "created_at", null: false
t.datetime "revoked_at"
t.string "scopes"
end

add_index 'oauth_access_grants', ['token'], name: 'index_oauth_access_grants_on_token', unique: true
add_index "oauth_access_grants", ["token"], name: "index_oauth_access_grants_on_token", unique: true

create_table 'oauth_access_tokens', force: true do |t|
t.integer 'resource_owner_id'
t.integer 'application_id'
t.string 'token', null: false
t.string 'refresh_token'
t.integer 'expires_in'
t.datetime 'revoked_at'
t.datetime 'created_at', null: false
t.string 'scopes'
create_table "oauth_access_tokens", force: true do |t|
t.integer "resource_owner_id"
t.integer "application_id"
t.string "token", null: false
t.string "refresh_token"
t.integer "expires_in"
t.datetime "revoked_at"
t.datetime "created_at", null: false
t.string "scopes"
end

add_index 'oauth_access_tokens', ['refresh_token'], name: 'index_oauth_access_tokens_on_refresh_token', unique: true
add_index 'oauth_access_tokens', ['resource_owner_id'], name: 'index_oauth_access_tokens_on_resource_owner_id'
add_index 'oauth_access_tokens', ['token'], name: 'index_oauth_access_tokens_on_token', unique: true
add_index "oauth_access_tokens", ["refresh_token"], name: "index_oauth_access_tokens_on_refresh_token", unique: true
add_index "oauth_access_tokens", ["resource_owner_id"], name: "index_oauth_access_tokens_on_resource_owner_id"
add_index "oauth_access_tokens", ["token"], name: "index_oauth_access_tokens_on_token", unique: true

create_table 'oauth_applications', force: true do |t|
t.string 'name', null: false
t.string 'uid', null: false
t.string 'secret', null: false
t.text 'redirect_uri', null: false
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.integer 'owner_id'
t.string 'owner_type'
t.string 'scopes', null: false, default: ''
create_table "oauth_applications", force: true do |t|
t.string "name", null: false
t.string "uid", null: false
t.string "secret", null: false
t.string "redirect_uri", limit: 2048, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "owner_id"
t.string "owner_type"
t.string "scopes", default: "", null: false
end

add_index 'oauth_applications', %w(owner_id owner_type), name: 'index_oauth_applications_on_owner_id_and_owner_type'
add_index 'oauth_applications', ['uid'], name: 'index_oauth_applications_on_uid', unique: true
add_index "oauth_applications", ["owner_id", "owner_type"], name: "index_oauth_applications_on_owner_id_and_owner_type"
add_index "oauth_applications", ["uid"], name: "index_oauth_applications_on_uid", unique: true

create_table 'users', force: true do |t|
t.string 'name'
t.datetime 'created_at', null: false
t.datetime 'updated_at', null: false
t.string 'password'
create_table "users", force: true do |t|
t.string "name"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password"
end

end
13 changes: 13 additions & 0 deletions spec/models/doorkeeper/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,18 @@ module Doorkeeper
expect(authenticated).to eq(app)
end
end

describe :scopes do
it 'fails on missing column with an upgrade notice' do
app = FactoryGirl.build :application
no_scopes_app = double(attributes: [])
allow(Application).to receive(:new).and_return(no_scopes_app)

expect { app.scopes }.to raise_error(
NameError,
/Missing column: `applications.scopes`/
)
end
end
end
end

0 comments on commit 8dfc57b

Please sign in to comment.