roles_controller.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. module Admin
  3. class RolesController < BaseController
  4. before_action :set_role, except: [:index, :new, :create]
  5. def index
  6. authorize :user_role, :index?
  7. @roles = UserRole.order(position: :desc).page(params[:page])
  8. end
  9. def new
  10. authorize :user_role, :create?
  11. @role = UserRole.new
  12. end
  13. def create
  14. authorize :user_role, :create?
  15. @role = UserRole.new(resource_params)
  16. @role.current_account = current_account
  17. if @role.save
  18. log_action :create, @role
  19. redirect_to admin_roles_path
  20. else
  21. render :new
  22. end
  23. end
  24. def edit
  25. authorize @role, :update?
  26. end
  27. def update
  28. authorize @role, :update?
  29. @role.current_account = current_account
  30. if @role.update(resource_params)
  31. log_action :update, @role
  32. redirect_to admin_roles_path
  33. else
  34. render :edit
  35. end
  36. end
  37. def destroy
  38. authorize @role, :destroy?
  39. @role.destroy!
  40. log_action :destroy, @role
  41. redirect_to admin_roles_path
  42. end
  43. private
  44. def set_role
  45. @role = UserRole.find(params[:id])
  46. end
  47. def resource_params
  48. params.require(:user_role).permit(:name, :color, :highlighted, :position, permissions_as_keys: [])
  49. end
  50. end
  51. end