feat(controller): admin user for enterprise support
This commit is contained in:
parent
56e3b24d7d
commit
feb39953ed
|
|
@ -24,7 +24,7 @@ type Api struct {
|
||||||
db db.Database
|
db db.Database
|
||||||
kv jetstream.KeyValue
|
kv jetstream.KeyValue
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
enterpise bool
|
enterpise config.Enterprise
|
||||||
}
|
}
|
||||||
|
|
||||||
const REQUEST_TIMEOUT = time.Second * 30
|
const REQUEST_TIMEOUT = time.Second * 30
|
||||||
|
|
@ -43,6 +43,11 @@ func NewApi(c *config.Config, js jetstream.JetStream, nc *nats.Conn, bridge brid
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Api) StartApi() {
|
func (a *Api) StartApi() {
|
||||||
|
|
||||||
|
if a.enterpise.SupportPassword != "" && a.enterpise.SupportEmail != "" {
|
||||||
|
go registerEnterpriseSupport(a.enterpise.SupportEmail, a.enterpise.SupportPassword, a.db)
|
||||||
|
}
|
||||||
|
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
authentication := r.PathPrefix("/api/auth").Subrouter()
|
authentication := r.PathPrefix("/api/auth").Subrouter()
|
||||||
authentication.HandleFunc("/login", a.generateToken).Methods("PUT")
|
authentication.HandleFunc("/login", a.generateToken).Methods("PUT")
|
||||||
|
|
@ -72,7 +77,7 @@ func (a *Api) StartApi() {
|
||||||
iot.HandleFunc("/{sn}/{mtp}/instances", a.deviceGetParameterInstances).Methods("PUT")
|
iot.HandleFunc("/{sn}/{mtp}/instances", a.deviceGetParameterInstances).Methods("PUT")
|
||||||
iot.HandleFunc("/{sn}/{mtp}/operate", a.deviceOperateMsg).Methods("PUT")
|
iot.HandleFunc("/{sn}/{mtp}/operate", a.deviceOperateMsg).Methods("PUT")
|
||||||
iot.HandleFunc("/{sn}/{mtp}/fw_update", a.deviceFwUpdate).Methods("PUT") //TODO: put it to work and generalize for usp and cwmp
|
iot.HandleFunc("/{sn}/{mtp}/fw_update", a.deviceFwUpdate).Methods("PUT") //TODO: put it to work and generalize for usp and cwmp
|
||||||
if a.enterpise {
|
if a.enterpise.Enable {
|
||||||
iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).Methods("GET")
|
iot.HandleFunc("/{sn}/sitesurvey", a.deviceSiteSurvey).Methods("GET")
|
||||||
iot.HandleFunc("/{sn}/connecteddevices", a.deviceConnectedDevices).Methods("GET")
|
iot.HandleFunc("/{sn}/connecteddevices", a.deviceConnectedDevices).Methods("GET")
|
||||||
iot.HandleFunc("/{sn}/traceroute", a.deviceTraceRoute).Methods("GET", "PUT")
|
iot.HandleFunc("/{sn}/traceroute", a.deviceTraceRoute).Methods("GET", "PUT")
|
||||||
|
|
@ -119,3 +124,28 @@ func (a *Api) StartApi() {
|
||||||
}()
|
}()
|
||||||
log.Println("Running REST API at port", a.port)
|
log.Println("Running REST API at port", a.port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registerEnterpriseSupport(email, password string, d db.Database) {
|
||||||
|
|
||||||
|
user := db.User{
|
||||||
|
Email: email,
|
||||||
|
Password: password,
|
||||||
|
Name: "Enterprise Support",
|
||||||
|
Level: db.AdminUser,
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
err := d.RegisterUser(user)
|
||||||
|
if err != nil {
|
||||||
|
if err == db.ErrorUserExists {
|
||||||
|
log.Println("Enterprise support user already registered.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
log.Println("Error to register enterprise support user:", err)
|
||||||
|
time.Sleep(time.Second * 5)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
log.Println("Enterprise support user registered successfully.")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ func (a *Api) registerAdminUser(w http.ResponseWriter, r *http.Request) {
|
||||||
utils.MarshallEncoder(err, w)
|
utils.MarshallEncoder(err, w)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !adminUserExists(users) {
|
if !adminUserExists(users, a.enterpise.SupportEmail) {
|
||||||
var user db.User
|
var user db.User
|
||||||
err = json.NewDecoder(r.Body).Decode(&user)
|
err = json.NewDecoder(r.Body).Decode(&user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -235,14 +235,14 @@ func (a *Api) registerAdminUser(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func adminUserExists(users []map[string]interface{}) bool {
|
func adminUserExists(users []map[string]interface{}, supportEmail string) bool {
|
||||||
|
|
||||||
if len(users) == 0 {
|
if len(users) == 0 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, x := range users {
|
for _, x := range users {
|
||||||
if x["level"].(db.UserLevels) == db.AdminUser && x["email"].(string) != "support@oktopus.app.br" {
|
if x["level"].(db.UserLevels) == db.AdminUser && x["email"].(string) != supportEmail {
|
||||||
log.Println("Admin exists")
|
log.Println("Admin exists")
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
@ -258,7 +258,7 @@ func (a *Api) adminUserExists(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
adminExits := adminUserExists(users)
|
adminExits := adminUserExists(users, a.enterpise.SupportEmail)
|
||||||
json.NewEncoder(w).Encode(adminExits)
|
json.NewEncoder(w).Encode(adminExits)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -213,7 +213,7 @@ func (a *Api) deviceWifi(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if device.Cwmp == entity.Online {
|
if device.Cwmp == entity.Online {
|
||||||
|
|
||||||
if a.enterpise {
|
if a.enterpise.Enable {
|
||||||
a.getEnterpriseResource("wifi", "get", device, sn, w, []byte{}, "cwmp", "098")
|
a.getEnterpriseResource("wifi", "get", device, sn, w, []byte{}, "cwmp", "098")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
@ -347,7 +347,7 @@ func (a *Api) deviceWifi(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
if device.Cwmp == entity.Online {
|
if device.Cwmp == entity.Online {
|
||||||
|
|
||||||
if a.enterpise {
|
if a.enterpise.Enable {
|
||||||
payload, err := io.ReadAll(r.Body)
|
payload, err := io.ReadAll(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
|
|
|
||||||
|
|
@ -29,11 +29,17 @@ type RestApi struct {
|
||||||
Ctx context.Context
|
Ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Enterprise struct {
|
||||||
|
Enable bool
|
||||||
|
SupportPassword string
|
||||||
|
SupportEmail string
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
RestApi RestApi
|
RestApi RestApi
|
||||||
Nats Nats
|
Nats Nats
|
||||||
Mongo Mongo
|
Mongo Mongo
|
||||||
Enterprise bool
|
Enterprise Enterprise
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewConfig() *Config {
|
func NewConfig() *Config {
|
||||||
|
|
@ -47,6 +53,8 @@ func NewConfig() *Config {
|
||||||
flApiPort := flag.String("api_port", lookupEnvOrString("REST_API_PORT", "8000"), "Rest api port")
|
flApiPort := flag.String("api_port", lookupEnvOrString("REST_API_PORT", "8000"), "Rest api port")
|
||||||
mongoUri := flag.String("mongo_uri", lookupEnvOrString("MONGO_URI", "mongodb://localhost:27017"), "uri for mongodb server")
|
mongoUri := flag.String("mongo_uri", lookupEnvOrString("MONGO_URI", "mongodb://localhost:27017"), "uri for mongodb server")
|
||||||
enterpise := flag.Bool("enterprise", lookupEnvOrBool("ENTERPRISE", false), "enterprise version enable")
|
enterpise := flag.Bool("enterprise", lookupEnvOrBool("ENTERPRISE", false), "enterprise version enable")
|
||||||
|
enterprise_support_password := flag.String("enterprise_support_password", lookupEnvOrString("ENTERPRISE_SUPPORT_PASSWORD", ""), "enterprise support password")
|
||||||
|
enterpise_support_email := flag.String("enterprise_support_email", lookupEnvOrString("ENTERPRISE_SUPPORT_EMAIL", ""), "enterprise support email")
|
||||||
flHelp := flag.Bool("help", false, "Help")
|
flHelp := flag.Bool("help", false, "Help")
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -80,7 +88,11 @@ func NewConfig() *Config {
|
||||||
Uri: *mongoUri,
|
Uri: *mongoUri,
|
||||||
Ctx: ctx,
|
Ctx: ctx,
|
||||||
},
|
},
|
||||||
Enterprise: *enterpise,
|
Enterprise: Enterprise{
|
||||||
|
Enable: *enterpise,
|
||||||
|
SupportPassword: *enterprise_support_password,
|
||||||
|
SupportEmail: *enterpise_support_email,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,6 @@ type UserLevels int32
|
||||||
const (
|
const (
|
||||||
NormalUser UserLevels = iota
|
NormalUser UserLevels = iota
|
||||||
AdminUser
|
AdminUser
|
||||||
OktopusUser
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user