2018-08-22 17:38:39 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
class UserIdentitiesController < BaseController
|
|
|
|
before_action :load_user
|
|
|
|
before_action :load_user_identity, only: %i(show update destroy)
|
|
|
|
|
|
|
|
def index
|
2018-08-24 22:41:26 +08:00
|
|
|
identities = @user.user_identities
|
|
|
|
.page(params.dig(:page, :number))
|
|
|
|
.per(params.dig(:page, :size))
|
|
|
|
render jsonapi: identities, each_serializer: UserIdentitySerializer
|
2018-08-22 17:38:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
identity = @user.user_identities.create!(user_identity_params)
|
2018-08-24 22:41:26 +08:00
|
|
|
render jsonapi: identity,
|
2018-08-22 17:38:39 +08:00
|
|
|
serializer: UserIdentitySerializer,
|
|
|
|
status: :created
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
2018-08-24 22:41:26 +08:00
|
|
|
render jsonapi: @identity, serializer: UserIdentitySerializer
|
2018-08-22 17:38:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
@identity.attributes = update_user_identity_params
|
|
|
|
if @identity.changed? && @identity.save!
|
2018-08-24 22:41:26 +08:00
|
|
|
render jsonapi: @identity, serializer: UserIdentitySerializer
|
2018-08-22 17:38:39 +08:00
|
|
|
else
|
2018-10-23 23:52:48 +08:00
|
|
|
render body: nil, status: :no_content
|
2018-08-22 17:38:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
@identity.destroy!
|
|
|
|
render body: nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_user
|
|
|
|
@user = current_user if current_user.id == params[:user_id].to_i
|
2018-11-07 22:43:44 +08:00
|
|
|
raise PermissionError.new(User, :read) unless @user
|
2018-08-22 17:38:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_user_identity
|
|
|
|
@identity = @user.user_identities.find(params[:id].to_i)
|
|
|
|
end
|
|
|
|
|
|
|
|
def user_identity_params
|
|
|
|
unless params.require(:data).require(:type) == 'user_identities'
|
2018-10-23 23:52:48 +08:00
|
|
|
raise TypeError
|
2018-08-22 17:38:39 +08:00
|
|
|
end
|
|
|
|
params.require(:data).require(:attributes)
|
|
|
|
params.permit(data: { attributes: %i(provider uid) })[:data]
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_user_identity_params
|
|
|
|
unless params.require(:data).require(:id).to_i == params[:id].to_i
|
2018-10-23 23:52:48 +08:00
|
|
|
raise IDMismatchError
|
2018-08-22 17:38:39 +08:00
|
|
|
end
|
|
|
|
user_identity_params
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|