update/account-disabled

This commit is contained in:
SowinskiBraeden committed 2022-01-14 19:39:40 -08:00
1 parent 9681ded4d0
commit 349a71e31c
2 files changed
+83 -25

No files matched your search

+57 -1
View File
@@ -363,15 +363,71 @@ func StudentLogin(c *fiber.Ctx) error {
"error": err, "error": err,
}) })
} }
defer cancel()
var localAccountDisabled = false
if student.Attempts >= 5 {
localAccountDisabled = true // Catches newly disbaled account before student obj is updated
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"accountdisabled": true,
"attempts": 0,
"updated_at": update_time,
},
}
_, updateErr := studentCollection.UpdateOne(
ctx,
bson.M{"sid": data["sid"]},
update,
)
if updateErr != nil {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "the student could not be updated",
"error": updateErr,
})
}
}
if localAccountDisabled || student.AccountDisabled {
cancel()
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{
"success": false,
"message": "Account is Disabled, contact an Admin",
})
}
var verified bool = student.ComparePasswords(data["password"]) var verified bool = student.ComparePasswords(data["password"])
if verified == false { if verified == false {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"attempts": student.Attempts + 1,
"updated_at": update_time,
},
}
_, updateErr := studentCollection.UpdateOne(
ctx,
bson.M{"sid": data["sid"]},
update,
)
cancel()
if updateErr != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "the student could not be updated",
"error": updateErr,
})
}
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{ return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
"success": false, "success": false,
"message": "incorrect password", "message": "incorrect password",
}) })
} }
defer cancel()
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{ claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
Issuer: student.SID, Issuer: student.SID,
+26 -24
View File
@@ -12,30 +12,32 @@ import (
) )
type Student struct { type Student struct {
ID primitive.ObjectID `bson:"_id"` ID primitive.ObjectID `bson:"_id"`
FirstName string `json:"firstname" validate:"required"` AccountDisabled bool `bson:"accountdisabled"`
MiddleName string `json:"middlename"` FirstName string `json:"firstname" validate:"required"`
LastName string `json:"lastname" validate:"required"` MiddleName string `json:"middlename"`
Age int `json:"age" validate:"required"` LastName string `json:"lastname" validate:"required"`
GradeLevel int `json:"gradelevel" validate:"required"` Age int `json:"age" validate:"required"`
Email string `json:"email" validate:"required"` GradeLevel int `json:"gradelevel" validate:"required"`
SchoolEmail string `json:"schoolemail"` Email string `json:"email" validate:"required"`
Password string `json:"-" validate:"min=10,max=32"` SchoolEmail string `json:"schoolemail"`
TempPassword bool `json:"temppassword"` Password string `json:"-" validate:"min=10,max=32"`
SID string `json:"sid"` // Student ID TempPassword bool `json:"temppassword"`
PEN string `json:"ped"` // Personal Education Number Attempts int `json:"attempts"` // login attempts max 5
Homeroom string `json:"homeroom"` SID string `json:"sid"` // Student ID
Locker string `json:"locker"` PEN string `json:"ped"` // Personal Education Number
YOG int `json:"yog"` // Year of Graduation Homeroom string `json:"homeroom"`
Address string `json:"address"` Locker string `json:"locker"`
City string `json:"city"` YOG int `json:"yog"` // Year of Graduation
Province string `json:"province"` Address string `json:"address"`
Postal string `json:"postal"` City string `json:"city"`
DOB string `json:"dob" validate:"required"` Province string `json:"province"`
Photo string `json:"photo"` Postal string `json:"postal"`
Contacts []string `json:"contacts"` // List of contact ID's rather than contact object DOB string `json:"dob" validate:"required"`
Created_at time.Time `json:"created_at"` Photo string `json:"photo"`
Updated_at time.Time `json:"updated_at"` Contacts []string `json:"contacts"` // List of contact ID's rather than contact object
Created_at time.Time `json:"created_at"`
Updated_at time.Time `json:"updated_at"`
} }
func (s *Student) HashPassword(password string) string { func (s *Student) HashPassword(password string) string {