appoxy_sessions 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1 @@
1
+ Nada.
@@ -0,0 +1,3 @@
1
+ require 'simple_record'
2
+ require File.join(File.dirname(__FILE__), "sessions", "user")
3
+
@@ -0,0 +1,57 @@
1
+ module Appoxy
2
+
3
+ module Sessions
4
+ module ApplicationController
5
+
6
+
7
+ def logout_keeping_session!
8
+ @current_user = nil # not logged in, and don't do it for me
9
+ session[:user_id] = nil # keeps the session but kill our variable
10
+ end
11
+
12
+
13
+ def logged_in?
14
+ #puts 'logged_in??'
15
+ #puts 'current_user=' + current_user.inspect
16
+ current_user
17
+ end
18
+
19
+
20
+ def current_user=(new_user)
21
+ session[:user_id] = new_user ? new_user.id : nil
22
+ @current_user = new_user
23
+ end
24
+
25
+
26
+ def current_user
27
+ @current_user ||= (login_from_session)
28
+ @current_user
29
+ end
30
+
31
+
32
+ def login_from_session
33
+ #puts 'Login from session=' + session[:user_id].inspect
34
+ User.find_by_id(session[:user_id]) if session[:user_id]
35
+ end
36
+
37
+
38
+ helper_method :logged_in?
39
+ helper_method :current_user
40
+
41
+
42
+ protected
43
+
44
+ #
45
+ def random_string(length=10)
46
+ chars = 'abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'
47
+ password = ''
48
+ length.times { password << chars[rand(chars.size)] }
49
+ password
50
+ end
51
+
52
+
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,85 @@
1
+ module Appoxy
2
+
3
+ module Sessions
4
+ module SessionsController
5
+
6
+ def create
7
+ logout_keeping_session!
8
+
9
+ #puts 'params=' + params.inspect
10
+ @email = params[:email]
11
+ @has_password = params[:has_password]
12
+ #puts 'has_pass? ' + @has_password.inspect
13
+
14
+ if params[:has_password].blank?
15
+ flash[:error] = "Please click the radio button to let us know if you have a password or not."
16
+ render :action=>"new"
17
+ return
18
+ end
19
+
20
+ if @has_password == "true"
21
+ user = User.authenticate(@email, params[:password])
22
+
23
+ if user
24
+ self.current_user = user
25
+ flash[:info] = "Logged in successfully."
26
+ orig_url = session[:return_to]
27
+ puts 'orig_url = ' + orig_url.to_s
28
+ session[:return_to] = nil
29
+ if !orig_url.nil?
30
+ redirect_to orig_url # if entered via a different url
31
+ else
32
+ redirect_to :controller=>"projects"
33
+ end
34
+ user.last_login = Time.now
35
+ user.save(:dirty=>true)
36
+ else
37
+ flash[:info] = "Invalid email or password. Please try again."
38
+ render :action => 'new'
39
+ end
40
+ else
41
+ # new user
42
+
43
+ redirect_to (new_user_path + "?email=" + @email)
44
+ end
45
+
46
+ end
47
+
48
+
49
+ def reset_password
50
+
51
+ unless verify_recaptcha
52
+ flash[:error] = "You are not human! Please try again."
53
+ render :action=>"forgot_password"
54
+ return
55
+ end
56
+
57
+ @email = params[:email]
58
+ unless User.email_is_valid? @email
59
+ flash[:error] = "You must enter a valid email."
60
+ render :action=>"forgot_password"
61
+ return
62
+ end
63
+
64
+ @user = User.find_by_email(@email)
65
+ unless @user
66
+ flash[:error] = "Email not found."
67
+ render :action=>"forgot_password"
68
+ return
69
+ end
70
+
71
+ newpass = random_string(8)
72
+
73
+ @user.password = newpass
74
+ @user.save(:dirty=>true)
75
+
76
+ Mailer.deliver_reset_password(@user, newpass)
77
+
78
+ flash[:success] = "Password reset. You should receive an email shortly with a new password."
79
+ redirect_to :action=>"new"
80
+
81
+ end
82
+
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,62 @@
1
+ module Appoxy
2
+
3
+ module Sessions
4
+
5
+ class User < SimpleRecord::Base
6
+
7
+ has_strings :email,
8
+ {:name => :password, :hashed=>true},
9
+ :first_name,
10
+ :last_name,
11
+ :remember_me,
12
+ :activation_code,
13
+ :status # invited, active
14
+
15
+ has_dates :last_login
16
+
17
+
18
+ def validate
19
+ errors.add("email", "is not valid") unless User.email_is_valid?(email)
20
+
21
+ if status == "invited"
22
+ # doesn't need password
23
+ else
24
+ errors.add("password", "must be at least 6 characters long.") if password.blank?
25
+ end
26
+ end
27
+
28
+
29
+ def self.email_is_valid?(email)
30
+ return email.present? && email =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
31
+ end
32
+
33
+
34
+ def is_active?
35
+ true#activation_code == nil
36
+ end
37
+
38
+
39
+ def self.authenticate(email, password)
40
+ #RAILS_DEFAULT_LOGGER.info "-------authenticating password------"
41
+
42
+ u = self.find :first, :conditions => ["email = ?", email]
43
+ return nil unless u
44
+ return nil unless u.is_active?
45
+ return nil if u.attributes["password"].nil? # if the user has no password (will this happen? maybe for invites...)
46
+
47
+ # This is a normal unencrypted password
48
+ if u.attributes["password"][0].length < 100
49
+ u.password = u.attributes["password"][0]
50
+ u.save
51
+ end
52
+
53
+ (u.password == password) ? u : nil
54
+ end
55
+
56
+
57
+ end
58
+
59
+ end
60
+
61
+ end
62
+
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: appoxy_sessions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Travis Reeder
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-02 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: simple_record
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: recaptcha
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ description: Appoxy Sessions gem description...
36
+ email: travis@appoxy.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.markdown
43
+ files:
44
+ - lib/appoxy_sessions.rb
45
+ - lib/sessions/application_controller.rb
46
+ - lib/sessions/sessions_controller.rb
47
+ - lib/sessions/user.rb
48
+ - README.markdown
49
+ has_rdoc: true
50
+ homepage: http://www.appoxy.com
51
+ licenses: []
52
+
53
+ post_install_message:
54
+ rdoc_options:
55
+ - --charset=UTF-8
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ version:
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.5
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Appoxy Sessions gem
77
+ test_files: []
78
+