diff --git a/controllers/authController.go b/controllers/authController.go index 5abc58c..6e57c56 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -80,31 +80,31 @@ func Enroll(c *fiber.Ctx) error { } var student models.Student - student.FirstName = data["firstname"] - student.MiddleName = data["middlename"] - student.LastName = data["lastname"] - student.Age, _ = strconv.Atoi(data["age"]) - student.GradeLevel, _ = strconv.Atoi(data["gradelevel"]) - student.DOB = data["dob"] - student.Email = data["email"] - student.Province = data["province"] - student.City = data["city"] - student.Address = data["address"] - student.Postal = data["postal"] - student.Contacts = []string{} + student.PersonalData.FirstName = data["firstname"] + student.PersonalData.MiddleName = data["middlename"] + student.PersonalData.LastName = data["lastname"] + student.PersonalData.Age, _ = strconv.Atoi(data["age"]) + student.SchoolData.GradeLevel, _ = strconv.Atoi(data["gradelevel"]) + student.PersonalData.DOB = data["dob"] + student.PersonalData.Email = data["email"] + student.PersonalData.Province = data["province"] + student.PersonalData.City = data["city"] + student.PersonalData.Address = data["address"] + student.PersonalData.Postal = data["postal"] + student.PersonalData.Contacts = []string{} - student.YOG = ((12 - student.GradeLevel) + time.Now().Year()) + 1 + student.SchoolData.YOG = ((12 - student.SchoolData.GradeLevel) + time.Now().Year()) + 1 - student.SchoolEmail = student.GenerateSchoolEmail() + student.SchoolData.SchoolEmail = student.GenerateSchoolEmail() // Disable login block - student.AccountDisabled = false - student.Attempts = 0 + student.AccountData.AccountDisabled = false + student.AccountData.Attempts = 0 // Generate temporary password tempPass := student.GeneratePassword(12, 1, 1, 1) - student.Password = student.HashPassword(tempPass) - student.TempPassword = true + student.SchoolData.Password = student.HashPassword(tempPass) + student.AccountData.TempPassword = true // Send student personal email temp password smtpHost := "smpt.gmail.com" @@ -114,7 +114,7 @@ func Enroll(c *fiber.Ctx) error { auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost) - err := smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{student.Email}, message) + err := smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{student.PersonalData.Email}, message) if err != nil { cancel() return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{ @@ -131,7 +131,7 @@ func Enroll(c *fiber.Ctx) error { break } } - student.SID = sid + student.SchoolData.SID = sid student.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) student.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) @@ -370,7 +370,7 @@ func StudentLogin(c *fiber.Ctx) error { } var localAccountDisabled = false - if student.Attempts >= 5 { + if student.AccountData.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{ @@ -396,7 +396,7 @@ func StudentLogin(c *fiber.Ctx) error { } } - if localAccountDisabled || student.AccountDisabled { + if localAccountDisabled || student.AccountData.AccountDisabled { cancel() return c.Status(fiber.StatusForbidden).JSON(fiber.Map{ "success": false, @@ -409,7 +409,7 @@ func StudentLogin(c *fiber.Ctx) error { update_time, _ := time.Parse(time.RFC3339, time.Now().Format(time.RFC3339)) update := bson.M{ "$set": bson.M{ - "attempts": student.Attempts + 1, + "attempts": student.AccountData.Attempts + 1, "updated_at": update_time, }, } @@ -435,7 +435,7 @@ func StudentLogin(c *fiber.Ctx) error { defer cancel() claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.StandardClaims{ - Issuer: student.SID, + Issuer: student.SchoolData.SID, ExpiresAt: time.Now().Add(time.Hour * 24).Unix(), // 1 Day }) token, err := claims.SignedString([]byte(SecretKey)) diff --git a/controllers/updateController.go b/controllers/updateController.go index 147003b..e5d84c0 100644 --- a/controllers/updateController.go +++ b/controllers/updateController.go @@ -291,6 +291,13 @@ func UpdateStudentPassword(c *fiber.Ctx) error { }) } +func ResetStudentPassword(c *fiber.Ctx) error { + return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ + "succes": nil, + "message": "not implimented", + }) +} + func UpdateStudentLocker(c *fiber.Ctx) error { return c.Status(fiber.StatusNotImplemented).JSON(fiber.Map{ "success": nil, diff --git a/models/studentModel.go b/models/studentModel.go index 7f3efed..3f1e929 100644 --- a/models/studentModel.go +++ b/models/studentModel.go @@ -11,33 +11,47 @@ import ( "go.mongodb.org/mongo-driver/bson/primitive" ) +type Locker struct { + ID primitive.ObjectID `bson:"_id"` + LockerNumber string `json:"lockernumber"` + LockerPasscode string `json:"lockerpasscode"` + LockerType string `json:"lockertype"` // Upper / Lower locker +} + type Student struct { - ID primitive.ObjectID `bson:"_id"` - AccountDisabled bool `bson:"accountdisabled"` - FirstName string `json:"firstname" validate:"required"` - MiddleName string `json:"middlename"` - LastName string `json:"lastname" validate:"required"` - Age int `json:"age" validate:"required"` - GradeLevel int `json:"gradelevel" validate:"required"` - Email string `json:"email" validate:"required"` - SchoolEmail string `json:"schoolemail"` - Password string `json:"-" validate:"min=10,max=32"` - TempPassword bool `json:"temppassword"` - Attempts int `json:"attempts"` // login attempts max 5 - SID string `json:"sid"` // Student ID - PEN string `json:"ped"` // Personal Education Number - Homeroom string `json:"homeroom"` - Locker string `json:"locker"` - YOG int `json:"yog"` // Year of Graduation - Address string `json:"address"` - City string `json:"city"` - Province string `json:"province"` - Postal string `json:"postal"` - DOB string `json:"dob" validate:"required"` - Photo string `json:"photo"` - 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"` + ID primitive.ObjectID `bson:"_id"` + PersonalData struct { + FirstName string `json:"firstname" validate:"required"` + MiddleName string `json:"middlename"` + LastName string `json:"lastname" validate:"required"` + Age int `json:"age" validate:"required"` + Email string `json:"email" validate:"required"` + Address string `json:"address"` + City string `json:"city"` + Province string `json:"province"` + Postal string `json:"postal"` + DOB string `json:"dob" validate:"required"` + Photo string `json:"photo"` + Contacts []string `json:"contacts"` // List of contact ID's rather than contact object + } + SchoolData struct { + GradeLevel int `json:"gradelevel" validate:"required"` + SchoolEmail string `json:"schoolemail"` + Password string `json:"-" validate:"min=10,max=32"` + SID string `json:"sid"` // Student ID + PEN string `json:"ped"` // Personal Education Number + Homeroom string `json:"homeroom"` + Locker Locker `json:"locker"` + YOG int `json:"yog"` // Year of Graduation + Photo string `json:"photo"` + } + AccountData struct { + AccountDisabled bool `bson:"accountdisabled"` + TempPassword bool `json:"temppassword"` + Attempts int `json:"attempts"` // login attempts max 5 + } + Created_at time.Time `json:"created_at"` + Updated_at time.Time `json:"updated_at"` } func (s *Student) HashPassword(password string) string { @@ -46,13 +60,13 @@ func (s *Student) HashPassword(password string) string { } func (s *Student) GenerateSchoolEmail() string { - var email string = strings.ToLower(string(s.FirstName[0])) + "." + strings.ToLower(s.LastName) + "@surreyschools.ca" + var email string = strings.ToLower(string(s.PersonalData.FirstName[0])) + "." + strings.ToLower(s.PersonalData.LastName) + "@surreyschools.ca" // Add check to see if email already exists return email } func (s *Student) ComparePasswords(password string) bool { - err := bcrypt.CompareHashAndPassword([]byte(s.Password), []byte(password)) + err := bcrypt.CompareHashAndPassword([]byte(s.SchoolData.Password), []byte(password)) if err != nil { return false }