223 lines
5.5 KiB
Crystal
223 lines
5.5 KiB
Crystal
require "commander"
|
|
require "tallboy"
|
|
require "wannabe_bool"
|
|
|
|
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 = "seed"
|
|
c.short = "Imports users from Untis"
|
|
c.long = c.short
|
|
|
|
c.run do
|
|
users = Auth.users
|
|
|
|
students = [] of Templates::Users::Student
|
|
users.classes.each do |cl|
|
|
c_db = Db::Class.create!({name: cl.name})
|
|
cl.students.each do |s|
|
|
password = Password.generate(Password::DEFAULT_LEN)
|
|
students << Templates::Users::Student.new(
|
|
class_name: cl.name,
|
|
user: Templates::Users::User.new(
|
|
first_name: s.first_name,
|
|
last_name: s.last_name,
|
|
username: s.username,
|
|
password: password
|
|
)
|
|
)
|
|
user = Db::User.create!({
|
|
username: s.username,
|
|
password: password,
|
|
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
|
|
|
|
teachers = [] of Templates::Users::User
|
|
Db::User.import(
|
|
users.teachers.map do |t|
|
|
password = Password.generate(Password::DEFAULT_LEN)
|
|
teachers << Templates::Users::User.new(
|
|
first_name: t.first_name,
|
|
last_name: t.last_name,
|
|
username: t.username,
|
|
password: password
|
|
)
|
|
|
|
Db::User.new({
|
|
username: t.username,
|
|
password: password,
|
|
first_name: t.first_name,
|
|
last_name: t.last_name,
|
|
role: Db::UserRole::Teacher,
|
|
admin: false,
|
|
})
|
|
end
|
|
)
|
|
|
|
html = Templates::Users.new(students, teachers).to_s
|
|
puts "Filepath: #{Auth.generate_pdf(html).filename}"
|
|
end
|
|
end
|
|
|
|
# cmd.commands.add do |c|
|
|
# c.use = "register <username> <role>"
|
|
# c.short = "Seeds the database with required data"
|
|
# c.long = c.short
|
|
|
|
# c.flags.add do |f|
|
|
# f.name = "admin"
|
|
# f.long = "--admin"
|
|
# f.default = false
|
|
# f.description = "Register as admin"
|
|
# end
|
|
|
|
# c.flags.add do |f|
|
|
# f.name = "yes"
|
|
# f.short = "-y"
|
|
# f.long = "--yes"
|
|
# f.default = false
|
|
# f.description = "Answer yes to all questions"
|
|
# end
|
|
|
|
# c.run do |opts, args|
|
|
# username = args[0]
|
|
# role = Db::UserRole.from_string(args[1].underscore)
|
|
# unless opts.bool["yes"]
|
|
# print "Register '#{username}' as '#{role.to_api}'#{opts.bool["admin"] ? " with admin privileges" : nil}? [y/N] "
|
|
# abort unless gets(chomp: true).not_nil!.strip.downcase == "y"
|
|
# end
|
|
|
|
# user = Db::User.create!(username: username, role: role.to_s, admin: opts.bool["admin"])
|
|
# if role == Db::UserRole::Student
|
|
# Db::Student.create!(user_id: user.id)
|
|
# end
|
|
|
|
# # Worker::Jobs::CacheLdapUserJob.new(user.id).enqueue
|
|
|
|
# puts "Done!"
|
|
# 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: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
|
|
end
|
|
end
|