update authorized.go, database.go, middleware.go, project.go, response.go, role.go, service.go
This commit is contained in:
1 parent
53257427c9
commit
b6133df436
7 files changed
+226
No files matched your search
@@ -0,0 +1,15 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type error struct {
|
||||
Message string `json:"message"`
|
||||
}
|
||||
|
||||
func NewErrorResponse(c *gin.Context, statusCode int, message string) {
|
||||
logrus.Error(message)
|
||||
c.AbortWithStatusJSON(statusCode, error{Message: message})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"esproject"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func MatchRole(c *gin.Context, role string) bool {
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("https://auth.easystartup.su/api/user/self"), nil)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
req.Header.Add("Authorization", c.GetHeader("Authorization"))
|
||||
req.Header.Add("Accept", "application/json")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
defer func(Body io.ReadCloser) {
|
||||
err := Body.Close()
|
||||
if err != nil {
|
||||
|
||||
}
|
||||
}(resp.Body)
|
||||
|
||||
// Check if the response status is 200 OK
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return false
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var result esproject.AuthorizedUser
|
||||
err = json.Unmarshal(body, &result)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
return result.Access == role
|
||||
}
|
||||
Reference in new issue
Block a user