diff --git a/app/controllers/asks_controller.rb b/app/controllers/asks_controller.rb index 615be41..dd1f291 100644 --- a/app/controllers/asks_controller.rb +++ b/app/controllers/asks_controller.rb @@ -202,6 +202,7 @@ class AsksController < ApplicationController ask_setting = AskSetting.first ask_setting = AskSetting.create() if ask_setting.nil? end + @ask_setting = ask_setting check_fields = ask_setting.default_setting.select{|k,v| v}.keys & ask_setting.default_setting_required.select{|k,v| v}.keys - AskSetting::No_required flag = true check_fields.each do |f| @@ -265,8 +266,18 @@ class AsksController < ApplicationController if (email_regex ? ::Regexp.new(email_regex).match(temp_params["mail"].to_s) : true) && flag @disp_fields_infos = AskSetting.get_disp_fields_infos(true, ask_setting, true, nil, true) @usage_rule = ask_setting.usage_rule + if temp_params['mail'].blank? + temp_params = temp_params.merge({ + situation: AskTicketStatus::DefaultKeys[1] + }) + end @ask_question.update_attributes(temp_params) - build_email(@ask_question) + if ask_setting.validate_enable && AskSafeEmail.where(:email=> @ask_question.mail).count == 0 + build_verification_email(@ask_question) + else + @ask_question.update(:situation => AskTicketStatus::DefaultKeys[1]) + build_email(@ask_question) + end redirect_to "#{params[:referer_url]}?method=thank" else redirect_to "#{params[:referer_url]}?method=sorry" @@ -284,6 +295,61 @@ class AsksController < ApplicationController {} end + def render_404 + render :file => "#{Rails.root}/app/views/errors/404", :layout => false, :status => :not_found, :formats => [:html] + end + + def verify_email + @ask_question = AskQuestion.where(:id=>params[:id]).first + if @ask_question.nil? + render_404 and return + elsif @ask_question.situation != AskTicketStatus::DefaultKeys[0] + @already_verify = true + else + @already_verify = false + @ask_question.update(:situation => AskTicketStatus::DefaultKeys[1]) + AskSafeEmail.create(:email=> @ask_question.mail) + build_email(@ask_question) + end + end + + def build_verification_email(email_er) + email = Email.new + email.save + email_er.email_id = email.id + email_er.save + + group_mail = email_er.mail + manager_emails = email_er.reviewer_emails + mail_sentdate = DateTime.now + + site = current_site rescue Site.first + mail_from = site.title_translations[site.default_locale] + host_url = Site.first.root_url rescue "http://" + if host_url == "http://" + host_url = request.protocol + request.host_with_port + end + verify_url = "#{host_url}/#{I18n.locale}/xhr/asks/verify_email/#{email_er.id}" + verify_link = "#{verify_url}" + mail_subject = mail_from+": #{t('ask.email_verification_notification')}" + email_er.email.update_attributes( + :mail_lang=> site.default_locale, + :create_user=>(current_user rescue nil), + :mail_sentdate=>mail_sentdate, + :module_app=>@module_app, + :mail_to=>group_mail, + :mail_subject=>mail_subject, + :template=>'asks/email_verification', + :template_data=>{ + "validation_email_content" => @ask_setting.validation_email_content, + "verify_link" => verify_link, + "site_host" => host_url, + }, + :mail_reply_to => (manager_emails.empty? ? nil : manager_emails) + ) + # email_er.email.deliver + end + def build_email(email_er) email = Email.new email.save diff --git a/app/models/ask_category_setting.rb b/app/models/ask_category_setting.rb index 3b1fb7c..15402e4 100644 --- a/app/models/ask_category_setting.rb +++ b/app/models/ask_category_setting.rb @@ -142,6 +142,12 @@ class AskCategorySetting tmp = nil if !(self.email_regex_enable) || ((self.default_setting[:mail] == false) rescue false) tmp end + + field :email_regex_enable, type: Boolean, default: false + field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z' + field :validate_enable, type: Boolean, default: false + field :validation_email_content, type: String, localize: true + field :tmp_sort_number, type: Hash, default: {} # For Frontend field :sort_number, type: Hash, default: {} field :default_sort_number, type: Hash, default: {} @@ -154,8 +160,7 @@ class AskCategorySetting field :usage_rule, type: String, default: '' field :category_id, type: String field :title_layout, type: Integer, default: 0 - field :email_regex_enable, type: Boolean, default: false - field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z' + field :need_check_customs, type: Array, default: [] #From AskSetting field :reject_customs, type: Array, default: [] #From AskSetting field :agree_customs, type: Array, default: [] #From AskSetting diff --git a/app/models/ask_question.rb b/app/models/ask_question.rb index 91fcc2c..d661de8 100644 --- a/app/models/ask_question.rb +++ b/app/models/ask_question.rb @@ -20,12 +20,13 @@ class AskQuestion field :reply, type: String field :comment, type: String - field :situation, type: String, default: "is_waiting" #預設待處理 field :send_email, type: Boolean, default: false + field :situation, type: String, default: "is_email_not_confirmed" # 預設email未驗證 field :email_id - field :custom_values, type: Hash,default: {} - field :agree_show,type: Boolean,default: false - field :agree_usage,type: Boolean,default: false + field :verify_email_id + field :custom_values, type: Hash, default: {} + field :agree_show, type: Boolean, default: false + field :agree_usage, type: Boolean, default: false field :review_time, type: DateTime belongs_to :reviewer , :class_name=>"MemberProfile", :foreign_key => :reviewer_id @@ -36,7 +37,11 @@ class AskQuestion self.serial_number = last_serial_number end def email - mail = Email.find(self.email_id) rescue nil + mail = Email.where(:id=>self.email_id).first + end + + def verify_email + verify_email = Email.where(:id=>self.verify_email_id).first end def reviewer_emails email_address = AskAdmin.or(:category_ids.in => [self.category_id, [], nil]).pluck(:email).select{|s| s.present?}.uniq rescue [] diff --git a/app/models/ask_safe_email.rb b/app/models/ask_safe_email.rb new file mode 100644 index 0000000..a2a103d --- /dev/null +++ b/app/models/ask_safe_email.rb @@ -0,0 +1,5 @@ +class AskSafeEmail + include Mongoid::Document + field :email, type: String + index({email: 1}, { unique: false, background: true }) +end \ No newline at end of file diff --git a/app/models/ask_setting.rb b/app/models/ask_setting.rb index 3457675..695a91d 100644 --- a/app/models/ask_setting.rb +++ b/app/models/ask_setting.rb @@ -93,6 +93,9 @@ class AskSetting end field :email_regex_enable, type: Boolean, default: false field :email_regex, type: String, default: '\A[^@\s]+@([^@.\s]+\.)+[^@.\s]+\z' + field :validate_enable, type: Boolean, default: false + field :validation_email_content, type: String, localize: true + field :tmp_sort_number, type: Hash, default: {} field :sort_number, type: Hash, default: {} field :default_setting, type: Hash,default: {title:true,ask_category_id: true,name: true,sex: true,mail: true,phone: true,appointment: true,recaptcha: true,agree_show: true,agree_usage: true} diff --git a/app/models/ask_ticket_status.rb b/app/models/ask_ticket_status.rb index 6561b6a..6985389 100644 --- a/app/models/ask_ticket_status.rb +++ b/app/models/ask_ticket_status.rb @@ -1,7 +1,7 @@ class AskTicketStatus include Mongoid::Document include Mongoid::Timestamps - DefaultKeys = ["is_waiting", "is_processed", "is_referral", "is_published"] + DefaultKeys = ["is_email_not_confirmed" , "is_waiting", "is_processed", "is_referral", "is_published"] field :title, type: String, localize: true field :is_default, type: Boolean, default: false # if true => cannot delete field :key, type: String diff --git a/app/views/admin/asks/_ask_setting.html.erb b/app/views/admin/asks/_ask_setting.html.erb index 7fe0d3a..4dce4ac 100644 --- a/app/views/admin/asks/_ask_setting.html.erb +++ b/app/views/admin/asks/_ask_setting.html.erb @@ -174,6 +174,25 @@ <%= f.text_field :email_regex, {:id=>"ask_email_regex", :style => "width: 30em;"}.merge(f.object.email_regex_enable ? {} : {:disabled=>"disabled"}) %> + +
+
+ +
+
+ +
+ <%= multiple_lang_tag_for_ask( + nil, + 'text_area', + 'validation_email_content_translations', + f.object.validation_email_content_translations, + { + class: 'ckeditor' + }) %> +
+
+