usergrid_ironhorse 0.0.2 → 0.0.3

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.
data/README.md CHANGED
@@ -29,21 +29,23 @@ Or install it yourself as:
29
29
 
30
30
  ### Getting started with the Usergrid_ironhorse SDK is super simple!
31
31
 
32
+ #### Setup
33
+
32
34
  * Add 'gem usergrid_ironhorse' to your Gemfile
33
35
  * Create a 'config/usergrid.yml' file that looks something like this (the
34
36
  auth_token is your application admin token):
35
37
  <pre>
36
38
  development:
37
- :application_url: http://localhost:8080/my-organization/my-application
38
- :auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
39
+ application_url: http://localhost:8080/my-organization/my-application
40
+ auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
39
41
 
40
42
  development:
41
- :application_url: http://localhost:8080/my-organization/my-application
42
- :auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
43
+ application_url: http://localhost:8080/my-organization/my-application
44
+ auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
43
45
 
44
46
  production:
45
- :application_url: http://api.usergrid.com/my-organization/my-application
46
- :auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
47
+ application_url: http://api.usergrid.com/my-organization/my-application
48
+ auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
47
49
  </pre>
48
50
  * Your User model should subclass Usergrid::Ironhorse::Base and extend
49
51
  Usergrid::Ironhorse::UserContext like so:
@@ -58,20 +60,55 @@ end
58
60
  * Use `User.clear_authentication(session)` to log out.
59
61
  * Propogate the authentication in your ApplicationController:
60
62
  <pre>
61
- before_filter :set_thread_context
62
- def set_thread_context
63
- User.set_thread_context session
64
- end
63
+ before_filter :set_thread_context
64
+ def set_thread_context
65
+ User.set_thread_context session
66
+ end
65
67
  </pre>
66
68
  * Optionally, if you need to access the User from your view, you may add something
67
69
  like the following to your ApplicationController:
68
70
  <pre>
69
- helper_method :current_user
70
- def current_user
71
- User.current_user
72
- end
71
+ helper_method :current_user
72
+ def current_user
73
+ User.current_user
74
+ end
73
75
  </pre>
74
76
 
77
+ #### Get going!
78
+
79
+ * Subclass Usergrid::Ironhorse::Base for your models.
80
+ Your models will automatically be stored in a collection according to the name of your
81
+ class as defined by Rails' ActiveModel::Naming module. (Which you may override by
82
+ implementing model_name if desired.)
83
+
84
+ <pre>
85
+ class Developer < Usergrid::Ironhorse::Base
86
+ validates :name, :presence => true # Yes, of course you can use validation
87
+
88
+ end
89
+ </pre>
90
+ * Now just use the Rails methods you're already familiar with:
91
+ <pre>
92
+
93
+ dev = Developer.new language: 'Ruby'
94
+ dev.valid? # nope!
95
+ dev.errors # {:name=>["can't be blank"]}
96
+ dev.name = 'Scott'
97
+ dev.save!
98
+
99
+ dev = Developer.find_or_create_by_name 'Scott'
100
+ dev.favorite_color = 'green' # assign new attributes automatically
101
+
102
+ dev = Developer.find_by_name 'Scott'
103
+ </pre>
104
+ * BTW: If you need to do management tasks, wrapping the work in an as_admin block
105
+ will use the auth_token from your settings:
106
+
107
+ <pre>
108
+ User.as_admin do
109
+ # do protected task
110
+ end
111
+ </pre>
75
112
 
76
113
  ## Contributing
77
114
 
@@ -85,8 +122,8 @@ We welcome your enhancements!
85
122
  4. Push your changes to the upstream branch (`git push origin my-new-feature`)
86
123
  5. Create new Pull Request
87
124
 
88
- We've got 100% rspec coverage and we're looking to keep it that way!*
89
- (*Not yet, but soon)
125
+ We're shooting for 100% rspec coverage, so keep that in mind!
126
+
90
127
  In order to run the tests, check out the Usergrid open source project
91
128
  (https://github.com/apigee/usergrid-stack), build, and launch it locally.
92
129
 
@@ -96,9 +133,14 @@ usergrid_ironhorse/spec/spec_settings.yaml to match.)
96
133
 
97
134
  ## Release notes
98
135
 
136
+ ### 0.0.3
137
+ * Internal
138
+ 1. Improve authentication and user propagation
139
+
99
140
  ### 0.0.2
100
141
  * New Features
101
142
  1. Authentication and user propagation features
143
+
102
144
  ### 0.0.1
103
145
  * Initial commit
104
146
  1. Support for most ActiveModel stuff including Validations
@@ -0,0 +1,26 @@
1
+ # overrides methods dealing with auth_token to operate on a thread basis
2
+ module Usergrid
3
+ class Resource
4
+ def options
5
+ options = @options.clone
6
+ auth_token = Thread.current[:usergrid_auth_token]
7
+ options[:headers].delete :Authorization
8
+ options[:headers][:Authorization] = "Bearer #{auth_token}" if auth_token
9
+ options
10
+ end
11
+
12
+ # gets user token and automatically set auth header for future requests on this Thread
13
+ # precondition: resource must already be set to the correct context (application or management)
14
+ def login(username, password)
15
+ params = { grant_type: "password", username: username, password: password }
16
+ response = self['token'].get({ params: params })
17
+ user_uuid = response.data['user']['uuid']
18
+ user_access_token = response.data['access_token']
19
+ Thread.current[:usergrid_user_id] = user_uuid
20
+ Thread.current[:usergrid_auth_token] = user_access_token
21
+ @current_user = self["/users/#{user_uuid}"].get.entity
22
+ response
23
+ end
24
+
25
+ end
26
+ end
@@ -147,11 +147,7 @@ module Usergrid
147
147
  # Creates a Usergrid::Resource
148
148
  def self.resource
149
149
  app = Usergrid::Application.new settings[:application_url]
150
- if Thread.current[:auth_token]
151
- app.auth_token = Thread.current[:auth_token]
152
- else
153
- app.auth_token = settings[:auth_token]
154
- end
150
+ #app.auth_token = Thread.current[:usergrid_auth_token]
155
151
  app[group]
156
152
  end
157
153
 
@@ -32,18 +32,12 @@ module Usergrid
32
32
 
33
33
  # allows admin actions to be done in a block
34
34
  def as_admin(&block)
35
- save_user_id = Thread.current[:usergrid_user_id]
36
35
  save_auth_token = Thread.current[:usergrid_auth_token]
37
- save_user = Thread.current[:usergrid_current_user]
38
36
  begin
39
- Thread.current[:usergrid_user_id] = nil
40
37
  Thread.current[:usergrid_auth_token] = Base.settings[:auth_token]
41
- Thread.current[:usergrid_current_user] = nil
42
38
  yield block
43
39
  ensure
44
- Thread.current[:usergrid_user_id] = save_user_id
45
40
  Thread.current[:usergrid_auth_token] = save_auth_token
46
- Thread.current[:usergrid_current_user] = save_user
47
41
  end
48
42
  end
49
43
 
@@ -1,5 +1,5 @@
1
1
  module Usergrid
2
2
  module Ironhorse
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
  end
5
5
  end
data/spec/spec_helper.rb CHANGED
@@ -54,7 +54,6 @@ end
54
54
  def create_random_application
55
55
  management = login_management
56
56
  organization = management.organization SPEC_SETTINGS[:organization][:name]
57
-
58
57
  app_name = "_test_app_#{SecureRandom.hex}"
59
58
  organization.create_application app_name
60
59
  management.application SPEC_SETTINGS[:organization][:name], app_name
@@ -2,6 +2,10 @@ describe Usergrid::Ironhorse::Base do
2
2
 
3
3
  it_should_behave_like 'ActiveModel'
4
4
 
5
+ class User < Usergrid::Ironhorse::Base
6
+ extend Usergrid::Ironhorse::UserContext
7
+ end
8
+
5
9
  before :all do
6
10
  @application = create_random_application
7
11
  Foo.configure!(@application.url, @application.auth_token)
@@ -22,6 +26,20 @@ describe Usergrid::Ironhorse::Base do
22
26
 
23
27
  describe 'subclasses should be able to' do
24
28
 
29
+ it "do tasks as admin when requested" do
30
+ organization = @foo.management.organization SPEC_SETTINGS[:organization][:name]
31
+
32
+ # should fail under current user's context
33
+ expect {
34
+ organization.create_application "_test_app_#{SecureRandom.hex}"
35
+ }.to raise_error RestClient::Unauthorized
36
+
37
+ # should succeed under admin context
38
+ User.as_admin do
39
+ organization.create_application "_test_app_#{SecureRandom.hex}"
40
+ end
41
+ end
42
+
25
43
  it 'be created and destroyed' do
26
44
  foo = Foo.create name: 'foo2'
27
45
  foo.persisted?.should be_true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usergrid_ironhorse
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-16 00:00:00.000000000 Z
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: usergrid_iron
@@ -138,6 +138,7 @@ files:
138
138
  - README.md
139
139
  - Rakefile
140
140
  - lib/extensions/hash.rb
141
+ - lib/extensions/resource.rb
141
142
  - lib/usergrid_ironhorse.rb
142
143
  - lib/usergrid_ironhorse/base.rb
143
144
  - lib/usergrid_ironhorse/query.rb
@@ -162,7 +163,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
162
163
  version: '0'
163
164
  segments:
164
165
  - 0
165
- hash: 2399657217160985810
166
+ hash: 3863127888232892362
166
167
  required_rubygems_version: !ruby/object:Gem::Requirement
167
168
  none: false
168
169
  requirements:
@@ -171,7 +172,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
171
172
  version: '0'
172
173
  segments:
173
174
  - 0
174
- hash: 2399657217160985810
175
+ hash: 3863127888232892362
175
176
  requirements: []
176
177
  rubyforge_project:
177
178
  rubygems_version: 1.8.24