mentorenwahl/backend/src/backend/cli.cr

291 lines
7.8 KiB
Crystal

require "commander"
require "tallboy"
require "wannabe_bool"
require "fancyline"
module Backend
CLI = Commander::Command.new do |cmd|
cmd.use = "backend"
cmd.short = "Mentorenwahl backend CLI"
cmd.run do
puts cmd.help
end
cmd.commands.add do |c|
c.use = "version"
c.short = "Prints version"
c.long = c.short
c.run do
puts VERSION
end
end
cmd.commands.add do |c|
c.use = "authors"
c.short = "Prints authors"
c.long = c.short
c.run do
puts AUTHORS.join("\n")
end
end
cmd.commands.add do |c|
c.use = "licenses"
c.short = "Prints licenses of projects used by this programs"
c.long = c.short
c.run do
puts LICENSES
end
end
cmd.commands.add do |c|
c.use = "run"
c.short = "Run the backend"
c.long = c.short
c.run do
Runner.new.run
end
end
cmd.commands.add do |c|
c.use = "schema"
c.short = "Prints out GraphQL schema"
c.long = c.short
c.run do
puts Api::Schema::SCHEMA.document.to_s
end
end
cmd.commands.add do |c|
c.use = "user:import"
c.short = "Imports users from Untis"
c.long = c.short
c.run do
users = Auth.users
users.classes.each do |cl|
c_db = Db::Class.create!({name: cl.name})
cl.students.each do |s|
password = Password.generate(Password::DEFAULT_LEN)
user = Db::User.create!({
username: s.username,
password: password,
initial_password: password,
password_changed: false,
first_name: s.first_name,
last_name: s.last_name,
role: Db::UserRole::Student,
admin: false,
})
Db::Student.create!({
user_id: user.id,
class_id: c_db.id,
})
end
end
Db::User.import(
users.teachers.map do |t|
password = Password.generate(Password::DEFAULT_LEN)
Db::User.new({
username: t.username,
password: password,
initial_password: password,
password_changed: false,
first_name: t.first_name,
last_name: t.last_name,
role: Db::UserRole::Teacher,
admin: false,
})
end
)
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "user:list"
cmd.short = "Lists all users"
cmd.long = cmd.short
cmd.run do
users = Db::User.query.to_a
table = Tallboy.table do
header ["id", "username", "first_name", "last_name", "role", "admin"]
users.each do |user|
row [
user.id,
user.username,
user.first_name,
user.last_name,
user.role,
user.admin,
]
end
end
puts table
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "user:export"
cmd.short = "Generates report for all users"
cmd.long = cmd.short
cmd.run do
time = Time.local
students = [] of Templates::Users::Student
teachers = [] of Templates::Users::User
Db::User.query
.with_student(&.with_class_model)
.with_teacher
.each do |user|
password = user.password_changed ? nil : user.initial_password
case user.role.to_api
in Api::Schema::UserRole::Student
students << Templates::Users::Student.new(
class_name: user.student.not_nil!.class_model.name,
user: Templates::Users::User.new(
first_name: user.first_name,
last_name: user.last_name,
username: user.username,
password: password
)
)
in Api::Schema::UserRole::Teacher
teachers << Templates::Users::User.new(
first_name: user.first_name,
last_name: user.last_name,
username: user.username,
password: password
)
end
end
html = Templates::Users.new(
time,
Db::Class.query.with_students.to_a.map { |cl| {cl.name, cl.students.count.to_i32} },
students,
teachers
).to_s
puts "Filepath: #{Auth.generate_pdf(html).filename}"
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "user:admin <id> <admin>"
cmd.short = "Gives or removed admin rights"
cmd.long = cmd.short
cmd.run do |_opts, args|
user = Db::User.find!(args[0].to_i)
user.admin = args[1].to_b
user.save!
table = Tallboy.table do
header ["id", "username", "first_name", "last_name", "role", "admin"]
row [
user.id,
user.username,
user.first_name,
user.last_name,
user.role,
user.admin,
]
end
puts table
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "user:reset <id>"
cmd.short = "Reset user's password"
cmd.long = cmd.short
cmd.run do |_opts, args|
user = Db::User.find!(args[0].to_i)
puts "Changing password for user:"
table = Tallboy.table do
header ["id", "username", "first_name", "last_name", "role", "admin"]
row [
user.id,
user.username,
user.first_name,
user.last_name,
user.role,
user.admin,
]
end
puts table
print "New password: "
new_password = gets(chomp: true).not_nil!
puts "New password is: \"#{Regex.escape(new_password)}\""
print "Reenter new password: "
if gets(chomp: true).not_nil! == new_password
user.password = new_password
user.password_changed = true
user.save!
Db::Token.query
.where { user_id == user.id }
.to_update
.set(active: false)
.execute
end
end
end
# ameba:disable Lint/ShadowingOuterLocalVar
cmd.commands.add do |cmd|
cmd.use = "assignments:export"
cmd.short = "Generates report for all assignments"
cmd.long = cmd.short
cmd.run do
time = Time.local
a_id = Db::Assignment.query.select(:id).where { active }.first!.id
html = Templates::Assignments.new(
time,
Db::Teacher.query
.with_student_assignments(&.where { assignment_id == a_id }.with_student(&.with_user.with_class_model))
.to_a
.select(&.student_assignments.count.positive?)
.map do |t|
Templates::Assignments::Assignment.new(
Templates::Assignments::User.new(t.user.first_name, t.user.last_name),
t.student_assignments
.map do |sa|
{
user: Templates::Assignments::User.new(sa.student.user.first_name, sa.student.user.last_name),
class: sa.student.class_model.name,
}
end
.sort_by! { |sa| {sa[:class], sa[:user].last_name, sa[:user].first_name} }
)
end
.sort_by! { |a| {a.teacher.last_name, a.teacher.first_name} }
).to_s
puts "Filepath: #{Auth.generate_pdf(html).filename}"
end
end
end
end