Module: Usman::ApiHelper

Included in:
Usman::Api::V1::BaseController
Defined in:
app/helpers/usman/api_helper.rb

Instance Method Summary collapse

Instance Method Details

#current_deviceObject



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, options| Device.find_by(api_token: token)}
  @current_registration = @current_device.registration if @current_device
  @current_user = @current_registration.user if @current_registration
end

#current_userObject



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, options| User.find_by(auth_token: token)}
end

#require_admin_auth_tokenObject



53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/usman/api_helper.rb', line 53

def require_admin_auth_token
  current_user
  unless @current_user && @current_user.is_admin?
    proc_code = Proc.new do
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end

#require_api_tokenObject



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/usman/api_helper.rb', line 27

def require_api_token
  current_device
  unless @current_device
    proc_code = Proc.new do
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  else
    @current_user = @current_device.try(:registration).try(:user)
  end
end

#require_auth_tokenObject



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
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end

#require_super_admin_auth_tokenObject



41
42
43
44
45
46
47
48
49
50
51
# File 'app/helpers/usman/api_helper.rb', line 41

def require_super_admin_auth_token
  current_user
  unless @current_user && @current_user.is_super_admin?
    proc_code = Proc.new do
      set_notification_messages("authentication.permission_denied", :error)
      raise AuthenticationError
    end
    render_json_response(proc_code)
    return
  end
end