180 lines
4.4 KiB
Crystal
180 lines
4.4 KiB
Crystal
# Mentorenwahl: A fullstack application for assigning mentors to students based on their whishes.
|
|
# Copyright (C) 2022 Dominic Grimm
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
module Backend
|
|
module Api
|
|
module Schema
|
|
@[GraphQL::Enum]
|
|
# Possible roles of a user
|
|
enum UserRole
|
|
Teacher
|
|
Student
|
|
|
|
# DB representation of the enum
|
|
def to_db : Db::UserRole
|
|
case self
|
|
in Teacher
|
|
Db::UserRole::Teacher
|
|
in Student
|
|
Db::UserRole::Student
|
|
end
|
|
end
|
|
|
|
# GraphQL representation of the DB enum
|
|
def self.from_db(role : Db::UserRole) : self
|
|
role.to_api
|
|
end
|
|
end
|
|
|
|
@[GraphQL::Object]
|
|
# User model
|
|
class User < GraphQL::BaseObject
|
|
include Helpers
|
|
|
|
db_object Db::User, Int32
|
|
|
|
# @ldap : Ldap::User?
|
|
|
|
# # LDAP user data
|
|
# def ldap : Ldap::User
|
|
# if @ldap.nil? && (raw_cache = Redis::CLIENT.get("ldap:user:#{id}")).nil?
|
|
# Worker::Jobs::CacheLdapUserJob.new(id).perform
|
|
# raw_cache = Redis::CLIENT.get("ldap:user:#{id}").not_nil!
|
|
# end
|
|
|
|
# (@ldap ||= Ldap::User.from_json(raw_cache.not_nil!)).not_nil!
|
|
# end
|
|
|
|
@[GraphQL::Field]
|
|
# User's first name
|
|
def first_name : String
|
|
# ldap.first_name
|
|
@model.first_name
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's last name
|
|
def last_name : String
|
|
# ldap.last_name
|
|
@model.last_name
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's full name
|
|
def name(formal : Bool = true) : String
|
|
# ldap.name(formal)
|
|
if formal
|
|
"#{@model.last_name}, #{@model.first_name}"
|
|
else
|
|
"#{@model.first_name} #{@model.last_name}"
|
|
end
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's LDAP username
|
|
def username : String
|
|
@model.username
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User is admin
|
|
def admin : Bool
|
|
@model.admin
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's role
|
|
def role : UserRole
|
|
@model.role.to_api
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's external ID
|
|
def external_id : Int32
|
|
case @model.role.to_api
|
|
when Db::UserRole::Teacher
|
|
@model.teacher
|
|
when Db::UserRole::Student
|
|
@model.student
|
|
end.not_nil!.id
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's external teacher object
|
|
def teacher : Teacher?
|
|
@model.teacher.try { |t| Teacher.new(t) }
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# User's external student object
|
|
def student : Student?
|
|
@model.student.try { |s| Student.new(s) }
|
|
end
|
|
end
|
|
|
|
@[GraphQL::InputObject]
|
|
# User creation input
|
|
class UserCreateInput < GraphQL::BaseInputObject
|
|
getter username
|
|
getter role
|
|
getter admin
|
|
|
|
@[GraphQL::Field]
|
|
def initialize(
|
|
@username : String,
|
|
@role : UserRole,
|
|
@admin : Bool = false
|
|
)
|
|
end
|
|
end
|
|
|
|
@[GraphQL::Object]
|
|
# Login payload returned after successful login
|
|
class LoginPayload < GraphQL::BaseObject
|
|
# Logged in user
|
|
property user
|
|
|
|
# JWT token
|
|
property token
|
|
|
|
def initialize(
|
|
@user : User,
|
|
@token : String
|
|
)
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Logged in user
|
|
def user : User
|
|
@user
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Raw bearer token
|
|
def token : String
|
|
@token
|
|
end
|
|
|
|
@[GraphQL::Field]
|
|
# Ready to use bearer token
|
|
def bearer : String
|
|
Auth::BEARER + @token
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|