Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gem "bootsnap", require: false

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri windows ]
gem "rdbg", '~> 0.1.0'
end

group :development do
Expand All @@ -68,3 +68,5 @@ group :test do
gem "capybara"
gem "selenium-webdriver"
end

gem 'gon', '~> 6.4'
13 changes: 12 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ GEM
erubi (1.13.0)
globalid (1.2.1)
activesupport (>= 6.1)
gon (6.4.0)
actionpack (>= 3.0.20)
i18n (>= 0.7)
multi_json
request_store (>= 1.0)
i18n (1.14.5)
concurrent-ruby (~> 1.0)
importmap-rails (2.0.1)
Expand Down Expand Up @@ -131,6 +136,7 @@ GEM
mini_mime (1.1.5)
minitest (5.25.0)
msgpack (1.7.2)
multi_json (1.15.0)
mutex_m (0.2.0)
net-imap (0.4.14)
date
Expand Down Expand Up @@ -199,11 +205,15 @@ GEM
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rake (13.2.1)
rdbg (0.1.0)
debug (>= 1.2.2)
rdoc (6.7.0)
psych (>= 4.0.0)
regexp_parser (2.9.2)
reline (0.5.9)
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
rexml (3.3.5)
strscan
rubyzip (2.3.2)
Expand Down Expand Up @@ -270,12 +280,13 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
capybara
debug
gon (~> 6.4)
importmap-rails
jbuilder
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.3, >= 7.1.3.4)
rdbg (~> 0.1.0)
selenium-webdriver
sprockets-rails
stimulus-rails
Expand Down
35 changes: 35 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,37 @@
class ApplicationController < ActionController::Base

private

def authenticate_user!
redirect_to new_session_path, notice: "Sign in to do that" unless user_signed_in?
end

def unauthenticate_user!
redirect_to root_path, notice: "Sign out to do that" if user_signed_in?
end

def current_user
Current.user ||= authenticate_user_form_session
end
helper_method :current_user

def authenticate_user_form_session
User.find(session[:user_name]) if session[:user_name].present?
end

def user_signed_in?
current_user.present?
end
helper_method :user_signed_in?

def login(user)
Current.user = user
reset_session
session[:user_name] = user.name
end

def logout
Current.user = nil
reset_session
end
end
28 changes: 28 additions & 0 deletions app/controllers/email_confirmations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class EmailConfirmationsController < ApplicationController
before_action :authenticate_user!
before_action :set_user_by_token, only: %i[ update ]

def create
EmailMailer.with(
user: current_user,
token: current_user.generate_token_for(:email_confirmation)
).email_confirmation.deliver_later

redirect_to "#", notice: "Check your email to confirm your email"
end

def update
if @user.update(verified: true)
redirect_to root_path, notice: "Email has been confirmed"
else
redirect_to root_path, alert: "Try again"
end
end

private

def set_user_by_token
@user = User.find_by_token_for(:email_confirmation, params[:token])
redirect_to root_path, alert: "Try again" unless @user.present?
end
end
7 changes: 7 additions & 0 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class HomeController < ApplicationController
def index
if user_signed_in?
gon.var_new = current_user.task_groups.find_or_create_by(name: current_user.default_task_group_name).tasks.order_by_time.pluck(:during_time)
end
end
end
41 changes: 41 additions & 0 deletions app/controllers/password_resets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class PasswordResetsController < ApplicationController
before_action :unauthenticate_user!
before_action :set_user_by_token, only: %i[ edit update ]

def new
end

def edit
end

def create
if user = User.find_by(email: params[:email])
PasswordMailer.with(
user: user,
token: user.generate_token_for(:password_reset)
).password_reset.deliver_later
end

redirect_to root_path, notice: "Check your email to reset your password"
end

def update
if @user.update(password_params)
redirect_to new_session_path, notice: "Password has been reset"
else
render :edit, status: :unprocessable_entity
end
end

private

def set_user_by_token
@user = User.find_by_token_for(:password_reset, params[:token])
redirect_to new_password_reset_path, alert: "Try again" unless @user.present?
end

def password_params
params.require(:user).permit(:password, :password_confirmation)
end

end
18 changes: 18 additions & 0 deletions app/controllers/passwords_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class PasswordsController < ApplicationController
before_action :authenticate_user!

def update
if current_user.update(password_params)
redirect_to edit_user_profile_path, notice: "Your password was updated succesfully"
else
redirect_to edit_user_profile_path, status: :unprocessable_entity
end
end

private

def password_params
params.require(:user).permit(:password, :password_confirmation, :password_challenge).with_defaults(password_challenge: "")
end

end
71 changes: 71 additions & 0 deletions app/controllers/secret_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
class SecretGroupsController < ApplicationController
before_action :authenticate_user!
before_action :set_secret_group, only: %i[ show edit update destroy ]

# GET /secret_groups or /secret_groups.json
def index
@secret_groups = current_user.secret_groups.all
end

# GET /secret_groups/name or /secret_groups/name.json
def show
end

# GET /secret_groups/new
def new
@secret_group = SecretGroup.new
end

# GET /secret_groups/1/edit
def edit
end

# POST /secret_groups or /secret_groups.json
def create
@secret_group = current_user.secret_groups.new(secret_group_params)

respond_to do |format|
if @secret_group.save
format.html { redirect_to user_secret_group_path(name: @secret_group.name, owner: @secret_group.owner), notice: "Secret group was successfully created." }
format.json { render :show, status: :created, location: @secret_group }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @secret_group.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /secret_groups/1 or /secret_groups/1.json
def update
respond_to do |format|
if @secret_group.update(secret_group_params)
format.html { redirect_to user_secret_group_path(owner: @secret_group.owner, name: @secret_group.name), notice: "Secret group was successfully updated." }
format.json { render :show, status: :ok, location: @secret_group }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @secret_group.errors, status: :unprocessable_entity }
end
end
end

# DELETE /secret_groups/1 or /secret_groups/1.json
def destroy
@secret_group.destroy!

respond_to do |format|
format.html { redirect_to user_secret_groups_path(owner: current_user.name), notice: "Secret group was successfully destroyed." }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_secret_group
@secret_group = current_user.secret_groups.find_by_name(params[:name])
end

# Only allow a list of trusted parameters through.
def secret_group_params
params.require(:secret_group).permit(:name, :description)
end
end
82 changes: 82 additions & 0 deletions app/controllers/secrets_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class SecretsController < ApplicationController
before_action :authenticate_user!, except: %i[show]

# GET /secrets?group_name=name or /secrets.json?group_name=name
def index
if params[:group_name].present?
@secretgroup = current_user.secret_groups.find_by_name(params[:group_name])
redirect_back fallback_location: root_path, notice: "Group not found" unless @secretgroup.present?
@secrets = @secretgroup.secrets

else
@secrets = current_user.secrets
end
end

# GET /secrets/1 or /secrets/1.json
def show
@secret = Secret.find(params[:id])
end

# GET /secrets/new
def new
@secret = current_user.secret_groups.find_by_name(params[:secret_group_name]).secrets.new
end

# GET /secrets/1/edit
def edit
@secret = current_user.secrets.find(params[:id])
redirect_back fallback_location: root_path, notice: 'Secret not found' unless @secret.present?
end

# POST /secrets or /secrets.json
def create
@secret = current_user.secrets.new(secret_create_params)
respond_to do |format|
if @secret.save
format.html { redirect_to secret_url(@secret), notice: 'Secret was successfully created.' }
format.json { render :show, status: :created, location: @secret }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @secret.errors, status: :unprocessable_entity }
end
end
end

# PATCH/PUT /secrets/1 or /secrets/1.json
def update
@secret = Secret.find(params[:id])
respond_to do |format|
if @secret.update(secret_update_params)
format.html do
redirect_to user_secret_group_secret_path(owner: @secret.owner, secret_group_name: @secret.group_name, id: @secret.id),
notice: 'Secret was successfully updated.'
end
format.json { render :show, status: :ok, location: @secret }
else
format.html { render :edit, status: :unprocessable_entity }
format.json { render json: @secret.errors, status: :unprocessable_entity }
end
end
end

# DELETE /secrets/1 or /secrets/1.json
def destroy
@secret = Secret.find(params[:id])
@secret.destroy!
respond_to do |format|
format.html { redirect_to root_path, notice: 'Secret was successfully destroyed.' }
format.json { head :no_content }
end
end

private

def secret_create_params
params.require(:secret).permit(:name, :description, :value, :interval, :is_google, :length, :group_name)
end

def secret_update_params
params.require(:secret).permit(:name, :description, :group_name)
end
end
31 changes: 31 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
class SessionsController < ApplicationController

def new
end

def create
if params[:email_or_user_name][/\A[^\s@]{4,16}\z/].present?
params[:name] = params[:email_or_user_name]
elsif params[:email_or_user_name].strip.downcase[/\A[a-z0-9+_.-]+@([a-z0-9]+\.)+[a-z]{2,6}\z/].present?
params[:name] = User.find_by(email: params[:email])
end

respond_to do |format|
if @user = User.authenticate_by(name: params[:name], password: params[:password])
login @user
format.html { redirect_to root_path, notice: "You have been signed in." }
format.json { render json: @user, status: :created, location: @user }
else
flash[:alert] = "Incorrect email or password"
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end

def destroy
logout
redirect_to root_path, notice: "You have been logged out."
end

end
Loading