require_dependency "educode_sales/application_controller" module EducodeSales class RolesController < ApplicationController # before_action :authenticate_admin def index respond_to do |format| format.html do end format.json do @roles = Role.page(params[:page]).per(params[:limit]) end end end def create role = Role.new(name: params[:name]) role.role_areas.build([{ clazz: 'Business' }, { clazz: 'SalePlan' }, { clazz: 'Teacher' }, { clazz: 'Operation' }]) if role.save render_success else render_failure role end end def new render layout: false end def edit role = Role.find(params[:id]) @permissions = role.permissions.pluck(:id) @areas = role.role_areas.map { |d| "#{d.clazz}-#{d.level}" } render layout: false end def rename @name = Role.find(params[:id]).name render layout: false end def update_role role = Role.find(params[:id]) if role.update(name: params[:name]) render_success else render_failure role end end def update permissions = [] role = Role.find(params[:id]) params[:permission].each do |d| d[1].each do |c| permissions << Permission.find_by(subject: d[0], action: c[1]) end end role.permissions = permissions role.save render json: { success: true } end def destroy role = Role.find(params[:id]) role.destroy render_success rescue ActiveRecord::DeleteRestrictionError => e render_failure '该角色已有用户存在,不能再删除' end def view role = Role.find(params[:id]) role.role_areas.find_by(clazz: 'Business').update(level: params[:business]) role.role_areas.find_by(clazz: 'Operation').update(level: params[:operation]) role.role_areas.find_by(clazz: 'SalePlan').update(level: params[:sale_plan]) role.role_areas.find_by(clazz: 'Teacher').update(level: params[:teacher]) render_success end end end