Update Admin Authenitcation
This commit is contained in:
1 parent
c01900968c
commit
b1a1315ca1
2 files changed
+48
-99
No files matched your search
@@ -27,6 +27,27 @@ const SecretKey = "secret"
|
|||||||
var systemEmail string = os.Getenv("SYSTEM_EMAIL")
|
var systemEmail string = os.Getenv("SYSTEM_EMAIL")
|
||||||
var systemPassword string = os.Getenv("SYSTEM_PASSWORD")
|
var systemPassword string = os.Getenv("SYSTEM_PASSWORD")
|
||||||
|
|
||||||
|
func AuthAdmin(c *fiber.Ctx) bool {
|
||||||
|
cookie := c.Cookies("jwt")
|
||||||
|
|
||||||
|
token, err := jwt.ParseWithClaims(cookie, &jwt.StandardClaims{}, func(token *jwt.Token) (interface{}, error) {
|
||||||
|
return []byte(SecretKey), nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
claims := token.Claims.(*jwt.StandardClaims)
|
||||||
|
|
||||||
|
var admin models.Admin
|
||||||
|
findErr := adminCollection.FindOne(context.TODO(), bson.M{"aid": claims.Issuer}).Decode(&admin)
|
||||||
|
if findErr != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func Enroll(c *fiber.Ctx) error {
|
func Enroll(c *fiber.Ctx) error {
|
||||||
var data map[string]string
|
var data map[string]string
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
@@ -40,22 +61,12 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var admin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -98,7 +109,7 @@ func Enroll(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
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.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{
|
||||||
@@ -151,22 +162,12 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var admin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,7 +199,7 @@ func RegisterTeacher(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
||||||
|
|
||||||
err = smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{teacher.Email}, message)
|
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{teacher.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{
|
||||||
@@ -253,22 +254,12 @@ func CreateAdmin(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var checkadmin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&checkadmin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -298,7 +289,7 @@ func CreateAdmin(c *fiber.Ctx) error {
|
|||||||
|
|
||||||
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
auth := smtp.PlainAuth("", systemEmail, systemPassword, smtpHost)
|
||||||
|
|
||||||
err = smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{admin.Email}, message)
|
err := smtp.SendMail(smtpHost+":"+smtpPort, auth, systemEmail, []string{admin.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{
|
||||||
@@ -670,22 +661,12 @@ func CreateContact(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var admin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"school-management/models"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
@@ -23,22 +22,12 @@ func UpdateStudentName(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var admin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,22 +86,12 @@ func UpdateStudentGradeLevel(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authorized admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var admin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -168,13 +147,12 @@ func UpdateStudentHomeroom(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
var admin models.Admin
|
// Ensure Authenticated admin sent request
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
if !AuthAdmin(c) {
|
||||||
if err != nil {
|
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "invalid admin id",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,22 +292,12 @@ func UpdateTeacherName(c *fiber.Ctx) error {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if admin sent request
|
// Ensure Authenticated admin sent request
|
||||||
if data["aid"] == "" {
|
if !AuthAdmin(c) {
|
||||||
cancel()
|
cancel()
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
||||||
"success": false,
|
"success": false,
|
||||||
"message": "admin id required",
|
"message": "Unauthorized: only an admin can perform this action",
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var admin models.Admin
|
|
||||||
err := adminCollection.FindOne(ctx, bson.M{"aid": data["aid"]}).Decode(&admin)
|
|
||||||
if err != nil {
|
|
||||||
cancel()
|
|
||||||
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
|
|
||||||
"success": false,
|
|
||||||
"message": "invalid admin id",
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user