Skip to content
Snippets Groups Projects
Forked from PortalMEC / portalmec
490 commits behind the upstream repository.
roles_controller.rb 2.19 KiB

# Copyright (C) 2015 Centro de Computacao Cientifica e Software Livre
# Departamento de Informatica - Universidade Federal do Parana
#
# This file is part of portalmec.
#
# portalmec is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# portalmec is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with portalmec.  If not, see <http://www.gnu.org/licenses/>.

class V1::RolesController < ApplicationController
  include ::Paginator

  before_action :authenticate_user!, except: [:index, :show]
  before_action :set_role, only: [:show, :update, :destroy]
  before_action :authorize!, only: [:update, :destroy]

  # GET /roles
  # GET /roles.json
  def index
    roles = paginate Role
    render json: roles
  end

  # GET /roles/1
  # GET /roles/1.json
  def show
    render json: @role
  end

  # POST /roles
  # POST /roles.json
  def create
    @role = Role.new(role_params)
    authorize @role

    if @role.save
      render json: @role, status: :created
    else
      render json: @role.errors, status: :unprocessable_entity
    end
  end

  # PATCH/PUT /roles/1
  # PATCH/PUT /roles/1.json
  def update
    if @role.update(role_params)
      render json: @role, status: :ok
    else
      render json: @role.errors, status: :unprocessable_entity
    end
  end

  # DELETE /roles/1
  # DELETE /roles/1.json
  def destroy
    @role.destroy
    response = { 'status': 'deleted' }
    render status: :ok, json: response
  end
  private

  # Use callbacks to share common setup or constraints between actions.
  def set_role
    @role = Role.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def role_params
    params.require(:role).permit(:name, :description)
  end

  def authorize!
    authorize @role
  end
end