Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: joshe/iamin
base: c09bb4f5e6
...
head fork: joshe/iamin
compare: df0001076f
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
2  Gemfile.lock
@@ -48,7 +48,7 @@ GEM
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
- mysql2 (0.2.7)
+ mysql2 (0.2.11)
polyglot (0.3.1)
rack (1.2.3)
rack-mount (0.6.14)
View
18 app/controllers/access_controller.rb
@@ -1,12 +1,14 @@
class AccessController < ApplicationController
+ before_filter :confirm_logged_in, :except => [:login, :attempt_login, :logout]
+
def index
menu
render 'menu'
end
def new
- @user = User.new
+ @user = User.new
end
def create
@@ -28,11 +30,14 @@ def login
end
def attempt_login
- authorized_user = User.authenticate(params[:username], params[:password])
+ @incomingController = params[:controller]
+ @incomingAction = params[:action]
+
+ authorized_user = User.authenticate(params[:email], params[:password])
if authorized_user
- # TODO: mark user as logged in
- flash[:message] = "You are now logged in."
- redirect_to :action => 'menu'
+ session[:user_id] = authorized_user.id
+ session[:email] = authorized_user.email
+ redirect_back(:action => 'index')
else
flash[:message] = "Invalid username/password combination"
redirect_to :action => 'login'
@@ -40,7 +45,8 @@ def attempt_login
end
def logout
- # TODO: mark user as logged out
+ session[:user_id] = nil
+ session[:email] = nil
flash[:message] = "You have been logged out."
redirect_to :action => "login"
end
View
31 app/controllers/application_controller.rb
@@ -1,3 +1,34 @@
class ApplicationController < ActionController::Base
protect_from_forgery
+
+ # redirect somewhere that will eventually return back to here
+ def redirect_away(*params)
+ session[:original_uri] = request.request_uri
+ redirect_to(*params)
+ end
+
+ # returns the person to either the original url from a redirect_away or to a default url
+ def redirect_back(*params)
+ uri = session[:original_uri]
+ session[:original_uri] = nil
+ if uri
+ redirect_to uri
+ else
+ redirect_to(*params)
+ end
+ end
+
+ protected
+
+ def confirm_logged_in
+ unless session[:user_id]
+ flash[:message] = "Please log in."
+ redirect_away(:controller => "access", :action => "login")
+ return false # halts the before_filter
+ else
+ @u = User.find(session[:user_id])
+ return true
+ end
+ end
+
end
View
3  app/controllers/events_controller.rb
@@ -1,7 +1,10 @@
class EventsController < ApplicationController
+ before_filter :confirm_logged_in
+
def index
@events = Event.all
+ session[:return_to] ||= request.referer
end
def new
View
4 app/views/access/login.html.haml
@@ -4,8 +4,8 @@
= form_tag :action => "attempt_login" do
%table
%tr
- %td= label_tag(:username)
- %td= text_field_tag(:username)
+ %td= label_tag(:email)
+ %td= text_field_tag(:email)
%tr
%td= label_tag(:password)
%td= password_field_tag(:password)
View
1  app/views/access/menu.html.haml
@@ -2,6 +2,7 @@
.menu
%h2 Admin menu
+ .username= "Logged in as #{session[:email]}"
%ul.identity
%li= link_to "Manage events", events_path
%li= link_to "Manage users", "#"
View
5 app/views/layouts/_header.html.haml
@@ -1 +1,4 @@
-%h1 Header
+%h1 Header
+- unless session[:user_id].nil?
+ .user= "Hi #{@u.first_name}"
+ = link_to 'Logout', :controller => "access", :action => "logout"

No commit comments for this range

Something went wrong with that request. Please try again.