Class: Auth::ProfilesController

Inherits:
ApplicationController show all
Includes:
Concerns::DeviseConcern, Concerns::TokenConcern
Defined in:
app/controllers/auth/profiles_controller.rb

Constant Summary collapse

CONDITIONS_FOR_TOKEN_AUTH =
[:get_user_id,:show,:update,:set_proxy_resource]
TCONDITIONS =
{:only => CONDITIONS_FOR_TOKEN_AUTH}

Instance Method Summary collapse

Methods inherited from ApplicationController

#authenticate_resource!, #check_for_create, #check_for_destroy, #check_for_update, #from_bson, #from_view, #not_found

Instance Method Details

#credential_existsObject

@used_in: email check if already exists. this method is only usable through web. not available currently for api use.



110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/auth/profiles_controller.rb', line 110

def credential_exists
  filt = permitted_params
  resource = get_model(filt["resource"])
  is_valid = false
  if resource
    conditions = resource.credential_exists(filt)
    is_valid = (resource.or(*conditions).count == 0)
  end
  respond_to do |format|
    format.json { render json: {"is_valid" => is_valid} }
  end
end

#get_user_idObject

here the idea is to just return the current_signed_in_resource’s id. it doesn’t have anything to do with the profiel since no id is sent into the params, so profile_resource will never be found.



77
78
79
80
81
82
83
# File 'app/controllers/auth/profiles_controller.rb', line 77

def get_user_id
  res = current_signed_in_resource
  res.m_client = self.m_client
  respond_with current_signed_in_resource do |format|
    format.json {render json: current_signed_in_resource.as_json({:show_id => true})}
  end
end

#initialize_varsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/auth/profiles_controller.rb', line 14

def initialize_vars
  puts "---------------------------------------------------"
  @resource_params = {}
  @profile_resource = nil
  @all_params = permitted_params.deep_symbolize_keys
  
  
    if collection = @all_params[:resource]
      
      if Auth.configuration.auth_resources[collection.singularize.capitalize]

        @resource_class = collection.singularize.capitalize.constantize
        @resource_symbol = collection.singularize.to_sym
        
        @resource_params = @all_params.fetch(@resource_symbol,{})
        
        @profile_resource = @all_params[:id] ? @resource_class.find_resource(@all_params[:id],current_signed_in_resource) : @resource_class.new(@resource_params)
      end
    end      
end

#set_proxy_resourceObject

THIS IS HOW YOU SET A PROXY USER AS AN ADMIN. this method takes an id. it also needs current signed in user to be an admin. it basically takes the @profile_resource then it shoves it into the session as proxy_resource_id and proxy_resource_class then it returns the profile_resource. it responds only to js it is meant to be used only for setting the proxied user by an admin in the web application. expect the params to contain params and params



95
96
97
98
99
100
101
102
# File 'app/controllers/auth/profiles_controller.rb', line 95

def set_proxy_resource
  not_found("that user doesn't exist") unless @profile_resource
  session[:proxy_resource_id] = @profile_resource.id.to_s
  session[:proxy_resource_class] = @profile_resource.class.name.to_s
  #puts "the session variables set are as follows:"
  #puts session[:proxy_resource_id]
  #puts session[:proxy_resource_class]
end

#showObject

this method needs token authentication, or for the user to be authenticated. this method also needs an :id, hence the profile_resource is returned. so what if i sign in as one user,and send in the id of another user?, no because we use the find_resource method, which also considers the current_signed_in_Resource.



39
40
41
# File 'app/controllers/auth/profiles_controller.rb', line 39

def show
  @profile_resource
end

#updateObject

this method needs the token authentication and an :id, hence the profile resource is updated. expected params hash: => “users”, :user => {:admin,:request_send_reset_password_link, :id}



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/auth/profiles_controller.rb', line 47

def update
  check_for_update(@profile_resource)
  
  if @resource_params[:admin]
    @profile_resource.admin = @resource_params[:admin]
  end

  if @resource_params[:created_by_admin]
    @profile_resource.created_by_admin = @resource_params[:created_by_admin]
  end

  @profile_resource.m_client = self.m_client
  
  
  respond_to do |format|
      if @profile_resource.save
         flash[:notice] = "Success"
        format.json {head :no_content}
        format.html {redirect_to profile_path({:id => @profile_resource.id.to_s, :resource => @profile_resource.class.name.pluralize.downcase.to_s})}
      else
         flash[:notice] = "Failed"
         format.json {render :json => @profile_resource.errors, :status => :unprocessable_entity}
         format.html {redirect_to profile_path({:id => @profile_resource.id.to_s, :resource => @profile_resource.class.name.pluralize.downcase.to_s})}
      end
    end
end