Module: RailsIdentity::ApplicationHelper

Included in:
ApplicationController
Defined in:
app/helpers/rails_identity/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#accept_tokenObject

Accepts a token if present. If not, it’s still ok. ALL errors are suppressed.



75
76
77
78
79
80
81
82
# File 'app/helpers/rails_identity/application_helper.rb', line 75

def accept_token()
  logger.debug("Accepts a token")
  begin
    get_token()
  rescue StandardError => e
    logger.error("Suppressing error: #{e.message}")
  end
end

#authorized?(obj) ⇒ Boolean

Determines if the user is authorized for the object.

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/rails_identity/application_helper.rb', line 96

def authorized?(obj)
  logger.debug("Checking to see if authorized to access object")
  if @auth_user.nil?
    # :nocov:
    return false
    # :nocov:
  elsif @auth_user.role >= Roles::ADMIN
    return true
  elsif obj.is_a? User
    return obj == @auth_user
  else
    return obj.user == @auth_user
  end
end

#find_object(model, uuid, error: Errors::ObjectNotFoundError) ⇒ Object

Finds an object by model and UUID and throws an error (which will be caught and re-thrown as an HTTP error.)

An Errors::ObjectNotFoundError is raised if specified to do so when the object could not be found using the uuid.



54
55
56
57
58
59
60
61
# File 'app/helpers/rails_identity/application_helper.rb', line 54

def find_object(model, uuid, error: Errors::ObjectNotFoundError)
  logger.debug("Attempting to get #{model.name} #{uuid}")
  obj = model.find_by_uuid(uuid)
  if obj.nil? && !error.nil?
    raise error, "#{model.name} #{uuid} cannot be found" 
  end
  return obj
end

#get_user(fallback: true) ⇒ Object

Helper method to get the user object in the request context. There are two ways to specify the user id–one in the routing or the auth context. Only admin can actually specify the user id in the routing.

An Errors::UnauthorizedError is raised if the authenticated user is not authorized for the specified user information.

An Errors::ObjectNotFoundError is raised if the specified user cannot be found.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/rails_identity/application_helper.rb', line 29

def get_user(fallback: true)
  user_id = params[:user_id]
  logger.debug("Attempting to get user #{user_id}")
  if !user_id.nil? && user_id != "current"
    @user = find_object(User, params[:user_id])  # will throw error if nil
    unless authorized?(@user)
      raise Errors::UnauthorizedError,
            "Not authorized to access user #{user_id}"
    end
  elsif fallback || user_id == "current"
    @user = @auth_user
  else
    # :nocov:
    raise Errors::ObjectNotFoundError, "User #{user_id} does not exist"
    # :nocov:
  end
end

#render_error(status, msg) ⇒ Object

Renders a single error.



7
8
9
# File 'app/helpers/rails_identity/application_helper.rb', line 7

def render_error(status, msg)
  render json: {errors: [msg]}, status: status
end

#render_errors(status, msgs) ⇒ Object

Renders multiple errors



14
15
16
# File 'app/helpers/rails_identity/application_helper.rb', line 14

def render_errors(status, msgs)
  render json: {errors: msgs}, status: status
end

#require_admin_tokenObject

Requires an admin session. All this means is that the session is issued for an admin user (role == 1000).



88
89
90
91
# File 'app/helpers/rails_identity/application_helper.rb', line 88

def require_admin_token
  logger.debug("Requires an admin token")
  get_token(required_role: Roles::ADMIN)
end

#require_tokenObject

Requires a token.



66
67
68
69
# File 'app/helpers/rails_identity/application_helper.rb', line 66

def require_token
  logger.debug("Requires a token")
  get_token
end