usergrid_ironhorse 0.0.3 → 0.0.4
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 +56 -36
- data/lib/extensions/resource.rb +2 -1
- data/lib/usergrid_ironhorse/user_context.rb +2 -0
- data/lib/usergrid_ironhorse/version.rb +1 -1
- data/spec/spec_helper.rb +1 -0
- data/spec/usergrid_ironhorse/base_spec.rb +13 -0
- metadata +2 -8
data/README.md
CHANGED
@@ -33,46 +33,24 @@ Or install it yourself as:
|
|
33
33
|
|
34
34
|
* Add 'gem usergrid_ironhorse' to your Gemfile
|
35
35
|
* Create a 'config/usergrid.yml' file that looks something like this (the
|
36
|
-
auth_token is your application
|
37
|
-
|
36
|
+
auth_token is your application token):
|
37
|
+
|
38
|
+
```
|
38
39
|
development:
|
39
40
|
application_url: http://localhost:8080/my-organization/my-application
|
40
41
|
auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
|
42
|
+
require_login: false
|
41
43
|
|
42
|
-
|
44
|
+
test:
|
43
45
|
application_url: http://localhost:8080/my-organization/my-application
|
44
46
|
auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
|
47
|
+
require_login: false
|
45
48
|
|
46
49
|
production:
|
47
50
|
application_url: http://api.usergrid.com/my-organization/my-application
|
48
51
|
auth_token: YWMtc4WjqhcbEeK6UhQQn9SVgQAAATpryjMnLy9oFaPbP-0qIxoUx_4vtaOmpmE
|
49
|
-
|
50
|
-
|
51
|
-
Usergrid::Ironhorse::UserContext like so:
|
52
|
-
<pre>
|
53
|
-
class User < Usergrid::Ironhorse::Base
|
54
|
-
extend Usergrid::Ironhorse::UserContext
|
55
|
-
|
56
|
-
end
|
57
|
-
</pre>
|
58
|
-
* Set up your authentication
|
59
|
-
* Use `User.authenticate(username, password, session)` to login.
|
60
|
-
* Use `User.clear_authentication(session)` to log out.
|
61
|
-
* Propogate the authentication in your ApplicationController:
|
62
|
-
<pre>
|
63
|
-
before_filter :set_thread_context
|
64
|
-
def set_thread_context
|
65
|
-
User.set_thread_context session
|
66
|
-
end
|
67
|
-
</pre>
|
68
|
-
* Optionally, if you need to access the User from your view, you may add something
|
69
|
-
like the following to your ApplicationController:
|
70
|
-
<pre>
|
71
|
-
helper_method :current_user
|
72
|
-
def current_user
|
73
|
-
User.current_user
|
74
|
-
end
|
75
|
-
</pre>
|
52
|
+
require_login: false
|
53
|
+
```
|
76
54
|
|
77
55
|
#### Get going!
|
78
56
|
|
@@ -81,14 +59,16 @@ Your models will automatically be stored in a collection according to the name o
|
|
81
59
|
class as defined by Rails' ActiveModel::Naming module. (Which you may override by
|
82
60
|
implementing model_name if desired.)
|
83
61
|
|
84
|
-
|
62
|
+
```
|
85
63
|
class Developer < Usergrid::Ironhorse::Base
|
86
64
|
validates :name, :presence => true # Yes, of course you can use validation
|
87
65
|
|
88
66
|
end
|
89
|
-
|
67
|
+
```
|
68
|
+
|
90
69
|
* Now just use the Rails methods you're already familiar with:
|
91
|
-
|
70
|
+
|
71
|
+
```
|
92
72
|
|
93
73
|
dev = Developer.new language: 'Ruby'
|
94
74
|
dev.valid? # nope!
|
@@ -100,15 +80,51 @@ end
|
|
100
80
|
dev.favorite_color = 'green' # assign new attributes automatically
|
101
81
|
|
102
82
|
dev = Developer.find_by_name 'Scott'
|
103
|
-
|
83
|
+
```
|
84
|
+
|
104
85
|
* BTW: If you need to do management tasks, wrapping the work in an as_admin block
|
105
86
|
will use the auth_token from your settings:
|
106
87
|
|
107
|
-
|
88
|
+
```
|
108
89
|
User.as_admin do
|
109
90
|
# do protected task
|
110
91
|
end
|
111
|
-
|
92
|
+
```
|
93
|
+
|
94
|
+
|
95
|
+
#### (Optional) Need to have user-specific logins to UserGrid?
|
96
|
+
|
97
|
+
* Create a User model and subclass `Usergrid::Ironhorse::Base` and `extend
|
98
|
+
Usergrid::Ironhorse::UserContext` like so:
|
99
|
+
|
100
|
+
```
|
101
|
+
class User < Usergrid::Ironhorse::Base
|
102
|
+
extend Usergrid::Ironhorse::UserContext
|
103
|
+
...
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
* Set up your authentication
|
108
|
+
* Use `User.authenticate(username, password, session)` to login.
|
109
|
+
* Use `User.clear_authentication(session)` to log out.
|
110
|
+
* Propogate the authentication in your ApplicationController:
|
111
|
+
|
112
|
+
```
|
113
|
+
before_filter :set_user_context
|
114
|
+
def set_user_context
|
115
|
+
User.set_context session
|
116
|
+
end
|
117
|
+
```
|
118
|
+
|
119
|
+
* Optionally, if you need to access the User from your view, you may add something
|
120
|
+
like the following to your ApplicationController:
|
121
|
+
|
122
|
+
```
|
123
|
+
helper_method :current_user
|
124
|
+
def current_user
|
125
|
+
User.current_user
|
126
|
+
end
|
127
|
+
```
|
112
128
|
|
113
129
|
## Contributing
|
114
130
|
|
@@ -133,6 +149,10 @@ usergrid_ironhorse/spec/spec_settings.yaml to match.)
|
|
133
149
|
|
134
150
|
## Release notes
|
135
151
|
|
152
|
+
### 0.0.4
|
153
|
+
* New Features
|
154
|
+
1. add require_login to config (with ability to skip individual logins)
|
155
|
+
|
136
156
|
### 0.0.3
|
137
157
|
* Internal
|
138
158
|
1. Improve authentication and user propagation
|
data/lib/extensions/resource.rb
CHANGED
@@ -3,7 +3,8 @@ module Usergrid
|
|
3
3
|
class Resource
|
4
4
|
def options
|
5
5
|
options = @options.clone
|
6
|
-
|
6
|
+
require_login = Usergrid::Ironhorse::Base.settings[:require_login] != false
|
7
|
+
auth_token = require_login ? Thread.current[:usergrid_auth_token] : Usergrid::Ironhorse::Base.settings[:auth_token]
|
7
8
|
options[:headers].delete :Authorization
|
8
9
|
options[:headers][:Authorization] = "Bearer #{auth_token}" if auth_token
|
9
10
|
options
|
@@ -47,6 +47,7 @@ module Usergrid
|
|
47
47
|
Thread.current[:usergrid_auth_token] = session[:usergrid_auth_token]
|
48
48
|
Thread.current[:usergrid_current_user] = nil
|
49
49
|
end
|
50
|
+
alias_method :set_context, :set_thread_context
|
50
51
|
|
51
52
|
# clears auth from current thread
|
52
53
|
def clear_thread_context(session)
|
@@ -54,6 +55,7 @@ module Usergrid
|
|
54
55
|
Thread.current[:usergrid_auth_token] = nil
|
55
56
|
Thread.current[:usergrid_current_user] = nil
|
56
57
|
end
|
58
|
+
alias_method :clear_context, :clear_thread_context
|
57
59
|
|
58
60
|
# returns the auth token for the current thread
|
59
61
|
def current_auth_token
|
data/spec/spec_helper.rb
CHANGED
@@ -26,6 +26,7 @@ SimpleCov.start
|
|
26
26
|
|
27
27
|
|
28
28
|
SPEC_SETTINGS = YAML::load_file(File.join File.dirname(__FILE__), 'spec_settings.yaml')
|
29
|
+
Usergrid::Ironhorse::Base.configure! nil, nil
|
29
30
|
|
30
31
|
def login_management
|
31
32
|
management = Usergrid::Resource.new(SPEC_SETTINGS[:api_url]).management
|
@@ -40,6 +40,19 @@ describe Usergrid::Ironhorse::Base do
|
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
43
|
+
it "do tasks as admin if require_login is false" do
|
44
|
+
organization = @foo.management.organization SPEC_SETTINGS[:organization][:name]
|
45
|
+
|
46
|
+
# should fail under current user's context
|
47
|
+
expect {
|
48
|
+
organization.create_application "_test_app_#{SecureRandom.hex}"
|
49
|
+
}.to raise_error RestClient::Unauthorized
|
50
|
+
|
51
|
+
# should succeed once require_login is false
|
52
|
+
User.settings[:require_login] = false
|
53
|
+
organization.create_application "_test_app_#{SecureRandom.hex}"
|
54
|
+
end
|
55
|
+
|
43
56
|
it 'be created and destroyed' do
|
44
57
|
foo = Foo.create name: 'foo2'
|
45
58
|
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.
|
4
|
+
version: 0.0.4
|
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-
|
12
|
+
date: 2012-11-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: usergrid_iron
|
@@ -161,18 +161,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
161
161
|
- - ! '>='
|
162
162
|
- !ruby/object:Gem::Version
|
163
163
|
version: '0'
|
164
|
-
segments:
|
165
|
-
- 0
|
166
|
-
hash: 3863127888232892362
|
167
164
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
168
165
|
none: false
|
169
166
|
requirements:
|
170
167
|
- - ! '>='
|
171
168
|
- !ruby/object:Gem::Version
|
172
169
|
version: '0'
|
173
|
-
segments:
|
174
|
-
- 0
|
175
|
-
hash: 3863127888232892362
|
176
170
|
requirements: []
|
177
171
|
rubyforge_project:
|
178
172
|
rubygems_version: 1.8.24
|