Module: Usman::ApiHelper
- Included in:
- Usman::Api::V1::BaseController
- Defined in:
- app/helpers/usman/api_helper.rb
Instance Method Summary collapse
- #current_device ⇒ Object
- #current_user ⇒ Object
- #require_admin_auth_token ⇒ Object
- #require_api_token ⇒ Object
- #require_auth_token ⇒ Object
- #require_super_admin_auth_token ⇒ Object
- #store_last_accessed_api ⇒ Object
Instance Method Details
#current_device ⇒ Object
8 9 10 11 12 13 |
# File 'app/helpers/usman/api_helper.rb', line 8 def current_device # Return if @current_device is already initialized else check if the device exists with the api token present in request header @current_device ||= authenticate_with_http_token { |token, | Device.find_by(api_token: token)} @current_registration = @current_device.registration if @current_device @current_user = @current_registration.user if @current_registration end |
#current_user ⇒ Object
3 4 5 6 |
# File 'app/helpers/usman/api_helper.rb', line 3 def current_user # Return if @current_user is already initialized else check if the user exists with the auth token present in request header @current_user ||= authenticate_with_http_token { |token, | User.find_by(auth_token: token)} end |
#require_admin_auth_token ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 |
# File 'app/helpers/usman/api_helper.rb', line 79 def require_admin_auth_token current_user unless @current_user && @current_user.is_admin? proc_code = Proc.new do ("authentication.permission_denied", :error) raise AuthenticationError end render_json_response(proc_code) return end end |
#require_api_token ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/usman/api_helper.rb', line 27 def require_api_token current_device unless @current_device proc_code = Proc.new do @success = false @errors = { heading: I18n.translate("api.general.permission_denied.heading"), message: I18n.translate("api.general.permission_denied.message") } end render_json_response(proc_code) return else @current_user = @current_device.try(:registration).try(:user) end if @current_user if @current_user.pending? proc_code = Proc.new do @success = false @errors = { heading: I18n.translate("api.authentication.user_is_pending.heading"), message: I18n.translate("api.authentication.user_is_pending.message") } end render_json_response(proc_code) return elsif @current_user.suspended? proc_code = Proc.new do @success = false @errors = { heading: I18n.translate("api.authentication.user_is_suspended.heading"), message: I18n.translate("api.authentication.user_is_suspended.message") } end render_json_response(proc_code) return end end end |
#require_auth_token ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'app/helpers/usman/api_helper.rb', line 15 def require_auth_token current_user unless @current_user proc_code = Proc.new do ("authentication.permission_denied", :error) raise AuthenticationError end render_json_response(proc_code) return end end |
#require_super_admin_auth_token ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/helpers/usman/api_helper.rb', line 67 def require_super_admin_auth_token current_user unless @current_user && @current_user.is_super_admin? proc_code = Proc.new do ("authentication.permission_denied", :error) raise AuthenticationError end render_json_response(proc_code) return end end |
#store_last_accessed_api ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'app/helpers/usman/api_helper.rb', line 91 def store_last_accessed_api if @current_device # Know what was the last API accessed and when # This is to catch the users who are inactive @current_device.last_accessed_api = request.original_url @current_device.last_accessed_at = Time.now @current_device.save end end |