#
#= LoginController
#
#Copyright::(c)2007-2019 MORITA Shintaro, Sysphonic. [http://sysphonic.com/]
#License::   MIT License (See LICENSE file)
#
class LoginController < ApplicationController
  layout('base')

  #=== index
  #
  #Does nothing about showing rhtml.
  #
  def index
    Log.add_info(request, params.inspect)
  end

  #=== login
  #
  #Does Login.
  #
  def login
    Log.add_info(request, '')   # Not to show passwords.

    user = User.authenticate(params[:user])

    if user.nil?

      flash[:notice] = '<span class=\'msg_bold\'>'+t('user.u_name')+'</span>'+t('msg.or')+'<span class=\'msg_bold\'>'+t('password.name')+'</span>'+t('msg.is_invalid')

      if params[:fwd_controller].blank?

        redirect_to(:controller => 'login', :action => 'index')
      else

        url_h = {:controller => 'login', :action => 'index', :fwd_controller => params[:fwd_controller], :fwd_action => params[:fwd_action]}

        unless params[:fwd_params].nil?
          params[:fwd_params].each do |key, val|
            url_h["fwd_params[#{key}]"] = val
          end
        end

        redirect_to(url_h)
      end

    else

      @login_user = LoginHelper.on_login(user, session)

      if params[:fwd_controller].blank?
        prms = ApplicationHelper.get_fwd_params(params)
        prms.delete('user')
        prms[:controller] = 'desktop'
        prms[:action] = 'show'
        redirect_to(prms)
      else
        url_h = {:controller => params[:fwd_controller], :action => params[:fwd_action]}
        url_h = url_h.update(params[:fwd_params].permit!) unless params[:fwd_params].nil?
        redirect_to(url_h)
      end
    end
  end

  #=== logout
  #
  #Does Logout.
  #
  def logout
    Log.add_info(request, params.inspect)

    reset_session

    prms = ApplicationHelper.get_fwd_params(params)
    prms[:controller] = 'login'
    prms[:action] = 'index'
    redirect_to(prms)
  end

  #=== send_password
  #
  #Sends User account information by E-mail.
  #
  def send_password
    Log.add_info(request, params.inspect)

    raise(RequestPostOnlyException) unless request.post?

    mail_addr = params[:thetisBoxEdit]
    begin
      users = User.where(email: mail_addr).to_a
    rescue => evar
    end

    if (users.nil? or users.empty?)
      Log.add_error(request, evar)
      flash[:notice] = 'ERROR:' + t('email.address_not_found')
    else
      user_passwords_h = {}
      users.each do |user|
        newpass = UsersHelper.generate_password
        user.update_attribute(:pass_md5, UsersHelper.generate_digest_pass(user.name, newpass))

        user_passwords_h[user] = newpass
      end

      NoticeMailer.password(user_passwords_h, ApplicationHelper.root_url(request)).deliver;
      flash[:notice] = t('email.sent')
    end

    render(:controller => 'login', :action => 'index')
  end
end
