2011-06-20 23:28:09 +00:00
|
|
|
require 'digest'
|
|
|
|
|
|
|
|
class UserController < ApplicationController
|
2011-06-21 20:44:19 +00:00
|
|
|
|
2011-06-20 23:28:09 +00:00
|
|
|
def auth
|
|
|
|
@res = {}
|
2011-06-21 20:44:19 +00:00
|
|
|
check = check_auth(params)
|
2011-06-20 23:28:09 +00:00
|
|
|
|
2011-06-21 20:44:19 +00:00
|
|
|
if check == true
|
|
|
|
@res[:status] = 'hello'
|
|
|
|
user = User.find_by_vkid(params[:mid])
|
|
|
|
if user.nil?
|
|
|
|
user = User.new
|
|
|
|
user.vkid = params[:mid]
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
@res[:id] = user.id
|
|
|
|
@res[:username] = user.name
|
|
|
|
elsif check == false
|
|
|
|
@res[:status] = 'bad signature'
|
|
|
|
else
|
|
|
|
@res[:status] = 'bad params'
|
|
|
|
end
|
|
|
|
|
|
|
|
render :json => @res
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2011-06-21 23:29:18 +00:00
|
|
|
@res = {}
|
|
|
|
check = check_auth(params)
|
|
|
|
|
|
|
|
if check == true
|
|
|
|
user = User.find_by_vkid(params[:mid])
|
|
|
|
unless params[:username].nil? or params[:email].nil?
|
|
|
|
user.name = params[:username]
|
|
|
|
user.email = params[:email]
|
|
|
|
user.save
|
|
|
|
end
|
|
|
|
@res[:username] = user.name
|
|
|
|
@res[:email] = user.email
|
|
|
|
end
|
2011-06-21 20:44:19 +00:00
|
|
|
|
2011-06-21 23:29:18 +00:00
|
|
|
render :json => @res
|
2011-06-21 20:44:19 +00:00
|
|
|
end
|
|
|
|
|
2011-06-22 00:05:12 +00:00
|
|
|
def fav
|
|
|
|
@res = {status: 'fail'}
|
|
|
|
check = check_auth(params)
|
|
|
|
|
|
|
|
if check == true
|
|
|
|
fav = Favorite.new
|
|
|
|
if not params[:artist].nil?
|
|
|
|
artist = Artist.find(params[:artist]);
|
|
|
|
unless artist.nil?
|
|
|
|
fav.artist_id = artist.id
|
|
|
|
res[:status] = 'added'
|
|
|
|
end
|
|
|
|
elsif not params[:album].nil?
|
|
|
|
album = Album.find(params[:album]);
|
|
|
|
unless album.nil?
|
|
|
|
fav.album_id = album.id
|
|
|
|
res[:status] = 'added'
|
|
|
|
end
|
|
|
|
elsif not params[:track].nil?
|
|
|
|
track = Track.find(params[:track]);
|
|
|
|
unless track.nil?
|
|
|
|
fav.track_id = track.id
|
|
|
|
res[:status] = 'added'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render :json => @res
|
|
|
|
end
|
|
|
|
|
2011-06-21 20:44:19 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def check_auth params
|
|
|
|
secret_key = request.host == 'beathaven.org' ? 'sdgwSbl3nNE4ZxafuPrp' : 's5zyjb693z6uV4rbhEyc'
|
2011-06-20 23:28:09 +00:00
|
|
|
unless params[:expire].nil? or params[:mid].nil? or params[:secret].nil? or params[:sid].nil? or params[:sig].nil?
|
|
|
|
# Calculating hash
|
2011-06-21 20:44:19 +00:00
|
|
|
hash = 'expire='+ params[:expire] +'mid='+ params[:mid] +'secret='+ params[:secret] +'sid='+ params[:sid] + secret_key
|
2011-06-20 23:28:09 +00:00
|
|
|
hash_md5 = Digest::MD5.hexdigest(hash)
|
|
|
|
if Digest::MD5.hexdigest(hash) == params[:sig]
|
2011-06-21 20:44:19 +00:00
|
|
|
return true
|
2011-06-20 23:28:09 +00:00
|
|
|
else
|
2011-06-21 20:44:19 +00:00
|
|
|
return false
|
2011-06-20 23:28:09 +00:00
|
|
|
end
|
|
|
|
else
|
2011-06-21 20:44:19 +00:00
|
|
|
return 'bad params'
|
2011-06-20 23:28:09 +00:00
|
|
|
end
|
|
|
|
end
|
2011-06-21 20:44:19 +00:00
|
|
|
|
2011-06-20 23:28:09 +00:00
|
|
|
end
|