detect new system to register default admin
This commit is contained in:
1 parent
20e9ae19f3
commit
17dd4e9561
6 files changed
+66
-5
No files matched your search
+3
-3
@@ -1,6 +1,6 @@
|
||||
mongoURI=mongodb://localhost:27017
|
||||
dbo=school
|
||||
secret=
|
||||
secret=<your 256 bit secret>
|
||||
PORT=8000
|
||||
SYSTEM_EMAIL=
|
||||
SYSTEM_PASSWORD=
|
||||
SYSTEM_EMAIL=<your system email>
|
||||
SYSTEM_PASSWORD=<your system password>
|
||||
@@ -1,12 +1,16 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/SowinskiBraeden/school-management-api/database"
|
||||
"github.com/SowinskiBraeden/school-management-api/models"
|
||||
"github.com/howeyc/gopass"
|
||||
|
||||
"net/smtp"
|
||||
|
||||
@@ -23,6 +27,57 @@ var studentCollection *mongo.Collection = database.OpenCollection(database.Clien
|
||||
var contactCollection *mongo.Collection = database.OpenCollection(database.Client, "contacts")
|
||||
var adminCollection *mongo.Collection = database.OpenCollection(database.Client, "admins")
|
||||
|
||||
func NewSystem() {
|
||||
count, err := adminCollection.CountDocuments(context.Background(), bson.D{})
|
||||
if err != nil {
|
||||
fmt.Println("Unable to detect new system")
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
fmt.Println("Admin account setup...")
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("First Name: ")
|
||||
firstname, _ := reader.ReadString('\n')
|
||||
fmt.Print("Last Name: ")
|
||||
lastname, _ := reader.ReadString('\n')
|
||||
fmt.Print("Email: ")
|
||||
email, _ := reader.ReadString('\n')
|
||||
fmt.Print("Password: ")
|
||||
password, _ := gopass.GetPasswd()
|
||||
|
||||
var admin models.Admin
|
||||
admin.FirstName = strings.TrimSuffix(firstname, "\n")
|
||||
admin.LastName = strings.TrimSuffix(lastname, "\n")
|
||||
admin.Email = strings.TrimSuffix(email, "\n")
|
||||
|
||||
admin.SchoolEmail = admin.GenerateSchoolEmail()
|
||||
|
||||
pass := strings.TrimSuffix(string(password), "\n")
|
||||
admin.Password = admin.HashPassword(pass)
|
||||
admin.TempPassword = false
|
||||
|
||||
var aid string
|
||||
for {
|
||||
aid = GenerateID(6)
|
||||
if ValidateID(aid) == true {
|
||||
break
|
||||
}
|
||||
}
|
||||
admin.AID = aid
|
||||
|
||||
admin.Created_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
admin.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
admin.ID = primitive.NewObjectID()
|
||||
|
||||
_, insertErr := adminCollection.InsertOne(context.Background(), admin)
|
||||
if insertErr != nil {
|
||||
fmt.Print("Failed to create admin")
|
||||
}
|
||||
|
||||
fmt.Println("Successfully created system admin")
|
||||
}
|
||||
}
|
||||
|
||||
var SecretKey = os.Getenv("secret")
|
||||
|
||||
var systemEmail string = os.Getenv("SYSTEM_EMAIL")
|
||||
|
||||
@@ -7,6 +7,7 @@ require (
|
||||
github.com/gofiber/fiber/v2 v2.26.0
|
||||
github.com/google/go-cmp v0.5.6 // indirect
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
go.mongodb.org/mongo-driver v1.7.2
|
||||
|
||||
@@ -42,6 +42,8 @@ github.com/google/go-cmp v0.5.6 h1:BKbKCqvP6I+rmFHt06ZmyQtvB8xAkWdhFyr0ZUNZcxQ=
|
||||
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
|
||||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef h1:A9HsByNhogrvm9cWb28sjiS3i7tcKCkflWFEkHfuAgM=
|
||||
github.com/howeyc/gopass v0.0.0-20210920133722-c8aef6fb66ef/go.mod h1:lADxMC39cJJqL93Duh1xhAs4I2Zs8mKS89XWXFGp9cs=
|
||||
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
|
||||
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||
@@ -128,6 +130,7 @@ golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBc
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d h1:FjkYO/PPp4Wi0EAUOVLxePm7qVW4r4ctbWpURyuOD0E=
|
||||
golang.org/x/sys v0.0.0-20211205182925-97ca703d548d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/gofiber/fiber/v2/middleware/cors"
|
||||
)
|
||||
|
||||
const version string = "\nv0.8.2-Alpha"
|
||||
const version string = "\nv0.8.3-Alpha"
|
||||
|
||||
func main() {
|
||||
fmt.Println(version)
|
||||
|
||||
+3
-1
@@ -7,8 +7,10 @@ import (
|
||||
)
|
||||
|
||||
func Setup(app *fiber.App) {
|
||||
// API Handling
|
||||
// Detect if system is new and needs default admin
|
||||
controllers.NewSystem()
|
||||
|
||||
// API Handling
|
||||
var routerPrefix string = "/api/v1"
|
||||
|
||||
// Student Authentication Handler
|
||||
|
||||
Reference in new issue
Block a user