1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/app/services/user_service.rb

69 lines
2.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
# helpers for managing users with special roles
class UserService
def self.create_partner(params)
generated_password = Devise.friendly_token.first(8)
group_id = Group.first.id
user = User.new(
email: params[:email],
username: "#{params[:first_name]}#{params[:last_name]}".parameterize,
password: generated_password,
password_confirmation: generated_password,
group_id: group_id
)
user.build_profile(
first_name: params[:first_name],
last_name: params[:last_name],
phone: '0000000000'
)
user.build_statistic_profile(
gender: true,
birthday: DateTime.current
)
saved = user.save
if saved
user.remove_role :member
user.add_role :partner
end
{ saved: saved, user: user }
end
def self.create_admin(params)
generated_password = Devise.friendly_token.first(8)
admin = User.new(params.merge(password: generated_password))
admin.send :set_slug
# we associate the admin group to prevent linking any other 'normal' group (which won't be deletable afterwards)
admin.group = Group.find_by(slug: 'admins')
# if the authentication is made through an SSO, generate a migration token
admin.generate_auth_migration_token unless AuthProvider.active.providable_type == DatabaseProvider.name
2019-12-02 10:39:42 +01:00
saved = admin.save
if saved
admin.send_confirmation_instructions
admin.add_role(:admin)
admin.remove_role(:member)
UsersMailer.delay.notify_user_account_created(admin, generated_password)
end
{ saved: saved, user: admin }
end
2020-04-21 17:24:22 +02:00
def self.create_manager(params)
generated_password = Devise.friendly_token.first(8)
manager = User.new(params.merge(password: generated_password))
manager.send :set_slug
saved = manager.save
if saved
manager.send_confirmation_instructions
2020-04-22 10:09:16 +02:00
manager.add_role(:manager)
2020-04-21 17:24:22 +02:00
manager.remove_role(:member)
UsersMailer.delay.notify_user_account_created(manager, generated_password)
end
{ saved: saved, user: manager }
end
end