93 lines
2.8 KiB
Ruby
93 lines
2.8 KiB
Ruby
class UserController < ApplicationController
|
|
|
|
@@invite_salt = 'Gouranga gives a fuck?!'
|
|
@@email_regex = /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
|
|
|
|
def login
|
|
@hide_player = 1
|
|
unless params[:email].nil? or params[:password].nil?
|
|
user_data = User.collection.find({email: params[:email], password: Digest::SHA256.hexdigest(params[:password])}).first
|
|
unless user_data.nil?
|
|
user_data['session_key'] = Digest::SHA256.hexdigest(request.session['session_id'])
|
|
User.collection.update({_id: user_data._id}, user_data.attributes)
|
|
redirect_to '/'
|
|
else
|
|
redirect_to '/login'
|
|
end
|
|
end
|
|
end
|
|
|
|
def register
|
|
@hide_player = 1
|
|
data = Invite.where(:email => params[:email], :code => params[:code]).first
|
|
unless data.nil?
|
|
@code = data.code
|
|
@email = data.email
|
|
else
|
|
redirect_to '/login'
|
|
end
|
|
end
|
|
|
|
def complete
|
|
@hide_player = 1
|
|
data = Invite.where(:email => params[:invite_email], :code => params[:invite_code]).first
|
|
unless data.nil?
|
|
if params[:email].match(@@email_regex).nil? or
|
|
params[:password].length < 6 or
|
|
params[:password] != params[:password_c]
|
|
redirect_to '/'
|
|
end
|
|
|
|
User.collection.insert({
|
|
email: params[:email],
|
|
password: Digest::SHA256.hexdigest(params[:password]),
|
|
name: '',
|
|
regdate: Time.now(),
|
|
referer: data.referer,
|
|
lastvisit: Time.now(),
|
|
invites: 0
|
|
})
|
|
|
|
Invite.collection.remove({email: params[:invite_email], code: params[:invite_code]})
|
|
else
|
|
redirect_to '/login'
|
|
end
|
|
end
|
|
|
|
def update
|
|
@data = User.collection.find({session_key: Digest::SHA256.hexdigest(request.session['session_id'])}).first
|
|
unless @data.nil?
|
|
if request.request_method == 'POST'
|
|
if params[:name]
|
|
@data.name = params[:name]
|
|
end
|
|
User.collection.update({_id: @data._id}, @data.attributes)
|
|
end
|
|
else
|
|
render :json => 'wtf?'
|
|
end
|
|
end
|
|
|
|
def invite
|
|
@data = User.collection.find({session_key: Digest::SHA256.hexdigest(request.session['session_id'])}).first
|
|
unless @data.nil?
|
|
if @data.invites > 0
|
|
pp 1
|
|
if request.request_method == 'POST'
|
|
unless params[:email].nil?
|
|
if params[:email].match(@@email_regex)
|
|
Invite.collection.insert({email: params[:email], code: Digest::SHA256.hexdigest(params[:email] << @@invite_salt), referer: @data._id})
|
|
@data.invites -= 1
|
|
User.collection.update({_id: @data._id}, @data.attributes)
|
|
@ok = true
|
|
end
|
|
end
|
|
end
|
|
pp 2
|
|
else
|
|
render :json => 'wtf?'
|
|
end
|
|
end
|
|
end
|
|
end
|