Skip to content

Commit

Permalink
Delete add Add admin's tenant & user when initializing Backend
Browse files Browse the repository at this point in the history
fix linter
  • Loading branch information
brianchennn committed Jul 13, 2023
1 parent eb081ac commit e7d2ed3
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
51 changes: 51 additions & 0 deletions backend/WebUI/api_webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,7 @@ func PostTenant(c *gin.Context) {
return
}

// fmt.Println("in Post Tenant")
if tenantData.TenantId == "" {
tenantData.TenantId = uuid.Must(uuid.NewRandom()).String()
}
Expand Down Expand Up @@ -1674,3 +1675,53 @@ func GetUEPDUSessionInfo(c *gin.Context) {
})
}
}

// Create Admin's Tenant & Account
func SetAdmin() {
err := mongoapi.RestfulAPIDeleteOne("tenantData", bson.M{"tenantName": "admin"})
if err != nil {
logger.InitLog.Errorf("RestfulAPIDeleteOne err: %+v", err)
}
err = mongoapi.RestfulAPIDeleteOne("userData", bson.M{"email": "admin"})
if err != nil {
logger.InitLog.Errorf("RestfulAPIDeleteOne err: %+v", err)
}

// Create Admin tenant
logger.InitLog.Infoln("Create tenant: admin")

adminTenantData := bson.M{
"tenantId": uuid.Must(uuid.NewRandom()).String(),
"tenantName": "admin",
}

_, err = mongoapi.RestfulAPIPutOne("tenantData", bson.M{"tenantName": "admin"}, adminTenantData)
if err != nil {
logger.InitLog.Errorf("RestfulAPIPutOne err: %+v", err)
}

AmdinTenant, err := mongoapi.RestfulAPIGetOne("tenantData", bson.M{"tenantName": "admin"})
if err != nil {
logger.InitLog.Errorf("RestfulAPIGetOne err: %+v", err)
}

// Create Admin user
logger.InitLog.Infoln("Create user: admin")

hash, err := bcrypt.GenerateFromPassword([]byte("free5gc"), 12)
if err != nil {
logger.InitLog.Errorf("GenerateFromPassword err: %+v", err)
}

adminUserData := bson.M{
"userId": uuid.Must(uuid.NewRandom()).String(),
"tenantId": AmdinTenant["tenantId"],
"email": "admin",
"encryptedPassword": string(hash),
}

_, err = mongoapi.RestfulAPIPutOne("userData", bson.M{"email": "admin"}, adminUserData)
if err != nil {
logger.InitLog.Errorf("RestfulAPIPutOne err: %+v", err)
}
}
26 changes: 1 addition & 25 deletions backend/webui_service/webui_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (

"github.com/gin-contrib/cors"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson"
"golang.org/x/crypto/bcrypt"

"github.com/free5gc/util/mongoapi"
"github.com/free5gc/webconsole/backend/WebUI"
Expand Down Expand Up @@ -79,32 +77,10 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {
return
}

// Create admin account
filter := bson.M{"email": "admin"}
hash, err := bcrypt.GenerateFromPassword([]byte("free5gc"), 12)
if err != nil {
logger.InitLog.Errorf("GenerateFromPassword err: %+v", err)
}

data := bson.M{
"userId": "1",
"tenantId": "1",
"email": "admin",
"encryptedPassword": string(hash),
}

existed, err := mongoapi.RestfulAPIPutOne("userData", filter, data)
if err != nil {
logger.InitLog.Errorf("RestfulAPIPutOne err: %+v", err)
}

if existed {
logger.InitLog.Infof("Admin existed.")
}

logger.InitLog.Infoln("Server started")

router := WebUI.NewRouter()
WebUI.SetAdmin()

router.Use(cors.New(cors.Config{
AllowMethods: []string{"GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"},
Expand Down

0 comments on commit e7d2ed3

Please sign in to comment.