ぼざクリタグ広場 https://hub.nizika.monster
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

50 lines
1.4 KiB

  1. class UsersController < ApplicationController
  2. def create
  3. user = User.create!(inheritance_code: SecureRandom.uuid, role: 'guest')
  4. render json: { code: user.inheritance_code,
  5. user: user.slice(:id, :name, :inheritance_code, :role) }
  6. end
  7. def verify
  8. ip_bin = IPAddr.new(request.remote_ip).hton
  9. ip_address = IpAddress.find_or_create_by!(ip_address: ip_bin)
  10. user = User.find_by(inheritance_code: params[:code])
  11. return render json: { valid: false } unless user
  12. UserIp.find_or_create_by!(user:, ip_address:)
  13. render json: { valid: true, user: user.slice(:id, :name, :inheritance_code, :role) }
  14. end
  15. def renew
  16. return head :unauthorized unless current_user
  17. user = current_user
  18. user.inheritance_code = SecureRandom.uuid
  19. user.save!
  20. render json: { code: user.inheritance_code }
  21. end
  22. def me
  23. user = User.find_by(inheritance_code: params[:code])
  24. return head :not_found unless user
  25. render json: user.slice(:id, :name, :inheritance_code, :role)
  26. end
  27. def update
  28. user = current_user
  29. return head :unauthorized if user&.id != params[:id].to_i
  30. name = params[:name]
  31. return head :bad_request if name.blank?
  32. if user.update(name:)
  33. render json: user.slice(:id, :name, :inheritance_code, :role), status: :created
  34. else
  35. render json: user.errors, status: :unprocessable_entity
  36. end
  37. end
  38. end