add org structure
This commit is contained in:
@@ -22,11 +22,11 @@ type Config struct {
|
|||||||
func New() *Config {
|
func New() *Config {
|
||||||
return &Config{
|
return &Config{
|
||||||
MSHosts: MSHostsConfig{
|
MSHosts: MSHostsConfig{
|
||||||
UsersHost: getEnv("ASTRA-USERS", ""),
|
UsersHost: getEnv("ASTRA-USERS", "http://localhost:8082"),
|
||||||
StoreHost: getEnv("ASTRA-FILE-STORAGE", ""),
|
StoreHost: getEnv("ASTRA-FILE-STORAGE", "http://localhost:8081"),
|
||||||
},
|
},
|
||||||
Server: ServerConfig{
|
Server: ServerConfig{
|
||||||
Port: getEnv("PORT", ""),
|
Port: getEnv("PORT", "8080"),
|
||||||
JWTSecret: getEnv("JWT_SECRET", "secret"),
|
JWTSecret: getEnv("JWT_SECRET", "secret"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
10
main.go
10
main.go
@@ -40,6 +40,8 @@ func main() {
|
|||||||
e.DELETE("/users/:id", users.DeleteUser)
|
e.DELETE("/users/:id", users.DeleteUser)
|
||||||
e.PATCH("/users/:id", users.UpdateUser)
|
e.PATCH("/users/:id", users.UpdateUser)
|
||||||
|
|
||||||
|
e.GET("/organizations", users.GetOrganizations)
|
||||||
|
|
||||||
// Store
|
// Store
|
||||||
store_g := e.Group("/store")
|
store_g := e.Group("/store")
|
||||||
store_g.GET("/*", store.GetFileFromStore)
|
store_g.GET("/*", store.GetFileFromStore)
|
||||||
@@ -48,5 +50,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func checkAuth(c echo.Context) error {
|
func checkAuth(c echo.Context) error {
|
||||||
return c.HTML(http.StatusOK, c.Request().Header.Get("UserId"))
|
result := c.Request().Header.Get("UserId") + ", " +
|
||||||
|
c.Request().Header.Get("UserRole") + ", " +
|
||||||
|
c.Request().Header.Get("UserOrganizationId") + ", " +
|
||||||
|
c.Request().Header.Get("UserRightMask") + ", " +
|
||||||
|
c.Request().Header.Get("UserPosition")
|
||||||
|
|
||||||
|
return c.HTML(http.StatusOK, result)
|
||||||
}
|
}
|
||||||
|
|||||||
8
model/position.go
Normal file
8
model/position.go
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
package model
|
||||||
|
|
||||||
|
type Position struct {
|
||||||
|
Name string `json:"name" gorm:"unique;index"`
|
||||||
|
RightMask int `json:"right_mask"`
|
||||||
|
OrganizationID uint `json:"organization_id"`
|
||||||
|
Users []User `json:"users,omitempty"`
|
||||||
|
}
|
||||||
@@ -5,9 +5,12 @@ import "gorm.io/gorm"
|
|||||||
type (
|
type (
|
||||||
User struct {
|
User struct {
|
||||||
gorm.Model
|
gorm.Model
|
||||||
UserName string `json:"userName" gorm:"unique"`
|
UserName string `json:"user_name" gorm:"unique;index"`
|
||||||
Password string `json:"password,omitempty"`
|
Password string `json:"password,omitempty"`
|
||||||
FirstName string `json:"firstName"`
|
FirstName string `json:"first_name"`
|
||||||
LastName string `json:"lastName"`
|
LastName string `json:"last_name"`
|
||||||
|
Role string `json:"role" gorm:"default:employee"`
|
||||||
|
PositionID uint `json:"-"`
|
||||||
|
Position Position `gorm:"-:migration"`
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,7 +4,14 @@ import (
|
|||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var NOT_ALLOWED_HEADERS = [2]string{"UserId", "Authorization"}
|
var NOT_ALLOWED_HEADERS = [6]string{
|
||||||
|
"UserId",
|
||||||
|
"Authorization",
|
||||||
|
"UserRole",
|
||||||
|
"UserOrganizationId",
|
||||||
|
"UserRightMask",
|
||||||
|
"UserPosition",
|
||||||
|
}
|
||||||
|
|
||||||
func FilterHeaders(next echo.HandlerFunc) echo.HandlerFunc {
|
func FilterHeaders(next echo.HandlerFunc) echo.HandlerFunc {
|
||||||
return func(c echo.Context) error {
|
return func(c echo.Context) error {
|
||||||
|
|||||||
@@ -21,6 +21,10 @@ var JWT_MW = echojwt.WithConfig(echojwt.Config{
|
|||||||
user := c.Get("user").(*jwt.Token)
|
user := c.Get("user").(*jwt.Token)
|
||||||
claims := user.Claims.(jwt.MapClaims)
|
claims := user.Claims.(jwt.MapClaims)
|
||||||
c.Request().Header.Set("UserId", claims["id"].(string))
|
c.Request().Header.Set("UserId", claims["id"].(string))
|
||||||
|
c.Request().Header.Set("UserRole", claims["role"].(string))
|
||||||
|
c.Request().Header.Set("UserOrganizationId", claims["position_name"].(string))
|
||||||
|
c.Request().Header.Set("UserRightMask", claims["position_rm"].(string))
|
||||||
|
c.Request().Header.Set("UserPosition", claims["position_org_id"].(string))
|
||||||
c.Request().Header.Del("Authorization")
|
c.Request().Header.Del("Authorization")
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
func GetFileFromStore(c echo.Context) error {
|
func GetFileFromStore(c echo.Context) error {
|
||||||
fileName := strings.ReplaceAll(c.Request().RequestURI, "/store", "")
|
fileName := strings.ReplaceAll(c.Request().RequestURI, "/store", "")
|
||||||
|
|
||||||
data, err, req := api.GetRequest(
|
data, err, resp := api.GetRequest(
|
||||||
config.Env.MSHosts.StoreHost+fileName,
|
config.Env.MSHosts.StoreHost+fileName,
|
||||||
c.Request().Header,
|
c.Request().Header,
|
||||||
)
|
)
|
||||||
@@ -19,5 +19,5 @@ func GetFileFromStore(c echo.Context) error {
|
|||||||
return c.NoContent(http.StatusBadGateway)
|
return c.NoContent(http.StatusBadGateway)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Blob(req.StatusCode, req.Header.Get("Content-Type"), data)
|
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -38,6 +38,10 @@ func Login(c echo.Context) (err error) {
|
|||||||
claims := token.Claims.(jwt.MapClaims)
|
claims := token.Claims.(jwt.MapClaims)
|
||||||
claims["id"] = strconv.Itoa(int(u.ID))
|
claims["id"] = strconv.Itoa(int(u.ID))
|
||||||
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
claims["exp"] = time.Now().Add(time.Hour * 72).Unix()
|
||||||
|
claims["role"] = u.Role
|
||||||
|
claims["position_name"] = u.Position.Name
|
||||||
|
claims["position_rm"] = strconv.Itoa(u.Position.RightMask)
|
||||||
|
claims["position_org_id"] = strconv.Itoa(int(u.Position.OrganizationID))
|
||||||
|
|
||||||
var _token, _ = token.SignedString([]byte(config.Env.Server.JWTSecret))
|
var _token, _ = token.SignedString([]byte(config.Env.Server.JWTSecret))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -109,3 +113,15 @@ func UpdateUser(c echo.Context) (err error) {
|
|||||||
|
|
||||||
return c.JSONBlob(http.StatusOK, data)
|
return c.JSONBlob(http.StatusOK, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetOrganizations(c echo.Context) error {
|
||||||
|
data, err, resp := api.GetRequest(
|
||||||
|
config.Env.MSHosts.UsersHost+"/organizations",
|
||||||
|
c.Request().Header, c.Request().URL.RawQuery)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return c.NoContent(http.StatusBadGateway)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Blob(resp.StatusCode, resp.Header.Get("Content-Type"), data)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user