2019-07-30 21:29:24 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-25 16:15:39 +00:00
|
|
|
# License: AGPL-3.0-or-later WITH Web-Template-Output-Additional-Permission-3.0-or-later
|
2018-03-25 17:30:42 +00:00
|
|
|
class RolesController < ApplicationController
|
2020-05-11 18:38:50 +00:00
|
|
|
include Controllers::Nonprofit::Current
|
|
|
|
include Controllers::Nonprofit::Authorization
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
before_action :authenticate_nonprofit_admin!
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def create
|
2019-08-06 14:07:55 +00:00
|
|
|
role = Role.create_for_nonprofit(role_params[:name].to_sym, role_params[:email], FetchNonprofit.with_params(params))
|
2019-07-30 21:29:24 +00:00
|
|
|
json_saved role, 'User successfully added!'
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
|
2019-07-30 21:29:24 +00:00
|
|
|
def destroy
|
|
|
|
role = Role.find(params[:id])
|
|
|
|
roles = role.user.roles.where(host_id: params[:nonprofit_id], name: role.name)
|
|
|
|
if roles.empty?
|
|
|
|
render json: { error: "We couldn't find that admin" }, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
roles.destroy_all
|
|
|
|
flash[:notice] = 'User successfully removed'
|
|
|
|
render json: {}
|
|
|
|
end
|
|
|
|
end
|
2019-08-06 14:07:55 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def role_params
|
|
|
|
params.require(:role).permit(:name, :email)
|
|
|
|
end
|
2018-03-25 17:30:42 +00:00
|
|
|
end
|