bug-fix/failing to disable account

This commit is contained in:
SowinskiBraeden committed 2022-05-26 12:46:57 -07:00
1 parent b708f1d815
commit c054e3f065
4 files changed
+75 -41

No files matched your search

+60 -31
View File
@@ -198,6 +198,7 @@ func Enroll(c *fiber.Ctx) error {
// Disable login block
student.AccountData.AccountDisabled = false
student.AccountData.Alerted = false
student.AccountData.Attempts = 0
// Generate temporary password
@@ -210,12 +211,11 @@ func Enroll(c *fiber.Ctx) error {
receiver := student.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": student.PersonalData.FirstName, "password": tempPass}); err {
if sent := r.Send("./templates/passwordChanged.html", map[string]string{"username": student.PersonalData.FirstName, "password": tempPass}); !sent {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to students email",
"error": err,
})
}
@@ -345,12 +345,11 @@ func RegisterTeacher(c *fiber.Ctx) error {
receiver := teacher.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName, "password": tempPass}); err {
if sent := r.Send("./templates/passwordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName, "password": tempPass}); !sent {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to teachers email",
"error": err,
})
}
@@ -433,12 +432,11 @@ func CreateAdmin(c *fiber.Ctx) error {
receiver := admin.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": admin.FirstName, "password": tempPass}); err {
if sent := r.Send("./templates/passwordChanged.html", map[string]string{"username": admin.FirstName, "password": tempPass}); !sent {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to students email",
"error": err,
})
}
@@ -496,7 +494,6 @@ func StudentLogin(c *fiber.Ctx) error {
var student models.Student
err := studentCollection.FindOne(ctx, bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
defer cancel()
if err != nil {
cancel()
@@ -507,21 +504,29 @@ func StudentLogin(c *fiber.Ctx) error {
})
}
var localAccountDisabled = false
if student.AccountData.Attempts >= 5 {
var verified bool = student.ComparePasswords(data["password"])
var localAccountDisabled bool = false
var localAttempts int = student.AccountData.Attempts
if !verified {
localAttempts += 1
}
if student.AccountData.Attempts >= 5 || localAttempts >= 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{
"AccountData.accountdisabled": true,
"AccountData.attempts": 0,
"accountdata.accountdisabled": true,
"accountdata.alerted": true,
"accountdata.attempts": 0,
"updated_at": update_time,
},
}
_, updateErr := studentCollection.UpdateOne(
ctx,
bson.M{"sid": data["sid"]},
bson.M{"schooldata.sid": data["sid"]},
update,
)
if updateErr != nil {
@@ -535,41 +540,42 @@ func StudentLogin(c *fiber.Ctx) error {
}
if localAccountDisabled || student.AccountData.AccountDisabled {
if !student.AccountData.Alerted {
// Send student email warning of disabled account
subject := "Account Disabled"
receiver := student.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if sent := r.Send("./templates/accountDisabled.html", map[string]string{"username": student.PersonalData.FirstName}); !sent {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to students email",
"error": err,
})
}
}
cancel()
// Send student email warning of disabled account
subject := "Account Disabled"
receiver := student.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/accountDisabled.html", map[string]string{"username": student.PersonalData.FirstName}); err {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to students email",
"error": err,
})
}
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": false,
"message": "Account is Disabled, contact an Admin",
})
}
var verified bool = student.ComparePasswords(data["password"])
if verified == false {
if !verified {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"AccountData.attempts": student.AccountData.Attempts + 1,
"accountdata.attempts": (student.AccountData.Attempts + 1),
"updated_at": update_time,
},
}
_, updateErr := studentCollection.UpdateOne(
ctx,
bson.M{"sid": data["sid"]},
bson.M{"schooldata.sid": data["sid"]},
update,
)
cancel()
@@ -580,10 +586,33 @@ func StudentLogin(c *fiber.Ctx) error {
"error": updateErr,
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": false,
"message": "incorrect password",
})
} else {
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
update := bson.M{
"$set": bson.M{
"accountdata.attempts": 0,
"updated_at": update_time,
},
}
_, updateErr := studentCollection.UpdateOne(
ctx,
bson.M{"schooldata.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,
})
}
}
defer cancel()
+12 -8
View File
@@ -342,7 +342,13 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
subject := "Password Changed"
receiver := student.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
r.Send("./templates/selfPasswordChanged.html", map[string]string{"username": student.PersonalData.FirstName})
if sent := r.Send("./templates/selfPasswordChanged.html", map[string]string{"username": student.PersonalData.FirstName}); !sent {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "failed to send email to student",
})
}
return c.Status(fiber.StatusOK).JSON(fiber.Map{
"success": true,
@@ -422,11 +428,10 @@ func ResetStudentPassword(c *fiber.Ctx) error {
receiver := student.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": student.PersonalData.FirstName, "password": tempPass}); err {
if sent := r.Send("./templates/passwordChanged.html", map[string]string{"username": student.PersonalData.FirstName, "password": tempPass}); !sent {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to students email",
"error": err,
})
}
@@ -1008,6 +1013,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
update := bson.M{
"$set": bson.M{
"accountdata.accountdisabled": false,
"accountdata.alerted": false,
"accountdata.attempts": 0,
"updated_at": update_time,
},
@@ -1022,7 +1028,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
cancel()
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "the student account could not be re-enabled",
"message": "the student account could not be enabled",
"error": updateErr,
})
}
@@ -1271,11 +1277,10 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
receiver := teacher.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/selfPasswordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName}); err {
if sent := r.Send("./templates/selfPasswordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName}); !sent {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to teachers email",
"error": err,
})
}
@@ -1356,11 +1361,10 @@ func ResetTeacherPassword(c *fiber.Ctx) error {
receiver := teacher.PersonalData.Email
r := NewRequest([]string{receiver}, subject)
if err := r.Send("./templates/passwordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName, "password": tempPass}); err {
if sent := r.Send("./templates/passwordChanged.html", map[string]string{"username": teacher.PersonalData.FirstName, "password": tempPass}); !sent {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"success": false,
"message": "Could not send password to teachers email",
"error": err,
})
}