Sha256: 2100ca8c5f7d0d075fd07226b161bc98085ef960ee7038e8bd2bdb2883244de1

Contents?: true

Size: 1.84 KB

Versions: 3

Compression:

Stored size: 1.84 KB

Contents

module Symphonia
  class LoginController < ApplicationController

    before_action { menu_item(:login) }

    def index
      if current_model.constantize.current.logged_in?
        return destroy
      else
        return new
      end
    end

    def new
      if current_model.constantize.current.logged_in?
        return redirect_to(after_login_path)
      end
      @model_session = "#{current_model}Session".constantize.new
      render :new
    end

    def create
      @model_session = "#{current_model}Session".constantize.new(login_session_params)
      if @model_session.save
        current_model.constantize.current = @model_session.user
        redirect_to after_login_path
      else
        render :new
      end
    end

    def login_session_params
      params.require(:login_session).permit(:login, :password, :remember_me).to_h
    end

    def destroy
      current_session && current_session.destroy
      redirect_to '/', notice: t(:text_successfully_logout)
    end

    private

    def current_model
      raise NotImplementedError
    end

    def current_session
      raise NotImplementedError
    end

    def after_login_path
      path = session[:return_to].presence
      path ||= Symphonia.config.after_login_path.presence
      path ||= get_first_menu_item_path(Symphonia::MenuManager.menu(:top_menu).values) || :admin_path
      case path
        when Symbol
          main_app.send(path)
        when Proc
          path.call(self)
        else
          path
      end
    end

    def get_first_menu_item_path(menu_items)
      menu_items.detect do |item|
        if (submenu = item[:children]).present?
          if (subitem = get_first_menu_item_path(submenu.values))
            return subitem
            break
          end
        else
          item[:if].nil? || item[:if].call
        end
      end.try(:[], :url)
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
symphonia-2.2.1 app/controllers/symphonia/login_controller.rb
symphonia-2.1.8 app/controllers/symphonia/login_controller.rb
symphonia-2.1.7 app/controllers/symphonia/login_controller.rb