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
|
var teacher models.Teacher
|
||||||
teacher.FirstName = data["firstname"].(string)
|
teacher.PersonalData.FirstName = data["firstname"].(string)
|
||||||
teacher.MiddleName = data["middlename"].(string)
|
teacher.PersonalData.MiddleName = data["middlename"].(string)
|
||||||
teacher.LastName = data["lastname"].(string)
|
teacher.PersonalData.LastName = data["lastname"].(string)
|
||||||
teacher.Email = data["email"].(string)
|
teacher.PersonalData.Email = data["email"].(string)
|
||||||
teacher.Province = data["province"].(string)
|
teacher.PersonalData.Province = data["province"].(string)
|
||||||
teacher.City = data["city"].(string)
|
teacher.PersonalData.City = data["city"].(string)
|
||||||
teacher.Postal = data["postal"].(string)
|
teacher.PersonalData.Postal = data["postal"].(string)
|
||||||
teacher.DOB = 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)
|
var tempPass string = teacher.GeneratePassword(12, 1, 1, 1)
|
||||||
teacher.Password = teacher.HashPassword(tempPass)
|
teacher.AccountData.Password = teacher.HashPassword(tempPass)
|
||||||
teacher.TempPassword = true
|
teacher.AccountData.TempPassword = true
|
||||||
|
|
||||||
// Send teacher personal email temp password
|
// Send teacher personal email temp password
|
||||||
message := []byte("Your temporary password is " + tempPass)
|
message := []byte("Your temporary password is " + tempPass)
|
||||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, "smtp.gmail.com")
|
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 {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
@@ -283,7 +308,7 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
teacher.TID = tid
|
teacher.SchoolData.TID = tid
|
||||||
|
|
||||||
teacher.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
teacher.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||||
teacher.Updated_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
|
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()
|
defer cancel()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -563,7 +588,7 @@ func TeacherLogin(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
|
claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{
|
||||||
Issuer: teacher.TID,
|
Issuer: teacher.SchoolData.TID,
|
||||||
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day
|
ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day
|
||||||
})
|
})
|
||||||
token, err := claims.SignedString([]byte(SecretKey))
|
token, err := claims.SignedString([]byte(SecretKey))
|
||||||
@@ -762,7 +787,7 @@ func Teacher(c *fiber.Ctx) error {
|
|||||||
claims := token.Claims.(*jwt.StandardClaims)
|
claims := token.Claims.(*jwt.StandardClaims)
|
||||||
|
|
||||||
var teacher models.Teacher
|
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 {
|
if findErr != nil {
|
||||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
|
|||||||
@@ -1083,7 +1083,7 @@ func UpdateTeacherHomeroom(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
result, updateErr := teacherCollection.UpdateOne(
|
result, updateErr := teacherCollection.UpdateOne(
|
||||||
ctx,
|
ctx,
|
||||||
bson.M{"tid": data["tid"]},
|
bson.M{"schooldata.tid": data["tid"]},
|
||||||
update,
|
update,
|
||||||
)
|
)
|
||||||
if updateErr != nil {
|
if updateErr != nil {
|
||||||
@@ -1111,9 +1111,88 @@ func UpdateTeacherPassword(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ResetTeacherPassword(c *fiber.Ctx) error {
|
func ResetTeacherPassword(c *fiber.Ctx) error {
|
||||||
return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{
|
var data map[string]string
|
||||||
"success": nil,
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
"message": "not implimented",
|
|
||||||
|
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_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"address": data["address"],
|
"personaldata.address": data["address"],
|
||||||
"city": data["city"],
|
"personaldata.city": data["city"],
|
||||||
"province": data["province"],
|
"personaldata.province": data["province"],
|
||||||
"postal": data["postal"],
|
"personaldata.postal": data["postal"],
|
||||||
"updated_at": update_time,
|
"updated_at": update_time,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
result, updateErr := teacherCollection.UpdateOne(
|
result, updateErr := teacherCollection.UpdateOne(
|
||||||
ctx,
|
ctx,
|
||||||
bson.M{"tid": data["tid"]},
|
bson.M{"schooldata.tid": data["tid"]},
|
||||||
update,
|
update,
|
||||||
)
|
)
|
||||||
if updateErr != nil {
|
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_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"email": data["email"],
|
"personaldata.email": data["email"],
|
||||||
"updated_at": update_time,
|
"updated_at": update_time,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
result, updateErr := teacherCollection.UpdateOne(
|
result, updateErr := teacherCollection.UpdateOne(
|
||||||
ctx,
|
ctx,
|
||||||
bson.M{"tid": data["tid"]},
|
bson.M{"schooldata.tid": data["tid"]},
|
||||||
update,
|
update,
|
||||||
)
|
)
|
||||||
if updateErr != nil {
|
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_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||||
update := bson.M{
|
update := bson.M{
|
||||||
"$set": bson.M{
|
"$set": bson.M{
|
||||||
"firstname": data["firstname"],
|
"personaldata.firstname": data["firstname"],
|
||||||
"middlename": data["middlename"],
|
"personaldata.middlename": data["middlename"],
|
||||||
"lastname": data["lastname"],
|
"personaldata.lastname": data["lastname"],
|
||||||
"updated_at": update_time,
|
"updated_at": update_time,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -85,10 +85,10 @@ func (s *Student) GenerateSchoolEmail(offset int, lastEmail string) string {
|
|||||||
email = lastEmail[:offset] + strings.ToLower(string(s.PersonalData.FirstName[offset])) + lastEmail[offset:]
|
email = lastEmail[:offset] + strings.ToLower(string(s.PersonalData.FirstName[offset])) + lastEmail[offset:]
|
||||||
}
|
}
|
||||||
if offset == len([]rune(s.PersonalData.FirstName))-1 {
|
if offset == len([]rune(s.PersonalData.FirstName))-1 {
|
||||||
email = strings.ToLower(s.PersonalData.FirstName) + "." + strings.ToLower(s.PersonalData.LastName)
|
email = strings.ToLower(s.PersonalData.FirstName) + "." + strings.ToLower(s.PersonalData.LastName) + "@surreyschools.ca"
|
||||||
}
|
}
|
||||||
if offset > len([]rune(s.PersonalData.FirstName))-1 {
|
if offset > len([]rune(s.PersonalData.FirstName))-1 {
|
||||||
email = strings.ToLower(s.PersonalData.FirstName) + "." + strings.ToLower(s.PersonalData.LastName) + strconv.Itoa(offset-len([]rune(s.PersonalData.FirstName)))
|
email = strings.ToLower(s.PersonalData.FirstName) + "." + strings.ToLower(s.PersonalData.LastName) + strconv.Itoa(offset-len([]rune(s.PersonalData.FirstName))) + "@surreyschools.ca"
|
||||||
}
|
}
|
||||||
return email
|
return email
|
||||||
}
|
}
|
||||||
|
|||||||
+54
-20
@@ -1,12 +1,17 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
|
"github.com/SowinskiBraeden/school-management-api/database"
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -18,25 +23,36 @@ var (
|
|||||||
allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
|
allCharSet = lowerCharSet + upperCharSet + specialCharSet + numberSet
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var teacherCollection *mongo.Collection = database.OpenCollection(database.Client, "teachers")
|
||||||
|
|
||||||
type Teacher struct {
|
type Teacher struct {
|
||||||
ID primitive.ObjectID `bson:"_id"`
|
ID primitive.ObjectID `bson:"_id"`
|
||||||
FirstName string `json:"firstname" validate:"required"`
|
PersonalData struct {
|
||||||
MiddleName string `json:"middlename"`
|
FirstName string `json:"firstname" validate:"required"`
|
||||||
LastName string `json:"lastname" validate:"required"`
|
MiddleName string `json:"middlename"`
|
||||||
Email string `json:"email" validate:"required"`
|
LastName string `json:"lastname" validate:"required"`
|
||||||
SchoolEmail string `json:"schoolemail"`
|
Email string `json:"email" validate:"required"`
|
||||||
Password string `json:"-" validate:"min=10,max=32"`
|
Address string `json:"address"`
|
||||||
TempPassword bool `json:"temppassword"`
|
City string `json:"city"`
|
||||||
TID string `json:"tid"` // Teacher ID
|
Province string `json:"province"`
|
||||||
Homeroom string `json:"homeroom"`
|
Postal string `json:"postal"`
|
||||||
Address string `json:"address"`
|
DOB string `json:"dob" validate:"required"`
|
||||||
City string `json:"city"`
|
} `json:"personaldata"`
|
||||||
Province string `json:"province"`
|
SchoolData struct {
|
||||||
Postal string `json:"postal"`
|
TID string `json:"tid"` // Teacher ID
|
||||||
DOB string `json:"dob" validate:"required"`
|
Homeroom string `json:"homeroom"`
|
||||||
Photo string `json:"string"`
|
PhotoName string `json:"photoname"` // name of photo in db
|
||||||
Created_at time.Time `json:"created_at"`
|
} `json:"schooldata"`
|
||||||
Updated_at time.Time `json:"updated_at"`
|
AccountData struct {
|
||||||
|
SchoolEmail string `json:"schoolemail"`
|
||||||
|
Password string `json:"-" validate:"min=10,max=32"`
|
||||||
|
AccountDisabled bool `bson:"accountdisabled"`
|
||||||
|
TempPassword bool `json:"temppassword"`
|
||||||
|
Attempts int `json:"attempts"` // login attempts max 5
|
||||||
|
HashHistory []string `json:"-"` // List of old hashed passwords (not including auto generated passwords)
|
||||||
|
} `json:"accountdata"`
|
||||||
|
Created_at time.Time `json:"created_at"`
|
||||||
|
Updated_at time.Time `json:"updated_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Teacher) HashPassword(password string) string {
|
func (t *Teacher) HashPassword(password string) string {
|
||||||
@@ -44,13 +60,31 @@ func (t *Teacher) HashPassword(password string) string {
|
|||||||
return string(hash)
|
return string(hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Teacher) GenerateSchoolEmail() string {
|
func (t *Teacher) EmailExists(email string) bool {
|
||||||
var email string = strings.ToLower(t.LastName) + "_" + strings.ToLower(string(t.FirstName[0])) + "@surreyschools.ca"
|
var teacher Teacher
|
||||||
|
findErr := teacherCollection.FindOne(context.TODO(), bson.M{"accountdata.schoolemail": email}).Decode(&teacher)
|
||||||
|
if findErr != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Teacher) GenerateSchoolEmail(offset int, lastEmail string) string {
|
||||||
|
var email string = strings.ToLower(t.PersonalData.LastName) + "_" + strings.ToLower(string(t.PersonalData.FirstName[0])) + "@surreyschools.ca"
|
||||||
|
if offset > 0 && offset < len([]rune(t.PersonalData.FirstName))-1 {
|
||||||
|
email = lastEmail[:strings.Index(lastEmail, "_")+offset+1] + strings.ToLower(string(t.PersonalData.FirstName[offset])) + "@surreyschools.ca"
|
||||||
|
}
|
||||||
|
if offset == len([]rune(t.PersonalData.FirstName))-1 {
|
||||||
|
email = strings.ToLower(t.PersonalData.LastName) + "_" + strings.ToLower(t.PersonalData.FirstName) + "@surreyschools.ca"
|
||||||
|
}
|
||||||
|
if offset > len([]rune(t.PersonalData.FirstName))-1 {
|
||||||
|
email = strings.ToLower(t.PersonalData.LastName) + "_" + strings.ToLower(t.PersonalData.FirstName) + strconv.Itoa(offset-len([]rune(t.PersonalData.FirstName))) + "@surreyschools.ca"
|
||||||
|
}
|
||||||
return email
|
return email
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *Teacher) ComparePasswords(password string) bool {
|
func (t *Teacher) ComparePasswords(password string) bool {
|
||||||
err := bcrypt.CompareHashAndPassword([]byte(t.Password), []byte(password))
|
err := bcrypt.CompareHashAndPassword([]byte(t.AccountData.Password), []byte(password))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user