API de comptabilité horaire.
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.

users_controller.rb 987B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. class Api::V1::UsersController < ApplicationController
  2. before_action :set_user, only: %i[show update destroy]
  3. before_action :check_owner, only: %i[update destroy]
  4. def index
  5. render json: User.all
  6. end
  7. def show
  8. render json: User.find(params[:id])
  9. end
  10. def create
  11. @user = User.new(user_params)
  12. if @user.save
  13. render json: @user, status: :created
  14. else
  15. render json: @user.errors, status: :unprocessable_entity
  16. end
  17. end
  18. def update
  19. if @user.update(user_params)
  20. render json: @user, status: :ok
  21. else
  22. render json: @user.errors, status: :unprocessable_entity
  23. end
  24. end
  25. def destroy
  26. @user.destroy
  27. head 204
  28. end
  29. private
  30. # Only allow a trusted parameter "white list" through.
  31. def user_params
  32. params.require(:user).permit(:email, :username, :password)
  33. end
  34. def set_user
  35. @user = User.find(params[:id])
  36. end
  37. def check_owner
  38. head :forbidden unless @user.id == current_user&.id
  39. end
  40. end