better default admin creation
This commit is contained in:
2 files changed
+33
-8
No files matched your search
@@ -26,14 +26,20 @@ 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{})
|
||||
func confirm(s string) bool {
|
||||
r := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Printf("%s [y/n]: ", s)
|
||||
res, err := r.ReadString('\n')
|
||||
if err != nil {
|
||||
fmt.Println("Unable to detect new system")
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
if count == 0 {
|
||||
fmt.Println("Admin account setup...")
|
||||
return strings.ToLower(strings.TrimSpace(res))[0] == 'y'
|
||||
}
|
||||
|
||||
func CreateDefaultAdmin() models.Admin {
|
||||
fmt.Println()
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
fmt.Print("First Name: ")
|
||||
firstname, _ := reader.ReadString('\n')
|
||||
@@ -73,13 +79,32 @@ func NewSystem() {
|
||||
admin.Updated_at, _ = time.Parse(time.RFC3339, time.Now().Format(time.RFC3339))
|
||||
admin.ID = primitive.NewObjectID()
|
||||
|
||||
_, insertErr := adminCollection.InsertOne(context.Background(), admin)
|
||||
return admin
|
||||
}
|
||||
|
||||
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...")
|
||||
|
||||
for {
|
||||
defaultAdmin := CreateDefaultAdmin()
|
||||
|
||||
if confirm("Are the above credentials correct?") {
|
||||
_, insertErr := adminCollection.InsertOne(context.Background(), defaultAdmin)
|
||||
if insertErr != nil {
|
||||
log.Printf("Failed to create an admin\n")
|
||||
}
|
||||
|
||||
log.Printf("Successfully created default admin")
|
||||
log.Printf("Your default admin ID is %s", aid)
|
||||
log.Printf("Your default admin ID is %s", defaultAdmin.AID)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user