Class: Usergrid::Resource
- Inherits:
-
Object
- Object
- Usergrid::Resource
- Defined in:
- lib/extensions/resource.rb
Instance Method Summary collapse
- #as_admin(&block) ⇒ Object
-
#login(username, password) ⇒ Object
gets user token and automatically set auth header for future requests on this Thread precondition: resource must already be set to the correct context (application or management).
- #options ⇒ Object
Instance Method Details
#as_admin(&block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/extensions/resource.rb', line 21 def as_admin(&block) save_auth_token = Thread.current[:usergrid_auth_token] begin unless Ironhorse::Base.settings[:auth_token] resource = RestClient::Resource.new Ironhorse::Base.settings[:application_url] response = resource['token'].post grant_type: 'client_credentials', client_id: Ironhorse::Base.settings[:client_id], client_secret: Ironhorse::Base.settings[:client_secret] Ironhorse::Base.settings[:auth_token] = MultiJson.load(response)['access_token'] end Thread.current[:usergrid_auth_token] = Ironhorse::Base.settings[:auth_token] yield block ensure Thread.current[:usergrid_auth_token] = save_auth_token end end |
#login(username, password) ⇒ Object
gets user token and automatically set auth header for future requests on this Thread precondition: resource must already be set to the correct context (application or management)
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/extensions/resource.rb', line 38 def login(username, password) params = { grant_type: "password", username: username, password: password } response = self['token'].get({ params: params }) user_uuid = response.data['user']['uuid'] user_access_token = response.data['access_token'] Thread.current[:usergrid_user_id] = user_uuid Thread.current[:usergrid_auth_token] = user_access_token @current_user = self["/users/#{user_uuid}"].get.entity response end |
#options ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/extensions/resource.rb', line 5 def = @options.clone require_login = Ironhorse::Base.settings[:require_login] != false if require_login auth_token = Thread.current[:usergrid_auth_token] else unless Ironhorse::Base.settings[:auth_token] as_admin {} end auth_token = Ironhorse::Base.settings[:auth_token] end [:headers].delete :Authorization [:headers][:Authorization] = "Bearer #{auth_token}" if auth_token end |