diff --git a/config/config.go b/config/config.go index c19973a..c516976 100644 --- a/config/config.go +++ b/config/config.go @@ -22,11 +22,11 @@ type Config struct { func New() *Config { return &Config{ MSHosts: MSHostsConfig{ - UsersHost: getEnv("ASTRA-USERS", ""), - StoreHost: getEnv("ASTRA-FILE-STORAGE", ""), + UsersHost: getEnv("ASTRA-USERS", "http://localhost:8082"), + StoreHost: getEnv("ASTRA-FILE-STORAGE", "http://localhost:8081"), }, Server: ServerConfig{ - Port: getEnv("PORT", ""), + Port: getEnv("PORT", "8080"), JWTSecret: getEnv("JWT_SECRET", "secret"), }, } diff --git a/main.go b/main.go index ca69d23..9cc7f37 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,8 @@ func main() { e.DELETE("/users/:id", users.DeleteUser) e.PATCH("/users/:id", users.UpdateUser) + e.GET("/organizations", users.GetOrganizations) + // Store store_g := e.Group("/store") store_g.GET("/*", store.GetFileFromStore) @@ -48,5 +50,11 @@ func main() { } 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) } diff --git a/model/position.go b/model/position.go new file mode 100644 index 0000000..94b5732 --- /dev/null +++ b/model/position.go @@ -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"` +} diff --git a/model/user.go b/model/user.go index 524f2b5..68c8623 100644 --- a/model/user.go +++ b/model/user.go @@ -5,9 +5,12 @@ import "gorm.io/gorm" type ( User struct { gorm.Model - UserName string `json:"userName" gorm:"unique"` - Password string `json:"password,omitempty"` - FirstName string `json:"firstName"` - LastName string `json:"lastName"` + UserName string `json:"user_name" gorm:"unique;index"` + Password string `json:"password,omitempty"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Role string `json:"role" gorm:"default:employee"` + PositionID uint `json:"-"` + Position Position `gorm:"-:migration"` } ) diff --git a/mw/filter_headers.go b/mw/filter_headers.go index d262577..b88f747 100644 --- a/mw/filter_headers.go +++ b/mw/filter_headers.go @@ -4,7 +4,14 @@ import ( "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 { return func(c echo.Context) error { diff --git a/mw/jwt.go b/mw/jwt.go index 925f85e..4255b7d 100644 --- a/mw/jwt.go +++ b/mw/jwt.go @@ -21,6 +21,10 @@ var JWT_MW = echojwt.WithConfig(echojwt.Config{ user := c.Get("user").(*jwt.Token) claims := user.Claims.(jwt.MapClaims) 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") }, }) diff --git a/services/store/store.go b/services/store/store.go index 10b8d99..2a36400 100644 --- a/services/store/store.go +++ b/services/store/store.go @@ -11,7 +11,7 @@ import ( func GetFileFromStore(c echo.Context) error { fileName := strings.ReplaceAll(c.Request().RequestURI, "/store", "") - data, err, req := api.GetRequest( + data, err, resp := api.GetRequest( config.Env.MSHosts.StoreHost+fileName, c.Request().Header, ) @@ -19,5 +19,5 @@ func GetFileFromStore(c echo.Context) error { 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) } diff --git a/services/users/users.go b/services/users/users.go index 7a394ec..eea6168 100644 --- a/services/users/users.go +++ b/services/users/users.go @@ -38,6 +38,10 @@ func Login(c echo.Context) (err error) { claims := token.Claims.(jwt.MapClaims) claims["id"] = strconv.Itoa(int(u.ID)) 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)) if err != nil { @@ -109,3 +113,15 @@ func UpdateUser(c echo.Context) (err error) { 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) +}