Merge pull request 'Teacher registration' (#8) from teacher-registration into main
All checks were successful
continuous-integration/drone/push Build is passing

Reviewed-on: #8
This commit is contained in:
Dominic Grimm 2022-01-20 19:21:32 +00:00
commit 401b5b3184
5 changed files with 56 additions and 26 deletions

View file

@ -47,30 +47,48 @@ module MW
true
end
def role?(role : Schema::UserRole) : Bool
@role == role == case @external
when Db::Admin
Schema::UserRole::Admin
when Db::Teacher
Schema::UserRole::Teacher
when Db::Student
Schema::UserRole::Student
def role?(external = true, *roles : Schema::UserRole) : Bool
return false unless authenticated?
roles.each do |role|
return true if @role == role && if external
role == case @external
when Db::Admin
Schema::UserRole::Admin
when Db::Teacher
Schema::UserRole::Teacher
when Db::Student
Schema::UserRole::Student
end
else
true
end
end
false
end
def role!(role : Schema::UserRole) : Bool
raise "Invalid permissions" unless role? role
def role!(external = true, *roles : Schema::UserRole) : Bool
raise "Invalid permissions" unless role? external, *roles
true
end
def admin? : Bool
role? Schema::UserRole::Admin
private macro role_check(*roles)
{% for role in roles %}
{% name = role.names.last.underscore %}
def {{ name }}?(external = true) : Bool
role? external, {{ role }}
end
def {{ name }}!(external = true) : Bool
role! external, {{ role }}
end
{% end %}
end
def admin! : Bool
role! Schema::UserRole::Admin
end
role_check Schema::UserRole::Admin, Schema::UserRole::Teacher, Schema::UserRole::Student
def self.db_eq_role?(external : Granite::Base, role : Schema::UserRole) : Bool
role == case external

View file

@ -86,6 +86,15 @@ module MW
id
end
@[GraphQL::Field]
def register_teacher(context : Context, input : TeacherInput) : Teacher
context.teacher? external: false
Teacher.new(
Db::Teacher.create!(user_id: context.user.not_nil!.id, max_students: input.max_students, skif: input.skif)
)
end
@[GraphQL::Field]
def create_student(context : Context, input : StudentCreateInput) : Student
context.admin!
@ -109,7 +118,7 @@ module MW
@[GraphQL::Field]
def create_vote(context : Context, input : VoteCreateInput) : Vote
context.role! UserRole::Student
context.student!
if input.teacher_ids.any? { |id| Db::Teacher.find(id).nil? }
raise "Teachers not found"

View file

@ -14,9 +14,7 @@ module MW
end
@[GraphQL::Field]
def max_students(context : Context) : Int32
context.admin!
def max_students : Int32
find!.max_students
end

View file

@ -2,13 +2,6 @@ require "graphql"
module MW
module Schema
@[GraphQL::Enum]
enum UserRole
Admin
Teacher
Student
end
@[GraphQL::Object]
class User < GraphQL::BaseObject
include Helpers::DbObject

View file

@ -0,0 +1,12 @@
require "graphql"
module MW
module Schema
@[GraphQL::Enum]
enum UserRole
Admin
Teacher
Student
end
end
end