g5_authenticatable 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93238b9dd28980b2722ffc334c98c7c29aed9952
4
- data.tar.gz: cd9eaa8d01343dd9463a0a6a2cc082331ec89f49
3
+ metadata.gz: 747e24b3b4e7c12529e9a71fdbd83b7a21579d87
4
+ data.tar.gz: b6714d5b386412ef225a318e88ce06e43b7ce05d
5
5
  SHA512:
6
- metadata.gz: 39a183780b26fb0bb1c730797504f549bd7510d10e76bf6946b52d818de6b5215649eebb48717150cffa551e337e0cea7c85e3891c9ee3a1196b6437aa4c080f
7
- data.tar.gz: 036629738e9fd3c4f1888f9bd53b82a1bf61485bf01305cc97da8ebb32c9a342d792ec2e3b68abc5db9eb91ecf6202f8f03079d5f7dcaf66d981ca1423eed60d
6
+ metadata.gz: 625231160d764bfef77765411f6d9389b5ebd5f3863e15da3b66dfd2b26175820333041ea1e972537cdf5be04cf3ff514a648db13080b018a4738748e058b546
7
+ data.tar.gz: 5790baf85bc87b51c52e81a42dff189c5f2fcabf61a9cbb12c63e9dc7a090b0a1b56bf7d6fb51c8d20606125ad89e552ba8dccb3be931e085dc5fd0b9f753d3d
@@ -12,3 +12,5 @@ before_script:
12
12
  env:
13
13
  global:
14
14
  - DEVISE_SECRET_KEY=foo
15
+ addons:
16
+ postgresql: "9.2"
data/README.md CHANGED
@@ -280,6 +280,11 @@ current_user.has_role?(:editor)
280
280
  G5 currently supports four different roles: `:super_admin`, `:admin`,
281
281
  `:editor`, and `:viewer` (the default role).
282
282
 
283
+ Two convenience methods have been added to the `G5Authenticatable::User`:
284
+
285
+ * `user.clients` will return a list of clients that the user has any access to. Will return all clients if the user has a global role
286
+ * `user.client_roles` will return a list of roles that relate directly to a client
287
+
283
288
  #### Policies and Scopes ####
284
289
 
285
290
  G5 Authenticatable uses [pundit](https://github.com/elabs/pundit) to encapsulate
@@ -47,6 +47,10 @@ module G5Authenticatable
47
47
  end
48
48
  end
49
49
 
50
+ def clients
51
+ G5Updatable::ClientPolicy::Scope.new(user, G5Updatable::Client).resolve
52
+ end
53
+
50
54
  private
51
55
  def self.extended_auth_attributes(auth_data)
52
56
  {
@@ -59,5 +63,6 @@ module G5Authenticatable
59
63
  end
60
64
 
61
65
 
66
+
62
67
  end
63
68
  end
@@ -70,4 +70,8 @@ class G5Authenticatable::BasePolicy
70
70
  def viewer?
71
71
  user.present? && user.has_role?(:viewer)
72
72
  end
73
+
74
+ def has_global_role?
75
+ super_admin? || admin? || editor? || viewer?
76
+ end
73
77
  end
@@ -0,0 +1,23 @@
1
+ module G5Updatable
2
+ class ClientPolicy < G5Authenticatable::BasePolicy
3
+ class Scope < G5Authenticatable::BasePolicy::BaseScope
4
+
5
+ def resolve
6
+ return scope.all if has_global_role?
7
+ scope.where(id: client_roles.map(&:resource_id))
8
+ end
9
+
10
+ def client_roles
11
+ G5Authenticatable::Role
12
+ .joins('INNER JOIN g5_updatable_clients ON g5_updatable_clients.id = g5_authenticatable_roles.resource_id')
13
+ .joins('INNER JOIN g5_authenticatable_users_roles ON g5_authenticatable_roles.id = g5_authenticatable_users_roles.role_id')
14
+ .where('g5_authenticatable_roles.resource_type = ? and g5_authenticatable_users_roles.user_id = ?', G5Updatable::Client.name, user.id)
15
+ end
16
+
17
+ def has_global_role?
18
+ G5Authenticatable::BasePolicy.new(user, G5Updatable::Client).has_global_role?
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -25,4 +25,5 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'g5_authenticatable_api', '~> 0.4.1'
26
26
  spec.add_dependency 'rolify', '~> 4.0'
27
27
  spec.add_dependency 'pundit', '~> 1.0'
28
+ spec.add_dependency 'g5_updatable', '~> 0.6.0'
28
29
  end
@@ -2,6 +2,7 @@ require 'g5_authenticatable/engine'
2
2
 
3
3
  require 'devise_g5_authenticatable'
4
4
  require 'g5_authenticatable_api'
5
+ require 'g5_updatable'
5
6
 
6
7
  module G5Authenticatable
7
8
  # When enabled, access tokens are always validated against the auth
@@ -1,3 +1,3 @@
1
1
  module G5Authenticatable
2
- VERSION = '0.6.0'
2
+ VERSION = '0.7.0'
3
3
  end
@@ -1,4 +1,5 @@
1
1
  Rails.application.routes.draw do
2
+ mount G5Updatable::Engine => '/g5_updatable'
2
3
  resources :posts
3
4
 
4
5
  resource :home, only: [:index, :show]
@@ -0,0 +1,25 @@
1
+ # This migration comes from g5_updatable (originally 20140709222005)
2
+ class CreateG5UpdatableClientsAndLocations < ActiveRecord::Migration
3
+ def change
4
+ create_table :g5_updatable_clients do |t|
5
+ t.string :uid
6
+ t.string :urn
7
+ t.json :properties
8
+
9
+ t.timestamps
10
+ end
11
+ add_index :g5_updatable_clients, :uid
12
+ add_index :g5_updatable_clients, :urn
13
+
14
+ create_table :g5_updatable_locations do |t|
15
+ t.string :uid
16
+ t.string :urn
17
+ t.string :client_uid
18
+ t.json :properties
19
+
20
+ t.timestamps
21
+ end
22
+ add_index :g5_updatable_locations, :uid
23
+ add_index :g5_updatable_locations, :urn
24
+ end
25
+ end
@@ -0,0 +1,19 @@
1
+ # This migration comes from g5_updatable (originally 20141030211945)
2
+ class CreateIntegrationSetting < ActiveRecord::Migration
3
+ def change
4
+ create_table :g5_updatable_integration_settings do |t|
5
+ t.string :uid
6
+ t.string :urn
7
+ t.string :location_uid
8
+ t.string :vendor_action
9
+ t.integer :job_frequency_in_minutes
10
+ t.json :properties
11
+ t.timestamps
12
+ end
13
+
14
+ add_index :g5_updatable_integration_settings, :urn
15
+ add_index :g5_updatable_integration_settings, :uid
16
+ add_index :g5_updatable_integration_settings, :vendor_action
17
+ add_index :g5_updatable_integration_settings, [:location_uid, :vendor_action], name: :g5_u_is_loc_action
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ # This migration comes from g5_updatable (originally 20141122211945)
2
+ class RemoveIntegrationSetting < ActiveRecord::Migration
3
+ def change
4
+ drop_table :g5_updatable_integration_settings
5
+ end
6
+ end
@@ -0,0 +1,10 @@
1
+ # This migration comes from g5_updatable (originally 20141211211945)
2
+ class AddNameToClientsAndLocations < ActiveRecord::Migration
3
+ def change
4
+ add_column :g5_updatable_clients, :name, :string
5
+ add_index :g5_updatable_clients, :name
6
+
7
+ add_column :g5_updatable_locations, :name, :string
8
+ add_index :g5_updatable_locations, :name
9
+ end
10
+ end
@@ -0,0 +1,11 @@
1
+ # This migration comes from g5_updatable (originally 20141211711945)
2
+ class UpdateNames < ActiveRecord::Migration
3
+ def change
4
+ G5Updatable::Client.all.each do |client|
5
+ client.update_attributes(name: client.properties['name'])
6
+ end
7
+ G5Updatable::Location.all.each do |location|
8
+ location.update_attributes(name: location.properties['name'])
9
+ end
10
+ end
11
+ end
@@ -11,7 +11,7 @@
11
11
  #
12
12
  # It's strongly recommended that you check this file into your version control system.
13
13
 
14
- ActiveRecord::Schema.define(version: 20150509061150) do
14
+ ActiveRecord::Schema.define(version: 20150603224036) do
15
15
 
16
16
  # These are extensions that must be enabled in order to support this database
17
17
  enable_extension "plpgsql"
@@ -56,6 +56,33 @@ ActiveRecord::Schema.define(version: 20150509061150) do
56
56
 
57
57
  add_index "g5_authenticatable_users_roles", ["user_id", "role_id"], name: "index_g5_authenticatable_users_roles_on_user_id_and_role_id", using: :btree
58
58
 
59
+ create_table "g5_updatable_clients", force: :cascade do |t|
60
+ t.string "uid"
61
+ t.string "urn"
62
+ t.json "properties"
63
+ t.datetime "created_at"
64
+ t.datetime "updated_at"
65
+ t.string "name"
66
+ end
67
+
68
+ add_index "g5_updatable_clients", ["name"], name: "index_g5_updatable_clients_on_name", using: :btree
69
+ add_index "g5_updatable_clients", ["uid"], name: "index_g5_updatable_clients_on_uid", using: :btree
70
+ add_index "g5_updatable_clients", ["urn"], name: "index_g5_updatable_clients_on_urn", using: :btree
71
+
72
+ create_table "g5_updatable_locations", force: :cascade do |t|
73
+ t.string "uid"
74
+ t.string "urn"
75
+ t.string "client_uid"
76
+ t.json "properties"
77
+ t.datetime "created_at"
78
+ t.datetime "updated_at"
79
+ t.string "name"
80
+ end
81
+
82
+ add_index "g5_updatable_locations", ["name"], name: "index_g5_updatable_locations_on_name", using: :btree
83
+ add_index "g5_updatable_locations", ["uid"], name: "index_g5_updatable_locations_on_uid", using: :btree
84
+ add_index "g5_updatable_locations", ["urn"], name: "index_g5_updatable_locations_on_urn", using: :btree
85
+
59
86
  create_table "posts", force: :cascade do |t|
60
87
  t.integer "author_id"
61
88
  t.string "content"
@@ -477,4 +477,5 @@ describe G5Authenticatable::User do
477
477
  end
478
478
  end
479
479
  end
480
+
480
481
  end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe G5Updatable::ClientPolicy do
4
+ subject(:policy) { described_class }
5
+
6
+ let(:user) { FactoryGirl.create(:g5_authenticatable_user) }
7
+ let(:user2) { FactoryGirl.create(:g5_authenticatable_user) }
8
+
9
+ before do
10
+ user.roles = []
11
+ user.save!
12
+ user2.add_role(:viewer, client_1)
13
+ end
14
+
15
+ let!(:client_1) { FactoryGirl.create(:g5_updatable_client) }
16
+ let!(:client_2) { FactoryGirl.create(:g5_updatable_client) }
17
+ let!(:client_3) { FactoryGirl.create(:g5_updatable_client) }
18
+
19
+ subject { G5Updatable::ClientPolicy::Scope.new(user, G5Updatable::Client).resolve }
20
+
21
+ context 'with global role' do
22
+ before { user.add_role :admin }
23
+ it 'returns all clients' do
24
+ expect(subject.length).to eq(3)
25
+ expect(subject).to include(client_1)
26
+ expect(subject).to include(client_2)
27
+ expect(subject).to include(client_3)
28
+ end
29
+ end
30
+
31
+ context 'with client role' do
32
+ before { user.add_role(:admin, client_1) }
33
+ it 'returns a single client' do
34
+ expect(subject.length).to eq(1)
35
+ expect(subject).to include(client_1)
36
+ end
37
+ end
38
+
39
+ context 'with many client roles' do
40
+ before do
41
+ user.add_role(:admin, client_1)
42
+ user.add_role(:admin, client_2)
43
+ user.add_role(:admin, client_3)
44
+ end
45
+ it 'returns all assigned clients' do
46
+ expect(subject.length).to eq(3)
47
+ expect(subject).to include(client_1)
48
+ expect(subject).to include(client_2)
49
+ expect(subject).to include(client_3)
50
+ end
51
+ end
52
+
53
+ context 'with no role' do
54
+ it 'returns no clients' do
55
+ expect(subject.length).to eq(0)
56
+ end
57
+ end
58
+
59
+ end
@@ -19,14 +19,17 @@ describe 'Default role-based authorization API' do
19
19
  end
20
20
 
21
21
  it 'includes all posts' do
22
- expect(json).to contain_exactly(
23
- hash_including('id' => post.id,
24
- 'author_id' => post.author.id,
25
- 'content' => post.content),
26
- hash_including('id' => other_post.id,
27
- 'author_id' => other_post.author.id,
28
- 'content' => other_post.content)
29
- )
22
+ expected = {
23
+ 'posts'=> [
24
+ hash_including('id' => post.id,
25
+ 'author_id' => post.author.id,
26
+ 'content' => post.content),
27
+ hash_including('id' => other_post.id,
28
+ 'author_id' => other_post.author.id,
29
+ 'content' => other_post.content)
30
+ ]
31
+ }
32
+ expect(json).to include(expected)
30
33
  end
31
34
  end
32
35
 
@@ -12,6 +12,8 @@ require 'rspec/rails'
12
12
  require 'capybara/rspec'
13
13
  require 'webmock/rspec'
14
14
  require 'g5_authenticatable/rspec'
15
+ require 'g5_updatable/rspec'
16
+ require 'g5_updatable/factories'
15
17
 
16
18
  Rails.backtrace_cleaner.remove_silencers!
17
19
 
@@ -45,6 +47,13 @@ RSpec.configure do |config|
45
47
  # --seed 1234
46
48
  config.order = 'random'
47
49
 
50
+ # These two settings work together to allow you to limit a spec run
51
+ # to individual examples or groups you care about by tagging them with
52
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
53
+ # get run.
54
+ config.filter_run :focus
55
+ config.run_all_when_everything_filtered = true
56
+
48
57
  config.infer_spec_type_from_file_location!
49
58
 
50
59
  config.after(:suite) { WebMock.disable! }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: g5_authenticatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - maeve
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-02 00:00:00.000000000 Z
11
+ date: 2015-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: devise_g5_authenticatable
@@ -80,6 +80,20 @@ dependencies:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
82
  version: '1.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: g5_updatable
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.6.0
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.6.0
83
97
  description: |-
84
98
  An engine that provides a basic User model,
85
99
  authentication logic, and remote credential
@@ -111,6 +125,7 @@ files:
111
125
  - app/models/g5_authenticatable/role.rb
112
126
  - app/models/g5_authenticatable/user.rb
113
127
  - app/policies/g5_authenticatable/base_policy.rb
128
+ - app/policies/g5_updatable/client_policy.rb
114
129
  - app/views/g5_authenticatable/error/auth_error.html.erb
115
130
  - app/views/layouts/g5_authenticatable/application.html.erb
116
131
  - config/initializers/devise.rb
@@ -192,6 +207,11 @@ files:
192
207
  - spec/dummy/db/migrate/20150428182339_add_g5_authenticatable_users_contact_info.rb
193
208
  - spec/dummy/db/migrate/20150429212919_create_g5_authenticatable_roles.rb
194
209
  - spec/dummy/db/migrate/20150509061150_create_posts.rb
210
+ - spec/dummy/db/migrate/20150603224032_create_g5_updatable_clients_and_locations.g5_updatable.rb
211
+ - spec/dummy/db/migrate/20150603224033_create_integration_setting.g5_updatable.rb
212
+ - spec/dummy/db/migrate/20150603224034_remove_integration_setting.g5_updatable.rb
213
+ - spec/dummy/db/migrate/20150603224035_add_name_to_clients_and_locations.g5_updatable.rb
214
+ - spec/dummy/db/migrate/20150603224036_update_names.g5_updatable.rb
195
215
  - spec/dummy/db/schema.rb
196
216
  - spec/dummy/lib/assets/.gitkeep
197
217
  - spec/dummy/log/.gitkeep
@@ -213,6 +233,7 @@ files:
213
233
  - spec/models/g5_authenticatable/user_spec.rb
214
234
  - spec/models/post_spec.rb
215
235
  - spec/policies/application_policy_spec.rb
236
+ - spec/policies/client_policy_spec.rb
216
237
  - spec/policies/post_policy_spec.rb
217
238
  - spec/requests/default_role_authorization_spec.rb
218
239
  - spec/requests/grape_api_spec.rb
@@ -306,6 +327,11 @@ test_files:
306
327
  - spec/dummy/db/migrate/20150428182339_add_g5_authenticatable_users_contact_info.rb
307
328
  - spec/dummy/db/migrate/20150429212919_create_g5_authenticatable_roles.rb
308
329
  - spec/dummy/db/migrate/20150509061150_create_posts.rb
330
+ - spec/dummy/db/migrate/20150603224032_create_g5_updatable_clients_and_locations.g5_updatable.rb
331
+ - spec/dummy/db/migrate/20150603224033_create_integration_setting.g5_updatable.rb
332
+ - spec/dummy/db/migrate/20150603224034_remove_integration_setting.g5_updatable.rb
333
+ - spec/dummy/db/migrate/20150603224035_add_name_to_clients_and_locations.g5_updatable.rb
334
+ - spec/dummy/db/migrate/20150603224036_update_names.g5_updatable.rb
309
335
  - spec/dummy/db/schema.rb
310
336
  - spec/dummy/lib/assets/.gitkeep
311
337
  - spec/dummy/log/.gitkeep
@@ -327,6 +353,7 @@ test_files:
327
353
  - spec/models/g5_authenticatable/user_spec.rb
328
354
  - spec/models/post_spec.rb
329
355
  - spec/policies/application_policy_spec.rb
356
+ - spec/policies/client_policy_spec.rb
330
357
  - spec/policies/post_policy_spec.rb
331
358
  - spec/requests/default_role_authorization_spec.rb
332
359
  - spec/requests/grape_api_spec.rb