users_controller.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. class UsersController < ApplicationController
  2. before_action :set_user, only: [:show, :edit, :update, :destroy]
  3. before_action :correct_user, only: [:edit, :update]
  4. def index
  5. respond_to do |format|
  6. @users = current_user.school ? current_user.school.users : User.all
  7. format.json
  8. format.html
  9. end
  10. end
  11. def show
  12. end
  13. def new
  14. end
  15. def edit
  16. end
  17. def update
  18. if @user.update(user_params)
  19. flash[:success] = 'Profile updated'
  20. redirect_to @user
  21. else
  22. render :edit
  23. end
  24. end
  25. private
  26. # Use callbacks to share common setup or constraints between actions.
  27. def set_user
  28. @user = User.find(params[:id])
  29. end
  30. # Only allow certain attributes to be updated over the web.
  31. def user_params
  32. allowed = [:login, :email, :password, :password_confirmation,
  33. :phone, :picture]
  34. if current_user.is_administrator_at?(@user.school)
  35. allowed += [:gender, :birth_date, :name]
  36. end
  37. params.require(:user).permit(*allowed)
  38. end
  39. # Confirms the correct user.
  40. def correct_user
  41. redirect_to(root_url) unless current_user?(@user) ||
  42. current_user.is_administrator_at?(@user.school)
  43. end
  44. end