Guest User

Untitled

a guest
Apr 12th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. USER MODEL :::
  2.  
  3. require 'digest/md5'
  4. class User < ActiveRecord::Base
  5.  
  6. attr_accessor :password_confirmation
  7.  
  8. validates_presence_of :username, :password, :email, :zipcode, :firstname, :lastname
  9.  
  10. validates_uniqueness_of :username, :email
  11.  
  12. validates_length_of :username, :within => 3..20
  13. validates_length_of :password, :within => 4..20
  14. validates_length_of :firstname, :within => 4..20
  15. validates_length_of :lastname, :within => 4..20
  16. validates_length_of :zipcode, :maximum => 5
  17. validates_length_of :email, :maximum => 40
  18.  
  19. validates_format_of :username, :with => /^[A-Z0-9_]*$/i, :message => "Username can only contain numbers, letters and underscore"
  20. validates_format_of :email, :with => /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i , :message => "Email address was not properly formated"
  21. validates_confirmation_of :password
  22.  
  23. def self.authenticate(login,pass)
  24. self.find_by_username_and_password(login, md5(pass))
  25. end
  26.  
  27. protected
  28.  
  29. def password=(val)
  30. self[:password] = self.class.md5(val) unless val.blank?
  31. end
  32.  
  33. def self.md5(pass)
  34. Digest::MD5.hexdigest("--golf-time--#{pass}---")
  35. end
  36.  
  37.  
  38. end
  39.  
  40. USER CONTROLLER ::::
  41. class UserController < ApplicationController
  42.  
  43. def register
  44. @user = User.new(params[:user])
  45. if request.post? and @user.save
  46. flash[:notice] = "User has been added"
  47. redirect_to :action => 'login'
  48. end
  49. end #register
  50.  
  51. def login
  52. @user = User.new(params[:user])
  53. if request.post? and params[:user]
  54. user = User.authenticate(@user.username, @user.password)
  55. if user
  56. session[:userid] = user.id
  57. flash[:notice] = "you have been logged in"
  58. redirect_to :action => 'edit'
  59. else
  60. @user.password = nil
  61. flash[:notice] = "invalid username and/or password"
  62. end
  63.  
  64. end
  65. end #login
  66.  
  67. def edit
  68. @user = User.find(session[:userid])
  69.  
  70. if request.post? and params[:user]
  71. @user.update_attributes(params[:user]);
  72. flash[:notice] = 'user info has been updated'
  73. end
  74.  
  75. end
  76. end
  77.  
  78.  
  79. FORM ERRORS :::
  80.  
  81. 2 errors prohibited this user from being saved
  82.  
  83. There were problems with the following fields:
  84.  
  85. * Password doesn't match confirmation
  86. * Password is too long (maximum is 20 characters)
Add Comment
Please, Sign In to add comment