module Symphonia class AccountsController < ApplicationController before_action :login_require, only: [:show, :edit, :update] before_action -> { menu_item(:my_account) }, only: [:show, :edit, :update] before_action :prepare_user, only: [:register, :create] helper Recaptcha::ClientHelper if defined? Recaptcha def show @user = current_account respond_to do |format| format.html { render(template: "#{@user.class.name.underscore.pluralize}/show") } format.json { render json: @user } end end def register menu_item(:register) end def create menu_item(:register) @user.attributes = user_params @user.status = if Symphonia.config[:self_activation_enabled] @user.class.statuses[:active] else @user.class.statuses[:pending] end respond_to do |format| verified = if defined?(Recaptcha) @user.valid? && verify_recaptcha(model: @user) else true end if verified && @user.save Notifier.activation_user(@user).deliver_now Notifier.user_registered(@user).deliver_now format.html { redirect_to '/', notice: t(:text_user_registered) } format.json { render status: :created } else format.html { render action: 'register' } format.json { render json: @user.errors, status: :unprocessable_entity } end end end def edit @user = current_account end def update @user = current_account @user.attributes = user_params respond_to do |format| @user.edited_by = User.current.logged_in? && User.current @user.edited_at = Time.now if @user.save format.html { redirect_to({ action: 'show' }, notice: t(:text_updated)) } format.json { head :no_content } format.js else format.html { render action: 'edit' } format.json { render json: @user.errors, status: :unprocessable_entity } end end end #---- def new_activation end def resend_activation @user = find_account_by_mail(params.require(:mail)) if @user if @user.active? redirect_to root_path, flash: { error: t(:text_user_alerady_active) } else @user.reset_perishable_token! Notifier.activation_user(@user).deliver_later redirect_to root_path, notice: t(:text_activation_resend) end else redirect_to root_path, flash: { error: t(:text_user_not_found) } end end def activation @user = find_account_by_token(params[:activation_code]) if @user @user.activate! redirect_to(login_path, notice: t(:text_activation_success)) else redirect_to root_path, flash: { error: t(:text_user_not_found_or_token_invalid) } end end def current end def admin end def reset_password @user = find_account_by_token(params.require(:id)) return render_404 if @user.nil? if params[:password] # && params[:password_confirmation] @user.password = params[:password] # @user.password_confirmation = params[:password_confirmation] end if @user.changed? && @user.save return redirect_to(user_current_path, notice: t(:text_updated)) end end def lost_password @user = find_account_by_mail(params[:mail]) if params[:mail] if @user if @user.active? @user.reset_perishable_token! Notifier.reset_password_user(@user).deliver_later redirect_to login_path, notice: t(:text_reset_password_resend) else redirect_to login_path, flash: { error: t("authlogic.error_messages.not_active") } end else respond_to do |format| format.html format.js end end end private def prepare_user return render_403 unless Symphonia.config[:allow_registrations] @user = User.new end def user_params params.require(:user).permit(:login, :name, :first_name, :last_name, :password, :password_confirmation, :email, :mail, preference_ids: []) end def current_account User.current end def find_account_by_mail(mail) User.where(email: mail).first end def find_account_by_token(id) User.find_using_perishable_token(id, 1.week) end end end