Guest User

Untitled

a guest
Mar 9th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. class UserController < ApplicationController
  2. cache_sweeper :audit_sweeper, :only => [:login, :update, :destroy]
  3. @user_list
  4.  
  5. def index
  6. # for the "specials" table
  7. @auctions = Auction.find(:all,
  8. :conditions => "status in (0, 1, 2)",
  9. :order => "end_date DESC",
  10. :limit => 10)
  11.  
  12.  
  13. # for the news auctions
  14. # NOTE: we'll probably want "sticky" to be time-specific
  15. # i.e., let an auction show up at the top of the list for
  16. # only for a certain amount of time
  17. #
  18. # TODO: below should probably be one query to help performance
  19. @news_lma = News.find(:all,
  20. :conditions => "external=0 AND image_path != ''",
  21. :order => "sticky DESC, date_posted DESC",
  22. :limit => 3)
  23.  
  24. @news_other = News.find(:all,
  25. :conditions => "external=1 AND image_path != ''",
  26. :order => "sticky DESC, date_posted DESC",
  27. :limit => 4)
  28. render :layout => false
  29.  
  30. end
  31.  
  32. def signup
  33. if request.post?
  34.  
  35. case params[:user][:type]
  36.  
  37. when "advertiser"
  38. @user = Advertiser.new(params[:user])
  39. @user.status = USER_WAIT_APPROVAL
  40. @user.roles << Role.find_by_name("advertiser")
  41.  
  42. when "publisher"
  43. @user = Publisher.new(params[:user])
  44. @user.status = USER_ACTIVE
  45. @user.activate
  46. @user.roles << Role.find_by_name("publisher")
  47. end
  48.  
  49. # FIXME: @user.status = USER_WAIT_APPROVAL
  50. # re-enable this before going into production, and remove
  51. # @user.status lines above
  52.  
  53. if @user.save
  54. if "advertiser" == @user.roles.first.name
  55. Notifications.deliver_confirmation_email(self, @user.name,
  56. @user.email, @user.activation_code)
  57. else
  58. Notifications.deliver_publisher_reg_email(@user.name, @user.email)
  59. admin_role = Role.find(:first, :conditions => "name='admin'")
  60. admins = []
  61. admin_role.users.each do |user|
  62. admins << user.email
  63. end
  64. Notifications.deliver_admin_reg_email(admins, @user.name, @user.id)
  65. end
  66.  
  67. flash[:notice] = MSG_SIGNUP_SUCCESS.t
  68. redirect_to :controller => "user", :action => "index" #Home, reminder: make a route to home.
  69.  
  70. else
  71. flash[:error] = MSG_SIGNUP_FAIL.t
  72. end
  73. end
  74. end
  75.  
  76. #The users have to provide some more information to be able to use the system
  77. # the way it should be used.
  78. def post_signup
  79. @user = User.find(:first, :conditions =>["id = ?", session[:user_id]])
  80. if request.post?
  81. params[:user][:first_time_login] = false
  82. if @user.update_attributes(params[:user])
  83. if @user.class.to_s == "Advertiser"
  84. flash[:notice] = MSG_REG_COMPLETED.t
  85. redirect_to :action => "welcome"
  86. else
  87. # user is Publisher
  88. flash[:notice] = MSG_CONTACT_COMPLETED.t
  89. redirect_to :action => "new", :controller => "media"
  90. end
  91. else
  92. flash[:notice] = MSG_USER_MISC.t
  93. end
  94. end
  95. end
  96.  
  97. def login
  98. if request.post?
  99. @user = User.authenticate(params[:user][:login], params[:user][:password])
  100.  
  101. #if session[:user_id] = @user #Is this necessary???
  102. if not @user.nil?
  103. @user.track_loggedin(@user.id)
  104. flash[:notice] = MSG_LOGIN_SUCCESS.t
  105. #It;successful enough to store just the user id in the session hash.
  106. session[:user_id] = @user.id
  107.  
  108. @user.save_with_validation(false)
  109. if not @user.post_signup_complete
  110. redirect_to :controller => "user", :action => "post_signup"
  111. else
  112. if @user.class.to_s == "Publisher"
  113. if @user.media.length == 0
  114. flash[:notice] = MSG_MEDIA_MISSING.t
  115. redirect_to :controller => "media", :action => "new"
  116. else
  117. redirect_to :controller => "user", :action => "welcome"
  118. end
  119. elsif @user.class.to_s == "Advertiser"
  120. redirect_to :controller => "user", :action => "welcome"
  121. else #Must be admin type user
  122. redirect_to :controller => "admin/users", :action => "index"
  123. end #Publisher if
  124. end #First time if
  125. else
  126. flash[:notice] = MSG_LOGIN_FAIL.t
  127. redirect_to :action => "index"
  128. end
  129. end
  130. end
  131.  
  132. def logout
  133. set_logout_time
  134. user_id = session[:user_id]
  135. User.track_loggedout(user_id)
  136. session[:user_id] = nil
  137. redirect_to :action => 'index'
  138. end
  139.  
  140. def forgot_password
  141. if request.post?
  142. u= User.find_by_email(params[:user][:email])
  143. if u and u.send_new_password
  144. flash[:notice] = MSG_PASSWD_FORGOT.t
  145. redirect_to :action => 'login'
  146. else
  147. flash[:notice] = MSG_PASSWD_FORGOT_FAIL.t
  148. redirect_to :action => "login"
  149. end
  150. end
  151. end
  152.  
  153. def change_password
  154. @user = User.find(session[:user_id])
  155. if request.post?
  156. @user.update_attributes(:password => params[:user][:password],
  157. :password_confirmation => params[:user][:password_confirmation])
  158. if @user.save
  159. flash[:notice] = MSG_PASSWD_CHANGE.t
  160. redirect_to :controller => "user", :action => "welcome"
  161. else
  162. flash[:notice] = MSG_PASSWD_CHANGE_FAIL.t
  163. end
  164. end
  165. end
  166.  
  167. def activate
  168. if params[:activation_code]
  169. @user = User.find_by_activation_code(params[:activation_code])
  170. if @user and @user.activate
  171. self.current_user = @user
  172. redirect_back_or_default(:controller => 'user', :action => 'index')
  173. flash[:notice] = "Your account has been activated."
  174. else
  175. flash[:error] = "Unable to activate the account. Did you provide the correct information?"
  176. end
  177. else
  178. flash.clear
  179. end
  180. end
  181.  
  182.  
  183. # Provide information about current user activity
  184. def welcome
  185. @user = User.find(session[:user_id])
  186. if @user.class.to_s == "Publisher"
  187. @auctions = Auction.find(:all,
  188. :conditions => ["user_id = ? and status in (0, 1, 2)", session[:user_id]],
  189. :limit => 5)
  190. sql = "SELECT DISTINCT offers.* " +
  191. "FROM offers " +
  192. "INNER JOIN reverse_auctions ON offers.reverse_auction_id = reverse_auctions.id " +
  193. "INNER JOIN media ON offers.medium_id = media.id " +
  194. "WHERE media.publisher_id = #{@user.id} " +
  195. "ORDER BY created_at DESC"
  196. @offers = Offer.find_by_sql(sql)
  197. else
  198. @auctions = Auction.find(:all,
  199. :conditions => ["winner_id = ?", session[:user_id]],
  200. :limit => 5)
  201. @maxbids = MaxBid.find(:all,
  202. :conditions => ["max_bids.user_id = ? and auctions.status in (0, 1, 2)", session[:user_id]],
  203. :include => :auction,
  204. :order => "updated_at DESC",
  205. :limit => 5)
  206. end
Add Comment
Please, Sign In to add comment