bsm_oa 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +0 -1
  3. data/Gemfile +6 -0
  4. data/Gemfile.lock +138 -142
  5. data/app/controllers/bsm_oa/accounts_controller.rb +19 -4
  6. data/app/controllers/bsm_oa/admin_controller.rb +5 -0
  7. data/app/controllers/bsm_oa/applications_controller.rb +36 -15
  8. data/app/controllers/bsm_oa/authorizations_controller.rb +7 -6
  9. data/app/controllers/bsm_oa/base_controller.rb +5 -0
  10. data/app/controllers/bsm_oa/roles_controller.rb +3 -4
  11. data/app/views/bsm_oa/applications/_application.html.erb +2 -2
  12. data/app/views/bsm_oa/applications/_inputs.html.erb +6 -11
  13. data/app/views/bsm_oa/applications/edit.html.erb +2 -2
  14. data/app/views/bsm_oa/applications/index.html.erb +1 -1
  15. data/app/views/bsm_oa/applications/new.html.erb +2 -2
  16. data/app/views/bsm_oa/applications/show.html.erb +40 -0
  17. data/app/views/bsm_oa/authorizations/_inputs.html.erb +2 -2
  18. data/app/views/bsm_oa/authorizations/edit.html.erb +1 -1
  19. data/app/views/bsm_oa/authorizations/new.html.erb +2 -3
  20. data/app/views/bsm_oa/roles/_inputs.html.erb +3 -4
  21. data/app/views/bsm_oa/roles/edit.html.erb +1 -1
  22. data/app/views/bsm_oa/roles/new.html.erb +1 -1
  23. data/app/views/bsm_oa/roles/show.html.erb +1 -1
  24. data/bsm_oa.gemspec +6 -11
  25. data/db/migrate/20150507113313_bsm_oa_create_doorkeeper_tables.rb +1 -1
  26. data/db/migrate/20150513155732_bsm_oa_create_tables.rb +1 -1
  27. data/lib/bsm_oa.rb +0 -3
  28. data/lib/bsm_oa/application.rb +31 -0
  29. data/lib/bsm_oa/authorization.rb +8 -15
  30. data/lib/bsm_oa/config.rb +5 -0
  31. data/lib/bsm_oa/engine.rb +2 -4
  32. data/lib/bsm_oa/role.rb +1 -1
  33. data/lib/bsm_oa/routes.rb +29 -3
  34. data/lib/bsm_oa/version.rb +1 -1
  35. data/spec/controllers/bsm_oa/accounts_controller_spec.rb +15 -12
  36. data/spec/controllers/bsm_oa/applications_controller_spec.rb +22 -28
  37. data/spec/controllers/bsm_oa/authorizations_controller_spec.rb +17 -22
  38. data/spec/controllers/bsm_oa/roles_controller_spec.rb +14 -16
  39. data/spec/factories.rb +1 -1
  40. data/spec/lib/bsm_oa/{application_mixin_spec.rb → application_spec.rb} +13 -9
  41. data/spec/lib/bsm_oa/authorization_spec.rb +5 -9
  42. data/spec/lib/bsm_oa/role_spec.rb +1 -1
  43. data/spec/spec_helper.rb +18 -8
  44. metadata +22 -104
  45. data/app/views/bsm_oa/accounts/show.json.jbuilder +0 -7
  46. data/app/views/bsm_oa/applications/_application.json.jbuilder +0 -1
  47. data/app/views/bsm_oa/applications/create.json.jbuilder +0 -1
  48. data/app/views/bsm_oa/applications/index.json.jbuilder +0 -1
  49. data/app/views/bsm_oa/applications/show.json.jbuilder +0 -1
  50. data/app/views/bsm_oa/applications/update.json.jbuilder +0 -1
  51. data/app/views/bsm_oa/authorizations/_authorization.json.jbuilder +0 -1
  52. data/app/views/bsm_oa/authorizations/index.json.jbuilder +0 -1
  53. data/app/views/bsm_oa/authorizations/toggle.json.jbuilder +0 -1
  54. data/lib/bsm_oa/application_mixin.rb +0 -37
@@ -1,9 +1,10 @@
1
1
  module BsmOa
2
- class AuthorizationsController < Doorkeeper::ApplicationController
2
+ class AuthorizationsController < AdminController
3
3
  respond_to :html
4
4
  respond_to :json, except: [:new, :edit]
5
5
  respond_to :js, only: [:toggle]
6
- before_filter :redirect_to_index_on_html, only: [:show]
6
+
7
+ before_action :redirect_to_index_on_html, only: [:show]
7
8
 
8
9
  def index
9
10
  @authorizations = apply_scopes(resource_scope)
@@ -33,19 +34,19 @@
33
34
  def update
34
35
  @authorization = resource_scope.find params[:id]
35
36
  @authorization.update(permitted_params)
36
- respond_with @authorization, location: bsm_oa_authorization_path(@authorization)
37
+ respond_with @authorization
37
38
  end
38
39
 
39
40
  def toggle
40
41
  @authorization = resource_scope.find params[:id]
41
- @authorization.toggle(params[:permission])
42
+ @authorization.toggle_permission!(params[:permission])
42
43
  respond_with @authorization
43
44
  end
44
45
 
45
46
  def destroy
46
47
  @authorization = resource_scope.find params[:id]
47
48
  @authorization.destroy
48
- respond_with @authorization, location: bsm_oa_authorization_path(@authorization)
49
+ respond_with @authorization, location: @authorization.role
49
50
  end
50
51
 
51
52
  protected
@@ -65,7 +66,7 @@
65
66
  end
66
67
 
67
68
  def permitted_params
68
- params.require(:authorization).permit :application_id, :permissions_string, :uid, :secret
69
+ params.require(:bsm_oa_authorization).permit :application_id, :uid, :secret
69
70
  end
70
71
 
71
72
  def redirect_to_index_on_html
@@ -0,0 +1,5 @@
1
+ module BsmOa
2
+ class BaseController < BsmOa.config.parent_controller.constantize
3
+ include Doorkeeper::Helpers::Controller
4
+ end
5
+ end
@@ -1,9 +1,8 @@
1
1
  module BsmOa
2
- class RolesController < Doorkeeper::ApplicationController
2
+ class RolesController < AdminController
3
3
  respond_to :html
4
4
  respond_to :json, except: [:new, :edit]
5
-
6
- has_scope :ordered, default: true, only: [:index]
5
+ has_scope :ordered, default: true, only: [:index]
7
6
 
8
7
  def index
9
8
  @roles = apply_scopes(resource_scope)
@@ -51,7 +50,7 @@ module BsmOa
51
50
  end
52
51
 
53
52
  def permitted_params
54
- params.require(:role).permit :name, :description
53
+ params.require(:bsm_oa_role).permit :name, :description
55
54
  end
56
55
 
57
56
  end
@@ -9,7 +9,7 @@
9
9
  <span class="label label-info"><%= "#{pm}" %></span>
10
10
  <% end %>
11
11
  <td>
12
- <%= link_to t('doorkeeper.applications.buttons.edit'), edit_oauth_application_path(application), class: 'btn btn-default btn-xs' %>
13
- <%= link_to 'Delete', oauth_application_path(application), data: { confirm: 'Are you sure?' }, method: :delete, class: 'btn btn-default btn-xs'%>
12
+ <%= link_to t('doorkeeper.applications.buttons.edit'), edit_doorkeeper_application_path(application), class: 'btn btn-default btn-xs' %>
13
+ <%= link_to 'Delete', application, data: { confirm: 'Are you sure?' }, method: :delete, class: 'btn btn-default btn-xs'%>
14
14
  </td>
15
15
  </tr>
@@ -1,11 +1,6 @@
1
- <%= f.input :name, placeholder: "Enter name"%>
2
-
3
- <%= f.input :uid, placeholder: "Enter application UID"%>
4
-
5
- <%= f.input :secret, placeholder: "Enter application secret"%>
6
-
7
- <%= f.input :redirect_uri, placeholder: "Enter redirect url"%>
8
-
9
- <%= f.input :permissions_string, placeholder: "Enter comma separated permissions"%>
10
-
11
- <%= f.button :submit %>
1
+ <%= f.text_field :name, placeholder: "Enter name"%>
2
+ <%= f.text_field :uid, placeholder: "Enter application UID"%>
3
+ <%= f.password_field :secret, placeholder: "Enter application secret"%>
4
+ <%= f.url_field :redirect_uri, placeholder: "Enter redirect url"%>
5
+ <%= f.text_field :permissions, placeholder: "Enter comma separated permissions"%>
6
+ <%= f.submit %>
@@ -1,10 +1,10 @@
1
1
  <div class="page-header">
2
2
  <div class="pull-right">
3
- <%= link_to "&larr; Back".html_safe, oauth_applications_path, class: 'btn btn-lg btn-default'%>
3
+ <%= link_to "&larr; Back".html_safe, bsm_oa_applications_path, class: 'btn btn-lg btn-default'%>
4
4
  </div>
5
5
  <h1>Edit Application</h1>
6
6
  </div>
7
7
 
8
- <%= simple_form_for @application, url: oauth_application_url(@application) do |f| %>
8
+ <%= form_for @application do |f| %>
9
9
  <%= render 'inputs', f: f %>
10
10
  <% end %>
@@ -1,6 +1,6 @@
1
1
  <div class="page-header">
2
2
  <div class="pull-right">
3
- <%= link_to 'New Application', new_oauth_application_path, class: 'btn btn-lg btn-primary' %>
3
+ <%= link_to 'New Application', new_bsm_oa_application_path, class: 'btn btn-lg btn-primary' %>
4
4
  </div>
5
5
  <h1>Applications</h1>
6
6
  </div>
@@ -1,10 +1,10 @@
1
1
  <div class="page-header">
2
2
  <div class="pull-right">
3
- <%= link_to "&larr; Back".html_safe, oauth_applications_path, class: 'btn btn-lg btn-default'%>
3
+ <%= link_to "&larr; Back".html_safe, bsm_oa_applications_path, class: 'btn btn-lg btn-default'%>
4
4
  </div>
5
5
  <h1>New Application</h1>
6
6
  </div>
7
7
 
8
- <%= simple_form_for @application, url: oauth_applications_url do |f| %>
8
+ <%= form_for @application do |f| %>
9
9
  <%= render 'inputs', f: f %>
10
10
  <% end %>
@@ -0,0 +1,40 @@
1
+ <div class="page-header">
2
+ <div class="pull-right">
3
+ <%= link_to "&larr; Back".html_safe, bsm_oa_applications_path, class: 'btn btn-lg btn-default'%>
4
+ </div>
5
+ <h1><%= @application.name %></h1>
6
+ </div>
7
+
8
+ <h3>Details</h3>
9
+ <div class="table-responsive">
10
+ <table class="table table-hover">
11
+ <tr>
12
+ <th>Name:</th>
13
+ <td><%= @application.name %></td>
14
+ </tr>
15
+ <tr>
16
+ <th>Callback URL:</th>
17
+ <td>
18
+ <%= @application.redirect_uri %>
19
+ </td>
20
+ </tr>
21
+ <tr>
22
+ <th>Permissions:</th>
23
+ <td>
24
+ <%= @application.permissions.to_sentence %>
25
+ </td>
26
+ </tr>
27
+ <tr>
28
+ <th>UID:</th>
29
+ <td>
30
+ <%= @application.uid %>
31
+ </td>
32
+ </tr>
33
+ <tr>
34
+ <th>Secret:</th>
35
+ <td>
36
+ <%= @application.secret %>
37
+ </td>
38
+ </tr>
39
+ </table>
40
+ </div>
@@ -1,2 +1,2 @@
1
- <%= f.association :application, as: :select, collection: Doorkeeper::Application.ordered, prompt: true %>
2
- <%= f.button :submit %>
1
+ <%= f.collection_select :application_id, BsmOa::Application.ordered, :id, :name, prompt: true %>
2
+ <%= f.submit %>
@@ -5,6 +5,6 @@
5
5
  <h1>Edit Authorization</h1>
6
6
  </div>
7
7
 
8
- <%= simple_form_for @authorization, url: bsm_oa_authorization_url do |f| %>
8
+ <%= form_for @authorization, url: bsm_oa_authorization_url do |f| %>
9
9
  <%= render 'inputs', f: f %>
10
10
  <% end %>
@@ -5,7 +5,6 @@
5
5
  <h1>New Authorization</h1>
6
6
  </div>
7
7
 
8
- <%= simple_form_for [@parent, @authorization], url: bsm_oa_role_bsm_oa_authorizations_url do |f| %>
9
- <%= f.association :application, as: :select, collection: Doorkeeper::Application.ordered, prompt: true %>
10
- <%= f.button :submit %>
8
+ <%= form_for [@parent, @authorization], url: bsm_oa_role_bsm_oa_authorizations_url do |f| %>
9
+ <%= render 'inputs', f: f %>
11
10
  <% end %>
@@ -1,5 +1,4 @@
1
- <%= f.input :name, placeholder: "Enter name" %>
2
- <%= f.input :description, placeholder: "Enter description" %>
3
-
4
- <%= f.button :submit %>
1
+ <%= f.text_field :name, placeholder: "Enter name" %>
2
+ <%= f.text_field :description, placeholder: "Enter description" %>
3
+ <%= f.submit %>
5
4
 
@@ -5,6 +5,6 @@
5
5
  <h1>Edit Role</h1>
6
6
  </div>
7
7
 
8
- <%= simple_form_for @role, url: bsm_oa_role_url(@role) do |f| %>
8
+ <%= form_for @role, url: bsm_oa_role_url(@role) do |f| %>
9
9
  <%= render 'inputs', f: f %>
10
10
  <% end %>
@@ -5,6 +5,6 @@
5
5
  <h1>New Role</h1>
6
6
  </div>
7
7
 
8
- <%= simple_form_for @role, url: bsm_oa_roles_path do |f| %>
8
+ <%= form_for @role, url: bsm_oa_roles_path do |f| %>
9
9
  <%= render 'inputs', f: f %>
10
10
  <% end %>
@@ -24,7 +24,7 @@
24
24
 
25
25
  <h3>
26
26
  Authorizations
27
- <%= link_to 'New', new_bsm_oa_role_bsm_oa_authorization_path(@role), class: 'btn btn-sm btn-primary' %>
27
+ <%= link_to 'New', [:new, @role, :bsm_oa_authorization], class: 'btn btn-sm btn-primary' %>
28
28
  </h3>
29
29
  <div class="table-responsive">
30
30
  <table class="table table-hover">
@@ -16,22 +16,17 @@ Gem::Specification.new do |s|
16
16
  s.test_files = `git ls-files -- spec/*`.split("\n")
17
17
  s.require_paths = ['lib']
18
18
 
19
- s.add_dependency 'railties', '>= 4.1', '< 5.0'
20
- s.add_dependency 'doorkeeper', '~> 3.0.0.rc'
21
- s.add_dependency 'responders', '~> 2.0'
22
- s.add_dependency 'jbuilder', '~> 2.2'
23
- s.add_dependency 'bsm-models'
19
+ s.add_dependency 'railties', '~> 5.0'
20
+ s.add_dependency 'doorkeeper', '~> 4.2.0'
21
+ s.add_dependency 'responders', '~> 2.1'
24
22
  s.add_dependency 'has_scope', '~> 0.6'
25
- s.add_dependency 'simple_form', '~> 3.1'
26
- s.add_dependency 'jquery-rails'
27
23
 
28
- s.add_development_dependency 'rails', '>= 4.1'
29
- s.add_development_dependency 'combustion', '~> 0.5.3'
24
+ s.add_development_dependency 'rails', '>= 5.0'
25
+ s.add_development_dependency 'combustion', '~> 0.7.0'
30
26
  s.add_development_dependency 'rspec-rails'
31
27
  s.add_development_dependency 'factory_girl'
32
- s.add_development_dependency 'json_spec'
33
28
  s.add_development_dependency 'faker'
34
- s.add_development_dependency 'shoulda-matchers'
35
29
  s.add_development_dependency 'database_cleaner'
36
30
  s.add_development_dependency 'sqlite3'
31
+ s.add_development_dependency 'rails-controller-testing'
37
32
  end
@@ -1,4 +1,4 @@
1
- class BsmOaCreateDoorkeeperTables < ActiveRecord::Migration
1
+ class BsmOaCreateDoorkeeperTables < ActiveRecord::Migration[5.0]
2
2
  def change
3
3
  create_table :oauth_applications do |t|
4
4
  t.string :name, null: false
@@ -1,4 +1,4 @@
1
- class BsmOaCreateTables < ActiveRecord::Migration
1
+ class BsmOaCreateTables < ActiveRecord::Migration[5.0]
2
2
  def change
3
3
  create_table :bsm_oa_authorizations do |t|
4
4
  t.integer :role_id, null: false
@@ -1,9 +1,6 @@
1
1
  require 'doorkeeper'
2
- require 'bsm-models'
3
2
  require 'responders'
4
- require 'jbuilder'
5
3
  require 'has_scope'
6
- require 'simple_form'
7
4
 
8
5
  require 'bsm_oa/version'
9
6
  require 'bsm_oa/engine'
@@ -0,0 +1,31 @@
1
+ module BsmOa
2
+ class Application < Doorkeeper::Application
3
+
4
+ has_many :authorizations, class_name: 'BsmOa::Authorization', inverse_of: :application, dependent: :destroy
5
+ has_many :roles, inverse_of: :applications, class_name: 'BsmOa::Role', through: :authorizations, foreign_key: :role_id
6
+
7
+ serialize :permissions, JSON
8
+ validate :must_have_simple_word_permissions
9
+
10
+ before_validation :normalize_permissions!
11
+
12
+ scope :ordered, -> { order(:name) }
13
+
14
+ # @param [Array|String] permissions
15
+ def permissions=(vals)
16
+ super Array.wrap(vals).map {|s| s.to_s.split(/[\s,]+/) }.flatten
17
+ end
18
+
19
+ protected
20
+
21
+ def must_have_simple_word_permissions
22
+ errors.add :permissions, :invalid if permissions.any? {|pm| pm =~ /[^a-z0-9]/ }
23
+ end
24
+
25
+ def normalize_permissions!
26
+ self.permissions = Array.wrap(permissions).reject(&:blank?).map(&:strip).map(&:downcase).uniq
27
+ end
28
+
29
+ end
30
+ end
31
+
@@ -4,10 +4,10 @@ module BsmOa
4
4
 
5
5
  # ---> ASSOCIATIONS
6
6
  belongs_to :role, inverse_of: :authorizations
7
- belongs_to :application, inverse_of: :authorizations, class_name: Doorkeeper::Application, foreign_key: :application_id
7
+ belongs_to :application, inverse_of: :authorizations, class_name: 'BsmOa::Application', foreign_key: :application_id
8
8
 
9
9
  # ---> ATTRIBUTES
10
- serialize :permissions, Bsm::Model::Coders::JsonColumn.new(Array)
10
+ serialize :permissions, JSON
11
11
  attr_readonly :application_id, :role_id, :application
12
12
 
13
13
  # ---> VALIDATIONS
@@ -21,26 +21,19 @@ module BsmOa
21
21
  scope :ordered, -> { order(id: :desc) }
22
22
 
23
23
  # @param [String] name permission name
24
- def toggle(name)
25
- if permissions.include?(name)
26
- self.permissions = permissions - [name]
27
- else
28
- self.permissions = permissions + [name]
29
- end
30
- save
31
- end
32
-
33
- def permissions_string=(str)
34
- self.permissions = str.split("\s")
24
+ def toggle_permission!(name)
25
+ update permissions: (permissions.include?(name) ? permissions - [name] : permissions + [name])
35
26
  end
36
27
 
37
- def permissions_string
38
- permissions.sort.join(' ')
28
+ # @param [Array|String] permissions
29
+ def permissions=(vals)
30
+ super Array.wrap(vals).map {|s| s.to_s.split(/[\s,]+/) }.flatten
39
31
  end
40
32
 
41
33
  protected
42
34
 
43
35
  def normalize_permissions!
36
+ self.permissions ||= []
44
37
  self.permissions = permissions.reject(&:blank?).map(&:strip).map(&:downcase).uniq
45
38
  self.permissions &= application.permissions if application
46
39
  end
@@ -15,5 +15,10 @@ module BsmOa
15
15
  @user_attrs ||= [:id, :email]
16
16
  end
17
17
 
18
+ def parent_controller(name = nil)
19
+ @parent_controller = name if name
20
+ @parent_controller ||= "ApplicationController"
21
+ end
22
+
18
23
  end
19
24
  end
@@ -15,11 +15,9 @@ module BsmOa
15
15
 
16
16
  initializer "bsm_oa.models" do
17
17
  ActiveSupport.on_load(:active_record) do
18
- require 'bsm_oa/application_mixin'
19
- require 'bsm_oa/authorization'
18
+ require 'bsm_oa/application'
20
19
  require 'bsm_oa/role'
21
-
22
- Doorkeeper::Application.send :include, ApplicationMixin
20
+ require 'bsm_oa/authorization'
23
21
  end
24
22
  end
25
23
 
@@ -4,7 +4,7 @@ module BsmOa
4
4
 
5
5
  # ---> ASSOCIATIONS
6
6
  has_many :authorizations, inverse_of: :role, dependent: :destroy
7
- has_many :applications, inverse_of: :roles, class_name: Doorkeeper::Application, through: :authorizations, foreign_key: :application_id
7
+ has_many :applications, inverse_of: :roles, class_name: 'BsmOa::Application', through: :authorizations, foreign_key: :application_id
8
8
 
9
9
  # ---> VALIDATIONS
10
10
  validates :name,
@@ -8,14 +8,40 @@ module BsmOa
8
8
  module Helper
9
9
 
10
10
  def mount_bsm_oa
11
- get 'me(.:format)', to: 'bsm_oa/accounts#show', as: :bsm_oa_me
12
- resources :roles, controller: 'bsm_oa/roles', as: :bsm_oa_roles do
11
+ mount_bsm_oa_me
12
+ mount_bsm_oa_admin
13
+ mount_bsm_oa_callbacks
14
+ end
15
+
16
+ def mount_bsm_oa_admin
17
+ mount_bsm_oa_applications
18
+ mount_bsm_oa_roles
19
+ mount_bsm_oa_authorizations
20
+ end
21
+
22
+ def mount_bsm_oa_me
23
+ get 'me(.:format)', to: BsmOa::AccountsController.action(:show), as: :bsm_oa_me
24
+ end
25
+
26
+ def mount_bsm_oa_applications
27
+ resources :applications, controller: 'bsm_oa/applications', as: :bsm_oa_applications
28
+ end
29
+
30
+ def mount_bsm_oa_roles
31
+ resources :roles, controller: 'bsm_oa/roles', as: :bsm_oa_roles
32
+ end
33
+
34
+ def mount_bsm_oa_authorizations
35
+ resources :roles, only: [], as: :bsm_oa_roles do
13
36
  resources :authorizations, controller: 'bsm_oa/authorizations', as: :bsm_oa_authorizations, shallow: true do
14
37
  put :toggle, on: :member, path: "toggle/:permission"
15
38
  end
16
39
  end
40
+ end
41
+
42
+ def mount_bsm_oa_callbacks
17
43
  use_doorkeeper do
18
- controllers applications: 'bsm_oa/applications'
44
+ skip_controllers :applications, :authorized_applications
19
45
  end
20
46
  end
21
47