2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Users with role :partner
|
2016-03-23 18:39:41 +01:00
|
|
|
class API::UsersController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def index
|
2019-01-14 12:57:31 +01:00
|
|
|
if current_user.admin? && params[:role] == 'partner'
|
2016-03-23 18:39:41 +01:00
|
|
|
@users = User.with_role(:partner).includes(:profile)
|
|
|
|
else
|
|
|
|
head 403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-01-14 12:57:31 +01:00
|
|
|
if current_user.admin?
|
2016-03-23 18:39:41 +01:00
|
|
|
generated_password = Devise.friendly_token.first(8)
|
2019-01-07 12:48:22 +01:00
|
|
|
@user = User.new(email: partner_params[:email],
|
|
|
|
username: "#{partner_params[:first_name]}#{partner_params[:last_name]}",
|
|
|
|
password: generated_password,
|
|
|
|
password_confirmation: generated_password,
|
|
|
|
group_id: Group.first.id)
|
|
|
|
@user.build_profile(first_name: partner_params[:first_name],
|
|
|
|
last_name: partner_params[:last_name],
|
|
|
|
gender: true,
|
|
|
|
birthday: Time.now,
|
|
|
|
phone: '0000000000')
|
2016-03-23 18:39:41 +01:00
|
|
|
|
|
|
|
if @user.save
|
|
|
|
@user.remove_role :member
|
|
|
|
@user.add_role :partner
|
|
|
|
render status: :created
|
|
|
|
else
|
|
|
|
render json: @user.errors.full_messages, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
|
|
|
head 403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-01-07 12:48:22 +01:00
|
|
|
|
2016-03-23 18:39:41 +01:00
|
|
|
def partner_params
|
|
|
|
params.require(:user).permit(:email, :first_name, :last_name)
|
|
|
|
end
|
|
|
|
end
|