aboutsummaryrefslogtreecommitdiffstats
path: root/doc
diff options
context:
space:
mode:
authorSven Moritz Hallberg <sm@khjk.org>2008-02-14 19:43:47 +0100
committerSven Moritz Hallberg <sm@khjk.org>2008-02-14 19:43:47 +0100
commita161d33779bb56fabe6466f15a8ae98881f55520 (patch)
tree883cff1b38e8533b23438bd311b8bd42fc892e4c /doc
parent1a1faa077c3c4ffc2bfe88a4466d748919fcdc45 (diff)
interpret &apos;
Diffstat (limited to 'doc')
0 files changed, 0 insertions, 0 deletions
Unnamed repository; edit this file 'description' to name the repository.MimesBrønn
aboutsummaryrefslogtreecommitdiffstats
path: root/app/models/post_redirect.rb
blob: a2c0d39a39ac06e9660848b37ffdf04f437cc1b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# == Schema Information
# Schema version: 52
#
# Table name: post_redirects
#
#  id                 :integer         not null, primary key
#  token              :text            not null
#  uri                :text            not null
#  post_params_yaml   :text            
#  created_at         :datetime        not null
#  updated_at         :datetime        not null
#  email_token        :text            not null
#  reason_params_yaml :text            
#  user_id            :integer         
#  circumstance       :text            default("normal"), not null
#

# models/post_redirect.rb:
# Saves an HTTP request, so it can be redirected to later.  For example, after
# registering / logging in. This can save POST requests, if post_params_yaml
# is not null.
#
# See check_in_post_redirect in controllers/application.rb for the hack that
# fakes the redirect to include POST parameters in request later.
#
# Copyright (c) 2007 UK Citizens Online Democracy. All rights reserved.
# Email: francis@mysociety.org; WWW: http://www.mysociety.org/
#
# $Id: post_redirect.rb,v 1.30 2008-04-21 16:44:06 francis Exp $

require 'openssl' # for random bytes function

class PostRedirect < ActiveRecord::Base
    # Optional, does a login confirm before redirect for use in email links.
    belongs_to :user

    # We store YAML version of POST parameters in the database
    def post_params=(params)
        self.post_params_yaml = params.to_yaml
    end
    def post_params
        if self.post_params_yaml.nil?   
            return {}
        end
        YAML.load(self.post_params_yaml)
    end

    # We store YAML version of textual "reason for redirect" parameters
    def reason_params=(reason_params)
        self.reason_params_yaml = reason_params.to_yaml
    end
    def reason_params
        YAML.load(self.reason_params_yaml)
    end

    # Extract just local path part, without domain or #
    def local_part_uri
        self.uri.match(/^http:\/\/.+?(\/[^#]+)/)
        return $1
    end

    # Makes a random token, suitable for using in URLs e.g confirmation messages.
    def self.generate_random_token
        bits = 12 * 8
        # Make range from value to double value, so number of digits in base 36
        # encoding is quite long always.
        rand_num = rand(max = 2**(bits+1)) + 2**bits
        rand_num.to_s(base=36)
    end

    # Make the token 
    def after_initialize
        # The token is used to return you to what you are doing after the login form.
        if not self.token
            self.token = PostRedirect.generate_random_token
        end
        # There is a separate token to use in the URL if we send a confirmation email.
        if not self.email_token
            self.email_token = PostRedirect.generate_random_token
        end
    end

    # Used by (rspec) test code only
    def self.get_last_post_redirect
        # XXX yeuch - no other easy way of getting the token so we can check
        # the redirect URL, as it is by definition opaque to the controller
        # apart from in the place that it redirects to.
        post_redirects = PostRedirect.find_by_sql("select * from post_redirects order by id desc limit 1")
        post_redirects.size.should == 1
        return post_redirects[0]
    end

    # Called from cron job delete-old-sessions
    def self.delete_old_post_redirects
        PostRedirect.delete_all "now() - updated_at > '1 year'"        
    end

end