Trying to get authlogic running

This commit is contained in:
Christoph Haas 2015-07-13 18:42:36 +02:00
parent 06f09c0486
commit 737ff62cfc
23 changed files with 306 additions and 4 deletions

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 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 user_sessions controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

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

View file

@ -5,6 +5,12 @@ class ApplicationController < ActionController::Base
before_filter :create_user_token
# Do not mention passwords in the log file
# filter_parameter_logging :password
# AuthLogic
helper_method :current_user_session, :current_user
# Assign the visitor a unique hash so that we can track their uploads
# and show them even before moderation.
def create_user_token
@ -12,4 +18,46 @@ class ApplicationController < ActionController::Base
session[:ip] ||= request.remote_ip
end
# -----------------
private
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.user
end
def require_user
logger.debug "ApplicationController::require_user"
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
def require_no_user
logger.debug "ApplicationController::require_no_user"
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
# redirect_to home_index_path
return false
end
end
def store_location
#session[:return_to] = request.request_uri
end
def redirect_back_or_default(default)
redirect_to(session[:return_to] || default)
session[:return_to] = nil
end
end

View file

@ -0,0 +1,22 @@
class UserSessionsController < ApplicationController
def new
@user_session = UserSession.new
end
def create
@user_session = UserSession.new(params[:user_session])
if @user_session.save
flash[:notice] = "Login successful!"
redirect_back_or_default account_url(@current_user)
else
render :action => :new
end
end
def destroy
current_user_session.destroy
flash[:notice] = "Logout successful!"
redirect_back_or_default new_user_session_url
end
end

View file

@ -0,0 +1,43 @@
class UsersController < ApplicationController
before_filter :require_user, :only => [:show, :edit, :update]
def new
@user = User.new
end
def create
@user = User.new(params[:user])
# Saving without session maintenance to skip
# auto-login which can't happen here because
# the User has not yet been activated
if @user.save
flash[:notice] = "Your account has been created."
redirect_to signup_url
else
flash[:notice] = "There was a problem creating you."
render :action => :new
end
end
def show
@user = current_user
end
def edit
@user = current_user
end
def update
@user = current_user # makes our views "cleaner" and more consistent
if @user.update_attributes(params[:user])
flash[:notice] = "Account updated!"
redirect_to account_url
else
render :action => :edit
end
end
end

View file

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

View file

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

View file

@ -1,2 +1,5 @@
class User < ActiveRecord::Base
acts_as_authentic do |c|
c.login_field = 'email'
end
end

View file

@ -0,0 +1,7 @@
# User sessions. Refers to the "User" class automatically (by its name).
# See also: https://github.com/binarylogic/authlogic
class UserSession < Authlogic::Session::Base
# specify configuration here, such as:
# logout_on_timeout true
# ...many more options in the documentation
end

View file

@ -0,0 +1,6 @@
- if target.errors.any?
div id="errorExplanation"
h2 Errors
ul
- target.errors.full_messages.each do |msg|
li = msg

View file

@ -0,0 +1,15 @@
- raise @user_session
- form_for @user_session, :as => :user_session, :url => { :action => "create" } do |f|
= render "shared/error_messages", :target => @user_session
= f.label :email
br
= f.text_field :email
br
= f.label :password
br
= f.password_field :password
br
= f.check_box :remember_me
= f.label :remember_me
br
= f.submit "Login"

View file

@ -0,0 +1,20 @@
= render "shared/error_messages", :target => @user
p
= form.label :name %
br
= form.text_field :name
p
= form.label :email
br
= form.text_field :email
p
= form.label :password, form.object.new_record? ? nil : "Change password"
br
= form.password_field :password
p
= form.label :password_confirmation
br
= form.password_field :password_confirmation
= link_to 'Edit Account', edit_account_path

28
app/views/users/show.slim Normal file
View file

@ -0,0 +1,28 @@
p
b Email:
=h @user.email
p
b Login count:
=h @user.login_count
p
b Last request at:
=h @user.last_request_at
p
b Last login at:
=h @user.last_login_at
p
b Current login at:
=h @user.current_login_at
p
b Last login ip:
=h @user.last_login_ip
p
b Current login ip:
=h @user.current_login_ip

View file

@ -21,6 +21,18 @@ Debshots::Application.routes.draw do
get 'about' => 'welcome#about'
get 'thumbnail/:name' => 'packages#thumbnail', as: :thumbnail_image, name: /[^\/]+/
# Authentication
resources :user_sessions
get 'login' => "user_sessions#new", :as => :login
get 'logout' => "user_sessions#destroy", :as => :logout
resources :users # give us our some normal resource routes for users
resource :user, :as => 'account' # a convenience route
#match 'signup' => 'users#new', :as => :signup
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

View file

@ -0,0 +1,23 @@
class ChangeUsersToAuthlogic < ActiveRecord::Migration
def change
change_table :users do |t|
# Remove the password column that was previously introduced
t.remove :password
# Create AuthLogic fields
t.string :password_salt, :null => false # optional, but highly recommended
t.string :persistence_token, :null => false # required
# Magic columns, just like ActiveRecord's created_at and updated_at. These are automatically maintained by Authlogic if they are present.
t.integer :login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
t.integer :failed_login_count, :null => false, :default => 0 # optional, see Authlogic::Session::MagicColumns
t.datetime :last_request_at # optional, see Authlogic::Session::MagicColumns
t.datetime :current_login_at # optional, see Authlogic::Session::MagicColumns
t.datetime :last_login_at # optional, see Authlogic::Session::MagicColumns
t.string :current_login_ip # optional, see Authlogic::Session::MagicColumns
t.string :last_login_ip # optional, see Authlogic::Session::MagicColumns
end
change_column :packages, :long_description, :text
end
end

View file

@ -0,0 +1,13 @@
class CreateUserSessions < ActiveRecord::Migration
def change
create_table :user_sessions do |t|
t.string :session_id, :null => false
t.text :data
t.timestamps
end
add_index :user_sessions, :session_id
add_index :user_sessions, :updated_at
end
end

View file

@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20150628113425) do
ActiveRecord::Schema.define(version: 20150713155458) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -64,12 +64,30 @@ ActiveRecord::Schema.define(version: 20150628113425) do
add_index "screenshots", ["id", "approved"], name: "id_approved", using: :btree
add_index "screenshots", ["id", "uploaderhash"], name: "id_uploaderhash", using: :btree
create_table "user_sessions", force: :cascade do |t|
t.string "session_id", null: false
t.text "data"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "user_sessions", ["session_id"], name: "index_user_sessions_on_session_id", using: :btree
add_index "user_sessions", ["updated_at"], name: "index_user_sessions_on_updated_at", using: :btree
create_table "users", force: :cascade do |t|
t.text "email"
t.text "realname"
t.text "password"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_salt", null: false
t.string "persistence_token", null: false
t.integer "login_count", default: 0, null: false
t.integer "failed_login_count", default: 0, null: false
t.datetime "last_request_at"
t.datetime "current_login_at"
t.datetime "last_login_at"
t.string "current_login_ip"
t.string "last_login_ip"
end
add_foreign_key "screenshots", "packages", name: "screenshots_package_id_fkey"

View file

@ -0,0 +1,7 @@
require "test_helper"
class UserSessionsControllerTest < ActionController::TestCase
def test_sanity
flunk "Need real tests"
end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class UsersControllerTest < ActionController::TestCase
def test_sanity
flunk "Need real tests"
end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class UserSessionsHelperTest < ActionView::TestCase
def test_sanity
flunk "Need real tests"
end
end

View file

@ -0,0 +1,7 @@
require "test_helper"
class UsersHelperTest < ActionView::TestCase
def test_sanity
flunk "Need real tests"
end
end