From 9043b2bd48829d2ce543107ed49c7f706caee23d Mon Sep 17 00:00:00 2001 From: titouanfreville Date: Tue, 14 Mar 2017 23:01:09 +0100 Subject: [PATCH 1/4] WIP --- api/api.go | 1 + api/member_route.go | 2 +- api/user_parameter_route.go | 13 ++++-- api/user_route.go | 91 ++++++++++++++++++++++--------------- 4 files changed, 65 insertions(+), 42 deletions(-) diff --git a/api/api.go b/api/api.go index 7f67b23..d6bc1e0 100644 --- a/api/api.go +++ b/api/api.go @@ -406,6 +406,7 @@ func StartAPI(hostname string, port string, DbConnectionInfo *configs.DbConnecti initRoleRoute(router) initUserRoute(router) initDevGetter(router) + initUserParameterRoute(router) // Passing -routes to the program will generate docs for the above // router definition. See the `routes.json` file in this folder for // the output. diff --git a/api/member_route.go b/api/member_route.go index f386df8..c7d88ac 100644 --- a/api/member_route.go +++ b/api/member_route.go @@ -104,7 +104,7 @@ func initMemberRoute(router chi.Router) { // default: genericError r.Post("/new", newMember) }) - router.Route("/channel/{channelID}/user/{userID}", func(r chi.Router) { + router.Route("/channel/:channelID/user/:userID", func(r chi.Router) { r.Use(tokenAuth.Verifier) r.Use(Authenticator) r.Use(memberContext) diff --git a/api/user_parameter_route.go b/api/user_parameter_route.go index 588d71b..eb58f5e 100644 --- a/api/user_parameter_route.go +++ b/api/user_parameter_route.go @@ -4,6 +4,8 @@ import ( "context" "net/http" + "log" + jwt "github.com/dgrijalva/jwt-go" "github.com/pressly/chi" chiRender "github.com/pressly/chi/render" @@ -16,8 +18,9 @@ const ( userParameterUser key = "userParameterUser" ) +// To be add into user routes. func initUserParameterRoute(router chi.Router) { - router.Route("/user/{userName}/parameters", func(r chi.Router) { + router.Route("/user/:userName/parameters", func(r chi.Router) { r.Use(tokenAuth.Verifier) r.Use(userParameterAuthenticator) // swagger:route GET /user/{userName}/parameters UserParameter getAllUserParameter @@ -66,7 +69,7 @@ func initUserParameterRoute(router chi.Router) { // 503: databaseError // default: genericError r.Post("/new", newUserParameter) - r.Route("/{parameterName}", func(r chi.Router) { + r.Route("/:parameterName", func(r chi.Router) { r.Use(tokenAuth.Verifier) r.Use(userParameterAuthenticator) r.Use(userparameterContext) @@ -101,7 +104,6 @@ func initUserParameterRoute(router chi.Router) { func userParameterAuthenticator(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() - if jwtErr, ok := ctx.Value(jwtErrorKey).(error); ok { if jwtErr != nil { render.JSON(w, 401, jwtErr) @@ -157,11 +159,14 @@ func userparameterContext(next http.Handler) http.Handler { store := datastores.Store() db := dbStore.db user := store.User().GetByUserName(userName, db) - if &user != nil { + if (user == models.User{}) { oldUserParameter = store.UserParameter().GetByID(user.IDUser, parameterName, db) } ctx := context.WithValue(r.Context(), oldUserParameterKey, oldUserParameter) ctx = context.WithValue(ctx, userParameterUser, user) + log.Print("New context : ----------------------- \n") + log.Print(ctx) + log.Print("\n------------------------------------- \n") next.ServeHTTP(w, r.WithContext(ctx)) }) } diff --git a/api/user_route.go b/api/user_route.go index 92dea51..fa69279 100644 --- a/api/user_route.go +++ b/api/user_route.go @@ -124,22 +124,22 @@ func initUserRoute(router chi.Router) { r.Get("/", getUserFromEmail) }) }) - r.Route("/username/", func(r chi.Router) { - r.Route("/:userName", func(r chi.Router) { - r.Use(userContext) - // swagger:route GET /user/username/{userName} Users getUserFromName - // - // Get user from username - // - // This will return the user object corresponding to provided username - // - // Responses: - // 200: userObjectSuccess - // 503: databaseError - // default: genericError - r.Get("/", getUserFromName) - }) - }) + // r.Route("/username/", func(r chi.Router) { + // r.Route("/:userName", func(r chi.Router) { + // r.Use(userContext) + // // swagger:route GET /user/username/{userName} Users getUserFromName + // // + // // Get user from username + // // + // // This will return the user object corresponding to provided username + // // + // // Responses: + // // 200: userObjectSuccess + // // 503: databaseError + // // default: genericError + // r.Get("/", getUserFromName) + // }) + // }) r.Route("/nickname/", func(r chi.Router) { r.Route("/:nickName", func(r chi.Router) { r.Use(userContext) @@ -190,6 +190,17 @@ func initUserRoute(router chi.Router) { }) r.Route("/:userID", func(r chi.Router) { r.Use(userContext) + // swagger:route GET /user/userName} Users getUserFromName + // + // Get user from username + // + // This will return the user object corresponding to provided username + // + // Responses: + // 200: userObjectSuccess + // 503: databaseError + // default: genericError + r.Get("/", getUserFromName) // swagger:route PUT /user/{userID} Users updateUser // // Update user @@ -221,7 +232,11 @@ func initUserRoute(router chi.Router) { func userContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { userID, err := strconv.ParseUint(chi.URLParam(r, "userID"), 10, 64) + userName := chi.URLParam(r, "userID") name := chi.URLParam(r, "userName") + if name == "" { + name = userName + } nickName := chi.URLParam(r, "nickName") firstName := chi.URLParam(r, "firstName") lastName := chi.URLParam(r, "lastName") @@ -236,6 +251,8 @@ func userContext(next http.Handler) http.Handler { ctx = context.WithValue(ctx, userDateKey, date) if err == nil { oldUser = datastores.Store().User().GetByID(userID, dbStore.db) + } else { + oldUser = datastores.Store().User().GetByUserName(userName, dbStore.db) } ctx = context.WithValue(ctx, oldUserKey, oldUser) next.ServeHTTP(w, r.WithContext(ctx)) @@ -306,7 +323,7 @@ func getUserFromName(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - name := r.Context().Value("userName").(string) + name := r.Context().Value(userNameKey).(string) user := store.User().GetByUserName(name, db) render.JSON(w, 200, user) } @@ -481,20 +498,20 @@ func updateUser(w http.ResponseWriter, r *http.Request) { render.JSON(w, error401.StatusCode, error401) return } - if err != nil || data.User == nil { render.JSON(w, error422.StatusCode, error422) return } - if err := db.DB().Ping(); err == nil { - err := store.User().Update(&user, data.User, db) - if err == nil { - render.JSON(w, 200, user) - return - } - + if err := db.DB().Ping(); err != nil { + render.JSON(w, error503.StatusCode, error503) + return } - render.JSON(w, error503.StatusCode, error503) + apperr := store.User().Update(&user, data.User, db) + if apperr != nil { + render.JSON(w, apperr.StatusCode, apperr) + return + } + render.JSON(w, 200, user) } func deleteUser(w http.ResponseWriter, r *http.Request) { @@ -511,18 +528,18 @@ func deleteUser(w http.ResponseWriter, r *http.Request) { Object: user, } db := dbStore.db - if err := db.DB().Ping(); err == nil { - err := store.User().Delete(&user, db) - if err == nil { - message.Success = true - message.Message = "User well removed." - render.JSON(w, 200, message) - return - } + if err := db.DB().Ping(); err != nil { + render.JSON(w, error503.StatusCode, error503) + return + } + apperr := store.User().Delete(&user, db) + if apperr != nil { message.Success = false - message.Message = err.Message - render.JSON(w, err.StatusCode, message.Message) + message.Message = apperr.Message + render.JSON(w, apperr.StatusCode, message.Message) return } - render.JSON(w, 503, error503) + message.Success = true + message.Message = "User well removed." + render.JSON(w, 200, message) } From c5666717f8831f2024099add85a75f60b44ee42a Mon Sep 17 00:00:00 2001 From: titouanfreville Date: Wed, 15 Mar 2017 11:56:08 +0100 Subject: [PATCH 2/4] Correct routes for user parameters --- api/api.go | 2 +- api/user_parameter_route.go | 79 ++++++++++--------------------------- api/user_route.go | 44 ++------------------- models/userParameter.go | 8 ++-- 4 files changed, 28 insertions(+), 105 deletions(-) diff --git a/api/api.go b/api/api.go index d6bc1e0..f8a6a35 100644 --- a/api/api.go +++ b/api/api.go @@ -406,7 +406,7 @@ func StartAPI(hostname string, port string, DbConnectionInfo *configs.DbConnecti initRoleRoute(router) initUserRoute(router) initDevGetter(router) - initUserParameterRoute(router) + // initUserParameterRoute(router) // Passing -routes to the program will generate docs for the above // router definition. See the `routes.json` file in this folder for // the output. diff --git a/api/user_parameter_route.go b/api/user_parameter_route.go index eb58f5e..4a0e263 100644 --- a/api/user_parameter_route.go +++ b/api/user_parameter_route.go @@ -4,8 +4,6 @@ import ( "context" "net/http" - "log" - jwt "github.com/dgrijalva/jwt-go" "github.com/pressly/chi" chiRender "github.com/pressly/chi/render" @@ -15,12 +13,12 @@ import ( const ( oldUserParameterKey key = "oldUserParameter" - userParameterUser key = "userParameterUser" ) // To be add into user routes. func initUserParameterRoute(router chi.Router) { - router.Route("/user/:userName/parameters", func(r chi.Router) { + // User ID will be in the context from user_route + router.Route("/parameters", func(r chi.Router) { r.Use(tokenAuth.Verifier) r.Use(userParameterAuthenticator) // swagger:route GET /user/{userName}/parameters UserParameter getAllUserParameter @@ -46,29 +44,6 @@ func initUserParameterRoute(router chi.Router) { // 503: databaseError // default: genericError r.Post("/", newUserParameter) - // swagger:route GET /user/{userName}/parameters/all UserParameter getAllUserParameter1 - // - // Get userparameters - // - // This will get all the userparameters available in the organisation. - // - // Responses: - // 200: userparameterArraySuccess - // 503: databaseError - // default: genericError - r.Get("/all", getAllUserParameter) - // swagger:route POST /user/{userName}/parameters/new UserParameter newUserParameter1 - // - // New userparameter - // - // This will create an userparameter for organisation userparameters library. - // - // Responses: - // 201: userparameterObjectSuccess - // 422: wrongEntity - // 503: databaseError - // default: genericError - r.Post("/new", newUserParameter) r.Route("/:parameterName", func(r chi.Router) { r.Use(tokenAuth.Verifier) r.Use(userParameterAuthenticator) @@ -84,7 +59,7 @@ func initUserParameterRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Put("/update", updateUserParameter) + r.Put("/", updateUserParameter) // swagger:route DELETE /user/{userName}/parameters/{parameterName} UserParameter deleteUserParameter // // Delete userparameter @@ -96,7 +71,7 @@ func initUserParameterRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Delete("/delete", deleteUserParameter) + r.Delete("/", deleteUserParameter) }) }) } @@ -110,25 +85,20 @@ func userParameterAuthenticator(next http.Handler) http.Handler { return } } - jwtToken, ok := ctx.Value(jwtTokenKey).(*jwt.Token) if !ok || jwtToken == nil || !jwtToken.Valid { render.JSON(w, 401, "token is not valid or does not exist") return } - tokenType, ok := jwtToken.Claims.(jwt.MapClaims)["type"] - if !ok { render.JSON(w, 401, "Token is not valid. Type is undifined") return } - if tokenType != "userauth" { render.JSON(w, 401, "Token is not an user auth one") return } - tokenUser, ok := jwtToken.Claims.(jwt.MapClaims)["name"].(string) tokenEmail, ok2 := jwtToken.Claims.(jwt.MapClaims)["email"].(string) userFromMail := "-*-" @@ -138,7 +108,7 @@ func userParameterAuthenticator(next http.Handler) http.Handler { } store := datastores.Store() db := dbStore.db - userName := chi.URLParam(r, "userName") + userName := ctx.Value(oldUserKey).(models.User).Username if ok2 { userFromMail = store.User().GetByEmail(tokenEmail, db).Username } @@ -153,20 +123,16 @@ func userParameterAuthenticator(next http.Handler) http.Handler { func userparameterContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - userName := chi.URLParam(r, "userName") + ctx := r.Context() parameterName := chi.URLParam(r, "parameterName") oldUserParameter := models.UserParameter{} store := datastores.Store() db := dbStore.db - user := store.User().GetByUserName(userName, db) - if (user == models.User{}) { + user := ctx.Value(oldUserKey).(models.User) + if (user != models.User{}) { oldUserParameter = store.UserParameter().GetByID(user.IDUser, parameterName, db) } - ctx := context.WithValue(r.Context(), oldUserParameterKey, oldUserParameter) - ctx = context.WithValue(ctx, userParameterUser, user) - log.Print("New context : ----------------------- \n") - log.Print(ctx) - log.Print("\n------------------------------------- \n") + ctx = context.WithValue(ctx, oldUserParameterKey, oldUserParameter) next.ServeHTTP(w, r.WithContext(ctx)) }) } @@ -178,21 +144,18 @@ func getAllUserParameter(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - user := r.Context().Value(userParameterUser).(models.User) + user := r.Context().Value(oldUserKey).(models.User) result := store.UserParameter().GetByUser(&user, db) render.JSON(w, 200, result) } func newUserParameter(w http.ResponseWriter, r *http.Request) { - var data struct { - UserParameter *models.UserParameter - OmitID interface{} `json:"id,omitempty"` - } + var UserParameter models.UserParameter store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.UserParameter == nil { + err := chiRender.Bind(request, &UserParameter) + if err != nil || (UserParameter == models.UserParameter{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -200,25 +163,23 @@ func newUserParameter(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.UserParameter().Save(data.UserParameter, db) + UserParameter.IDUser = r.Context().Value(oldUserKey).(models.User).IDUser + apperr := store.UserParameter().Save(&UserParameter, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 201, data.UserParameter) + render.JSON(w, 201, UserParameter) } func updateUserParameter(w http.ResponseWriter, r *http.Request) { - var data struct { - UserParameter *models.UserParameter - OmitID interface{} `json:"id,omitempty"` - } + var UserParameter models.UserParameter store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(request, &UserParameter) userparameter := r.Context().Value(oldUserParameterKey).(models.UserParameter) - if err != nil || data.UserParameter == nil { + if err != nil || (UserParameter == models.UserParameter{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -226,7 +187,7 @@ func updateUserParameter(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.UserParameter().Update(&userparameter, data.UserParameter, db) + apperr := store.UserParameter().Update(&userparameter, &UserParameter, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/user_route.go b/api/user_route.go index fa69279..329706a 100644 --- a/api/user_route.go +++ b/api/user_route.go @@ -49,29 +49,6 @@ func initUserRoute(router chi.Router) { // 503: databaseError // default: genericError r.Post("/", newUser) - // swagger:route GET /user/all Users getAllUser1 - // - // Get users - // - // This will get all the users available in the organisation. - // - // Responses: - // 200: userArraySuccess - // 503: databaseError - // default: genericError - r.Get("/all", getAllUser) - // swagger:route POST /user/new Users newUser1 - // - // New user - // - // This will create an user for organisation users library. - // - // Responses: - // 201: userObjectSuccess - // 422: wrongEntity - // 503: databaseError - // default: genericError - r.Post("/new", newUser) // swagger:route POST /user/invite Users inviteUser // // Invite user @@ -124,22 +101,6 @@ func initUserRoute(router chi.Router) { r.Get("/", getUserFromEmail) }) }) - // r.Route("/username/", func(r chi.Router) { - // r.Route("/:userName", func(r chi.Router) { - // r.Use(userContext) - // // swagger:route GET /user/username/{userName} Users getUserFromName - // // - // // Get user from username - // // - // // This will return the user object corresponding to provided username - // // - // // Responses: - // // 200: userObjectSuccess - // // 503: databaseError - // // default: genericError - // r.Get("/", getUserFromName) - // }) - // }) r.Route("/nickname/", func(r chi.Router) { r.Route("/:nickName", func(r chi.Router) { r.Use(userContext) @@ -212,7 +173,7 @@ func initUserRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Put("/update", updateUser) + r.Put("/", updateUser) // swagger:route PUT /user/{userID} Users deleteUser // // Delete user @@ -224,7 +185,8 @@ func initUserRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: deleteMessage - r.Delete("/delete", deleteUser) + r.Delete("/", deleteUser) + initUserParameterRoute(r) }) }) } diff --git a/models/userParameter.go b/models/userParameter.go index 86d6c73..4383ed6 100644 --- a/models/userParameter.go +++ b/models/userParameter.go @@ -26,19 +26,19 @@ type UserParameter struct { // Default langage // // required: true - Local string `gorm:"column:local;not null; unique" json:"local,omitempty"` + Local string `gorm:"column:local" json:"local,omitempty"` // Default time zone // // required: true - TimeZone string `gorm:"column:timeZone;not null; unique;" json:"time_zone,omitempty"` + TimeZone string `gorm:"column:timeZone" json:"time_zone,omitempty"` // Default start of non notification period // // required: true - SleepStart int `gorm:"column:sleepStart;not null;unique" json:"sleep_start,omitempty"` + SleepStart int `gorm:"column:sleepStart" json:"sleep_start,omitempty"` // Default end of non notification period // // required: true - SleepEnd int `gorm:"column:sleepEnd;not null;unique" json:"sleep_end,omitempty"` + SleepEnd int `gorm:"column:sleepEnd" json:"sleep_end,omitempty"` } // IsValid is used to check validity of UserParameter objects From 783dcf61d00a54cf0d23d8c85254e17c40253da0 Mon Sep 17 00:00:00 2001 From: titouanfreville Date: Tue, 28 Mar 2017 00:35:56 +0200 Subject: [PATCH 3/4] Corrected some bugs. WIP : member correction. --- api/api.go | 4 +- api/channel_route.go | 97 ++++++---------- api/jwtauth.go | 4 +- api/member_route.go | 214 ++++++++++++++++++++++-------------- api/message_route.go | 46 +++----- api/organisation_route.go | 24 ++-- api/parameter_route.go | 24 ++-- api/role_route.go | 24 ++-- api/user_parameter_route.go | 2 +- api/user_route.go | 25 ++--- models/member.go | 8 +- 11 files changed, 228 insertions(+), 244 deletions(-) diff --git a/api/api.go b/api/api.go index f8a6a35..160252c 100644 --- a/api/api.go +++ b/api/api.go @@ -262,6 +262,7 @@ func loginMiddleware(w http.ResponseWriter, r *http.Request) { request := r.Body err := chiRender.Bind(request, &data) if err != nil { + log.Print("422 Here - loginMiddleware") render.JSON(w, error422.StatusCode, error422) return } @@ -291,7 +292,7 @@ func initOrganisation(w http.ResponseWriter, r *http.Request) { ctx := r.Context() if jwtErr, ok := ctx.Value(jwtErrorKey).(error); ok { if jwtErr != nil { - render.JSON(w, 401, jwtErr) + render.JSON(w, 401, "Token not found. You Are not allowed to proceed without token.") return } } @@ -367,6 +368,7 @@ func newPublicUser(w http.ResponseWriter, r *http.Request) { render.JSON(w, 401, "You can't sign up if organisation is not public or your email domain was unauthorized.") } if err != nil || data.User == nil { + log.Print("422 here. New Public User") render.JSON(w, error422.StatusCode, error422) } else { if err := db.DB().Ping(); err == nil { diff --git a/api/channel_route.go b/api/channel_route.go index a928196..d5317ca 100644 --- a/api/channel_route.go +++ b/api/channel_route.go @@ -5,6 +5,8 @@ import ( "net/http" "strconv" + "log" + jwt "github.com/dgrijalva/jwt-go" "github.com/pressly/chi" chiRender "github.com/pressly/chi/render" @@ -45,29 +47,6 @@ func initChannelRoute(router chi.Router) { // 503: databaseError // default: genericError r.Post("/", newChannel) - // swagger:route GET /channel/all Channels getAllChannel1 - // - // Get channels - // - // This will get all the channels available in the organisation. - // - // Responses: - // 200: channelArraySuccess - // 503: databaseError - // default: genericError - r.Get("/all", getAllChannel) - // swagger:route POST /channel/new Channels newChannel1 - // - // New channel - // - // This will create an channel for organisation channels library. - // - // Responses: - // 201: channelObjectSuccess - // 422: wrongEntity - // 503: databaseError - // default: genericError - r.Post("/new", newChannel) // swagger:route GET /channel/public Channels getPublicChannel // // Get public channels @@ -106,24 +85,19 @@ func initChannelRoute(router chi.Router) { r.Get("/", getChannelFromType) }) }) - r.Route("/name/", func(r chi.Router) { - r.Route("/:channelName", func(r chi.Router) { - r.Use(channelContext) - // swagger:route GET /channel/name/{channelName} Channels getChannelFromName - // - // Get nammed channel - // - // This will get the channels having provided name in the organisation. - // - // Responses: - // 200: channelObjectSuccess - // 503: databaseError - // default: genericError - r.Get("/", getChannelFromName) - }) - }) r.Route("/:channelID", func(r chi.Router) { r.Use(channelContext) + // swagger:route GET /channel/{channelID} Channels getChannelFromID + // + // Get nammed channel - Currently in conflict with channel/{channelID}/member/{} + // + // This will get the channels having provided name in the organisation. + // + // Responses: + // 200: channelObjectSuccess + // 503: databaseError + // default: genericError + r.Get("/", getChannelFromID) // swagger:route PUT /channel/{channelID} Channels updateChannel // // Update channel @@ -135,7 +109,7 @@ func initChannelRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Put("/update", updateChannel) + r.Put("/", updateChannel) // swagger:route DELETE /channel/{channelID} Channels deleteChannel // // Delete channel @@ -146,7 +120,8 @@ func initChannelRoute(router chi.Router) { // 200: deleteMessage // 503: databaseError // default: genericError - r.Delete("/delete", deleteChannel) + r.Delete("/", deleteChannel) + initMemberOverChannel(r) }) }) } @@ -178,13 +153,15 @@ func canArchive(currentChannelID uint64, token *jwt.Token) bool { func channelContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { channelID, err := strconv.ParseUint(chi.URLParam(r, "channelID"), 10, 64) - name := chi.URLParam(r, "channelName") + name := chi.URLParam(r, "channelID") channelType := chi.URLParam(r, "channelType") oldChannel := models.Channel{} ctx := context.WithValue(r.Context(), channelNameKey, name) ctx = context.WithValue(ctx, channelTypeKey, channelType) if err == nil { oldChannel = datastores.Store().Channel().GetByID(channelID, dbStore.db) + } else { + oldChannel = datastores.Store().Channel().GetByName(name, dbStore.db) } ctx = context.WithValue(ctx, oldChannelKey, oldChannel) next.ServeHTTP(w, r.WithContext(ctx)) @@ -227,15 +204,13 @@ func getPrivateChannel(w http.ResponseWriter, r *http.Request) { } -func getChannelFromName(w http.ResponseWriter, r *http.Request) { - store := datastores.Store() +func getChannelFromID(w http.ResponseWriter, r *http.Request) { db := dbStore.db if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - name := r.Context().Value(channelNameKey).(string) - channel := store.Channel().GetByName(name, db) + channel := r.Context().Value(oldChannelKey).(models.Channel) render.JSON(w, 200, channel) } @@ -252,10 +227,7 @@ func getChannelFromType(w http.ResponseWriter, r *http.Request) { } func newChannel(w http.ResponseWriter, r *http.Request) { - var data struct { - Channel *models.Channel - OmitID interface{} `json:"id,omitempty"` - } + var Channel models.Channel token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser("global", false, "", token) { res := error401 @@ -266,8 +238,9 @@ func newChannel(w http.ResponseWriter, r *http.Request) { store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Channel == nil { + err := chiRender.Bind(request, &Channel) + if err != nil || Channel == (models.Channel{}) { + log.Print("422 here - new channel") render.JSON(w, error422.StatusCode, error422) return } @@ -275,32 +248,30 @@ func newChannel(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - rerr := store.Channel().Save(data.Channel, db) + rerr := store.Channel().Save(&Channel, db) if err != nil { render.JSON(w, rerr.StatusCode, rerr) return } - render.JSON(w, 201, data.Channel) + render.JSON(w, 201, Channel) } func updateChannel(w http.ResponseWriter, r *http.Request) { - var data struct { - Channel *models.Channel - OmitID interface{} `json:"id,omitempty"` - } + var Channel models.Channel channel := r.Context().Value(oldChannelKey).(models.Channel) token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser(channel.ChannelName, false, "", token) { res := error401 - res.Message = "You don't have the right to manage user." + res.Message = "You don't have the right to manage channels." render.JSON(w, error401.StatusCode, error401) return } store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Channel == nil { + err := chiRender.Bind(request, &Channel) + if err != nil || Channel == (models.Channel{}) { + log.Print("422 here - Update channel") render.JSON(w, error422.StatusCode, error422) return } @@ -308,7 +279,7 @@ func updateChannel(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - rerr := store.Channel().Update(&channel, data.Channel, db) + rerr := store.Channel().Update(&channel, &Channel, db) if err == nil { render.JSON(w, rerr.StatusCode, rerr) return @@ -321,7 +292,7 @@ func deleteChannel(w http.ResponseWriter, r *http.Request) { token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser(channel.ChannelName, false, "", token) { res := error401 - res.Message = "You don't have the right to manage user." + res.Message = "You don't have the right to manage channels." render.JSON(w, error401.StatusCode, error401) return } diff --git a/api/jwtauth.go b/api/jwtauth.go index f88b2d9..3de62b1 100644 --- a/api/jwtauth.go +++ b/api/jwtauth.go @@ -210,7 +210,7 @@ func Authenticator(next http.Handler) http.Handler { if jwtErr, ok := ctx.Value(jwtErrorKey).(error); ok { if jwtErr != nil { - render.JSON(w, 401, jwtErr) + render.JSON(w, 401, "Token not found. You Are not allowed to proceed without token.") return } } @@ -245,7 +245,7 @@ func allowUserCreationFromToken(next http.Handler) http.Handler { if jwtErr, ok := ctx.Value(jwtErrorKey).(error); ok { if jwtErr != nil { - render.JSON(w, 401, jwtErr) + render.JSON(w, 401, "Token not found. You Are not allowed to proceed without token.") return } } diff --git a/api/member_route.go b/api/member_route.go index c7d88ac..cff7956 100644 --- a/api/member_route.go +++ b/api/member_route.go @@ -2,6 +2,7 @@ package api import ( "context" + "log" "net/http" "strconv" @@ -17,22 +18,22 @@ const ( oldMemberKey key = "oldMember" ) -func initMemberRoute(router chi.Router) { - router.Route("/member", func(r chi.Router) { +func initMemberOverChannel(channelRoutes chi.Router) { + channelRoutes.Route("/member", func(r chi.Router) { r.Use(tokenAuth.Verifier) r.Use(Authenticator) - // swagger:route GET /member Members getAllMember + // swagger:route POST channel Members getMemberFromChannel // - // Get members + // Get member into channel // - // This will get all the members available in the organisation. + // This will return all users in provided channel // // Responses: - // 200: memberArraySuccess + // 200: memberObjectSuccess // 503: databaseError // default: genericError - r.Get("/", getAllMember) - // swagger:route POST /member Members newMember + r.Get("/", getMemberFromChannel) + // swagger:route POST channel/{channelID}/member Members newMember // // New member // @@ -44,17 +45,72 @@ func initMemberRoute(router chi.Router) { // 503: databaseError // default: genericError r.Post("/", newMember) - // swagger:route GET /member/all Members getAllMember1 - // - // Get members - // - // This will get all the members available in the organisation. - // - // Responses: - // 200: memberArraySuccess - // 503: databaseError - // default: genericError - r.Get("/all", getAllMember) + }) + // swagger:route POST channel/{channelID}/role/{roleID} Members getMemberFromRole + // + // Get member having channel specifics roles + // + // This will return all members having a Specific role for a channel + // + // Responses: + // 200: memberObjectSuccess + // 503: databaseError + // default: genericError + channelRoutes.Route("/role", func(r chi.Router) { + r.Route("/:roleID", func(r chi.Router) { + r.Use(memberContext) + r.Get("/", getMemberFromRole) + }) + }) + channelRoutes.Route("/user", func(r chi.Router) { + r.Route("/:userID", func(r chi.Router) { + r.Use(tokenAuth.Verifier) + r.Use(Authenticator) + r.Use(memberContext) + // swagger:route GET channel/{channelID}/user/{userID} Members getMemberFromUser + // + // Get channel user is member of + // + // This will return all channel provided user is in + // + // Responses: + // 200: memberObjectSuccess + // 503: databaseError + // default: genericError + r.Get("/", getMemberFromUser) + // swagger:route PUT /channel/{channelID}/user/{userID} Members updateMember + // + // Update member + // + // This will return the new member object + // + // Responses: + // 200: memberObjectSuccess + // 422: wrongEntity + // 503: databaseError + // default: genericError + r.Put("/", updateMember) + // swagger:route DELETE /channel/{channelID}/user/{userID} Members deleteMember + // + // Delete member + // + // This will return the new member object + // + // Responses: + // 200: memberObjectSuccess + // 422: wrongEntity + // 503: databaseError + // default: genericError + r.Delete("/", deleteMember) + }) + }) +} + +func initMemberOverUser(userRoutes chi.Router) { + userRoutes.Route("/channels/:channelID", func(r chi.Router) { + r.Use(tokenAuth.Verifier) + r.Use(Authenticator) + r.Use(memberContext) // swagger:route POST /channel Members getMemberFromChannel // // Get member into channel @@ -66,19 +122,7 @@ func initMemberRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Post("/channel", getMemberFromChannel) - // swagger:route POST /user Members getMemberFromUser - // - // Get channel user is member of - // - // This will return all channel provided user is in - // - // Responses: - // 200: memberObjectSuccess - // 422: wrongEntity - // 503: databaseError - // default: genericError - r.Post("/user", getMemberFromUser) + r.Get("/", getMemberFromChannel) // swagger:route POST /user Members getMemberFromRole // // Get member having channel specifics roles @@ -90,25 +134,8 @@ func initMemberRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Post("/role", getMemberFromRole) - // swagger:route POST /member/new Members newMember1 - // - // New member - // - // This will create an member for organisation members library. - // - // Responses: - // 201: memberObjectSuccess - // 422: wrongEntity - // 503: databaseError - // default: genericError - r.Post("/new", newMember) - }) - router.Route("/channel/:channelID/user/:userID", func(r chi.Router) { - r.Use(tokenAuth.Verifier) - r.Use(Authenticator) - r.Use(memberContext) - // swagger:route PUT /channel/{channelID}/user/{userID} Members updateMember + userRoutes.Post("/role/:roleID", getMemberFromRole) + // swagger:route PUT user/{userID}/channel/{channelID} Members updateMember // // Update member // @@ -119,8 +146,8 @@ func initMemberRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Put("/update", updateMember) - // swagger:route DELETE /channel/{channelID}/user/{userID} Members deleteMember + r.Put("/", updateMember) + // swagger:route DELETE user/{userID}/channel/{channelID} Members deleteMember // // Delete member // @@ -131,19 +158,54 @@ func initMemberRoute(router chi.Router) { // 422: wrongEntity // 503: databaseError // default: genericError - r.Delete("/delete", deleteMember) + r.Delete("/", deleteMember) }) } func memberContext(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - channelID, err := strconv.ParseUint(chi.URLParam(r, "channelID"), 10, 64) - userID, err := strconv.ParseUint(chi.URLParam(r, "userID"), 10, 64) + ctx := r.Context() + var ok bool + var channel models.Channel + var user models.User + var userFromParam models.User + channel, ok = ctx.Value(oldChannelKey).(models.Channel) + if !ok { + channel = models.Channel{} + } + user, ok = ctx.Value(oldUserKey).(models.User) + if !ok { + user = models.User{} + userID, err := strconv.ParseUint(chi.URLParam(r, "userID"), 10, 64) + if err == nil { + userFromParam = datastores.Store().User().GetByID(userID, dbStore.db) + } else { + userID := chi.URLParam(r, "userID") + userFromParam = datastores.Store().User().GetByUserName(userID, dbStore.db) + } + } oldMember := models.Member{} - if err == nil { - oldMember = datastores.Store().Member().GetByID(channelID, userID, dbStore.db) + if user != (models.User{}) { + channelID, err := strconv.ParseUint(chi.URLParam(r, "channelID"), 10, 64) + if err != nil { + channeName := chi.URLParam(r, "channelID") + channel = datastores.Store().Channel().GetByName(channeName, dbStore.db) + channelID = channel.IDChannel + } + oldMember = datastores.Store().Member().GetByID(channelID, user.IDUser, dbStore.db) + } else if channel != (models.Channel{}) { + userID, err := strconv.ParseUint(chi.URLParam(r, "userID"), 10, 64) + if err != nil { + userName := chi.URLParam(r, "userID") + user = datastores.Store().User().GetByUserName(userName, dbStore.db) + userID = user.IDUser + } + oldMember = datastores.Store().Member().GetByID(channel.IDChannel, userID, dbStore.db) } - ctx := context.WithValue(r.Context(), oldMemberKey, oldMember) + ctx = context.WithValue(ctx, oldMemberKey, oldMember) + ctx = context.WithValue(ctx, oldChannelKey, channel) + log.Print("Context user:", userFromParam) + ctx = context.WithValue(ctx, oldUserKey, userFromParam) next.ServeHTTP(w, r.WithContext(ctx)) }) } @@ -160,45 +222,27 @@ func getAllMember(w http.ResponseWriter, r *http.Request) { } func getMemberFromUser(w http.ResponseWriter, r *http.Request) { - var data struct { - User *models.User - OmitID interface{} `json:"id,omitempty"` - } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.User == nil { - render.JSON(w, error422.StatusCode, error422) - return - } + user := r.Context().Value(oldUserKey).(models.User) if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - role := store.Member().GetByUser(data.User, db) - render.JSON(w, 200, role) + member := store.Member().GetByUser(&user, db) + render.JSON(w, 200, member) } func getMemberFromChannel(w http.ResponseWriter, r *http.Request) { - var data struct { - Channel *models.Channel - OmitID interface{} `json:"id,omitempty"` - } store := datastores.Store() db := dbStore.db - request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Channel == nil { - render.JSON(w, error422.StatusCode, error422) - return - } + channel := r.Context().Value(oldChannelKey).(models.Channel) if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - role := store.Member().GetByChannel(data.Channel, db) - render.JSON(w, 200, role) + member := store.Member().GetByChannel(&channel, db) + render.JSON(w, 200, member) } func getMemberFromRole(w http.ResponseWriter, r *http.Request) { @@ -218,8 +262,8 @@ func getMemberFromRole(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - role := store.Member().GetByRole(data.Role, db) - render.JSON(w, 200, role) + member := store.Member().GetByRole(data.Role, db) + render.JSON(w, 200, member) } func newMember(w http.ResponseWriter, r *http.Request) { diff --git a/api/message_route.go b/api/message_route.go index 5781375..20b5f92 100644 --- a/api/message_route.go +++ b/api/message_route.go @@ -174,15 +174,12 @@ func getMessageFromDate(w http.ResponseWriter, r *http.Request) { } func getMessageFromUser(w http.ResponseWriter, r *http.Request) { - var data struct { - User *models.User - OmitID interface{} `json:"id,omitempty"` - } + var User models.User store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.User == nil { + err := chiRender.Bind(request, &User) + if err != nil || User == (models.User{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -190,20 +187,17 @@ func getMessageFromUser(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - role := store.Message().GetByCreator(data.User, db) + role := store.Message().GetByCreator(&User, db) render.JSON(w, 200, role) } func getMessageFromChannel(w http.ResponseWriter, r *http.Request) { - var data struct { - Channel *models.Channel - OmitID interface{} `json:"id,omitempty"` - } + var Channel models.Channel store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Channel == nil { + err := chiRender.Bind(request, &Channel) + if err != nil || Channel == (models.Channel{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -211,20 +205,17 @@ func getMessageFromChannel(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - role := store.Message().GetByChannel(data.Channel, db) + role := store.Message().GetByChannel(&Channel, db) render.JSON(w, 200, role) } func newMessage(w http.ResponseWriter, r *http.Request) { - var data struct { - Message *models.Message - OmitID interface{} `json:"id,omitempty"` - } + var Message models.Message store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Message == nil { + err := chiRender.Bind(request, &Message) + if err != nil || Message == (models.Message{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -232,25 +223,22 @@ func newMessage(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Message().Save(data.Message, db) + apperr := store.Message().Save(&Message, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 201, data.Message) + render.JSON(w, 201, Message) } func updateMessage(w http.ResponseWriter, r *http.Request) { - var data struct { - Message *models.Message - OmitID interface{} `json:"id,omitempty"` - } + var Message models.Message store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(request, &Message) message := r.Context().Value(oldMessageKey).(models.Message) - if err != nil || data.Message == nil { + if err != nil || Message == (models.Message{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -258,7 +246,7 @@ func updateMessage(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Message().Update(&message, data.Message, db) + apperr := store.Message().Update(&message, &Message, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/organisation_route.go b/api/organisation_route.go index 6a0e8d1..13ba131 100644 --- a/api/organisation_route.go +++ b/api/organisation_route.go @@ -116,10 +116,7 @@ func getAllOrganisation(w http.ResponseWriter, r *http.Request) { } func newOrganisation(w http.ResponseWriter, r *http.Request) { - var data struct { - Organisation *models.Organisation - OmitID interface{} `json:"id,omitempty"` - } + var Organisation models.Organisation token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -130,8 +127,8 @@ func newOrganisation(w http.ResponseWriter, r *http.Request) { store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Organisation == nil { + err := chiRender.Bind(request, &Organisation) + if err != nil || Organisation == (models.Organisation{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -139,19 +136,16 @@ func newOrganisation(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Organisation().Save(data.Organisation, db) + apperr := store.Organisation().Save(&Organisation, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 201, data.Organisation) + render.JSON(w, 201, Organisation) } func updateOrganisation(w http.ResponseWriter, r *http.Request) { - var data struct { - Organisation *models.Organisation - OmitID interface{} `json:"id,omitempty"` - } + var Organisation models.Organisation store := datastores.Store() db := dbStore.db token := r.Context().Value(jwtTokenKey).(*jwt.Token) @@ -162,16 +156,16 @@ func updateOrganisation(w http.ResponseWriter, r *http.Request) { return } request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(request, &Organisation) organisation := r.Context().Value(oldOrganisationKey).(models.Organisation) - if err != nil || data.Organisation == nil { + if err != nil || Organisation == (models.Organisation{}) { render.JSON(w, error422.StatusCode, error422) } if err := db.DB().Ping(); err != nil { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Organisation().Update(&organisation, data.Organisation, db) + apperr := store.Organisation().Update(&organisation, &Organisation, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/parameter_route.go b/api/parameter_route.go index e92ecf8..667a3f2 100644 --- a/api/parameter_route.go +++ b/api/parameter_route.go @@ -108,10 +108,7 @@ func getAllParameter(w http.ResponseWriter, r *http.Request) { } func newParameter(w http.ResponseWriter, r *http.Request) { - var data struct { - Parameter *models.Parameter - OmitID interface{} `json:"id,omitempty"` - } + var Parameter models.Parameter token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -122,8 +119,8 @@ func newParameter(w http.ResponseWriter, r *http.Request) { store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Parameter == nil { + err := chiRender.Bind(request, &Parameter) + if err != nil || Parameter == (models.Parameter{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -131,19 +128,16 @@ func newParameter(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Parameter().Save(data.Parameter, db) + apperr := store.Parameter().Save(&Parameter, db) if err != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 200, data.Parameter) + render.JSON(w, 200, Parameter) } func updateParameter(w http.ResponseWriter, r *http.Request) { - var data struct { - Parameter *models.Parameter - OmitID interface{} `json:"id,omitempty"` - } + var Parameter models.Parameter token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageOrganisation(token) { res := error401 @@ -154,9 +148,9 @@ func updateParameter(w http.ResponseWriter, r *http.Request) { store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(request, &Parameter) parameter := r.Context().Value(oldParameterKey).(models.Parameter) - if err != nil || data.Parameter == nil { + if err != nil || Parameter == (models.Parameter{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -164,7 +158,7 @@ func updateParameter(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Parameter().Update(¶meter, data.Parameter, db) + apperr := store.Parameter().Update(¶meter, &Parameter, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/role_route.go b/api/role_route.go index 9ed1a57..695d197 100644 --- a/api/role_route.go +++ b/api/role_route.go @@ -186,15 +186,12 @@ func getRoleFromRight(w http.ResponseWriter, r *http.Request) { } func newRole(w http.ResponseWriter, r *http.Request) { - var data struct { - Role *models.Role - OmitID interface{} `json:"id,omitempty"` - } + var Role models.Role store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.Role == nil { + err := chiRender.Bind(request, &Role) + if err != nil || Role == (models.Role{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -202,25 +199,22 @@ func newRole(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Role().Save(data.Role, db) + apperr := store.Role().Save(&Role, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return } - render.JSON(w, 201, data.Role) + render.JSON(w, 201, Role) } func updateRole(w http.ResponseWriter, r *http.Request) { - var data struct { - Role *models.Role - OmitID interface{} `json:"id,omitempty"` - } + var Role models.Role store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(request, &Role) role := r.Context().Value(oldRoleKey).(models.Role) - if err != nil || data.Role == nil { + if err != nil || Role == (models.Role{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -228,7 +222,7 @@ func updateRole(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.Role().Update(&role, data.Role, db) + apperr := store.Role().Update(&role, &Role, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/api/user_parameter_route.go b/api/user_parameter_route.go index 4a0e263..83e9647 100644 --- a/api/user_parameter_route.go +++ b/api/user_parameter_route.go @@ -81,7 +81,7 @@ func userParameterAuthenticator(next http.Handler) http.Handler { ctx := r.Context() if jwtErr, ok := ctx.Value(jwtErrorKey).(error); ok { if jwtErr != nil { - render.JSON(w, 401, jwtErr) + render.JSON(w, 401, "Token not found. You Are not allowed to proceed without token.") return } } diff --git a/api/user_route.go b/api/user_route.go index 329706a..b88cba0 100644 --- a/api/user_route.go +++ b/api/user_route.go @@ -187,6 +187,7 @@ func initUserRoute(router chi.Router) { // default: deleteMessage r.Delete("/", deleteUser) initUserParameterRoute(r) + initMemberOverUser(r) }) }) } @@ -372,10 +373,7 @@ func getUserFromRole(w http.ResponseWriter, r *http.Request) { } func newUser(w http.ResponseWriter, r *http.Request) { - var data struct { - User *models.User - OmitID interface{} `json:"id,omitempty"` - } + var User models.User store := datastores.Store() token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser("global", false, "", token) { @@ -386,8 +384,8 @@ func newUser(w http.ResponseWriter, r *http.Request) { } db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) - if err != nil || data.User == nil { + err := chiRender.Bind(request, &User) + if err != nil || User == (models.User{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -395,9 +393,9 @@ func newUser(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.User().Save(data.User, db) + apperr := store.User().Save(&User, db) if err == nil { - render.JSON(w, 201, data.User) + render.JSON(w, 201, User) return } render.JSON(w, apperr.StatusCode, apperr) @@ -444,14 +442,11 @@ func inviteUser(w http.ResponseWriter, r *http.Request) { } func updateUser(w http.ResponseWriter, r *http.Request) { - var data struct { - User *models.User - OmitID interface{} `json:"id,omitempty"` - } + var User models.User store := datastores.Store() db := dbStore.db request := r.Body - err := chiRender.Bind(request, &data) + err := chiRender.Bind(request, &User) user := r.Context().Value(oldUserKey).(models.User) token := r.Context().Value(jwtTokenKey).(*jwt.Token) if !canManageUser("global", true, user.Username, token) { @@ -460,7 +455,7 @@ func updateUser(w http.ResponseWriter, r *http.Request) { render.JSON(w, error401.StatusCode, error401) return } - if err != nil || data.User == nil { + if err != nil || (User == models.User{}) { render.JSON(w, error422.StatusCode, error422) return } @@ -468,7 +463,7 @@ func updateUser(w http.ResponseWriter, r *http.Request) { render.JSON(w, error503.StatusCode, error503) return } - apperr := store.User().Update(&user, data.User, db) + apperr := store.User().Update(&user, &User, db) if apperr != nil { render.JSON(w, apperr.StatusCode, apperr) return diff --git a/models/member.go b/models/member.go index 27b1d05..7a6e6ff 100644 --- a/models/member.go +++ b/models/member.go @@ -16,9 +16,11 @@ type Member struct { IDUser uint64 `gorm:"column:idUser; not null;" json:"id_user,omitempty"` Channel Channel `db:"-" json:"-"` // required: true - IDChannel uint64 `gorm:"column:idChannel; not null;" json:"id_channel,omitempty"` - Role Role `db:"-" json:"-"` - IDRole uint64 `gorm:"column:idRole; not null;" json:"id_role,omitempty"` + IDChannel uint64 `gorm:"column:idChannel; not null;" json:"id_channel,omitempty"` + Role Role `db:"-" json:"-"` + IDRole uint64 `gorm:"column:idRole; not null;" json:"id_role,omitempty"` + TimedOut bool `gorm:"column:timedOut; not null;" json:"timed_out, omitempty"` + TimeOutEnd int64 `gorm:"column:timeOutEnd" json:"timeout_end, omitempty"` } // IsValid check validity of member object From 8453cd0e3efbb41ef738e251b8c101607643851b Mon Sep 17 00:00:00 2001 From: titouanfreville Date: Tue, 28 Mar 2017 00:52:36 +0200 Subject: [PATCH 4/4] WIP : managed channels/../user/... routes --- api/member_route.go | 2 -- datastores/members_store.go | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/api/member_route.go b/api/member_route.go index cff7956..fc6e2a2 100644 --- a/api/member_route.go +++ b/api/member_route.go @@ -2,7 +2,6 @@ package api import ( "context" - "log" "net/http" "strconv" @@ -204,7 +203,6 @@ func memberContext(next http.Handler) http.Handler { } ctx = context.WithValue(ctx, oldMemberKey, oldMember) ctx = context.WithValue(ctx, oldChannelKey, channel) - log.Print("Context user:", userFromParam) ctx = context.WithValue(ctx, oldUserKey, userFromParam) next.ServeHTTP(w, r.WithContext(ctx)) }) diff --git a/datastores/members_store.go b/datastores/members_store.go index 2ea1c2a..e6dfc7b 100644 --- a/datastores/members_store.go +++ b/datastores/members_store.go @@ -76,7 +76,7 @@ func (msi MemberStoreImpl) GetChannelMember(user *models.User, channel *models.C // GetByUser get member from user func (msi MemberStoreImpl) GetByUser(user *models.User, db *gorm.DB) []models.Member { members := []models.Member{} - db.Table("members").Select("*").Joins("natural join users").Where("users.idUser = ?", user.IDUser).Find(&members) + db.Table("members").Select("*").Joins("inner join users on members.idUser=users.idUser").Where("users.idUser = ?", user.IDUser).Find(&members) return members }