Added Login, authenticate_user, current_user

This commit is contained in:
Saurabh Bhatia 2014-04-14 18:40:17 +08:00
parent 5eae7b8346
commit 3bbb6a482c
21 changed files with 148 additions and 21 deletions

View File

@ -0,0 +1 @@
//= require basic

View File

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View File

@ -0,0 +1,3 @@
// Place all the styles related to the sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View File

@ -1,4 +1,4 @@
class Admin::DashboardsController < ApplicationController
class Admin::DashboardsController < OrbitAdminController
layout "back_end"
def index

View File

@ -1,4 +1,4 @@
class Admin::ItemsController < ApplicationController
class Admin::ItemsController < OrbitAdminController
include OrbitHelper
layout "structure"

View File

@ -3,7 +3,7 @@ class ApplicationController < ActionController::Base
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_action :set_locale
helper_method :current_site
helper_method :current_site, :current_user
def default_url_options(options={})
{ locale: I18n.locale }
@ -24,4 +24,23 @@ class ApplicationController < ActionController::Base
def current_site
@current_site = Site.find_by(site_active: true)
end
private
def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
protected
def authenticate_user
if session[:user_id]
# set current user object to @current_user object variable
@current_user = User.find(session[:user_id])
return true
else
redirect_to new_session_path
return false
end
end
end

View File

@ -0,0 +1,3 @@
class OrbitAdminController < ApplicationController
before_action :authenticate_user
end

View File

@ -0,0 +1,22 @@
class SessionsController < ApplicationController
layout "authentication"
def new
end
def create
user = User.find_by(user_name: params[:user_name])
if user && user.authenticate(params[:password])
session[:user_id] = user.id
redirect_to admin_dashboards_path, :notice => "Logged in!"
else
flash.now.alert = "Invalid email or password"
render "new"
end
end
def destroy
session[:user_id] = nil
redirect_to root_url, :notice => "Logged out!"
end
end

View File

@ -10,8 +10,6 @@ module ApplicationHelper
"/module/#{module_name}/show"
end
def render_menu
# json_file = File.read(File.join(Rails.root, 'public', "menu.json"))
# @items = JSON.parse(json_file)
@ -146,4 +144,5 @@ module ApplicationHelper
def active_for_action(controller_name, action_name)
((controller.controller_name.eql?(controller_name) || request.fullpath.eql?(controller_name)) && controller.action_name.eql?(action_name)) ? 'active' : nil
end
end

View File

@ -0,0 +1,2 @@
module SessionsHelper
end

View File

@ -6,7 +6,7 @@ class User
field :user_name, type: String
field :email, type: String
field :password_digest, type:
field :password_digest, type: String
has_secure_password

View File

@ -6,10 +6,8 @@
<ul class="dropdown-menu" role="menu" aria-labelledby="orbit-start">
<li><%= link_to content_tag(:i, nil, :class => 'icons-gauge') + ' ' + t(:dashboard_), admin_dashboards_path, tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, :class => 'icons-users') + ' ' + t(:member_), tabindex: '-1' %></li>
<% if (is_admin? rescue false) %>
<li class="divider"></li>
<li><%= link_to content_tag(:i, nil, :class => 'icons-cog') + ' ' + t('site.settings'), admin_items_path, tabindex: '-1' %></li>
<% end %>
</ul>
</li>

View File

@ -11,21 +11,23 @@
<li id="orbit-language" class="dropdown">
<a href="#" role="button" class="dropdown-toggle" data-toggle="dropdown" title="<%#= t('site.language')%>"><%= t(:_locale, :locale => I18n.locale) %></a>
<ul class="dropdown-menu" role="menu" aria-labelledby="orbit-language">
<% current_site.in_use_locales.each do |locale| %>
<%= content_tag :li, (link_to t(:_locale, :locale => locale)), :class => (I18n.locale.eql?(locale.to_sym) ? 'active' : nil), tabindex: "-1" %>
<% end %>
</ul>
</li>
<% if current_user.present? %>
<!-- user -->
<li id="orbit-user" class="dropdown">
<a href="#" role="button" class="dropdown-toggle" data-toggle="dropdown">
<%= image_tag 'menber-pic.png', class: "user-pic" %>
<span><%= %></span>
<span><%= current_user.user_name %></span>
</a>
<ul class="dropdown-menu" role="menu" aria-labelledby="orbit-user">
<li><%= link_to content_tag(:i, nil, class: 'icons-tools') + ' ' + t("users.change_passwd"), tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, class: 'icons-vcard') + ' ' + t("users.user_basic_data"), tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, class: 'icons-screen') + ' ' + t(:desktop), tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, class: 'icons-logout') + ' ' + t(:logout), tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, class: 'icons-tools') + ' ' + t("users.change_passwd"), tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, class: 'icons-vcard') + ' ' + t("users.user_basic_data"), tabindex: '-1' %></li>
<li><%= link_to content_tag(:i, nil, class: 'icons-logout') + ' ' + t(:logout), logout_path, tabindex: '-1' %></li>
<li class="divider"></li>
<li><a tabindex="-1" href="#"><i class="icons-lifebuoy"></i> <%= t(:help) %></a></li>
</ul>
@ -37,19 +39,19 @@
<i class="icons-login"></i> <span class="hide"><%= t(:login) %></span>
</a>
</li>
<!-- Log in Modal -->
<!-- Log in Modal -->
<div id="login" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="ModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="ModalLabel"><%= t(:login_orbit) %></h3>
</div>
<div class="modal-body">
<%= form_for :user, url: "", html: {class: 'container'} do |f| %>
<%= form_for :user, url: sessions_path, html: {class: 'container'} do |f| %>
<div class="input-prepend">
<span class="add-on">
<i class="icon-user"></i>
</span>
<%= f.text_field :user_id, class: "input-xlarge" , placeholder: t("users.user_id") %>
<%= f.text_field :user_name, class: "input-xlarge" , placeholder: t("users.user_id") %>
</div>
<div class="input-prepend">
<span class="add-on">
@ -72,4 +74,5 @@
<div class="modal-footer">
</div>
</div>
<% end %>
</ul>

View File

@ -1,9 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<title>RulingOrbit - Administrator</title>
<title><%= current_site.title %> | <%= t('dashboard_') %></title>
<%= render 'shared/meta' %>
<%= render 'shared/google_font' %>
<%= stylesheet_link_tag "back_end", media: "all", "data-turbolinks-track" => true %>
<%= javascript_include_tag "jquery11" %>
<%= javascript_include_tag "back_end" %>
<%= render 'shared/ie_html5_fix' %>
<%= csrf_meta_tags %>
</head>
<body>
@ -11,6 +14,5 @@
<%= render 'layouts/orbit_bar_backend' %>
<%= render 'layouts/side_bar' %>
<%= yield %>
</body>
</html>

View File

@ -0,0 +1,51 @@
<section id="main-wrap">
<div class="sign-in have-other-sign-in">
<% flash.each do |key, msg| %>
<%= content_tag :p, msg, :class => [key, "alert alert-error in fade"] %>
<% end %>
<div class="form">
<h3 class="login-logo">Log In to Orbit</h3>
<div>
<input name="utf8" type="hidden" value="" />
<input name="authenticity_token" type="hidden" value="" />
</div>
<div class="form-block">
<div class="form-list clearfix">
<form class="content" accept-charset="UTF-8" action="/sessions" method="post">
<%= form_tag sessions_path do %>
<div class="control-group clear">
<label for="user_email">
<i class="icon-user"></i>
</label>
<%= text_field_tag :user_name, params[:user_name], :placeholder => t("users.user_id"), :id=>"user_email" %>
</div>
<div class="control-group clear">
<label for="user_password">
<i class="icon-lock"></i>
</label>
<%= password_field_tag :password, :placeholder => t(:dots), :id=>"user_password" %>
</div>
<br/>
<div class="field">
<%= label_tag :remember_me %>
<%= check_box_tag :remember_me, 1, params[:remember_me] %>
</div>
<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
<%= t(:login) %>
<% end %>
</form>
</div>
</div>
<div class="pull-right">
<%= link_to content_tag(:small, t(:forgot_password)) %>
</div>
<br/>
<% end %>
</div>
</div>
</section>

View File

@ -1,2 +1,2 @@
gem 'announcement', git: 'git@gitlab.tp.rulingcom.com:saurabh/announcement-test.git'
gem 'announcement', git: '/Users/saurabhbhatia/announcement-test'
gem 'ad_banner', git: 'git@gitlab.tp.rulingcom.com:saurabh/adbanner-test.git'

View File

@ -41,6 +41,7 @@ en:
dashboard_: Dashboard
desktop: Desktop
homepage: Homepage
password: Password
site:
system_preference: System Preference

View File

@ -1,4 +1,5 @@
zh_tw:
_locale: 中文
more: "更多"
site_: 網站
site_info: 基本資訊
@ -18,6 +19,7 @@ zh_tw:
logout: 登出
dashboard_: 儀表版
desktop: 桌面
password: 密碼
site:
system_preference: 系統狀態

View File

@ -1,5 +1,10 @@
Orbit::Application.routes.draw do
resources :sessions
get "logout", to: "sessions#destroy", as: "logout"
get "/pages/edit_view" => "pages#edit_view"
get "/pages/preview" => "pages#preview"

View File

@ -0,0 +1,9 @@
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
test "should get new" do
get :new
assert_response :success
end
end

View File

@ -0,0 +1,4 @@
require 'test_helper'
class SessionsHelperTest < ActionView::TestCase
end