2016-11-06 18:29:00 +08:00
|
|
|
module Users
|
|
|
|
class InvitationsController < Devise::InvitationsController
|
2017-05-12 04:20:06 +08:00
|
|
|
include InputSanitizeHelper
|
2016-11-06 18:29:00 +08:00
|
|
|
include UsersGenerator
|
|
|
|
|
2016-11-23 21:28:32 +08:00
|
|
|
prepend_before_action :check_captcha, only: [:update]
|
|
|
|
|
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-25 18:15:10 +08:00
|
|
|
|
2016-11-06 18:29:00 +08:00
|
|
|
def update
|
2017-01-24 23:57:14 +08:00
|
|
|
# Instantialize a new team with the provided name
|
2017-01-25 00:06:51 +08:00
|
|
|
@team = Team.new
|
2017-01-24 23:57:14 +08:00
|
|
|
@team.name = params[:team][:name]
|
2016-11-06 18:29:00 +08:00
|
|
|
|
|
|
|
super do |user|
|
|
|
|
if user.errors.empty?
|
2017-01-24 23:57:14 +08:00
|
|
|
@team.created_by = user
|
|
|
|
@team.save
|
2016-11-06 18:29:00 +08:00
|
|
|
|
2017-01-25 00:06:51 +08:00
|
|
|
UserTeam.create(
|
2016-11-06 18:29:00 +08:00
|
|
|
user: user,
|
2017-01-24 23:57:14 +08:00
|
|
|
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
|
2017-01-24 23:57:14 +08:00
|
|
|
unless @team.valid?
|
2016-11-18 17:57:03 +08:00
|
|
|
# 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
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# In any case, add the team name error
|
|
|
|
resource.errors.add(:base, @team.errors.to_a.first)
|
2016-11-18 17:57:03 +08:00
|
|
|
return resource
|
|
|
|
end
|
|
|
|
|
|
|
|
super
|
2016-11-06 18:29:00 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def invite_users
|
|
|
|
@invite_results = []
|
|
|
|
@too_many_emails = false
|
|
|
|
|
|
|
|
cntr = 0
|
|
|
|
@emails.each do |email|
|
|
|
|
cntr += 1
|
|
|
|
|
|
|
|
if cntr > Constants::INVITE_USERS_LIMIT
|
|
|
|
@too_many_emails = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
password = generate_user_password
|
|
|
|
|
|
|
|
# Check if user already exists
|
|
|
|
user = nil
|
|
|
|
user = User.find_by_email(email) if User.exists?(email: email)
|
|
|
|
|
|
|
|
result = { email: email }
|
|
|
|
|
|
|
|
if user.present?
|
|
|
|
result[:status] = :user_exists
|
|
|
|
result[:user] = user
|
|
|
|
else
|
|
|
|
# Validate the user data
|
|
|
|
error = !(Constants::BASIC_EMAIL_REGEX === email)
|
|
|
|
error = validate_user(email, email, password).count > 0 unless error
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-11-06 18:29:00 +08:00
|
|
|
if !error
|
|
|
|
user = User.invite!(
|
|
|
|
full_name: email,
|
|
|
|
email: email,
|
|
|
|
initials: email.upcase[0..1],
|
|
|
|
skip_invitation: true
|
|
|
|
)
|
2016-11-10 21:45:24 +08:00
|
|
|
user.update(invited_by: @user)
|
2016-11-06 18:29:00 +08:00
|
|
|
|
|
|
|
result[:status] = :user_created
|
|
|
|
result[:user] = user
|
|
|
|
|
|
|
|
# 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
|
|
|
|
else
|
|
|
|
# Return invalid status
|
|
|
|
result[:status] = :user_invalid
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
if @team.present? && result[:status] != :user_invalid
|
2017-01-25 00:06:51 +08:00
|
|
|
if UserTeam.exists?(user: user, team: @team)
|
2017-01-24 23:57:14 +08:00
|
|
|
user_team =
|
2017-01-25 00:06:51 +08:00
|
|
|
UserTeam.where(user: user, team: @team).first
|
2016-11-06 18:29:00 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
result[:status] = :user_exists_and_in_team
|
2016-11-06 18:29:00 +08:00
|
|
|
else
|
2017-01-24 23:57:14 +08:00
|
|
|
# Also generate user team relation
|
2017-01-25 00:06:51 +08:00
|
|
|
user_team = UserTeam.new(
|
2016-11-06 18:29:00 +08:00
|
|
|
user: user,
|
2017-01-24 23:57:14 +08:00
|
|
|
team: @team,
|
2016-11-06 18:29:00 +08:00
|
|
|
role: @role
|
|
|
|
)
|
2017-01-24 23:57:14 +08:00
|
|
|
user_team.save
|
2016-11-06 18:29:00 +08:00
|
|
|
|
2016-11-07 16:47:30 +08:00
|
|
|
generate_notification(
|
|
|
|
@user,
|
|
|
|
user,
|
2017-01-24 23:57:14 +08:00
|
|
|
user_team.role_str,
|
|
|
|
user_team.team
|
2016-11-07 16:47:30 +08:00
|
|
|
)
|
|
|
|
|
2016-11-15 22:04:15 +08:00
|
|
|
if result[:status] == :user_exists && !user.confirmed?
|
2017-01-24 23:57:14 +08:00
|
|
|
result[:status] = :user_exists_unconfirmed_invited_to_team
|
2016-11-15 22:04:15 +08:00
|
|
|
elsif result[:status] == :user_exists
|
2017-01-24 23:57:14 +08:00
|
|
|
result[:status] = :user_exists_invited_to_team
|
2016-11-06 18:29:00 +08:00
|
|
|
else
|
2017-01-24 23:57:14 +08:00
|
|
|
result[:status] = :user_created_invited_to_team
|
2016-11-06 18:29:00 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
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
|
|
|
|
|
2016-11-25 18:15:10 +08:00
|
|
|
def update_sanitized_params
|
|
|
|
# Solution for Devise < 4.0.0
|
2017-10-17 22:00:26 +08:00
|
|
|
devise_parameter_sanitizer.permit(:sign_up, keys: [:full_name])
|
2016-11-25 18:15:10 +08:00
|
|
|
end
|
|
|
|
|
2016-11-23 21:28:32 +08:00
|
|
|
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
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
def generate_notification(user, target_user, role, team)
|
|
|
|
title = I18n.t('notifications.assign_user_to_team',
|
2016-11-07 16:47:30 +08:00
|
|
|
assigned_user: target_user.name,
|
|
|
|
role: role,
|
2017-01-24 23:57:14 +08:00
|
|
|
team: team.name,
|
2016-11-07 16:47:30 +08:00
|
|
|
assigned_by_user: user.name)
|
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
message = "#{I18n.t('search.index.team')} #{team.name}"
|
2016-11-07 16:47:30 +08:00
|
|
|
notification = Notification.create(
|
|
|
|
type_of: :assignment,
|
2017-05-12 16:10:50 +08:00
|
|
|
title: sanitize_input(title),
|
2017-05-12 04:20:06 +08:00
|
|
|
message: sanitize_input(message)
|
2016-11-07 16:47:30 +08:00
|
|
|
)
|
|
|
|
|
2017-08-10 20:29:01 +08:00
|
|
|
if target_user.settings[:notifications][:assignments]
|
2016-11-07 16:47:30 +08:00
|
|
|
UserNotification.create(notification: notification, user: target_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-06 18:29:00 +08:00
|
|
|
def check_invite_users_permission
|
|
|
|
@user = current_user
|
2017-04-18 23:46:02 +08:00
|
|
|
@emails = params[:emails].map(&:downcase)
|
2017-01-25 00:06:51 +08:00
|
|
|
@team = Team.find_by_id(params['teamId'])
|
2016-11-06 18:29:00 +08:00
|
|
|
@role = params['role']
|
2016-11-03 18:27:17 +08:00
|
|
|
|
2016-11-06 18:29:00 +08:00
|
|
|
render_403 if @emails && @emails.empty?
|
2017-01-24 23:57:14 +08:00
|
|
|
render_403 if @team && !is_admin_of_team(@team)
|
2017-01-25 00:06:51 +08:00
|
|
|
render_403 if @role && !UserTeam.roles.keys.include?(@role)
|
2016-11-06 18:29:00 +08:00
|
|
|
end
|
2016-11-03 18:27:17 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|