jam-cloud/web/app/controllers/users_controller.rb

558 lines
18 KiB
Ruby
Raw Normal View History

# -*- coding: utf-8 -*-
require 'builder'
class Slide
attr_accessor :img_url, :header, :vid_url
def initialize(header, img_url, vid_url)
@img_url = img_url # preview image
@header = header
@vid_url = vid_url + '?autoplay=1'
end
end
class UsersController < ApplicationController
include ClientHelper
2012-11-12 12:59:43 +00:00
before_filter :signed_in_user,
2012-09-03 22:03:16 +00:00
only: [:index, :edit, :update, :destroy]
before_filter :correct_user, only: [:edit, :update]
before_filter :admin_user, only: :destroy
before_filter :is_native_client
before_filter :ip_blacklist, :only => [:create]
2013-02-08 03:11:47 +00:00
rescue_from 'JamRuby::PermissionError' do |exception|
@exception = exception
render :file => 'public/403.html', :status => 403, :layout => false
end
2012-11-12 12:59:43 +00:00
def index
@users = User.paginate(page: params[:page])
end
def show
@user = User.find(params[:id])
end
def new
@no_user_dropdown = true
2020-04-04 22:51:36 +00:00
@invited_user = load_invited_user(params)
2020-11-30 00:24:28 +00:00
@plan_code = params[:plan_code] || ''
2020-04-04 22:51:36 +00:00
if @invited_user && @invited_user.email
user = User.find_by_email(@invited_user.email)
if user && (!current_user || current_user.id != user.id)
sign_in(user)
friend_request = FriendRequest.find_by_user_id_and_friend_id(@invited_user.sender_id, user.id)
if !friend_request
redirect_url = "/client#/home"
else
redirect_url = "/client#/home/accept-friend-request/d1=#{friend_request.id}"
end
redirect_to redirect_url
return
end
end
if current_user
2016-08-03 01:46:15 +00:00
if params["redirect-to"]
redirect_to params["redirect-to"]
else
redirect_to client_url
end
return
end
2014-02-03 21:19:14 +00:00
@fb_signup = load_facebook_signup(params)
# check if the email specified by @fb_signup already exists in the databse--if so, log them in and redirect
if @fb_signup && @fb_signup.email
user = User.find_by_email_and_email_confirmed(@fb_signup, true)
if user
# update user_authorization for user because this is fresher
user.update_fb_authorization(@fb_signup)
sign_in(user)
redirect_to client_url
return
end
end
# check if the uid specified by @fb_signup already exists in the databse--if so, log them in and redirect
if @fb_signup && @fb_signup.uid
user_authorization = UserAuthorization.find_by_uid_and_provider(@fb_signup.uid, 'facebook')
# update user_authorization for user because this is fresher
if user_authorization
user_authorization.user.update_fb_authorization(@fb_signup)
sign_in(user_authorization.user)
redirect_to client_url
return
end
end
2015-05-28 13:20:14 +00:00
@affiliate_partner = load_affiliate_partner(params)
2013-03-15 04:23:37 +00:00
@signup_postback = load_postback(@invited_user, @fb_signup, @affiliate_partner, request.path)
load_location(request.remote_ip)
2013-03-15 04:23:37 +00:00
@user = User.new
@user.musician = true # default the UI to musician as selected option
2013-03-15 04:23:37 +00:00
# preseed the form with the invited email as a convenience to the user
2014-02-03 21:19:14 +00:00
@user.email = @invited_user.email unless @invited_user.nil?
if @fb_signup
@user.email = @fb_signup.email
@user.first_name = @fb_signup.first_name
@user.last_name = @fb_signup.last_name
@user.gender = @fb_signup.gender
2013-03-15 04:23:37 +00:00
end
2021-03-17 09:14:58 +00:00
rend = _render('new')
render rend[:template], :layout => rend[:layout]
end
def create
if current_user
redirect_to client_url
return
end
2013-03-15 04:23:37 +00:00
signup_hint = SignupHint.where(anonymous_user_id: anonymous_user.id).where('expires_at > ?', Time.now).first if anonymous_user
2014-02-03 21:19:14 +00:00
@fb_signup = load_facebook_signup(params)
# check if the email specified by @fb_signup already exists in the database--if so, log them in and redirect
2014-02-03 21:19:14 +00:00
if @fb_signup && @fb_signup.email
user = User.find_by_email_and_email_confirmed(@fb_signup, true)
if user
# update user_authorization for user because this is fresher
user.update_fb_authorization(@fb_signup)
sign_in(user)
redirect_url = handle_signup_hint(user, signup_hint, client_url)
redirect_to redirect_url
2014-02-03 21:19:14 +00:00
return
end
end
# check if the uid specified by @fb_signup already exists in the database--if so, log them in and redirect
2014-02-03 21:19:14 +00:00
if @fb_signup && @fb_signup.uid
user_authorization = UserAuthorization.find_by_uid_and_provider(@fb_signup.uid, 'facebook')
# update user_authorization for user because this is fresher
if user_authorization
user_authorization.user.update_fb_authorization(@fb_signup)
sign_in(user_authorization.user)
redirect_url = handle_signup_hint(user_authorization.user, signup_hint, client_url)
redirect_to redirect_url
2014-02-03 21:19:14 +00:00
return
end
end
2013-03-15 04:23:37 +00:00
@invited_user = load_invited_user(params)
2015-05-28 13:20:14 +00:00
@affiliate_partner = load_affiliate_partner(params)
@signup_postback = load_postback(@invited_user, @fb_signup, @affiliate_partner, request.path)
2013-03-15 04:23:37 +00:00
2013-03-15 04:23:37 +00:00
instruments = fixup_instruments(params[:jam_ruby_user][:instruments])
birth_date = fixup_birthday(params[:jam_ruby_user]["birth_date(2i)"], params[:jam_ruby_user]["birth_date(3i)"], params[:jam_ruby_user]["birth_date(1i)"])
location = { :country => params[:jam_ruby_user][:country], :state => params[:jam_ruby_user][:state], :city => params[:jam_ruby_user][:city]}
2020-11-30 00:24:28 +00:00
desired_plan_code = params[:jam_ruby_user][:desired_plan_code]
if desired_plan_code ==''
desired_plan_code = nil
end
terms_of_service = params[:jam_ruby_user][:terms_of_service].nil? || params[:jam_ruby_user][:terms_of_service] == "0"? false : true
musician = params[:jam_ruby_user][:musician]
2013-03-15 04:23:37 +00:00
2021-03-19 16:57:52 +00:00
skip_recaptcha = params[:jam_ruby_user][:landing_general] == "1"
2021-03-19 20:27:51 +00:00
2014-02-03 21:19:14 +00:00
@user = UserManager.new.signup(remote_ip: request.remote_ip,
2014-04-20 22:54:49 +00:00
first_name: params[:jam_ruby_user][:first_name],
last_name: params[:jam_ruby_user][:last_name],
email: params[:jam_ruby_user][:email],
password: params[:jam_ruby_user][:password],
password_confirmation: params[:jam_ruby_user][:password_confirmation],
terms_of_service: terms_of_service,
instruments: instruments,
birth_date: birth_date,
location: location,
musician: musician,
2021-03-19 20:27:51 +00:00
skip_recaptcha: skip_recaptcha,
recaptcha_response: params['g-recaptcha-response'],
2014-04-20 22:54:49 +00:00
invited_user: @invited_user,
fb_signup: @fb_signup,
signup_confirm_url: ApplicationHelper.base_uri(request) + "/confirm",
2015-05-28 13:20:14 +00:00
affiliate_referral_id: cookies[:affiliate_visitor],
2016-05-23 17:26:32 +00:00
affiliate_partner: @affiliate_partner,
2018-02-15 04:16:32 +00:00
timezone: current_timezone,
2020-11-30 00:24:28 +00:00
origin: origin_cookie,
desired_plan_code: desired_plan_code )
2021-03-17 09:14:58 +00:00
rend = _render('new')
2012-11-14 05:57:10 +00:00
# check for errors
if @user.errors.any?
# render any @user.errors on error
2013-03-15 04:23:37 +00:00
load_location(request.remote_ip, location)
gon.signup_errors = true
gon.musician_instruments = instruments
2020-11-30 00:24:28 +00:00
gon.plan_code = desired_plan_code
2021-03-17 09:14:58 +00:00
render rend[:template], :layout => rend[:layout]
2012-11-14 05:57:10 +00:00
else
sign_in @user
new_user(@user, signup_hint) # sets a cookie used for GA analytics (one-time new user stuff in JavaScript)
2021-03-17 09:14:58 +00:00
#destination = @user.musician ? :congratulations_musician : :congratulations_fan
redirect_params = { friend: @invited_user.nil? ? nil : @invited_user.sender.name }
destination = rend[:template] == 'new2021' ? landing_client_downloads_path(redirect_params) : congratulations_musician_path(redirect_params)
redirect_url = handle_signup_hint(@user, signup_hint, destination)
redirect_to redirect_url
end
end
def congratulations_fan
@no_user_dropdown = true
render :layout => "web"
end
2012-11-12 12:59:43 +00:00
def congratulations_musician
@no_user_dropdown = true
render :layout => "web"
2012-11-12 12:59:43 +00:00
end
2013-09-25 15:34:53 +00:00
def downloads
@no_user_dropdown = true
@page_context = 'standalone'
2021-03-17 09:14:58 +00:00
rend = _render('downloads')
render rend[:template], :layout => rend[:layout]
end
def downloads2021
render :downloads2021, layout: 'jk2021'
2013-09-25 15:34:53 +00:00
end
# DO NOT USE CURRENT_USER IN THIS ROUTINE UNLESS REDIRECTING. IT'S CACHED FOR THE WHOLE SITE
2015-04-09 03:43:57 +00:00
def home
if current_user
redirect_to "/client#/home"
return
end
2016-01-08 22:13:59 +00:00
gon.news = News.all
2016-01-08 21:40:10 +00:00
render :layout => "modern"
end
2012-11-12 12:59:43 +00:00
def signup_confirm
2013-03-08 06:45:06 +00:00
signup_token = params[:signup_token]
@user = UserManager.new.signup_confirm(signup_token, request.remote_ip)
2012-11-12 12:59:43 +00:00
if !@user.nil? && !@user.errors.any?
#UserMailer.welcome_message(@user).deliver_now
elsif !@user.nil?
2013-03-08 06:45:06 +00:00
# new user with validation errors;
logger.debug("#{@user} has errors. can not sign in until remedied. #{@user.errors.inspect}")
2013-03-08 06:45:06 +00:00
end
# let page have signup_token in javascript
gon.signup_token = signup_token
2012-11-12 12:59:43 +00:00
# let errors fall through to signup_confirm.html.erb
render :layout => 'web'
2012-11-12 12:59:43 +00:00
end
def edit
end
def update
if @user.update_attributes(params[:jam_ruby_user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
def destroy
User.find(params[:id]).destroy
flash[:success] = "User destroyed."
redirect_to users_url
end
2012-12-28 07:31:12 +00:00
def request_reset_password
2013-07-05 08:24:43 +00:00
render 'request_reset_password', :layout => 'landing'
2012-12-28 07:31:12 +00:00
end
def reset_password
begin
2013-07-05 08:24:43 +00:00
@reset_password_email = params[:jam_ruby_user][:email]
if @reset_password_email.blank?
@reset_password_error = "Please enter an email address"
render 'request_reset_password', :layout => 'landing'
return
end
@reset_password_email.strip!
unless User::VALID_EMAIL_REGEX.match(@reset_password_email)
@reset_password_error = "Please enter a valid email address"
2013-07-05 08:24:43 +00:00
render 'request_reset_password', :layout => 'landing'
return
end
@user = User.reset_password(@reset_password_email, ApplicationHelper.base_uri(request))
render 'sent_reset_password', :layout => 'landing'
2012-12-28 07:31:12 +00:00
rescue JamRuby::JamArgumentError
2013-07-05 08:24:43 +00:00
# Dont tell the user if this error occurred to prevent scraping email addresses.
render 'sent_reset_password', :layout => 'landing'
2012-12-28 07:31:12 +00:00
end
end
def reset_password_token
2013-07-05 08:24:43 +00:00
render 'reset_password_token', :layout => 'landing'
2012-12-28 07:31:12 +00:00
end
def reset_password_token_post
2012-12-28 07:31:12 +00:00
begin
User.set_password_from_token(params[:jam_ruby_user][:email], params[:jam_ruby_user][:token], params[:jam_ruby_user][:password], params[:jam_ruby_user][:password_confirmation])
redirect_to reset_password_complete_path({email: params[:jam_ruby_user][:email]})
rescue JamRuby::JamArgumentError => e
@password_error = e.field_message
2012-12-28 07:31:12 +00:00
params[:email] = params[:jam_ruby_user][:email]
params[:token] = params[:jam_ruby_user][:token]
2013-07-05 08:24:43 +00:00
render 'reset_password_token', :layout => 'landing'
2012-12-28 07:31:12 +00:00
end
end
def reset_password_complete
render 'reset_password_complete', :layout => 'landing'
end
def finalize_update_email
# this corresponds to when the user clink a link in their new email address to configure they want to use it,
# and verify their new address is real
token = params[:token]
gon.ensure = true
gon.update_email_token = token
render :layout => 'landing'
end
def jnlp
headers["Content-Type"] = "application/x-java-jnlp-file"
headers["Cache-Control"] = "public"
headers["Content-Disposition"] = "attachment;filename='ping#{params[:isp]}.jnlp'"
jnlp = ''
xml = Builder::XmlMarkup.new(:indent => 2, :target => jnlp)
xml.instruct!
jnlpurl = isp_ping_url(:isp => params[:isp],
:format => :jnlp,
:host => 'www.jamkazam.com',
:port => '80')
xml.jnlp(:spec => '1.0+',
:href => jnlpurl,
:codebase => "http://www.jamkazam.com/isp") do
xml.information do
xml.title 'Ping'
xml.vendor 'JamKazam'
end
xml.resources do
xml.j2se(:version => "1.6+", :href => "http://java.sun.com/products/autodl/j2se")
xml.jar(:href => 'http://www.jamkazam.com/isp/ping.jar', :main => 'true')
end
xml.tag!('application-desc',
:name => "Ping",
'main-class' => "com.jamkazam.ping.Ping",
:width => "400",
:height => "600") do
xml.comment!('usage: Ping [label=]addr[:port] ... [-c <count>] [-s <size>] -u <url> -i <isp> [-a]')
xml.argument('foo=etch.dyndns.org:4442')
xml.argument('bar=etch.dyndns.org:4442')
xml.argument("-uhttp://www.jamkazam.com#{isp_scoring_path}")
xml.argument("-i#{params[:isp]}")
xml.argument('-a')
end
xml.update(:check => 'background')
end
send_data jnlp, :type=>"application/x-java-jnlp-file"
end
def isp
@isps = {
2013-06-15 10:46:30 +00:00
'tw' => ['Time Warner', 'tw.jpg'],
'vz' => ['Verizon', 'vz.png'],
'att' => ['AT&T', 'att.png'],
'cc' => ['Comcast', 'cc.png'],
'other' => ['Other', 'other.jpg']
}
render :layout => "landing"
end
def endorse
2014-05-11 00:17:44 +00:00
if uu = current_user ||
2014-05-19 15:12:59 +00:00
uu = User.where(['id = ? AND first_social_promoted_at IS NULL',params[:id]]).limit(1).first
uu.first_social_promoted_at = Time.now
uu.save!
2014-05-11 00:17:44 +00:00
end if params[:id].present? && (service=params[:service]).present?
2014-05-11 00:17:44 +00:00
service ||= 'facebook'
2015-05-15 21:14:18 +00:00
url = CGI::escape('https://www.jamkazam.com')
2014-05-11 00:17:44 +00:00
txt = CGI::escape('Check out JamKazam -- Play music together over the Internet as if in the same room')
if 'twitter'==service
2014-05-11 00:17:44 +00:00
url = "https://twitter.com/intent/tweet?text=#{txt}&url=#{url}"
elsif 'facebook'==service
2015-05-15 21:14:18 +00:00
url = "https://www.facebook.com/sharer/sharer.php?u=#{url}&t=#{txt}"
elsif 'google'==service
2014-05-11 00:17:44 +00:00
url = "https://plus.google.com/share?url=#{url}"
end
2014-05-11 06:42:20 +00:00
if 'email'==params[:src]
js =<<JS
<script type="text/javascript">
$(function() {
2016-05-31 13:35:04 +00:00
// // removed because we are juggling 20 events max in GA
// JK.GA.trackJKSocial(JK.GA.Categories.jkLike, '#{service}', 'email');
2014-05-11 06:42:20 +00:00
window.location = "#{url}";
});
</script>
JS
render :inline => js, :layout => 'landing'
else
redirect_to url
end
end
def unsubscribe
unless @user = User.read_access_token(params[:user_token])
redirect_to '/'
end if params[:user_token].present?
#if request.get?
#elsif request.post?
@user.subscribe_email = false
@user.save!
#end
render text: 'You have been unsubscribed.'
end
private
def _render(action)
2021-03-17 09:14:58 +00:00
case action
when 'new'
2021-03-17 09:14:58 +00:00
request.path == '/landing/general/signup' ? { layout: 'jk2021', template: 'new2021' } : { layout: 'web', template: 'new' }
when 'downloads'
2021-03-17 09:14:58 +00:00
request.path == '/landing/general/downloads' ? { layout: 'jk2021', template: 'downloads2021' } : { layout: 'web', template: 'downloads' }
end
end
def is_native_client
@nativeClient = is_native_client?
end
2012-12-22 00:56:49 +00:00
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end
2013-03-15 04:23:37 +00:00
# the User Model expects instruments in a different format than the form submits it
# so we have to fix it up.
def fixup_instruments(original_instruments)
# if an instrument is selected by the user in the form, it'll show up in this array
instruments = []
# ok, sweep through all the fields submitted, looking for selected instruments.
# also, make up priority because we don't ask for it (but users can fix it later on their profile)
priority = 0
unless original_instruments == nil
original_instruments.each do |key, value|
if !value["selected"].nil?
instruments << { :instrument_id => key, :proficiency_level => value["proficiency"].to_i, :priority => priority }
priority = priority + 1
end
2013-03-15 04:23:37 +00:00
end
end
2020-04-05 01:27:22 +00:00
if instruments.length == 0
# default to guitar when non-specified
instruments << {:instrument_id => "acoustic guitar", :proficiency_level => 3, :priority => 1}
end
2013-03-15 04:23:37 +00:00
return instruments
end
# the User Model expects instruments in a different format than the form submits it
# so we have to fix it up.
def fixup_birthday(month, day, year)
if month.blank? || day.blank? || year.blank?
# invalid birthdate, so return nil
return nil
end
return Date.new(year.to_i, month.to_i, day.to_i)
end
2014-02-03 21:19:14 +00:00
def load_facebook_signup(params)
lookup_id = params[:facebook_signup]
FacebookSignup.find_by_lookup_id(lookup_id)
end
2013-03-15 04:23:37 +00:00
def load_invited_user(params)
# check if this an anonymous request, or result of invitation code
invitation_code = params[:invitation_code]
invited_user = nil
unless invitation_code.nil?
# we only want to find invitations that have not been accepted
invited_user = InvitedUser.find_by_invitation_code(invitation_code)
end
return invited_user
end
2015-05-28 13:20:14 +00:00
def load_affiliate_partner(params)
partner_id = params[:affiliate_partner_id]
AffiliatePartner.find(partner_id) if partner_id
end
2013-03-15 04:23:37 +00:00
def load_location(remote_ip, location = nil)
# useful if you need to repro something on 127.0.0.1
# remote_ip = ' 23.119.29.89'
2013-03-15 04:23:37 +00:00
@location = location
if @location.nil?
@location = GeoIpLocations.lookup(remote_ip)
2013-03-15 04:23:37 +00:00
end
@location[:country] = "US" if @location[:country].nil?
2013-03-15 04:23:37 +00:00
@countriesx = MaxMindManager.countries
2013-03-15 04:23:37 +00:00
# populate regions based on current country
@regions = MaxMindManager.regions(@location[:country])
@cities = @location[:state].nil? ? [] : MaxMindManager.cities(@location[:country], @location[:state])
end
def load_postback(invited_user, fb_signup, affiliate_partner, request_path)
2014-02-03 21:19:14 +00:00
query = {}
query[:invitation_code] = invited_user.invitation_code if invited_user
query[:facebook_signup] = fb_signup.lookup_id if fb_signup
2015-05-28 13:20:14 +00:00
query[:affiliate_partner_id] = affiliate_partner.id if affiliate_partner
if query.length > 0
request_path + "?" + query.to_query
else
request_path
end
end
end