Skip to content
Snippets Groups Projects
Commit 2f64fe93 authored by Per Cederqvist's avatar Per Cederqvist
Browse files

Handle login/logout.

Use a user_id cookie to store the login ID.  Added a global logout action.
Added users/login.
parent 32b01a4f
Branches
Tags
No related merge requests found
# Filters added to this controller will be run for all controllers in the application. # Filters added to this controller will be run for all controllers in the application.
# Likewise, all the methods added will be available for all controllers. # Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base class ApplicationController < ActionController::Base
def logout
cookies.delete :user_id
flash[:notice] = "You have been logged out"
redirect_to :controller => "users", :action => "login"
end
end end
...@@ -13,10 +13,17 @@ class RecordingsController < ApplicationController ...@@ -13,10 +13,17 @@ class RecordingsController < ApplicationController
end end
def list_owned def list_owned
@owner = User.find(params[:user_id]) user_id = cookies[:user_id]
if user_id.nil?
logger.add(Logger::DEBUG, "No user_id cookie found")
redirect_to :controller => "users", :action => "login"
return
end
@owner = User.find(user_id)
@recording_pages, @recordings = paginate :recordings, @recording_pages, @recordings = paginate :recordings,
:per_page => 10, :per_page => 10,
:conditions => [ "(user_id = ? OR user_id IS NULL)", params[:user_id]], :conditions => [ "(user_id = ? OR user_id IS NULL)", user_id],
:order => "updated_on" :order => "updated_on"
end end
......
...@@ -48,4 +48,25 @@ class UsersController < ApplicationController ...@@ -48,4 +48,25 @@ class UsersController < ApplicationController
User.find(params[:id]).destroy User.find(params[:id]).destroy
redirect_to :action => 'list' redirect_to :action => 'list'
end end
def login
logger.add(Logger::DEBUG, "This is a log message!")
@user_pages, @users = paginate :users, :per_page => 10
if params[:id].nil?
return
end
user = User.find(params[:id])
if user.nil?
flash.now[:notice] = "User id " + params[:id] + " not found"
return
end
cookies[:user_id] = params[:id]
flash[:notice] = "Welcome, " + user.username
redirect_to :controller => "recordings", :action => "list_owned"
end
end end
<h1>Login</h1>
<p>Welcome! Who are you?</p>
<% for user in @users %>
<% form_for :user, @user do |f| %>
<input type="hidden" name="id" value="<%= user.id %>" />
<input type="submit" value="<%= user.username %>" />
<% end %>
<% end %>
<%= link_to 'Previous page', { :page => @user_pages.current.previous } if @user_pages.current.previous %>
<%= link_to 'Next page', { :page => @user_pages.current.next } if @user_pages.current.next %>
<br />
<%= link_to 'New user', :action => 'new' %>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment