diff --git a/app/controllers/passwords_controller.rb b/app/controllers/passwords_controller.rb index 189232cb..3d8be306 100644 --- a/app/controllers/passwords_controller.rb +++ b/app/controllers/passwords_controller.rb @@ -1,6 +1,7 @@ class PasswordsController < ApplicationController allow_unauthenticated_access before_action :set_user_by_token, only: %i[ edit update ] + layout "session" def new end @@ -9,7 +10,6 @@ def create if user = User.find_by(email: params[:email]) PasswordsMailer.reset(user).deliver_later end - redirect_to new_session_path, notice: "Password reset instructions sent (if user with that email address exists)." end @@ -25,9 +25,10 @@ def update end private - def set_user_by_token - @user = User.find_by_password_reset_token!(params[:token]) - rescue ActiveSupport::MessageVerifier::InvalidSignature - redirect_to new_password_path, alert: "Password reset link is invalid or has expired." - end + + def set_user_by_token + @user = User.find_by_password_reset_token!(params[:token]) + rescue ActiveSupport::MessageVerifier::InvalidSignature + redirect_to new_password_path, alert: "Password reset link is invalid or has expired." + end end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 0112caaa..13a35c9b 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -1,6 +1,7 @@ class SessionsController < ApplicationController allow_unauthenticated_access only: %i[ new create ] rate_limit to: 10, within: 3.minutes, only: :create, with: -> { redirect_to new_session_url, alert: "Try again later." } + layout "session" def new end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3004a556..16aed061 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,54 @@ class UsersController < ApplicationController + before_action :set_user, only: %i[ edit update destroy ] + def index + @users = User.all + end + + def new + @user = User.new + end + + def create + @user = User.new(user_params) + + respond_to do |format| + if @user.save + format.html { redirect_to users_path, notice: "User was successfully created." } + else + format.html { render :new, status: :unprocessable_entity } + end + end + end + + def edit + end + + def update + respond_to do |format| + if @user.update(user_params) + format.html { redirect_to users_path, notice: "User was successfully updated." } + else + format.html { render :edit, status: :unprocessable_entity } + end + end + end + + def destroy + @user.destroy! + + respond_to do |format| + format.html { redirect_to users_path, status: :see_other, notice: "User was successfully destroyed." } + end + end + + private + + def set_user + @user = User.find(params.expect(:id)) + end + + def user_params + params.expect(user: [ :email, :password, :is_admin ]) end end diff --git a/app/models/user.rb b/app/models/user.rb index a4cd542f..bd0f7a58 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,6 +4,6 @@ class User < ApplicationRecord normalizes :email, with: ->(e) { e.strip.downcase } - validates :email, presence: true, uniqueness: true + validates :email, presence: true, uniqueness: true, format: URI::MailTo::EMAIL_REGEXP validates :password_digest, presence: true end diff --git a/app/views/layouts/_sidebar.html.erb b/app/views/layouts/_sidebar.html.erb index 47f5728a..89ac0ea1 100644 --- a/app/views/layouts/_sidebar.html.erb +++ b/app/views/layouts/_sidebar.html.erb @@ -1,4 +1,4 @@ - +