CS 683 Emerging Technologies Fall Semester, 2005 Doc 25 Rails Transactions, Sessions, Filters Nov 29, 2005 Copyright ©, All rights reserved. 2005 SDSU.

Slides:



Advertisements
Similar presentations
Not like the State of Virginia. What is State in ASP.NET? Services (like web services) are Stateless. This means if you make a second request to a server,
Advertisements

Cookies, Sessions. Server Side Includes You can insert the content of one file into another file before the server executes it, with the require() function.
Presenter: James Huang Date: Sept. 29,  HTTP and WWW  Bottle Web Framework  Request Routing  Sending Static Files  Handling HTML  HTTP Errors.
The Librarian Web Page Carol Wolf CS396X. Create new controller  To create a new controller that can manage more than just books, type ruby script/generate.
Forms Authority Database Store Username and Passwords: ASP.NET framework allows you to control access to pages, classes, or methods based on username and.
OWASP WEBGOAT Alaa Darabseh Department of Computer Science
Website Development Registering Users – Introducing Cookies.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 21 Rails Intro Nov 10, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 23 Rails Model Example Nov 17, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
CS 580 Client-Server Programming Spring Semester, 2006 Doc 24 Some Ruby GUI 2 Apr 27, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney,
CS 683 Emerging Technologies Fall Semester, 2005 Doc 24 Rails Database Nov 22, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile.
CS 635 Advanced Object-Oriented Design & Programming Spring Semester, 2006 Doc 2 Terms & Testing Jan 24, 2006 Copyright ©, All rights reserved SDSU.
Session Management A290/A590, Fall /25/2014.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 22 Rails Basic Action Pack & Active Record Nov 15, 2005 Copyright ©, All rights reserved SDSU.
Chapter 10 Maintaining State Information Using Cookies.
Ruby on Rails CSCI 6314 David Gaspar Jennifer Garcia Avila.
E.halFILE 2.2 New Application Features Session II.
Cookies Set a cookie – setcookie() Extract data from a cookie - $_COOKIE Augment user authentication script with a cookie.
CHAPTER 12 COOKIES AND SESSIONS. INTRO HTTP is a stateless technology Each page rendered by a browser is unrelated to other pages – even if they are from.
Securing Ruby on Rails CIS 6939 Web Engineering with Ruby on Rails University of North Florida Stephen Jones 8 July 2007.
CSC 2720 Building Web Applications Cookies, URL-Rewriting, Hidden Fields and Session Management.
Copyright ®xSpring Pte Ltd, All rights reserved Versions DateVersionDescriptionAuthor May First version. Modified from Enterprise edition.NBL.
Online Music Store MSE Project Presentation I Presented by: Reshma Sawant Major Professor: Dr. Daniel Andresen.
Finish configuration cloudclinica root jdbc:postgresql:5432//localhost/cc_db JDBC Url: JDBC Driver: User name: Password: ******** org.postgresql.Driver.
Copyright 2007 © The OWASP Foundation Permission is granted to copy, distribute and/or modify this document under the terms of the OWASP License. The OWASP.
SHOPPING CARTS CHAPTER 19. E-COMMERCE Typically, an e-commerce site will have public pages and admin pages.
1 Dr Alexiei Dingli Web Science Stream Advanced ROR.
Web Programming Language Week 7 Dr. Ken Cosh Security, Sessions & Cookies.
COMP3121 E-Commerce Technologies Richard Henson University of Worcester November 2011.
SSL, Single Sign On, and External Authentication Presented By Jeff Kelley April 12, 2005.
Chapter 8 Cookies And Security JavaScript, Third Edition.
Week seven CIT 354 Internet II. 2 Objectives Database_Driven User Authentication Using Cookies Session Basics Summary Homework and Project 2.
Lecture 8 – Cookies & Sessions SFDV3011 – Advanced Web Development 1.
© Copyright IBM Corporation 2007 AP/Americas April 15-18, 2007 Anaheim, California Introduction to RubyOnRails - a J2EE replacement? Russell Scheerer –
Cookies Web Browser and Server use HTTP protocol to communicate and HTTP is a stateless protocol. But for a commercial website it is required to maintain.
1 Chapter 9 – Cookies, Sessions, FTP, and More spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science.
Cookies & Session Web Technology
InfoBox A Personal Information Manager Built with Ruby on Rails Dustin Martin.
CS 580 Client-Server Programming Spring Semester, 2006 Doc 16 Some Parsing Mar 30, 2006 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
COOKIES and SESSIONS. COOKIES A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each.
CS 683 Emerging Technologies Fall Semester, 2005 Doc 19 Ruby Regexp, Expression, Exceptions, Modules Nov 8, 2005 Copyright ©, All rights reserved
Cookies and Sessions IDIA 618 Fall 2014 Bridget M. Blodgett.
Google App Engine MemCache ae-09-session
Create, Update and Delete Carol Wolf Computer Science.
CIS 451: Cookies Dr. Ralph D. Westfall February, 2009.
Web2.0 Secure Development Practice Bruce Xia
CS 683 Emerging Technologies Fall Semester, 2005 Doc 26 Rails Miscellaneous Dec 1, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500.
CS 142 Lecture Notes: FormsSlide 1 Simple Form Product: Price:
1.NET Web Forms Applications: Main Form © 2002 by Jerry Post.
ITM © Port,Kazman 1 ITM 352 Cookies. ITM © Port,Kazman 2 Problem… r How do you identify a particular user when they visit your site (or any.
©SoftMooreSlide 1 Cookies. ©SoftMooreSlide 2 Cookies Basic idea –web application sends a simple name/value pair to the client –when the client connects.
CS 160 and CMPE/SE 131 Software Engineering February 9 Class Meeting Department of Computer Science Department of Computer Engineering San José State University.
Cookies and Sessions in PHP. Arguments for the setcookie() Function There are several arguments you can use i.e. setcookie(‘name’, ‘value’, expiration,
Unit-6 Handling Sessions and Cookies. Concept of Session Session values are store in server side not in user’s machine. A session is available as long.
COOKIES AND SESSIONS.
Cookies Tutorial Cavisson Systems Inc..
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
ITM 352 Cookies.
Web Programming Language
What is Cookie? Cookie is small information stored in text file on user’s hard drive by web server. This information is later used by web browser to retrieve.
Web Systems Development (CSC-215)
New User Guide Learning how to use your NxPay Account
Web Systems Development (CSC-215)
Cookies Cookie :- A cookie is often used to identify a user. A cookie is often used to identify a user. A cookie is a small file that the server embeds.
CS 142 Lecture Notes: Cookies
Model – View – Controller Pattern
HTML5 and Local Storage.
C/S Windows Overview Nigel Pilsbury.
Technical Integration Guide
Web Programming Language
Presentation transcript:

CS 683 Emerging Technologies Fall Semester, 2005 Doc 25 Rails Transactions, Sessions, Filters Nov 29, 2005 Copyright ©, All rights reserved SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA USA. OpenContent ( license defines the copyright on this document.

2 References Agile Web Development with Rails, Thomas & Hanson, The Pragmatic Bookshelf, 2005 Active Record Documentation,

3 Transactions Simple Bank Account idnumberbalance 1a100 2b200 accounts class Account < ActiveRecord::Base def withdraw(amount) balance_and_save(-amount) end def deposit(amount) balance_and_save(amount) end def balance_and_save(amount) self.balance += amount save! end def validate errors.add(:balance, "is negative") if balance < 0 end

4 Successful Transaction class BookTest < Test::Unit::TestCase fixtures :accounts def = = Account.find_by_number('b') end def do end assert_equal assert_equal end a_account: id: 1 number: a balance: b_account: id: 2 number: b balance: test/unit/account_test.rbtest/fixtures/accounts.yml

5 Rolled Back Transaction def test_invalid_transaction assert_raise(ActiveRecord::RecordInvalid) do end } assert_equal assert_equal end a_account: id: 1 number: a balance: b_account: id: 2 number: b balance: test/fixtures/accounts.ymltest/unit/account_test.rb

6 Local Verses Database Values def test_no_local_rollback assert_raise(ActiveRecord::RecordInvalid) { end } assert_equal assert_equal end a_account: id: 1 number: a balance: b_account: id: 2 number: b balance: test/fixtures/accounts.ymltest/unit/account_test.rb

7 Cached Local Value def test_invalid_transaction assert_raise(ActiveRecord::RecordInvalid) do end } assert_equal assert_equal end a_account: id: 1 number: a balance: b_account: id: 2 number: b balance: test/fixtures/accounts.ymltest/unit/account_test.rb

8 Databases & Rails idnumberbalance 1a100 2b200 object a id: 1 number: a balance: 100 Multiple Ruby threads can have copies of same data Database and Ruby thread data can become inconsistent The longer Ruby code holds a value the higher the chance of inconsistencies

9 Cookies class CookieController < ApplicationController def setting_cookies cookies[:name] = 'Sam' cookies[:time] = Time.now.to_s cookies[:special] = { :value => "good customer", :expires => 30.days.from_now, :path => "/cat"} redirect_to :action => "reading_cookies" end def reading_cookies cookie_time = cookies[:time] foo = cookies[:foo] render(:text => "Time = #{cookie_time}, foo = #{foo}") end Cookie Options :domain browser sends cookie only to pages in the given domain :expires When the cookie is no longer valid :path browser sends cookie only to pages whose leading part of the request path matches given path :secure browser only sends cookie if request uses https :value value of the cookie, must be a string

10 Sessions Adding data to session session[:foo] = 'bar' Accessing session data data = session[:foo] Session id stored in cookie Session data stored on server PStore ActiveRecord MemoryStore FileStore Can store serializable objects Clearing old session data an issue

11 Session Example class StoreController < ApplicationController model :cart before_filter :find_cart def add_to_cart product = product.price) end private def = (session[:cart] ||= Cart.new) end class Cart attr_reader :items attr_reader :total def initialize empty! end def add_product(name, << += price end def = = 0.0 end Example modified from Agile Web Development with Rails

12 Filters Before Called before any action After Called after each action Around Both before and after an action Filters can be method blocks class AdminController < ApplicationController before_filter do |controller| logger.info("Performing #{controller.action_name}") end def index list render :action => 'list' end Before filter block Output in log/development.log Already contains useful log data Example modified from Agile Web Development with Rails

13 Around Filter class TimingFilter def = Time.now end def after(controller) elapsed = Time.now action = controller.action_name controller.logger.info("#{action} took #{elapsed} seconds to run") end class AdminController < ApplicationController around_filter TimingFilter.new def index list render :action => 'list' end app/models/timing_filter.rb app/controllers/admin_controller.rb Example modified from Agile Web Development with Rails

14 before filter class AdminController < ApplicationController before_filter :authorize, :except => :login def index list render :action => 'list' end def authorize unless session[:user] redirect_to(:action => "login") end def login if request.get? session[:user] = nil else if params[:user][:name] == 'fred' session[:user] = 'fred' redirect_to(:action => "index") else flash[:notice] = "Invalid user" end Login 'login' %> Username views/admin/login.rhtml Example modified from Agile Web Development with Rails