1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-01 12:24:28 +01:00
fab-manager/app/controllers/api/users_controller.rb
Sylvain 0cd841da33 rubocop api controllers
TODO:
 - events controller
 - availabilies controller
 - members controller
 - plans controller
2019-01-16 16:28:25 +01:00

47 lines
1.4 KiB
Ruby

# frozen_string_literal: true
# API Controller for resources of type Users with role :partner
class API::UsersController < API::ApiController
before_action :authenticate_user!
def index
if current_user.admin? && params[:role] == 'partner'
@users = User.with_role(:partner).includes(:profile)
else
head 403
end
end
def create
if current_user.admin?
generated_password = Devise.friendly_token.first(8)
@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')
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
def partner_params
params.require(:user).permit(:email, :first_name, :last_name)
end
end