Guest User

Untitled

a guest
Apr 19th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1.  
  2. # cat user.rb
  3.  
  4. require 'digest/sha1'
  5. class User < ActiveRecord::Base
  6. #
  7. # Ads.
  8. #
  9. # Virtual attribute for the unencrypted password.
  10. #
  11. attr_accessor :password
  12. validates_presence_of :email
  13. validates_presence_of :password, :if => :password_required?
  14. validates_presence_of :password_confirmation, :if => :password_required?
  15. validates_length_of :password, :within => 4..40, :if => :password_required?
  16. validates_confirmation_of :password, :if => :password_required?
  17. validates_length_of :email, :within => 3..100
  18. validates_uniqueness_of :email, :case_sensitive => false
  19. before_save :encrypt_password
  20. #
  21. # Authenticate user by her e-mail and unencrypted password.
  22. #
  23. def self.authenticate(email, password)
  24. u = find_by_email(email) # Salt.
  25. u && u.authenticated?(password) ? u : nil
  26. end
  27. #
  28. # Encrypt data with the salt.
  29. #
  30. def self.encrypt(password, salt)
  31. Digest::SHA1.hexdigest("--#{salt}--#{password}--")
  32. end
  33. #
  34. # Encrypt password with the user salt.
  35. #
  36. def encrypt(password)
  37. self.class.encrypt(password, salt)
  38. end
  39. def authenticated?(password)
  40. crypted_password == encrypt(password)
  41. end
  42. def remember_token?
  43. remember_token_expires_at && Time.now.utc < remember_token_expires_at
  44. end
  45. #
  46. # Create and unset fields to remember users between browser closes.
  47. #
  48. def remember_me
  49. self.remember_token_expires_at = 2.weeks.from_now.utc
  50. self.remember_token = encrypt("#{email}--#{remember_token_expires_at}")
  51. save(false)
  52. end
  53. def forget_me
  54. self.remember_token_expires_at = nil
  55. self.remember_token = nil
  56. save(false)
  57. end
  58. #
  59. # Forums.
  60. #
  61. concerned_with :validation, :states, :activation, :posting
  62. formats_attributes :bio
  63. belongs_to :site, :counter_cache => true
  64. validates_presence_of :site_id
  65. has_many :posts, :order => "#{Post.table_name}.created_at desc"
  66. has_many :topics, :order => "#{Topic.table_name}.created_at desc"
  67. has_many :moderatorships, :dependent => :delete_all
  68. has_many :forums, :through => :moderatorships, :source => :forum
  69. has_many :monitorships, :dependent => :delete_all
  70. has_many :monitored_topics, :through => :monitorships, :source => :topic, :conditions => {"#{Monitorship.table_name}.active" => true}
  71. has_permalink :login
  72. attr_readonly :posts_count, :last_seen_at
  73. def self.prefetch_from(records)
  74. find(:all, :select => 'distinct *', :conditions => ['id in (?)', records.collect(&:user_id).uniq])
  75. end
  76. def self.index_from(records)
  77. prefetch_from(records).index_by(&:id)
  78. end
  79. def available_forums
  80. @available_forums ||= site.ordered_forums - forums
  81. end
  82. def moderator_of?(forum)
  83. admin? || Moderatorship.exists?(:user_id => id, :forum_id => forum.id)
  84. end
  85. def display_name
  86. n = read_attribute(:display_name)
  87. n.blank? ? login : n
  88. end
  89. #
  90. # Keep track of when a user was last seen.
  91. #
  92. def seen!
  93. now = Time.now.utc
  94. self.class.update_all ['last_seen_at = ?', now], ['id = ?', id]
  95. write_attribute :last_seen_at, now
  96. end
  97. def to_param
  98. permalink
  99. end
  100. protected
  101. #
  102. # Before filter.
  103. #
  104. def encrypt_password
  105. return if password.blank?
  106. self.salt = Digest::SHA1.hexdigest("--#{Time.now.to_s}--#{email}--") if new_record?
  107. self.crypted_password = encrypt(password)
  108. end
  109. def password_required?
  110. crypted_password.blank? || !password.blank?
  111. end
  112. def first_record_is_admin
  113. if User.count == 0
  114. self.isAdmin = true
  115. end
  116. end
  117. end
  118.  
  119. # cat users_controller.rb
  120.  
  121. class UsersController < ApplicationController
  122. before_filter :admin_required, :only => [:suspend, :unsuspend, :destroy, :purge, :edit]
  123. before_filter :find_user, :only => [:update, :show, :edit, :suspend, :unsuspend, :destroy, :purge]
  124. before_filter :login_required, :only => [:settings, :update]
  125. def index
  126. @users = current_site.users.paginate :all, :page => current_page
  127. end
  128. #
  129. # Render new.rhtml.
  130. #
  131. def new
  132. end
  133. def create
  134. cookies.delete :auth_token
  135. @user = current_site.users.build(params[:user])
  136. @user.register! if @user.valid?
  137. unless @user.new_record?
  138. self.current_user = @user
  139. redirect_back_or_default('/')
  140. flash[:notice] = "thank you for joining"
  141. else
  142. render :action => 'new'
  143. end
  144. end
  145. def settings
  146. @user = current_user
  147. render :action => "edit"
  148. end
  149. def edit
  150. @user = find_user
  151. end
  152. def update
  153. @user = admin? ? find_user : current_user
  154. respond_to do |format|
  155. if @user.update_attributes(params[:user])
  156. flash[:notice] = 'account updated'
  157. format.html { redirect_to(settings_path) }
  158. format.xml { head :ok }
  159. else
  160. format.html { render :action => "edit" }
  161. format.xml { render :xml => @user.errors, :status => :unprocessable_entity }
  162. end
  163. end
  164. end
  165. def activate
  166. self.current_user = params[:forum_activation_code].blank? ? :false : current_site.all_users.find_in_state(:first, :pending, :conditions => {:forum_activation_code => params[:forum_activation_code]})
  167. if logged_in?
  168. current_user.activate!
  169. flash[:notice] = "signup complete"
  170. end
  171. redirect_back_or_default('/')
  172. end
  173. def suspend
  174. @user.suspend!
  175. flash[:notice] = "user suspended"
  176. redirect_to users_path
  177. end
  178. def unsuspend
  179. @user.unsuspend!
  180. flash[:notice] = "user unsuspended"
  181. redirect_to users_path
  182. end
  183. def destroy
  184. @user.delete!
  185. redirect_to users_path
  186. end
  187. def purge
  188. @user.destroy
  189. redirect_to users_path
  190. end
  191. protected
  192. def find_user
  193. @user = if admin?
  194. current_site.all_users.find_by_permalink(params[:id])
  195. else
  196. current_site.users.find_by_permalink(params[:id])
  197. end or raise ActiveRecord::RecordNotFound
  198. end
  199. def authorized?
  200. admin? || params[:id].blank? || @user == current_user
  201. end
  202. end
Add Comment
Please, Sign In to add comment