update/reset teacher password
This commit is contained in:
1 parent
6bae52d632
commit
cca343a84d
4 files changed
+193
-55
No files matched your search
@@ -245,26 +245,51 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var teacher models.Teacher
|
||||
teacher.FirstName = data["firstname"].(string)
|
||||
teacher.MiddleName = data["middlename"].(string)
|
||||
teacher.LastName = data["lastname"].(string)
|
||||
teacher.Email = data["email"].(string)
|
||||
teacher.Province = data["province"].(string)
|
||||
teacher.City = data["city"].(string)
|
||||
teacher.Postal = data["postal"].(string)
|
||||
teacher.DOB = data["postal"].(string)
|
||||
teacher.PersonalData.FirstName = data["firstname"].(string)
|
||||
teacher.PersonalData.MiddleName = data["middlename"].(string)
|
||||
teacher.PersonalData.LastName = data["lastname"].(string)
|
||||
teacher.PersonalData.Email = data["email"].(string)
|
||||
teacher.PersonalData.Province = data["province"].(string)
|
||||
teacher.PersonalData.City = data["city"].(string)
|
||||
teacher.PersonalData.Postal = data["postal"].(string)
|
||||
teacher.PersonalData.DOB = data["postal"].(string)
|
||||
|
||||
teacher.SchoolEmail = teacher.GenerateSchoolEmail()
|
||||
var photo models.Photo
|
||||
photo.Name = uuid.New().String()
|
||||
photo.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
photo.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
photo.ID = primitive.NewObjectID()
|
||||
|
||||
defaultImage, _ := os.ReadFile("./database/defaultImage.txt")
|
||||
photo.Base64 = string(defaultImage)
|
||||
|
||||
teacher.SchoolData.PhotoName = photo.Name
|
||||
|
||||
var schoolEmail string = ""
|
||||
offset := 0
|
||||
for {
|
||||
schoolEmail = teacher.GenerateSchoolEmail(offset, schoolEmail)
|
||||
if !teacher.EmailExists(schoolEmail) {
|
||||
break
|
||||
}
|
||||
offset++
|
||||
}
|
||||
teacher.AccountData.SchoolEmail = schoolEmail
|
||||
teacher.AccountData.HashHistory = []string{}
|
||||
|
||||
// Disable login block
|
||||
teacher.AccountData.AccountDisabled = false
|
||||
teacher.AccountData.Attempts = 0
|
||||
|
||||
var tempPass string = teacher.GeneratePassword(12, 1, 1, 1)
|
||||
teacher.Password = teacher.HashPassword(tempPass)
|
||||
teacher.TempPassword = true
|
||||
teacher.AccountData.Password = teacher.HashPassword(tempPass)
|
||||
teacher.AccountData.TempPassword = true
|
||||
|
||||
// Send teacher personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{teacher.Email}, message)
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{teacher.PersonalData.Email}, message)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
@@ -283,7 +308,7 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
||||
break
|
||||
}
|
||||
}
|
||||
teacher.TID = tid
|
||||
teacher.SchoolData.TID = tid
|
||||
|
||||
teacher.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
teacher.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
@@ -541,7 +566,7 @@ func TeacherLogin(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var teacher models.Teacher
|
||||
err := teacherCollection.FindOne(ctx, bson.M{"tid": data["tid"]}).Decode(&teacher)
|
||||
err := teacherCollection.FindOne(ctx, bson.M{"schooldata.tid": data["tid"]}).Decode(&teacher)
|
||||
defer cancel()
|
||||
|
||||
if err != nil {
|
||||
@@ -563,7 +588,7 @@ func TeacherLogin(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
|
||||
Issuer: teacher.TID,
|
||||
Issuer: teacher.SchoolData.TID,
|
||||
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day
|
||||
})
|
||||
token, err := claims.SignedString([]byte(SecretKey))
|
||||
@@ -762,7 +787,7 @@ func Teacher(c *fiber.Ctx) error {
|
||||
claims := token.Claims.(*jwt.StandardClaims)
|
||||
|
||||
var teacher models.Teacher
|
||||
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"tid": claims.Issuer}).Decode(&teacher)
|
||||
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": claims.Issuer}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
|
||||
@@ -1083,7 +1083,7 @@ func UpdateTeacherHomeroom(c *fiber.Ctx) error {
|
||||
|
||||
result, updateErr := teacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"tid": data["tid"]},
|
||||
bson.M{"schooldata.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -1111,9 +1111,88 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func ResetTeacherPassword(c *fiber.Ctx) error {
|
||||
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
||||
"success": nil,
|
||||
"message": "not implimented",
|
||||
var data map[string]string
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
|
||||
if err := c.BodyParser(&data); err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Failed to parse body",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
// Check required fields are included (email must be personal email)
|
||||
if data["tid"] == "" || data["email"] == "" {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "missing required fields",
|
||||
})
|
||||
}
|
||||
|
||||
var teacher models.Teacher
|
||||
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"schooldata.tid": data["tid"]}).Decode(&teacher)
|
||||
if findErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "teacher not found",
|
||||
})
|
||||
}
|
||||
|
||||
if teacher.PersonalData.Email != data["email"] {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Your personal email is incorrect",
|
||||
})
|
||||
}
|
||||
|
||||
tempPass := teacher.GeneratePassword(12, 1, 1, 1)
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"accountdata.password": teacher.HashPassword(tempPass),
|
||||
"accountdata.temppassword": true,
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
result, updateErr := studentCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"schooldata.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "the teacher password could not be updated",
|
||||
"error": updateErr,
|
||||
})
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
// Send student personal email temp password
|
||||
message := []byte("Your temporary password is " + tempPass)
|
||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
||||
|
||||
err := smtp.SendMail("smtp.gmail.com:587", auth, systemEmail, []string{teacher.PersonalData.Email}, message)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||
"success": false,
|
||||
"message": "Could not send password to teachers email",
|
||||
"error": err,
|
||||
})
|
||||
}
|
||||
|
||||
return c.Status(fiber.StatusOK).JSON(fiber.Map{
|
||||
"success": true,
|
||||
"message": "successfully updated teacher password",
|
||||
"result": result,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1151,17 +1230,17 @@ func UpdateTeacherAddress(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"address": data["address"],
|
||||
"city": data["city"],
|
||||
"province": data["province"],
|
||||
"postal": data["postal"],
|
||||
"updated_at": update_time,
|
||||
"personaldata.address": data["address"],
|
||||
"personaldata.city": data["city"],
|
||||
"personaldata.province": data["province"],
|
||||
"personaldata.postal": data["postal"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
result, updateErr := teacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"tid": data["tid"]},
|
||||
bson.M{"schooldata.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -1222,14 +1301,14 @@ func UpdateTeacherEmail(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"email": data["email"],
|
||||
"updated_at": update_time,
|
||||
"personaldata.email": data["email"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
result, updateErr := teacherCollection.UpdateOne(
|
||||
ctx,
|
||||
bson.M{"tid": data["tid"]},
|
||||
bson.M{"schooldata.tid": data["tid"]},
|
||||
update,
|
||||
)
|
||||
if updateErr != nil {
|
||||
@@ -1292,10 +1371,10 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
||||
update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
update := bson.M{
|
||||
"$set": bson.M{
|
||||
"firstname": data["firstname"],
|
||||
"middlename": data["middlename"],
|
||||
"lastname": data["lastname"],
|
||||
"updated_at": update_time,
|
||||
"personaldata.firstname": data["firstname"],
|
||||
"personaldata.middlename": data["middlename"],
|
||||
"personaldata.lastname": data["lastname"],
|
||||
"updated_at": update_time,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user