Class: Auth::ApplicationController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/auth/application_controller.rb

Instance Method Summary collapse

Instance Method Details

#authenticate_resource!Object

will call authenticate_(first_key_in_the_auth_resources) if there is no currently signed in scoep will return true, for the first auth_resource that gives a current_(user/whatever) if nothing returns true, will redirect to not_found, use this function wherever you want to protect a controller just using devise authentication. only makes sense to use in the scope of the web app.



144
145
146
147
148
149
150
151
# File 'app/controllers/auth/application_controller.rb', line 144

def authenticate_resource!
	send("authenticate_#{Auth.configuration.auth_resources.keys.first.downcase}!") if (signed_in? == false)
	Auth.configuration.auth_resources.keys.each do |model|
		break if @resource_for_web_app = send("current_#{model.downcase}")
	end
	return if @resource_for_web_app
	not_found("Could not authenticate")
end

#build_model_from_paramsObject



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/auth/application_controller.rb', line 70

def build_model_from_params
	puts "params are: #{params.to_s}"
     	pp = permitted_params
     	puts "the permitted_params are:"
     	puts permitted_params.to_s

     	@model_params = pp.fetch(get_model_class_name.to_sym,{})
     	puts "model params are:"
     	puts @model_params.to_s

     	@model = pp[:id] ?  @model_class.find_self(pp[:id],current_signed_in_resource) : @model_class.new(@model_params)

end

#check_for_create(obj) ⇒ Object



130
131
132
133
# File 'app/controllers/auth/application_controller.rb', line 130

def check_for_create(obj)
	not_found if obj.nil?
	obj.new_record? or not_found("this is not a new record")
end

#check_for_destroy(obj) ⇒ Object



135
136
137
# File 'app/controllers/auth/application_controller.rb', line 135

def check_for_destroy(obj)
	not_found("please provide a cart id") if obj.new_record?
end

#check_for_update(obj) ⇒ Object



124
125
126
127
128
# File 'app/controllers/auth/application_controller.rb', line 124

def check_for_update(obj)
	puts "Came to check for update."
	not_found if obj.nil?
   	not_found("please provide a valid id for the update") if obj.new_record?
end

#from_bson(bson_doc, klass) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/auth/application_controller.rb', line 85

def from_bson(bson_doc,klass)

	if !bson_doc.nil?

		user = Mongoid::Factory.from_db(klass,bson_doc)
		return user

	else

		return nil

	end

end

#from_view(view, klass) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/controllers/auth/application_controller.rb', line 100

def from_view(view,klass)

	if !view.nil? && view.count > 0

		user = Mongoid::Factory.from_db(klass,view.first)
		return user

	else

		return nil

	end

end

#get_model_class_nameObject

will downcase and singularize the controller name.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/auth/application_controller.rb', line 30

def get_model_class_name
	
	class_name = nil

	self.class.name.scan(/::(?<plural_controller_name>[A-Za-z]+)Controller$/) do |ll|

		jj = Regexp.last_match
		
		plural_controller_name = jj[:plural_controller_name]

		class_name = plural_controller_name.singularize.downcase

	end

	not_found("could not determine class name") unless class_name
	
	puts "class name: #{class_name}"
	

	return class_name

end

#instantiate_classesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/auth/application_controller.rb', line 53

def instantiate_classes

	if Auth.configuration.send("#{get_model_class_name}_class")

		begin
			instance_variable_set("@model_class",Auth.configuration.send("#{get_model_class_name}_class").constantize)
		rescue 
			not_found("could not instantiate class #{get_model_class_name}")
		end

	else
		not_found("#{get_model_class_name} class not defined in configuration")
	end

end

#not_found(error = 'Not Found') ⇒ Object

CURRENTLY BEING USED IN THE DUMMY APP IN OTP_CONTROLLER RENDERS A NOT FOUND RESPONSE, in case the user is not found.

Raises:

  • (ActionController::RoutingError)


118
119
120
# File 'app/controllers/auth/application_controller.rb', line 118

def not_found(error = 'Not Found')
	  raise ActionController::RoutingError.new(error)
end