diff --git a/.env.example b/.env.example index 41a14d0..5e571ec 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,6 @@ mongoURI=mongodb://localhost:27017 dbo=school -secret= +secret= PORT=8000 -SYSTEM_EMAIL= -SYSTEM_PASSWORD= \ No newline at end of file +SYSTEM_EMAIL= +SYSTEM_PASSWORD= \ No newline at end of file diff --git a/controllers/authController.go b/controllers/authController.go index af828c2..a0d28a9 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -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") diff --git a/go.mod b/go.mod index 2e89f0e..bd5f359 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index b1ec608..5085e36 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index e966495..cf3e69c 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/routes/routes.go b/routes/routes.go index 1bc45b4..01c7870 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -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