scinote-web/app/controllers/users/invitations_controller.rb

198 lines
5.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-11-06 18:29:00 +08:00
module Users
class InvitationsController < Devise::InvitationsController
include InputSanitizeHelper
2016-11-06 18:29:00 +08:00
include UsersGenerator
include NotificationsHelper
2016-11-06 18:29:00 +08:00
prepend_before_action :check_captcha, only: [:update]
prepend_before_action :check_captcha_for_invite, only: [:invite_users]
2016-11-06 18:29:00 +08:00
before_action :check_invite_users_permission, only: :invite_users
2017-06-30 16:23:28 +08:00
before_action :update_sanitized_params, only: :update
2016-11-06 18:29:00 +08:00
def update
return super unless Rails.configuration.x.new_team_on_signup
# Instantialize a new team with the provided name
@team = Team.new
@team.name = params[:team][:name]
2016-11-06 18:29:00 +08:00
super do |user|
if user.errors.empty?
@team.created_by = user
@team.save
2016-11-06 18:29:00 +08:00
UserTeam.create(
2016-11-06 18:29:00 +08:00
user: user,
team: @team,
2016-11-06 18:29:00 +08:00
role: 'admin'
)
end
2016-02-12 23:52:43 +08:00
end
end
2016-11-06 18:29:00 +08:00
def accept_resource
return super unless Rails.configuration.x.new_team_on_signup
unless @team.valid?
# Find the user being invited
resource = User.find_by_invitation_token(
update_resource_params[:invitation_token],
false
)
# Check if user's data (passwords etc.) is valid
resource.assign_attributes(
update_resource_params.except(:invitation_token)
)
resource.valid? # Call validation to generate errors
# In any case, add the team name error
resource.errors.add(:base, @team.errors.to_a.first)
return resource
end
super
2016-11-06 18:29:00 +08:00
end
def invite_users
@invite_results = []
@too_many_emails = false
@emails.each_with_index do |email, email_counter|
# email_counter starts with 0
if email_counter >= Constants::INVITE_USERS_LIMIT
2016-11-06 18:29:00 +08:00
@too_many_emails = true
break
end
result = { email: email }
unless Constants::BASIC_EMAIL_REGEX.match?(email)
result[:status] = :user_invalid
@invite_results << result
next
end
# Check if user already exists
user = User.find_by_email(email)
2016-11-06 18:29:00 +08:00
if user
2016-11-06 18:29:00 +08:00
result[:status] = :user_exists
result[:user] = user
else
user = User.invite!(
full_name: email,
email: email,
initials: email.upcase[0..1],
skip_invitation: true
)
user.update(invited_by: @user)
2016-11-06 18:29:00 +08:00
result[:status] = :user_created
result[:user] = user
2016-11-06 18:29:00 +08:00
# Sending email invitation is done in background job to prevent
# issues with email delivery. Also invite method must be call
# with :skip_invitation attribute set to true - see above.
user.delay.deliver_invitation
2016-11-06 18:29:00 +08:00
end
if @team && user
user_team = UserTeam.find_by_user_id_and_team_id(user.id, @team.id)
if user_team
result[:status] = :user_exists_and_in_team
2016-11-06 18:29:00 +08:00
else
# Also generate user team relation
user_team = UserTeam.new(
2016-11-06 18:29:00 +08:00
user: user,
team: @team,
2016-11-06 18:29:00 +08:00
role: @role
)
user_team.save
2016-11-06 18:29:00 +08:00
generate_notification(
@user,
user,
user_team.team,
user_team.role_str
)
Activities::CreateActivityService
.call(activity_type: :invite_user_to_team,
owner: current_user,
subject: current_team,
team: current_team,
message_items: {
team: current_team.id,
user_invited: user.id,
role: user_team.role_str
})
result[:status] = if result[:status] == :user_exists && !user.confirmed?
:user_exists_unconfirmed_invited_to_team
elsif result[:status] == :user_exists
:user_exists_invited_to_team
else
:user_created_invited_to_team
end
2016-11-06 18:29:00 +08:00
end
result[:user_team] = user_team
2016-11-06 18:29:00 +08:00
end
@invite_results << result
end
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'shared/invite_users_modal_results.html.erb'
)
}
end
end
2016-02-12 23:52:43 +08:00
end
2016-11-06 18:29:00 +08:00
private
def update_sanitized_params
# Solution for Devise < 4.0.0
devise_parameter_sanitizer.permit(:accept_invitation, keys: [:full_name])
end
def check_captcha
if Rails.configuration.x.enable_recaptcha
unless verify_recaptcha
self.resource = resource_class.new
resource.invitation_token = update_resource_params[:invitation_token]
respond_with_navigational(resource) { render :edit }
end
end
end
def check_captcha_for_invite
if Rails.configuration.x.enable_recaptcha
unless verify_recaptcha
render json: { recaptcha_error: t('invite_users.errors.recaptcha') },
status: :unprocessable_entity
end
end
end
2016-11-06 18:29:00 +08:00
def check_invite_users_permission
@user = current_user
@emails = params[:emails]&.map(&:downcase)
@team = Team.find_by_id(params['teamId'])
2016-11-06 18:29:00 +08:00
@role = params['role']
2019-08-14 20:47:02 +08:00
return render_403 if @team && @role.nil? # if we select team, we must select role
return render_403 if @emails.blank? # We must have at least one email
return render_403 if @team && !can_manage_team_users?(@team) # if we select team, we must check permission
return render_403 if @role && !UserTeam.roles.key?(@role) # if we select role, we must check that this role exist
2016-11-06 18:29:00 +08:00
end
end
2016-02-12 23:52:43 +08:00
end