Advertisement
Guest User

Untitled

a guest
Jul 30th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. ##Controller - users_controller.rb
  2.  
  3. def register
  4. if request.get?
  5. @user = User.new
  6. else
  7. @user = User.new(params[:users])
  8.  
  9. # According to breakpointer, the above line translates into:
  10. # #<User:0x227c100 @password="testt", @email="test@test.net", @new_record=true, @attributes={"installation_id"=>0, "password_hash"=>"", "email"=>""}>
  11.  
  12. if @user.save
  13.  
  14. # According to development.log, the above line generates the following SQL statement
  15. # SQL (0.000303) INSERT INTO users (`installation_id`, `password_hash`, `email`) VALUES(0, '3da541559918a808c2402bba5012f6c60b27661c', '')
  16.  
  17. flash[:notice] = "User #{@user.email} created"
  18. redirect_to(:action => 'index')
  19. end
  20. end
  21. end
  22.  
  23. ##Model - user.rb
  24.  
  25. require 'digest/sha1'
  26.  
  27. class User < ActiveRecord::Base
  28. attr_accessor :password
  29. attr_accessible :email, :password
  30. validates_uniqueness_of :email, :message => "already exists in the database"
  31. validates_presence_of :email, :password
  32. validates_format_of :email, :with => /^([^@\s] )@((?:[-a-z0-9] \.) [a-z]{2,})$/i,
  33. :message => "Invalid email address."
  34.  
  35.  
  36. def self.login(email, password)
  37. password_hash = encrypt(password || "")
  38. find(:first, :conditions => ["email = ? and password_hash = ?", email, password_hash])
  39. end
  40.  
  41. def try_to_login
  42. User.login(self.email, self.password) ||
  43. User.find_by_email_and_password_hash(email, "")
  44. end
  45.  
  46. def before_create
  47. self.password_hash = User.encrypt(self.password)
  48. end
  49.  
  50. def after_create
  51. @password = nil
  52. end
  53.  
  54. private
  55. def self.encrypt(password)
  56. Digest::SHA1.hexdigest(password)
  57. end
  58. end
  59.  
  60. ## view - register.rhtml
  61.  
  62. <%= form_tag %>
  63. <%= error_messages_for 'user' %>
  64. <table>
  65. <tr>
  66. <td><label>Email:</label></td>
  67. <td><%= text_field "users", "email" %></td>
  68. </tr>
  69. <tr>
  70. <td><label>Password:</label></td>
  71. <td><%= password_field "users", "password" %></td>
  72. </tr>
  73. <tr>
  74. <td><label>Password Confirmation:</label></td>
  75. <td><%= password_field "users", "password_confirmation" %></td>
  76. </tr>
  77. <tr>
  78. <td><label>Select an Installation</label></td>
  79. <td><%= collection_select("users", "installation_id", @installation, :id, :basename, {:prompt => "--"})%></td>
  80. </tr>
  81. <tr>
  82. <td></td>
  83. <td style="text-align: right;"><%= submit_tag "Register" %></td>
  84. </tr>
  85. </table>
  86. <%= end_form_tag %>
  87.  
  88. ## Database Schema - (from) Schema.rb
  89.  
  90. create_table "users", :force => true do |t|
  91. t.column "email", :string, :default => "", :null => false
  92. t.column "password_hash", :string, :default => ""
  93. t.column "installation_id", :integer, :default => 0, :null => false
  94. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement