Guest User

Untitled

a guest
Mar 2nd, 2018
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1.  
  2. require 'authenticated_user'
  3.  
  4. class User < WebFront
  5. include LoginEngine::AuthenticatedUser
  6. validates_captcha :on => :create
  7. has_many :comments
  8. has_many :forums
  9. has_many :prefrences
  10. has_one :user_pin
  11. has_many :portfolios
  12. end
  13.  
  14.  
  15. # authenticated_user.rb
  16.  
  17. require 'digest/sha1'
  18.  
  19. # this model expects a certain database layout and its based on the name/login pattern.
  20.  
  21. # how does authenticate works here
  22. # you pass the password to the model and using username it picks the password and corresponding salt
  23. # for that user. Both are already encrypted and it used method self.hashed to hash the thing and
  24. # compare against the value stored in the database.
  25.  
  26. module LoginEngine
  27. module AuthenticatedUser
  28.  
  29. def self.included(base)
  30. base.class_eval do
  31.  
  32. # use the table name given
  33. attr_accessor :new_password
  34.  
  35.  
  36. validates_presence_of :firstname
  37. validates_presence_of :lastname
  38. validates_presence_of :country
  39.  
  40.  
  41. validates_presence_of :city
  42. validates_presence_of :pincode
  43. validates_presence_of :state
  44.  
  45. validates_length_of :login, :within => 3..40
  46. validates_length_of :addr_line1, :within => 4..40
  47.  
  48. validates_uniqueness_of :login
  49. validates_uniqueness_of :email
  50. validates_format_of :email, :with => /^[^@]+@.+$/
  51. #validates_numericality_of :pincode
  52. validates_presence_of :password, :if => :validate_password?
  53.  
  54. validates_confirmation_of :password, :if => :validate_password?
  55. validates_length_of :password, { :minimum => 5, :if => :validate_password? }
  56. validates_length_of :password, { :maximum => 40, :if => :validate_password? }
  57.  
  58.  
  59. protected
  60. attr_accessor :password, :password_confirmation
  61. after_save :falsify_new_password
  62. after_validation :crypt_password
  63.  
  64. end
  65. base.extend(ClassMethods)
  66. end
  67.  
  68. # COntroller code:
  69. def signup_user
  70. @user = User.new(params[:user])
  71. @user.new_password = true
  72. @user.verified = 1 unless $account_info[:use_email_notification]
  73.  
  74. if @user.save
  75. key = @user.generate_security_token
  76. url = url_for(:action => 'verify_users', :user_id => @user.id, :key => key)
  77.  
  78. flash[:success] = 'Signup successful!'
  79. if $account_info[:use_email_notification]
  80. UserNotify.deliver_signup(@user, params[:user][:password], url)
  81. flash[:success] << ' Please check your registered email account to verify your account registration and continue with login.'
  82. else
  83. flash[:success] << ' Please log in.'
  84. end
  85. redirect_to :action => 'login'
  86. else
  87. logger.info "Errors while submitting the page"
  88. flash[:invalid] = @user.errors.full_messages
  89. $stock.log_error flash[:invalid]
  90. render :partial => "signup_form",:status => 444
  91. end
  92.  
  93. @user[:captcha_validation] = ""
  94. end
Add Comment
Please, Sign In to add comment