bug-fix/failing to disable account
This commit is contained in:
4 files changed
+63
-29
No files matched your search
@@ -198,6 +198,7 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
// Disable login block
|
// Disable login block
|
||||||
student.AccountData.AccountDisabled = false
|
student.AccountData.AccountDisabled = false
|
||||||
|
student.AccountData.Alerted = false
|
||||||
student.AccountData.Attempts = 0
|
student.AccountData.Attempts = 0
|
||||||
|
|
||||||
// Generate temporary password
|
// Generate temporary password
|
||||||
@@ -210,12 +211,11 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
receiver := student.PersonalData.Email
|
receiver := student.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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()
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Could not send password to students email",
|
"message": "Could not send password to students email",
|
||||||
"error": err,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -345,12 +345,11 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
receiver := teacher.PersonalData.Email
|
receiver := teacher.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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()
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Could not send password to teachers email",
|
"message": "Could not send password to teachers email",
|
||||||
"error": err,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -433,12 +432,11 @@ func CreateAdmin(c *fiber.Ctx) error {
|
|||||||
receiver := admin.Email
|
receiver := admin.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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()
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Could not send password to students email",
|
"message": "Could not send password to students email",
|
||||||
"error": err,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,7 +494,6 @@ func StudentLogin(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
var student models.Student
|
var student models.Student
|
||||||
err := studentCollection.FindOne(ctx, bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
|
err := studentCollection.FindOne(ctx, bson.M{"schooldata.sid": data["sid"]}).Decode(&student)
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
@@ -507,21 +504,29 @@ func StudentLogin(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var localAccountDisabled = false
|
var verified bool = student.ComparePasswords(data["password"])
|
||||||
if student.AccountData.Attempts >= 5 {
|
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
|
localAccountDisabled = true // Catches newly disbaled account before student obj is updated
|
||||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"AccountData.accountdisabled": true,
|
"accountdata.accountdisabled": true,
|
||||||
"AccountData.attempts": 0,
|
"accountdata.alerted": true,
|
||||||
|
"accountdata.attempts": 0,
|
||||||
"updated_at": update_time,
|
"updated_at": update_time,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, updateErr := studentCollection.UpdateOne(
|
_, updateErr := studentCollection.UpdateOne(
|
||||||
ctx,
|
ctx,
|
||||||
bson.M{"sid": data["sid"]},
|
bson.M{"schooldata.sid": data["sid"]},
|
||||||
update,
|
update,
|
||||||
)
|
)
|
||||||
if updateErr != nil {
|
if updateErr != nil {
|
||||||
@@ -535,14 +540,14 @@ func StudentLogin(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if localAccountDisabled || student.AccountData.AccountDisabled {
|
if localAccountDisabled || student.AccountData.AccountDisabled {
|
||||||
cancel()
|
|
||||||
|
|
||||||
|
if !student.AccountData.Alerted {
|
||||||
// Send student email warning of disabled account
|
// Send student email warning of disabled account
|
||||||
subject := "Account Disabled"
|
subject := "Account Disabled"
|
||||||
receiver := student.PersonalData.Email
|
receiver := student.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
r := NewRequest([]string{receiver}, subject)
|
||||||
|
|
||||||
if err := r.Send("./templates/accountDisabled.html", map[string]string{"username": student.PersonalData.FirstName}); err {
|
if sent := r.Send("./templates/accountDisabled.html", map[string]string{"username": student.PersonalData.FirstName}); !sent {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
@@ -550,26 +555,27 @@ func StudentLogin(c *fiber.Ctx) error {
|
|||||||
"error": err,
|
"error": err,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
cancel()
|
||||||
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Account is Disabled, contact an Admin",
|
"message": "Account is Disabled, contact an Admin",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var verified bool = student.ComparePasswords(data["password"])
|
if !verified {
|
||||||
if verified == false {
|
|
||||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"AccountData.attempts": student.AccountData.Attempts + 1,
|
"accountdata.attempts": (student.AccountData.Attempts + 1),
|
||||||
"updated_at": update_time,
|
"updated_at": update_time,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
_, updateErr := studentCollection.UpdateOne(
|
_, updateErr := studentCollection.UpdateOne(
|
||||||
ctx,
|
ctx,
|
||||||
bson.M{"sid": data["sid"]},
|
bson.M{"schooldata.sid": data["sid"]},
|
||||||
update,
|
update,
|
||||||
)
|
)
|
||||||
cancel()
|
cancel()
|
||||||
@@ -580,10 +586,33 @@ func StudentLogin(c *fiber.Ctx) error {
|
|||||||
"error": updateErr,
|
"error": updateErr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "incorrect password",
|
"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()
|
defer cancel()
|
||||||
|
|
||||||
|
|||||||
@@ -342,7 +342,13 @@ func UpdateStudentPassword(c *fiber.Ctx) error {
|
|||||||
subject := "Password Changed"
|
subject := "Password Changed"
|
||||||
receiver := student.PersonalData.Email
|
receiver := student.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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{
|
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||||
"success": true,
|
"success": true,
|
||||||
@@ -422,11 +428,10 @@ func ResetStudentPassword(c *fiber.Ctx) error {
|
|||||||
receiver := student.PersonalData.Email
|
receiver := student.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Could not send password to students email",
|
"message": "Could not send password to students email",
|
||||||
"error": err,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1008,6 +1013,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
|
|||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"accountdata.accountdisabled": false,
|
"accountdata.accountdisabled": false,
|
||||||
|
"accountdata.alerted": false,
|
||||||
"accountdata.attempts": 0,
|
"accountdata.attempts": 0,
|
||||||
"updated_at": update_time,
|
"updated_at": update_time,
|
||||||
},
|
},
|
||||||
@@ -1022,7 +1028,7 @@ func RemoveStudentsDisabled(c *fiber.Ctx) error {
|
|||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "the student account could not be re-enabled",
|
"message": "the student account could not be enabled",
|
||||||
"error": updateErr,
|
"error": updateErr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -1271,11 +1277,10 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
|
|||||||
receiver := teacher.PersonalData.Email
|
receiver := teacher.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Could not send password to teachers email",
|
"message": "Could not send password to teachers email",
|
||||||
"error": err,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1356,11 +1361,10 @@ func ResetTeacherPassword(c *fiber.Ctx) error {
|
|||||||
receiver := teacher.PersonalData.Email
|
receiver := teacher.PersonalData.Email
|
||||||
r := NewRequest([]string{receiver}, subject)
|
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{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "Could not send password to teachers email",
|
"message": "Could not send password to teachers email",
|
||||||
"error": err,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ type Student struct {
|
|||||||
SchoolEmail string `json:"schoolemail"`
|
SchoolEmail string `json:"schoolemail"`
|
||||||
Password string `json:"-" validate:"min=10,max=32"`
|
Password string `json:"-" validate:"min=10,max=32"`
|
||||||
AccountDisabled bool `bson:"accountdisabled"`
|
AccountDisabled bool `bson:"accountdisabled"`
|
||||||
|
Alerted bool `bson:"alerted"`
|
||||||
TempPassword bool `json:"temppassword"`
|
TempPassword bool `json:"temppassword"`
|
||||||
Attempts int `json:"attempts"` // login attempts max 5
|
Attempts int `json:"attempts"` // login attempts max 5
|
||||||
HashHistory []string `json:"-"` // List of old hashed passwords (not including auto generated passwords)
|
HashHistory []string `json:"-"` // List of old hashed passwords (not including auto generated passwords)
|
||||||
|
|||||||
+2
-2
@@ -74,8 +74,8 @@ func Setup(app *fiber.App) {
|
|||||||
|
|
||||||
// General Command Handling
|
// General Command Handling
|
||||||
app.Post(routerPrefix+"/admin/updateLockerCombo", controllers.UpdateLockerCombo)
|
app.Post(routerPrefix+"/admin/updateLockerCombo", controllers.UpdateLockerCombo)
|
||||||
app.Post(routerPrefix+"/admin/renableStudent", controllers.RemoveStudentsDisabled)
|
app.Post(routerPrefix+"/admin/enableStudent", controllers.RemoveStudentsDisabled)
|
||||||
app.Post(routerPrefix+"/admin/renableTeacher", controllers.RemoveTeachersDisabled)
|
app.Post(routerPrefix+"/admin/enableTeacher", controllers.RemoveTeachersDisabled)
|
||||||
|
|
||||||
// Delete Handler
|
// Delete Handler
|
||||||
app.Post(routerPrefix+"/remove/student", controllers.RemoveStudent)
|
app.Post(routerPrefix+"/remove/student", controllers.RemoveStudent)
|
||||||
|
|||||||
Reference in new issue
Block a user