update code

This commit is contained in:
2024-07-04 12:19:44 +07:00
parent dd3a9ebea4
commit 219fa12590
9 changed files with 152 additions and 103 deletions
+63 -20
View File
@@ -4,30 +4,26 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"io"
"log"
"net/http"
"os"
"github.com/IceWhaleTech/CasaOS-UserService/pkg/config"
"github.com/IceWhaleTech/CasaOS-UserService/service/model"
)
type OMVService interface {
LoginSession(userName string, password string) string
Logout()
GetUser(username string) string
Logout(sessionID string) (string, error)
GetUser(username string, sessionID string) (string, error)
SetUser(m model.UserDBModel) model.UserDBModel
ApplyChange()
}
type omvService struct {
}
// LoginSession implements OMVService.
func (o *omvService) LoginSession(userName string, password string) string {
panic("unimplemented")
}
func LoginSession(username string, password string) string {
func (o *omvService) LoginSession(username string, password string) string {
postBody, _ := json.Marshal(map[string]interface{}{
"service": "session",
"method": "login",
@@ -37,22 +33,51 @@ func LoginSession(username string, password string) string {
},
})
responseBody := bytes.NewBuffer(postBody)
response, err := http.Post("http://10.0.0.4:1081/rpc.php", "application/json", responseBody)
response, err := http.Post(config.AppInfo.OMVServer, "application/json", responseBody)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
}
responseData, err := ioutil.ReadAll(response.Body)
responseData, err := io.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
return string(responseData)
}
func (o *omvService) Logout() {
// Implement logout logic here
func (o *omvService) Logout(sessionID string) (string, error) {
postBody, _ := json.Marshal(map[string]interface{}{
"service": "UserMgmt",
"method": "logout",
"params": nil,
})
responseBody := bytes.NewBuffer(postBody)
req, err := http.NewRequest("POST", config.AppInfo.OMVServer, responseBody)
if err != nil {
return "", fmt.Errorf("error creating request: %v", err)
}
req.Header.Set("X-OPENMEDIAVAULT-SESSIONID", sessionID) // Set session ID header
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return "", fmt.Errorf("error making request: %v", err)
}
defer resp.Body.Close() // Ensure the response body is closed
// Check for HTTP errors
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP error: %s", resp.Status)
}
// Read the response body
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %v", err)
}
return string(responseData), nil
}
func (o *omvService) GetUser(username string) string {
func (o *omvService) GetUser(username string, sessionID string) (string, error) {
// Prepare the RPC request
postBody, _ := json.Marshal(map[string]interface{}{
"service": "UserMgmt",
"method": "getUser",
@@ -61,16 +86,34 @@ func (o *omvService) GetUser(username string) string {
},
})
responseBody := bytes.NewBuffer(postBody)
response, err := http.Post("http://10.0.0.4:1081/rpc.php", "application/json", responseBody)
// Create HTTP request and set session ID header
req, err := http.NewRequest("POST", config.AppInfo.OMVServer, responseBody)
if err != nil {
fmt.Print(err.Error())
os.Exit(1)
return "", fmt.Errorf("error creating request: %v", err)
}
responseData, err := ioutil.ReadAll(response.Body)
req.Header.Set("X-OPENMEDIAVAULT-SESSIONID", sessionID) // Set session ID header
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
return "", fmt.Errorf("error making request: %v", err)
}
return string(responseData)
defer resp.Body.Close() // Ensure the response body is closed
// Check for HTTP errors
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("HTTP error: %s", resp.Status)
}
// Read the response body
responseData, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %v", err)
}
return string(responseData), nil
}
func (o *omvService) SetUser(m model.UserDBModel) model.UserDBModel {
+3 -3
View File
@@ -39,9 +39,6 @@ type store struct {
omv OMVService
}
func (c *store) OMV() OMVService {
return c.omv
}
func (c *store) Event() EventService {
return c.event
}
@@ -52,6 +49,9 @@ func (c *store) Gateway() external.ManagementService {
func (c *store) User() UserService {
return c.user
}
func (c *store) OMV() OMVService {
return c.omv
}
func (c *store) MessageBus() *message_bus.ClientWithResponses {
client, _ := message_bus.NewClientWithResponses("", func(c *message_bus.Client) error {
// error will never be returned, as we always want to return a client, even with wrong address,