Class: Ditty::Auth

Inherits:
Application show all
Defined in:
lib/ditty/controllers/auth.rb

Instance Method Summary collapse

Instance Method Details

#failed_loginObject



28
29
30
31
32
# File 'lib/ditty/controllers/auth.rb', line 28

def 
  broadcast(:user_failed_login, target: self, details: "IP: #{request.ip}")
  flash[:warning] = 'Invalid credentials. Please try again.'
  redirect "#{settings.map_path}/auth/login"
end

#find_template(views, name, engine, &block) ⇒ Object



9
10
11
12
# File 'lib/ditty/controllers/auth.rb', line 9

def find_template(views, name, engine, &block)
  super(views, name, engine, &block) # Root
  super(::Ditty::App.view_folder, name, engine, &block) # Basic Plugin
end

#omniauth_callback(provider) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/ditty/controllers/auth.rb', line 20

def omniauth_callback(provider)
  return  unless env['omniauth.auth']
  user = User.first(email: env['omniauth.auth']['info']['email'])
  user = register_user if user.nil? && ['ldap', 'google_oauth2'].include?(provider)
  return  if user.nil?
  (user)
end

#redirect_pathObject



14
15
16
17
18
# File 'lib/ditty/controllers/auth.rb', line 14

def redirect_path
  return "#{settings.map_path}/" unless env['omniauth.origin']
  return "#{settings.map_path}/" if env['omniauth.origin'] =~ %r{/#{settings.map_path}/auth/?}
  env['omniauth.origin']
end

#register_userObject



42
43
44
45
46
47
# File 'lib/ditty/controllers/auth.rb', line 42

def register_user
  user = User.create(email: env['omniauth.auth']['info']['email'])
  broadcast(:user_register, target: self, values: { user: user }, details: "IP: #{request.ip}")
  flash[:info] = 'Successfully Registered.'
  user
end

#successful_login(user) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/ditty/controllers/auth.rb', line 34

def (user)
  halt 200 if request.xhr?
  self.current_user = user
  broadcast(:user_login, target: self, details: "IP: #{request.ip}")
  flash[:success] = 'Logged In'
  redirect redirect_path
end