1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/controllers/api/auth_providers_controller.rb
2016-12-15 14:16:24 +01:00

89 lines
2.8 KiB
Ruby

class API::AuthProvidersController < API::ApiController
before_action :set_provider, only: [:show, :update, :destroy]
def index
@providers = policy_scope(AuthProvider)
end
def create
authorize AuthProvider
@provider = AuthProvider.new(provider_params)
if @provider.save
render :show, status: :created, location: @provider
else
render json: @provider.errors, status: :unprocessable_entity
end
end
def update
authorize AuthProvider
if @provider.update(provider_params)
render :show, status: :ok, location: @provider
else
render json: @provider.errors, status: :unprocessable_entity
end
end
def show
authorize AuthProvider
end
def destroy
authorize AuthProvider
if @provider.safe_destroy
head :no_content
else
render json: @provider.errors, status: :unprocessable_entity
end
end
def mapping_fields
authorize AuthProvider
render :mapping_fields, status: :ok
end
def active
authorize AuthProvider
@provider = AuthProvider.active
end
def send_code
authorize AuthProvider
user = User.find_by(email: params[:email])
if user&.auth_token
if AuthProvider.active.providable_type != DatabaseProvider.name
NotificationCenter.call type: 'notify_user_auth_migration',
receiver: user,
attached_object: user
render json: {status: 'processing'}, status: :ok
else
render json: {status: 'error', error: I18n.t('members.current_authentication_method_no_code')}, status: :bad_request
end
else
render json: {status: 'error', error: I18n.t('members.requested_account_does_not_exists')}, status: :bad_request
end
end
private
def set_provider
@provider = AuthProvider.find(params[:id])
end
def provider_params
if params['auth_provider']['providable_type'] == DatabaseProvider.name
params.require(:auth_provider).permit(:name, :providable_type)
elsif params['auth_provider']['providable_type'] == OAuth2Provider.name
params.require(:auth_provider).permit(:name, :providable_type, providable_attributes: [
:id, :base_url, :token_endpoint, :authorization_endpoint, :logout_endpoint, :profile_url, :client_id, :client_secret,
o_auth2_mappings_attributes: [
:id, :local_model, :local_field, :api_field, :api_endpoint, :api_data_type, :_destroy,
transformation: [:type, :format, :true_value, :false_value, mapping: [:from, :to]]
]
])
end
end
end