123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- package api
- import (
- "errors"
- "net/http"
- "github.com/go-chi/chi"
- "github.com/go-chi/render"
- "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/dao"
- "wkla.no-ip.biz/gogs/Willie/MsgService/MessageService/model"
- )
- // UsersRoutes routes to the user interface
- func UsersRoutes() *chi.Mux {
- router := chi.NewRouter()
- router.Get("/me", GetMeEndpoint)
- router.With(RoleCheck([]string{"admin"})).Get("/", GetUsersEndpoint)
- router.With(RoleCheck([]string{"admin"})).Post("/", PostUserEndpoint)
- router.With(RoleCheck([]string{"admin"})).Get("/{username}", GetUserEndpoint)
- router.With(RoleCheck([]string{"admin"})).Put("/{username}", PutUserEndpoint)
- router.With(RoleCheck([]string{"admin"})).Delete("/{username}", DeleteUserEndpoint)
- return router
- }
- //GetMeEndpoint getting all user infos
- func GetMeEndpoint(response http.ResponseWriter, request *http.Request) {
- username, _, _ := request.BasicAuth()
- user, ok := dao.GetStorage().GetUser(username)
- if !ok {
- render.Render(response, request, ErrInternalServer(errors.New("")))
- return
- }
- user.Password = ""
- user.NewPassword = ""
- user.Salt = []byte{}
- render.JSON(response, request, user)
- }
- //GetUsersEndpoint getting all user infos
- func GetUsersEndpoint(response http.ResponseWriter, request *http.Request) {
- users, err := dao.GetStorage().GetUsers()
- if err != nil {
- render.Render(response, request, ErrInternalServer(err))
- return
- }
- myUsers := make([]model.User, 0)
- for _, user := range users {
- user.Password = ""
- user.NewPassword = ""
- user.Salt = []byte{}
- myUsers = append(myUsers, user)
- }
- render.JSON(response, request, myUsers)
- }
- //GetUserEndpoint getting a user info
- func GetUserEndpoint(response http.ResponseWriter, request *http.Request) {
- username := chi.URLParam(request, "username")
- user, ok := dao.GetStorage().GetUser(username)
- user.Password = ""
- user.NewPassword = ""
- user.Salt = []byte{}
- if !ok {
- render.Render(response, request, ErrInternalServer(errors.New("")))
- return
- }
- render.JSON(response, request, user)
- }
- //PostUserEndpoint adding a new user
- func PostUserEndpoint(response http.ResponseWriter, request *http.Request) {
- var user model.User
- err := render.DefaultDecoder(request, &user)
- if err != nil {
- render.Render(response, request, ErrInvalidRequest(err))
- return
- }
- adminusername, _, _ := request.BasicAuth()
- admin, ok := dao.GetStorage().GetUser(adminusername)
- if !ok {
- render.Render(response, request, ErrInternalServer(errors.New("")))
- return
- }
- if !admin.Admin {
- render.Render(response, request, ErrForbidden)
- return
- }
- idm := dao.GetIDM()
- user, err = idm.AddUser(user)
- if err != nil {
- render.Render(response, request, ErrInvalidRequest(err))
- return
- }
- user.Salt = []byte{}
- user.Password = "#####"
- user.NewPassword = ""
- render.Status(request, http.StatusCreated)
- render.JSON(response, request, user)
- }
- //PutUserEndpoint change password of user
- func PutUserEndpoint(response http.ResponseWriter, request *http.Request) {
- username := chi.URLParam(request, "username")
- var user model.User
- err := render.DefaultDecoder(request, &user)
- if err != nil {
- render.Render(response, request, ErrInvalidRequest(err))
- return
- }
- if username != user.Name {
- render.Render(response, request, ErrInvalidRequest(errors.New("username should be identically")))
- return
- }
- adminusername, _, _ := request.BasicAuth()
- admin, ok := dao.GetStorage().GetUser(adminusername)
- if !ok {
- render.Render(response, request, ErrInternalServer(errors.New("")))
- return
- }
- if (adminusername != username) && !admin.Admin {
- render.Render(response, request, ErrForbidden)
- return
- }
- idm := dao.GetIDM()
- err = idm.ChangePWD(username, user.NewPassword, user.Password)
- if err != nil {
- render.Render(response, request, ErrInvalidRequest(err))
- return
- }
- user.Salt = []byte{}
- user.Password = "#####"
- user.NewPassword = ""
- render.JSON(response, request, user)
- }
- //DeleteUserEndpoint deleting a user
- func DeleteUserEndpoint(response http.ResponseWriter, request *http.Request) {
- username := chi.URLParam(request, "username")
- adminusername, _, _ := request.BasicAuth()
- admin, ok := dao.GetStorage().GetUser(adminusername)
- if !ok {
- render.Render(response, request, ErrInternalServer(errors.New("")))
- return
- }
- if !admin.Admin {
- render.Render(response, request, ErrForbidden)
- return
- }
- idm := dao.GetIDM()
- err := idm.DeleteUser(username)
- if err != nil {
- render.Render(response, request, ErrInvalidRequest(err))
- return
- }
- return
- }
|