User management
Some checks are pending
CI / scan_ruby (push) Waiting to run
CI / scan_js (push) Waiting to run
CI / lint (push) Waiting to run
CI / test (push) Waiting to run

This commit is contained in:
sto 2025-03-22 09:16:38 +01:00
parent ce5b729fef
commit 0b47cc4d8a
7 changed files with 70 additions and 8 deletions

View File

@ -51,6 +51,7 @@ module Authentication
end
def current_user
return unless Current.session
return unless Current.session[:user_id]
User.find(Current.session[:user_id])
end

View File

@ -3,6 +3,7 @@ class UsersController < ApplicationController
def index
@title = "All users"
@users = User.all
end
def edit
@ -11,7 +12,7 @@ class UsersController < ApplicationController
def update
if @user.update(user_params)
redirect_to @user
redirect_to contests_path
else
render :edit, status: :unprocessable_entity
end
@ -22,6 +23,18 @@ class UsersController < ApplicationController
end
def new
@title = "New user"
@user = User.new()
end
def create
@user = User.new(user_params)
if @user.save
redirect_to users_path
else
@title = "New user"
render :new, status: :unprocessable_entity
end
end
def destroy
@ -34,6 +47,6 @@ class UsersController < ApplicationController
end
def user_params
params.expect(user: [ :username, :email_address ])
params.expect(user: [ :username, :email_address, :password ])
end
end

View File

@ -7,6 +7,9 @@ html
- if @current_user
.float-end style="margin-top: -8px;"
nav.navbar.bg-body-primary
- if @current_user.admin
a.navbar-brand href=users_path
| Users
a.navbar-brand href=contests_path
| Home
a.navbar-brand href=user_path(@current_user)

View File

@ -1,4 +1,6 @@
= form_with model: user do |form|
= form_with model: user, method: method do |form|
- if method == :patch
h4 General settings
.row.mb-3
.col
.input-group
@ -9,7 +11,23 @@
.row.mb-3
.col
.form-floating
= form.text_field :email_address, autocomplete: "off", class: "form-control", type: "email"
= form.text_field :email_address, autocomplete: "off", class: "form-control"
= form.label :email_address, class: "required"
div
- if method == :post
.row.mb-3
.col
.form-floating
= form.password_field :password, autocomplete: "off", class: "form-control"
= form.label :password, class: "required"
= form.submit "Save", class: "btn btn-primary"
- if method == :patch
h4.mt-5 Change password
= form_with model: user, method: method do |form|
.row.mb-3
.col
.form-floating
= form.password_field :password, autocomplete: "off", class: "form-control"
= form.label :password, class: "required"
| New password
= form.submit "Save new password", class: "btn btn-primary"

View File

@ -1 +1 @@
= render "form", user: @user
= render "form", user: @user, method: :patch

View File

@ -0,0 +1,27 @@
table.table.table-striped.table-hover
thead
tr
th scope="col"
| Id
th scope="col"
| Name
th scope="col"
| Admin?
th scope="col"
| # contests
tbody
- @users.each do |user|
tr scope="row"
td
= user.id
td
= user.username
td
= user.admin ? "Yes" : "No"
td
= user.contests.length
.row
.col
a.btn.btn-primary href=new_user_path
| New user

View File

@ -1 +1 @@
= render "form", user: @user
= render "form", user: @user, method: :post