diff --git a/docs/docs.go b/docs/docs.go index 19698ed3dc6ceadc879b3e1f062fd32121932ac1..cf30202b3fb0d89fde8a104b485c022b647a0734 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -56,6 +56,9 @@ const docTemplate = `{ }, "post": { "description": "Add a user to database", + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], @@ -63,6 +66,17 @@ const docTemplate = `{ "admin" ], "summary": "Add User", + "parameters": [ + { + "description": "Admin Add User Payload", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.AdminAddUserPayload" + } + } + ], "responses": { "200": { "description": "OK", @@ -71,16 +85,18 @@ const docTemplate = `{ } } } - }, - "delete": { - "description": "Delete a user from database", + } + }, + "/admin/user/{email}": { + "get": { + "description": "Get a user from database", "produces": [ "application/json" ], "tags": [ "admin" ], - "summary": "Delete User By Id", + "summary": "Get User By Email", "responses": { "200": { "description": "OK", @@ -90,15 +106,18 @@ const docTemplate = `{ } } }, - "patch": { - "description": "Update a user from database", + "delete": { + "description": "Delete a user from database", + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], "tags": [ "admin" ], - "summary": "Update User By Id", + "summary": "Delete User By Id", "responses": { "200": { "description": "OK", @@ -107,18 +126,30 @@ const docTemplate = `{ } } } - } - }, - "/admin/user/{id}": { - "get": { - "description": "Get a user from database", + }, + "patch": { + "description": "Update a user from database", + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], "tags": [ "admin" ], - "summary": "Get User By Email", + "summary": "Update User By Id", + "parameters": [ + { + "description": "Admin Update User Payload", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.AdminUpdateUserPayload" + } + } + ], "responses": { "200": { "description": "OK", @@ -428,6 +459,56 @@ const docTemplate = `{ } }, "definitions": { + "admin.AdminAddUserPayload": { + "type": "object", + "required": [ + "email", + "name", + "role" + ], + "properties": { + "email": { + "description": "User Email", + "type": "string", + "example": "someone@example.com" + }, + "name": { + "description": "User name", + "type": "string", + "example": "someone" + }, + "role": { + "description": "User Role", + "type": "string", + "example": "admin" + } + } + }, + "admin.AdminUpdateUserPayload": { + "type": "object", + "required": [ + "email", + "name", + "role" + ], + "properties": { + "email": { + "description": "User Email", + "type": "string", + "example": "someone@example.com" + }, + "name": { + "description": "User name", + "type": "string", + "example": "someone" + }, + "role": { + "description": "User Role", + "type": "string", + "example": "admin" + } + } + }, "confirm.ConfirmRequestPayload": { "description": "Information that should be available when you confirm a password reset", "type": "object", diff --git a/docs/swagger.json b/docs/swagger.json index e2502ca224c9009741eb5222e9f56a4019a8f95c..b0c36239bb54582921dae56d834e5903e59ebe55 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -48,6 +48,9 @@ }, "post": { "description": "Add a user to database", + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], @@ -55,6 +58,17 @@ "admin" ], "summary": "Add User", + "parameters": [ + { + "description": "Admin Add User Payload", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.AdminAddUserPayload" + } + } + ], "responses": { "200": { "description": "OK", @@ -63,16 +77,18 @@ } } } - }, - "delete": { - "description": "Delete a user from database", + } + }, + "/admin/user/{email}": { + "get": { + "description": "Get a user from database", "produces": [ "application/json" ], "tags": [ "admin" ], - "summary": "Delete User By Id", + "summary": "Get User By Email", "responses": { "200": { "description": "OK", @@ -82,15 +98,18 @@ } } }, - "patch": { - "description": "Update a user from database", + "delete": { + "description": "Delete a user from database", + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], "tags": [ "admin" ], - "summary": "Update User By Id", + "summary": "Delete User By Id", "responses": { "200": { "description": "OK", @@ -99,18 +118,30 @@ } } } - } - }, - "/admin/user/{id}": { - "get": { - "description": "Get a user from database", + }, + "patch": { + "description": "Update a user from database", + "consumes": [ + "application/json" + ], "produces": [ "application/json" ], "tags": [ "admin" ], - "summary": "Get User By Email", + "summary": "Update User By Id", + "parameters": [ + { + "description": "Admin Update User Payload", + "name": "data", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/admin.AdminUpdateUserPayload" + } + } + ], "responses": { "200": { "description": "OK", @@ -420,6 +451,56 @@ } }, "definitions": { + "admin.AdminAddUserPayload": { + "type": "object", + "required": [ + "email", + "name", + "role" + ], + "properties": { + "email": { + "description": "User Email", + "type": "string", + "example": "someone@example.com" + }, + "name": { + "description": "User name", + "type": "string", + "example": "someone" + }, + "role": { + "description": "User Role", + "type": "string", + "example": "admin" + } + } + }, + "admin.AdminUpdateUserPayload": { + "type": "object", + "required": [ + "email", + "name", + "role" + ], + "properties": { + "email": { + "description": "User Email", + "type": "string", + "example": "someone@example.com" + }, + "name": { + "description": "User name", + "type": "string", + "example": "someone" + }, + "role": { + "description": "User Role", + "type": "string", + "example": "admin" + } + } + }, "confirm.ConfirmRequestPayload": { "description": "Information that should be available when you confirm a password reset", "type": "object", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 8ddb11962c76d0f6a7e4f281daa04c14132b9128..e01ccea263c225cf596ae8d2dd0a9750a1422851 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,4 +1,42 @@ definitions: + admin.AdminAddUserPayload: + properties: + email: + description: User Email + example: someone@example.com + type: string + name: + description: User name + example: someone + type: string + role: + description: User Role + example: admin + type: string + required: + - email + - name + - role + type: object + admin.AdminUpdateUserPayload: + properties: + email: + description: User Email + example: someone@example.com + type: string + name: + description: User name + example: someone + type: string + role: + description: User Role + example: admin + type: string + required: + - email + - name + - role + type: object confirm.ConfirmRequestPayload: description: Information that should be available when you confirm a password reset @@ -126,8 +164,8 @@ paths: tags: - common /admin/user: - delete: - description: Delete a user from database + get: + description: Get all users from database produces: - application/json responses: @@ -135,11 +173,20 @@ paths: description: OK schema: $ref: '#/definitions/web.BaseResponse' - summary: Delete User By Id + summary: Get All User tags: - admin - get: - description: Get all users from database + post: + consumes: + - application/json + description: Add a user to database + parameters: + - description: Admin Add User Payload + in: body + name: data + required: true + schema: + $ref: '#/definitions/admin.AdminAddUserPayload' produces: - application/json responses: @@ -147,11 +194,14 @@ paths: description: OK schema: $ref: '#/definitions/web.BaseResponse' - summary: Get All User + summary: Add User tags: - admin - patch: - description: Update a user from database + /admin/user/{email}: + delete: + consumes: + - application/json + description: Delete a user from database produces: - application/json responses: @@ -159,11 +209,11 @@ paths: description: OK schema: $ref: '#/definitions/web.BaseResponse' - summary: Update User By Id + summary: Delete User By Id tags: - admin - post: - description: Add a user to database + get: + description: Get a user from database produces: - application/json responses: @@ -171,12 +221,20 @@ paths: description: OK schema: $ref: '#/definitions/web.BaseResponse' - summary: Add User + summary: Get User By Email tags: - admin - /admin/user/{id}: - get: - description: Get a user from database + patch: + consumes: + - application/json + description: Update a user from database + parameters: + - description: Admin Update User Payload + in: body + name: data + required: true + schema: + $ref: '#/definitions/admin.AdminUpdateUserPayload' produces: - application/json responses: @@ -184,7 +242,7 @@ paths: description: OK schema: $ref: '#/definitions/web.BaseResponse' - summary: Get User By Email + summary: Update User By Id tags: - admin /auth/login: diff --git a/handler/admin/addUser.go b/handler/admin/addUser.go index 7b53f3d3d4977c157d67048d3079c40e46e446fe..02c631be6d59ed66f628b57dfa4ace8191b05249 100644 --- a/handler/admin/addUser.go +++ b/handler/admin/addUser.go @@ -2,6 +2,7 @@ package admin import ( "net/http" + req "gitlab.informatika.org/ocw/ocw-backend/model/web/admin/addUser" ) // Index godoc @@ -10,9 +11,32 @@ import ( // @Summary Add User // @Description Add a user to database // @Produce json +// @Accept json +// @Param data body req.AdminAddUserPayload true "Admin Add User Payload" // @Success 200 {object} web.BaseResponse // @Router /admin/user [post] func (route AdminHandlerImpl) AddUser(w http.ResponseWriter, r *http.Request){ - payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.AddUser()) + payload := req.AdminAddUserPayload{} + + if r.Header.Get("Content-Type") != "application/json" { + payload := route.WrapperUtil.ErrorResponseWrap("this service only receive json input", nil) + route.HttpUtil.WriteJson(w, http.StatusUnsupportedMediaType, payload) + return + } + + if err := route.HttpUtil.ParseJson(r, &payload); err != nil { + payload := route.WrapperUtil.ErrorResponseWrap("invalid json input", err.Error()) + route.HttpUtil.WriteJson(w, http.StatusUnprocessableEntity, payload) + return + } + + err := route.AdminService.AddUser(payload) + + if err != nil { + payload := route.WrapperUtil.ErrorResponseWrap(err.Error(), nil) + route.HttpUtil.WriteJson(w, http.StatusBadRequest, payload) + return + } + route.HttpUtil.WriteSuccessJson(w, payload) } \ No newline at end of file diff --git a/handler/admin/deleteUser.go b/handler/admin/deleteUser.go index 0be96ccc00451aaf0dd1490d6b29154a1b2d62dd..9979a47a2279814f9de6c9302c3a555531a659aa 100644 --- a/handler/admin/deleteUser.go +++ b/handler/admin/deleteUser.go @@ -2,6 +2,7 @@ package admin import ( "net/http" + "path" ) // Index godoc @@ -10,9 +11,23 @@ import ( // @Summary Delete User By Id // @Description Delete a user from database // @Produce json +// @Accept json // @Success 200 {object} web.BaseResponse -// @Router /admin/user [delete] +// @Router /admin/user/{email} [delete] func (route AdminHandlerImpl) DeleteUser(w http.ResponseWriter, r *http.Request){ - payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.DeleteUser()) - route.HttpUtil.WriteSuccessJson(w, payload) + email := path.Base(r.URL.Path) + + // get user from database + err := route.AdminService.DeleteUser(email) + + if err != nil { + // error handling + payload := route.WrapperUtil.ErrorResponseWrap("error", err.Error()) + route.HttpUtil.WriteJson(w, http.StatusUnprocessableEntity, payload) + return + } + + // return user + result := route.WrapperUtil.SuccessResponseWrap(email) + route.HttpUtil.WriteJson(w, http.StatusOK, result) } \ No newline at end of file diff --git a/handler/admin/getUserByEmail.go b/handler/admin/getUserByEmail.go index 0ad7a59fce2214548fd0aaae8b2443e4b5595ca7..fd21301db325bf6d59eb8df4404f352ba4fd2b29 100644 --- a/handler/admin/getUserByEmail.go +++ b/handler/admin/getUserByEmail.go @@ -2,6 +2,7 @@ package admin import ( "net/http" + "path" ) // Index godoc @@ -11,8 +12,22 @@ import ( // @Description Get a user from database // @Produce json // @Success 200 {object} web.BaseResponse -// @Router /admin/user/{id} [get] +// @Router /admin/user/{email} [get] func (route AdminHandlerImpl) GetUserByEmail(w http.ResponseWriter, r *http.Request) { - payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.GetUserByEmail()) - route.HttpUtil.WriteSuccessJson(w, payload) + // email := r.URL.Query().Get("email") + email := path.Base(r.URL.Path) + + // get user from database + user, err := route.AdminService.GetUserByEmail(email) + + if err != nil { + // error handling + payload := route.WrapperUtil.ErrorResponseWrap("error", err.Error()) + route.HttpUtil.WriteJson(w, http.StatusUnprocessableEntity, payload) + return + } + + // return user + result := route.WrapperUtil.SuccessResponseWrap(user) + route.HttpUtil.WriteJson(w, http.StatusOK, result) } diff --git a/handler/admin/updateUser.go b/handler/admin/updateUser.go index c6531ec04835aabca40eedf74f3f008bc27d017c..877d5c84a3785626e9736ecf1d433d37fdf1df40 100644 --- a/handler/admin/updateUser.go +++ b/handler/admin/updateUser.go @@ -2,6 +2,8 @@ package admin import ( "net/http" + "path" + req "gitlab.informatika.org/ocw/ocw-backend/model/web/admin/updateUser" ) // Index godoc @@ -10,9 +12,35 @@ import ( // @Summary Update User By Id // @Description Update a user from database // @Produce json +// @Accept json +// @Param data body req.AdminUpdateUserPayload true "Admin Update User Payload" // @Success 200 {object} web.BaseResponse -// @Router /admin/user [patch] +// @Router /admin/user/{email} [patch] func (route AdminHandlerImpl) UpdateUser(w http.ResponseWriter, r *http.Request){ - payload := route.WrapperUtil.SuccessResponseWrap(route.AdminService.UpdateUser()) + email := path.Base(r.URL.Path) + // TODO: how to change email + + payload := req.AdminUpdateUserPayload{} + + if r.Header.Get("Content-Type") != "application/json" { + payload := route.WrapperUtil.ErrorResponseWrap("this service only receive json input", nil) + route.HttpUtil.WriteJson(w, http.StatusUnsupportedMediaType, payload) + return + } + + if err := route.HttpUtil.ParseJson(r, &payload); err != nil { + payload := route.WrapperUtil.ErrorResponseWrap("invalid json input", err.Error()) + route.HttpUtil.WriteJson(w, http.StatusUnprocessableEntity, payload) + return + } + + err := route.AdminService.UpdateUser(email, payload) + + if err != nil { + payload := route.WrapperUtil.ErrorResponseWrap(err.Error(), nil) + route.HttpUtil.WriteJson(w, http.StatusBadRequest, payload) + return + } + route.HttpUtil.WriteSuccessJson(w, payload) } \ No newline at end of file diff --git a/middleware/di.go b/middleware/di.go index 3e73df4ca24a398c120b42551be10f5b5d1f030c..d83e52f49c9e67f0ad90f29e745108cc6ecb6121 100644 --- a/middleware/di.go +++ b/middleware/di.go @@ -4,6 +4,7 @@ import ( "github.com/google/wire" "gitlab.informatika.org/ocw/ocw-backend/middleware/cleanpath" "gitlab.informatika.org/ocw/ocw-backend/middleware/cors" + "gitlab.informatika.org/ocw/ocw-backend/middleware/guard" "gitlab.informatika.org/ocw/ocw-backend/middleware/log" "gitlab.informatika.org/ocw/ocw-backend/middleware/recoverer" "gitlab.informatika.org/ocw/ocw-backend/middleware/trailslash" @@ -24,6 +25,8 @@ var middlewareCollectionSet = wire.NewSet( // Trailslash wire.Struct(new(trailslash.TrailSlashMiddleware), "*"), + + guard.NewBuilder, ) var MiddlewareSet = wire.NewSet( diff --git a/middleware/guard/builder.go b/middleware/guard/builder.go new file mode 100644 index 0000000000000000000000000000000000000000..75850ebf45a8ef08662c76311156cd2a067a4ff3 --- /dev/null +++ b/middleware/guard/builder.go @@ -0,0 +1,43 @@ +package guard + +import ( + "net/http" + + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" + "gitlab.informatika.org/ocw/ocw-backend/service/logger" + "gitlab.informatika.org/ocw/ocw-backend/utils/token" + "gitlab.informatika.org/ocw/ocw-backend/utils/wrapper" +) + +type GuardBuilder struct { + GuardMiddleware +} + +func NewBuilder( + token token.TokenUtil, + logger logger.Logger, + wrapper wrapper.WrapperUtil, +) *GuardBuilder { + return &GuardBuilder{ + GuardMiddleware{ + Token: token, + Role: []user.UserRole{}, + Logger: logger, + WrapperUtil: wrapper, + }, + } +} + +func (g *GuardBuilder) AddRole(role ...user.UserRole) *GuardBuilder { + g.GuardMiddleware.Role = role + return g +} + +func (g *GuardBuilder) Build() func(http.Handler) http.Handler { + return g.GuardMiddleware.Handle +} + +func (g *GuardBuilder) BuildSimple(role user.UserRole) func(http.Handler) http.Handler { + g.AddRole(role) + return g.Build() +} diff --git a/middleware/guard/guard.go b/middleware/guard/guard.go new file mode 100644 index 0000000000000000000000000000000000000000..86ef217bdeba97319a087d49809730f49cb3cd87 --- /dev/null +++ b/middleware/guard/guard.go @@ -0,0 +1,73 @@ +package guard + +import ( + "encoding/json" + "net/http" + "strings" + + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" + authToken "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token" + "gitlab.informatika.org/ocw/ocw-backend/service/logger" + "gitlab.informatika.org/ocw/ocw-backend/utils/token" + "gitlab.informatika.org/ocw/ocw-backend/utils/wrapper" +) + +type GuardMiddleware struct { + Token token.TokenUtil + Role []user.UserRole + Logger logger.Logger + wrapper.WrapperUtil +} + +func (g GuardMiddleware) Handle(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + + if len(g.Role) > 0 { + authorization := r.Header.Get("Authorization") + + if authorization != "" { + g.Logger.Info("Unauthorized access detected") + + w.WriteHeader(http.StatusUnauthorized) + payload := g.WrapperUtil.ErrorResponseWrap("authorization is required", nil) + + parser := json.NewEncoder(w) + parser.Encode(payload) + return + } + + tokenString := strings.Split(authorization, " ")[1] + claim, err := g.Token.Validate(tokenString, authToken.Access) + + if err != nil { + g.Logger.Info("Invalid token request") + parser := json.NewEncoder(w) + + w.WriteHeader(http.StatusUnauthorized) + payload := g.WrapperUtil.ErrorResponseWrap(err.Error(), nil) + parser.Encode(payload) + return + } + + isAuthorized := false + + for _, user := range g.Role { + if user == claim.Role { + isAuthorized = true + } + } + + if !isAuthorized { + g.Logger.Info("Unauthorized user access") + parser := json.NewEncoder(w) + + w.WriteHeader(http.StatusForbidden) + payload := g.WrapperUtil.ErrorResponseWrap("current user role is prohibited to access this resources", nil) + parser.Encode(payload) + return + } + } + + next.ServeHTTP(w, r) + }) +} diff --git a/model/web/admin/addUser/request.go b/model/web/admin/addUser/request.go new file mode 100644 index 0000000000000000000000000000000000000000..c08f743abc16843b3c0ca60c3725ac24d348db05 --- /dev/null +++ b/model/web/admin/addUser/request.go @@ -0,0 +1,17 @@ +package admin + +// AdminAddUserPayload Request Payload +// @Description Information that should be available when admin add user + +// TODO: find a way to make default password for new user + +type AdminAddUserPayload struct { + // User name + Name string `json:"name" validate:"required" example:"someone"` + + // User Email + Email string `json:"email" validate:"required,email" example:"someone@example.com"` + + // User Role + Role string `json:"role" validate:"required" example:"admin"` +} diff --git a/model/web/admin/updateUser/request.go b/model/web/admin/updateUser/request.go new file mode 100644 index 0000000000000000000000000000000000000000..437d35fa7e7f5f415fea0ab49ec84f09c246ff7e --- /dev/null +++ b/model/web/admin/updateUser/request.go @@ -0,0 +1,15 @@ +package admin + +// AdminUpdateUserPayload Request Payload +// @Description Information that should be available when admin update user + +type AdminUpdateUserPayload struct { + // User name + Name string `json:"name" validate:"required" example:"someone"` + + // User Email + Email string `json:"email" validate:"required,email" example:"someone@example.com"` + + // User Role + Role string `json:"role" validate:"required" example:"admin"` +} diff --git a/repository/user/user.go b/repository/user/user.go index f68ccb7032f192d474a767cb485002437151260a..7650db26644afab82c1c4f045e316f9b36ad468c 100644 --- a/repository/user/user.go +++ b/repository/user/user.go @@ -62,6 +62,6 @@ func (repo UserRepositoryImpl) Update(user user.User) error { return repo.db.Save(user).Error } -func (repo UserRepositoryImpl) Delete(username string) error { - return repo.db.Where("username = ?", username).Delete(&user.User{}).Error +func (repo UserRepositoryImpl) Delete(email string) error { + return repo.db.Where("email = ?", email).Delete(&user.User{}).Error } diff --git a/routes/admin/route.go b/routes/admin/route.go index 01f19943524626adb4aea1b73f5a912d4fdee3fa..d1ff589b86faa181e6e671bf7111ab3efed0b239 100644 --- a/routes/admin/route.go +++ b/routes/admin/route.go @@ -3,18 +3,23 @@ package admin import ( "github.com/go-chi/chi/v5" "gitlab.informatika.org/ocw/ocw-backend/handler/admin" + "gitlab.informatika.org/ocw/ocw-backend/middleware/guard" + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" ) type AdminRoutes struct { admin.AdminHandler + *guard.GuardBuilder } func (adr AdminRoutes) Register(r chi.Router) { r.Route("/admin", func(r chi.Router) { + r.Use(adr.GuardBuilder.BuildSimple(user.Admin)) + r.Get("/user", adr.AdminHandler.GetAllUser) - r.Get("/user/{id}", adr.AdminHandler.GetUserByEmail) + r.Get("/user/{email}", adr.AdminHandler.GetUserByEmail) r.Post("/user", adr.AdminHandler.AddUser) - r.Patch("/user/{id}", adr.AdminHandler.UpdateUser) - r.Delete("/user/{id}", adr.AdminHandler.DeleteUser) + r.Patch("/user/{email}", adr.AdminHandler.UpdateUser) + r.Delete("/user/{email}", adr.AdminHandler.DeleteUser) }) } diff --git a/service/admin/addUser.go b/service/admin/addUser.go index 644bde9d0547485f4eb3ef45a4876d12327550c6..93e53b0a0cccb8921ec5a00b742c6d1b00df9c93 100644 --- a/service/admin/addUser.go +++ b/service/admin/addUser.go @@ -1,16 +1,29 @@ package admin -// import ( - // "errors" - // "time" +import ( + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" + req "gitlab.informatika.org/ocw/ocw-backend/model/web/admin/addUser" +) - // "github.com/golang-jwt/jwt/v4" - // "gitlab.informatika.org/ocw/ocw-backend/model/web" - // "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/login" - // tokenModel "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token" - // "gorm.io/gorm" -// ) +func (as AdminServiceImpl) AddUser(payload req.AdminAddUserPayload) error { + // change role payload from string to user.UserRole + var role user.UserRole -func (AdminServiceImpl) AddUser() string { - return "add user" + // TODO: move this + if (payload.Role == "admin") { + role = user.Admin + } else if (payload.Role == "contributor") { + role = user.Contributor + } else if (payload.Role == "member") { + role = user.Student + } + + err := as.UserRepository.Add(user.User{ + Email: payload.Email, + Name: payload.Name, + Role: role, + IsActivated: false, + }) + + return err } \ No newline at end of file diff --git a/service/admin/deleteUser.go b/service/admin/deleteUser.go index 0e34cf7227863d1865c5de527b1bfa12806c8573..2ed1a1dc451ed20ce9b9667a0722aa3dbd40cc96 100644 --- a/service/admin/deleteUser.go +++ b/service/admin/deleteUser.go @@ -1,16 +1,6 @@ package admin -// import ( - // "errors" - // "time" - - // "github.com/golang-jwt/jwt/v4" - // "gitlab.informatika.org/ocw/ocw-backend/model/web" - // "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/login" - // tokenModel "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token" - // "gorm.io/gorm" -// ) - -func (AdminServiceImpl) DeleteUser() string { - return "delete user" -} +func (as AdminServiceImpl) DeleteUser(email string) error { + err := as.UserRepository.Delete(email) + return err +} \ No newline at end of file diff --git a/service/admin/getUserByEmail.go b/service/admin/getUserByEmail.go index e577536e5acccc895a4861a6845d9cd3b13018d8..3c43845eec62c4fac3ae33486233c8aa118bbe87 100644 --- a/service/admin/getUserByEmail.go +++ b/service/admin/getUserByEmail.go @@ -1,16 +1,11 @@ package admin -// import ( - // "errors" - // "time" +import ( + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" +) - // "github.com/golang-jwt/jwt/v4" - // "gitlab.informatika.org/ocw/ocw-backend/model/web" - // "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/login" - // tokenModel "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token" - // "gorm.io/gorm" -// ) - -func (AdminServiceImpl) GetUserByEmail() string { - return "get user by email" +func (as AdminServiceImpl) GetUserByEmail(email string) (*user.User, error) { + var users *user.User + users, nil := as.UserRepository.Get(email) + return users, nil } \ No newline at end of file diff --git a/service/admin/impl.go b/service/admin/impl.go index 7bc0dd3364dc3f46e47d282c647bb26380d4a31c..7c1dc75819f6423ab8a5a15678526df81f0de402 100644 --- a/service/admin/impl.go +++ b/service/admin/impl.go @@ -6,4 +6,4 @@ import ( type AdminServiceImpl struct { UserRepository user.UserRepository -} \ No newline at end of file +} diff --git a/service/admin/type.go b/service/admin/type.go index d624c56274802d9fc9523aa092d21f1fed14a314..228b5674efdb4758be6fc0c80585eb698c9a95f2 100644 --- a/service/admin/type.go +++ b/service/admin/type.go @@ -2,12 +2,14 @@ package admin import ( "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" + addUser "gitlab.informatika.org/ocw/ocw-backend/model/web/admin/addUser" + updateUser "gitlab.informatika.org/ocw/ocw-backend/model/web/admin/updateUser" ) type AdminService interface { GetAllUser() ([]user.User, error) - GetUserByEmail() string - AddUser() string - UpdateUser() string - DeleteUser() string + GetUserByEmail(email string) (*user.User, error) + AddUser(payload addUser.AdminAddUserPayload) error + UpdateUser(email string, payload updateUser.AdminUpdateUserPayload) error + DeleteUser(email string) error } diff --git a/service/admin/updateUser.go b/service/admin/updateUser.go index e3bce672ae924615b9eb4df9ebda6d12c8afad00..859f7d878195d925aa03852c29155887a099ad79 100644 --- a/service/admin/updateUser.go +++ b/service/admin/updateUser.go @@ -1,16 +1,29 @@ package admin -// import ( - // "errors" - // "time" +import ( + "gitlab.informatika.org/ocw/ocw-backend/model/domain/user" + req "gitlab.informatika.org/ocw/ocw-backend/model/web/admin/updateUser" +) - // "github.com/golang-jwt/jwt/v4" - // "gitlab.informatika.org/ocw/ocw-backend/model/web" - // "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/login" - // tokenModel "gitlab.informatika.org/ocw/ocw-backend/model/web/auth/token" - // "gorm.io/gorm" -// ) +func (as AdminServiceImpl) UpdateUser(email string, payload req.AdminUpdateUserPayload) error { + // change role payload from string to user.UserRole + var role user.UserRole -func (AdminServiceImpl) UpdateUser() string { - return "update user" + // TODO: move this + if (payload.Role == "admin") { + role = user.Admin + } else if (payload.Role == "contributor") { + role = user.Contributor + } else if (payload.Role == "member") { + role = user.Student + } + + err := as.UserRepository.Update(user.User{ + Email: payload.Email, + Name: payload.Name, + Role: role, // TODO: Change this + IsActivated: false, + }) + + return err } \ No newline at end of file