Добавлен основные классы для сервиса авторизаци

This commit is contained in:
Ганеев Артем
2025-10-28 20:49:54 +03:00
parent def3552a67
commit 736b8031f8
26 changed files with 904 additions and 40 deletions

View File

@@ -1,37 +1,49 @@
package internal
type RegistrationUser struct {
Username string `json:"username"`
Password string `json:"password"`
}
const (
Student UserRole = iota
Teacher
Admin
import (
"errors"
"fmt"
)
type User struct {
Username string
Password string
UserRole UserRole
Id int `json:"-" db:"id"`
Name string `json:"name" binding:"required"`
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
UserRole string `json:"-" db:"role"`
}
func (user *User) ChangeRole(userRole UserRole) {
user.UserRole = userRole
type AuthUser struct {
Username string `json:"username" binding:"required"`
Password string `json:"password" binding:"required"`
}
type UserRole int
type RefreshTokenRequest struct {
RefreshToken string `json:"refreshToken" binding:"required"`
}
func (role UserRole) String() string {
switch role {
case Student:
return "student"
case Teacher:
return "teacher"
case Admin:
return "admin"
default:
return "unknown"
type UserRole string
const (
Student UserRole = "student"
Teacher UserRole = "teacher"
Admin UserRole = "admin"
)
func (userrole UserRole) ToString() string {
return string(userrole)
}
var ErrInvalidRole = errors.New("invalid role")
func FromString(in string) (UserRole, error) {
switch in {
case Student.ToString():
return Student, nil
case Teacher.ToString():
return Teacher, nil
case Admin.ToString():
return Admin, nil
}
return Student, fmt.Errorf("%q is not a valid role: %w", in, ErrInvalidRole)
}