2016-02-12 23:52:43 +08:00
|
|
|
class Users::RegistrationsController < Devise::RegistrationsController
|
2016-11-22 21:57:41 +08:00
|
|
|
prepend_before_action :check_captcha, only: [:create]
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def avatar
|
2016-07-21 19:11:15 +08:00
|
|
|
user = User.find_by_id(params[:id]) || current_user
|
2016-02-12 23:52:43 +08:00
|
|
|
style = params[:style] || "icon_small"
|
2016-08-18 02:49:20 +08:00
|
|
|
# TODO: Maybe avatar should be an Asset, so it's methods could be used,
|
2016-08-17 21:51:04 +08:00
|
|
|
# e.g. presigned_url in this case
|
2016-07-21 19:11:15 +08:00
|
|
|
redirect_to user.avatar.url(style.to_sym), status: 307
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2016-08-09 20:39:24 +08:00
|
|
|
# Validates asset and then generates S3 upload posts, because
|
|
|
|
# otherwise untracked files could be uploaded to S3
|
2016-02-12 23:52:43 +08:00
|
|
|
def signature
|
|
|
|
respond_to do |format|
|
|
|
|
format.json {
|
|
|
|
|
|
|
|
# Changed avatar values are only used for pre-generating S3 key
|
|
|
|
# and user object is not persisted with this values.
|
2016-08-18 02:49:20 +08:00
|
|
|
current_user.empty_avatar avatar_params[:file].original_filename,
|
|
|
|
avatar_params[:file].size
|
2016-08-05 23:00:29 +08:00
|
|
|
|
2016-08-18 02:49:20 +08:00
|
|
|
validation_asset = Asset.new(avatar_params)
|
|
|
|
if current_user.valid? && validation_asset.valid?
|
|
|
|
render json: {
|
|
|
|
posts: generate_upload_posts
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if validation_asset.errors[:file].any?
|
2016-08-05 23:00:29 +08:00
|
|
|
# Add file content error
|
2016-08-18 02:49:20 +08:00
|
|
|
current_user.errors[:avatar] << validation_asset.errors[:file].first
|
2016-08-05 23:00:29 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
|
|
|
status: 'error',
|
|
|
|
errors: current_user.errors
|
2016-08-05 23:00:29 +08:00
|
|
|
}, status: :bad_request
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_resource(resource, params)
|
2016-07-21 19:11:15 +08:00
|
|
|
@user_avatar_url = avatar_path(current_user, :thumb)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
if params.include? :change_password
|
|
|
|
# Special handling if changing password
|
|
|
|
params.delete(:change_password)
|
|
|
|
if (
|
|
|
|
resource.valid_password?(params[:current_password]) and
|
|
|
|
params.include? :password and
|
|
|
|
params.include? :password_confirmation and
|
|
|
|
params[:password].blank?
|
|
|
|
) then
|
|
|
|
# If new password is blank and we're in process of changing
|
|
|
|
# password, add error to the resource and return false
|
|
|
|
resource.errors.add(:password, :blank)
|
|
|
|
false
|
|
|
|
else
|
|
|
|
resource.update_with_password(params)
|
|
|
|
end
|
|
|
|
elsif params.include? :change_avatar
|
|
|
|
params.delete(:change_avatar)
|
|
|
|
unless params.include? :avatar
|
|
|
|
resource.errors.add(:avatar, :blank)
|
|
|
|
false
|
|
|
|
else
|
|
|
|
resource.update_without_password(params)
|
|
|
|
end
|
|
|
|
elsif params.include? :email or params.include? :password
|
|
|
|
# For changing email or password, validate current_password
|
|
|
|
resource.update_with_password(params)
|
|
|
|
|
|
|
|
else
|
|
|
|
# For changing some attributes, no current_password validation
|
|
|
|
# is required
|
|
|
|
resource.update_without_password(params)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Override default registrations_controller.rb implementation
|
|
|
|
# to support JSON
|
|
|
|
def update
|
|
|
|
change_password = account_update_params.include? :change_password
|
|
|
|
respond_to do |format|
|
|
|
|
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
|
|
|
|
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
|
|
|
|
|
|
|
|
resource_updated = update_resource(resource, account_update_params)
|
2016-09-21 21:54:12 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
yield resource if block_given?
|
|
|
|
if resource_updated
|
|
|
|
# Set "needs confirmation" flash if neccesary
|
|
|
|
if is_flashing_format?
|
|
|
|
flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
|
|
|
|
:update_needs_confirmation : :updated
|
|
|
|
set_flash_message :notice, flash_key
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set "password successfully updated" flash if neccesary
|
|
|
|
if change_password
|
|
|
|
set_flash_message :notice, :password_changed
|
|
|
|
end
|
|
|
|
|
|
|
|
format.html {
|
|
|
|
sign_in resource_name, resource, bypass: true
|
|
|
|
respond_with resource, location: edit_user_registration_path
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
flash.keep
|
|
|
|
sign_in resource_name, resource, bypass: true
|
2016-08-05 23:00:29 +08:00
|
|
|
render json: {}
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
clean_up_passwords resource
|
|
|
|
format.html {
|
|
|
|
respond_with resource, location: edit_user_registration_path
|
|
|
|
}
|
|
|
|
format.json {
|
2016-08-18 02:49:20 +08:00
|
|
|
render json: resource.errors, status: :bad_request
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-30 22:04:02 +08:00
|
|
|
def new
|
|
|
|
render_403 && return unless Rails.configuration.x.enable_user_registration
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def create
|
2017-01-30 22:04:02 +08:00
|
|
|
render_403 && return unless Rails.configuration.x.enable_user_registration
|
|
|
|
|
2016-11-25 16:56:07 +08:00
|
|
|
build_resource(sign_up_params)
|
|
|
|
valid_resource = resource.valid?
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# Create new team for the new user
|
2017-01-25 00:06:51 +08:00
|
|
|
@team = Team.new
|
2017-01-24 23:57:14 +08:00
|
|
|
@team.name = params[:team][:name]
|
|
|
|
valid_team = @team.valid?
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
if valid_team && valid_resource
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# this must be called after @team variable is defined. Otherwise this
|
2016-02-12 23:52:43 +08:00
|
|
|
# variable won't be accessable in view.
|
|
|
|
super do |resource|
|
2017-01-05 22:27:18 +08:00
|
|
|
# Set the confirmed_at == created_at IF not using email confirmations
|
|
|
|
unless Rails.configuration.x.enable_email_confirmations
|
|
|
|
resource.update(confirmed_at: resource.created_at)
|
|
|
|
end
|
|
|
|
|
2016-11-25 16:56:07 +08:00
|
|
|
if resource.valid? && resource.persisted?
|
2017-01-24 23:57:14 +08:00
|
|
|
@team.created_by = resource # set created_by for oraganization
|
|
|
|
@team.save
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# Add this user to the team as owner
|
2017-01-25 00:06:51 +08:00
|
|
|
UserTeam.create(
|
2016-02-12 23:52:43 +08:00
|
|
|
user: resource,
|
2017-01-24 23:57:14 +08:00
|
|
|
team: @team,
|
2016-02-12 23:52:43 +08:00
|
|
|
role: :admin
|
|
|
|
)
|
2016-10-12 18:02:04 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# set current team to new user
|
|
|
|
resource.current_team_id = @team.id
|
2016-10-12 18:02:04 +08:00
|
|
|
resource.save
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
render :new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
# Called upon creating User (before .save). Permits parameters and extracts
|
|
|
|
# initials from :full_name (takes at most 4 chars). If :full_name is empty, it
|
|
|
|
# uses "PLCH" as a placeholder (user won't get error complaining about
|
|
|
|
# initials being empty.
|
|
|
|
def sign_up_params
|
|
|
|
tmp = params.require(:user).permit(:full_name, :initials, :email, :password, :password_confirmation)
|
|
|
|
initials = tmp[:full_name].titleize.scan(/[A-Z]+/).join()
|
2016-10-13 16:00:36 +08:00
|
|
|
initials = if initials.strip.empty?
|
|
|
|
'PLCH'
|
|
|
|
else
|
|
|
|
initials[0..Constants::USER_INITIALS_MAX_LENGTH]
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
tmp.merge(:initials => initials)
|
|
|
|
end
|
|
|
|
|
|
|
|
def account_update_params
|
|
|
|
params.require(:user).permit(
|
|
|
|
:full_name,
|
|
|
|
:initials,
|
|
|
|
:avatar,
|
|
|
|
:avatar_file_name,
|
|
|
|
:email,
|
|
|
|
:password,
|
|
|
|
:password_confirmation,
|
|
|
|
:current_password,
|
|
|
|
:change_password,
|
|
|
|
:change_avatar
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2016-08-05 23:00:29 +08:00
|
|
|
def avatar_params
|
|
|
|
params.permit(
|
2016-08-18 02:49:20 +08:00
|
|
|
:file
|
2016-08-05 23:00:29 +08:00
|
|
|
)
|
2016-08-18 02:49:20 +08:00
|
|
|
end
|
2016-08-05 23:00:29 +08:00
|
|
|
|
|
|
|
# Generates posts for uploading files (many sizes of same file)
|
|
|
|
# to S3 server
|
2016-02-12 23:52:43 +08:00
|
|
|
def generate_upload_posts
|
|
|
|
posts = []
|
|
|
|
file_size = current_user.avatar_file_size
|
|
|
|
content_type = current_user.avatar_content_type
|
|
|
|
s3_post = S3_BUCKET.presigned_post(
|
|
|
|
key: current_user.avatar.path[1..-1],
|
|
|
|
success_action_status: '201',
|
|
|
|
acl: 'private',
|
|
|
|
storage_class: "STANDARD",
|
|
|
|
content_length_range: file_size..file_size,
|
|
|
|
content_type: content_type
|
|
|
|
)
|
|
|
|
posts.push({
|
|
|
|
url: s3_post.url,
|
|
|
|
fields: s3_post.fields
|
|
|
|
})
|
|
|
|
|
|
|
|
current_user.avatar.options[:styles].each do |style, option|
|
|
|
|
s3_post = S3_BUCKET.presigned_post(
|
|
|
|
key: current_user.avatar.path(style)[1..-1],
|
|
|
|
success_action_status: '201',
|
|
|
|
acl: 'public-read',
|
|
|
|
storage_class: "REDUCED_REDUNDANCY",
|
2016-10-05 23:45:20 +08:00
|
|
|
content_length_range: 1..Constants::FILE_MAX_SIZE_MB.megabytes,
|
2016-02-12 23:52:43 +08:00
|
|
|
content_type: content_type
|
|
|
|
)
|
|
|
|
posts.push({
|
|
|
|
url: s3_post.url,
|
|
|
|
fields: s3_post.fields,
|
|
|
|
style_option: option,
|
|
|
|
mime_type: content_type
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
posts
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-11-22 21:57:41 +08:00
|
|
|
def check_captcha
|
|
|
|
if Rails.configuration.x.enable_recaptcha
|
|
|
|
unless verify_recaptcha
|
2016-11-29 22:53:58 +08:00
|
|
|
# Construct new resource before rendering :new
|
2016-11-22 21:57:41 +08:00
|
|
|
self.resource = resource_class.new sign_up_params
|
2016-11-29 22:53:58 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# Also validate team
|
2017-01-25 00:06:51 +08:00
|
|
|
@team = Team.new(name: params[:team][:name])
|
2017-01-24 23:57:14 +08:00
|
|
|
@team.valid?
|
2016-11-29 22:53:58 +08:00
|
|
|
|
2016-11-22 21:57:41 +08:00
|
|
|
respond_with_navigational(resource) { render :new }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# Redirect to login page after signing up
|
|
|
|
def after_sign_up_path_for(resource)
|
|
|
|
new_user_session_path
|
|
|
|
end
|
|
|
|
|
|
|
|
# Redirect to login page after signing up
|
|
|
|
def after_inactive_sign_up_path_for(resource)
|
|
|
|
new_user_session_path
|
|
|
|
end
|
|
|
|
end
|