From 20158f4c37d9b4948fadb92fb70040180968ba5f Mon Sep 17 00:00:00 2001 From: pai0id Date: Fri, 19 Jul 2024 11:57:11 +0300 Subject: [PATCH] get clearnce docs, feed, membs --- internal/app/members.go | 2 +- internal/ports/documents.go | 40 +++++++++++++++++++++++++ internal/ports/feed.go | 40 +++++++++++++++++++++++++ internal/ports/members.go | 60 ++++++++++++++++++++++++++++++------- 4 files changed, 131 insertions(+), 11 deletions(-) diff --git a/internal/app/members.go b/internal/app/members.go index fa6dd79..87e262e 100644 --- a/internal/app/members.go +++ b/internal/app/members.go @@ -110,7 +110,7 @@ func (s *MembersService) UpdateMember(ctx context.Context, member *domain.Member return nil } -func (s *MembersService) GetClearancePost(ctx context.Context, resp *responses.CheckResponse) (*responses.GetClearance, error) { +func (s *MembersService) GetClearance(ctx context.Context, resp *responses.CheckResponse) (*responses.GetClearance, error) { if resp.IsAdmin { return &responses.GetClearance{Access: true, Comment: ""}, nil } diff --git a/internal/ports/documents.go b/internal/ports/documents.go index 4d69984..bdf2e4f 100644 --- a/internal/ports/documents.go +++ b/internal/ports/documents.go @@ -53,6 +53,7 @@ func (h *DocumentsHandler) Routes() chi.Router { r.Post("/categories/", h.r.Wrap(h.PostCategory)) r.Delete("/categories/{id}", h.r.Wrap(h.DeleteCategory)) r.Put("/categories/{id}", h.r.Wrap(h.UpdateCategory)) + r.Get("/clearance/post/", h.r.Wrap(h.GetClearancePost)) return r } @@ -717,3 +718,42 @@ func (h *DocumentsHandler) UpdateCategory(w http.ResponseWriter, req *http.Reque return handler.OkResponse(nil) } + +// GetClearancePost checks if the member is allowed to post/del/put +// +// @Summary Check member`s post clearance +// @Description Check if the member is allowed to post/del/put +// @Tags auth.documents +// @Success 200 {object} responses.GetClearance +// @Failure 401 +// @Failure 500 +// @Router /documents/clearance/post/ [get] +// @Security Authorised +func (h *DocumentsHandler) GetClearancePost(w http.ResponseWriter, req *http.Request) handler.Response { + h.logger.Info("DocumentsHandler: got GetClearancePost request") + + accessToken, err := getAccessToken(req) + if err != nil { + h.logger.Warnf("can't get access token GetClearancePost: %v", err) + return handler.UnauthorizedResponse() + } + + checkResp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: accessToken}) + if err != nil || !checkResp.Valid { + h.logger.Warnf("can't GuardService.Check on GetClearancePost: %v", err) + return handler.UnauthorizedResponse() + } + + h.logger.Infof("DocumentsHandler: GetClearancePost Authenticated: %v", checkResp.MemberID) + + cleaResp, err := h.documents.GetClearancePost(context.Background(), checkResp) + + if err != nil { + h.logger.Warnf("can't DocumentsService.GetClearancePost: %v", err) + return handler.InternalServerErrorResponse() + } + + h.logger.Info("DocumentsHandler: GetClearancePost Done") + + return handler.OkResponse(cleaResp) +} diff --git a/internal/ports/feed.go b/internal/ports/feed.go index 0dcb502..8b5e0a5 100644 --- a/internal/ports/feed.go +++ b/internal/ports/feed.go @@ -50,6 +50,7 @@ func (h *FeedHandler) Routes() chi.Router { r.Post("/encounters/", h.r.Wrap(h.PostEncounter)) r.Delete("/encounters/{id}", h.r.Wrap(h.DeleteEncounter)) r.Put("/encounters/{id}", h.r.Wrap(h.UpdateEncounter)) + r.Get("/clearance/post/", h.r.Wrap(h.GetClearancePost)) return r } @@ -626,3 +627,42 @@ func (h *FeedHandler) UpdateEncounter(w http.ResponseWriter, req *http.Request) return handler.OkResponse(nil) } + +// GetClearancePost checks if the member is allowed to post/del/put +// +// @Summary Check member`s post clearance +// @Description Check if the member is allowed to post/del/put +// @Tags auth.feed +// @Success 200 {object} responses.GetClearance +// @Failure 401 +// @Failure 500 +// @Router /feed/clearance/post/ [get] +// @Security Authorised +func (h *FeedHandler) GetClearancePost(w http.ResponseWriter, req *http.Request) handler.Response { + h.logger.Info("FeedHandler: got GetClearancePost request") + + accessToken, err := getAccessToken(req) + if err != nil { + h.logger.Warnf("can't get access token GetClearancePost: %v", err) + return handler.UnauthorizedResponse() + } + + checkResp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: accessToken}) + if err != nil || !checkResp.Valid { + h.logger.Warnf("can't GuardService.Check on GetClearancePost: %v", err) + return handler.UnauthorizedResponse() + } + + h.logger.Infof("FeedHandler: GetClearancePost Authenticated: %v", checkResp.MemberID) + + cleaResp, err := h.feed.GetClearancePost(context.Background(), checkResp) + + if err != nil { + h.logger.Warnf("can't FeedService.GetClearancePost: %v", err) + return handler.InternalServerErrorResponse() + } + + h.logger.Info("FeedHandler: GetClearancePost Done") + + return handler.OkResponse(cleaResp) +} diff --git a/internal/ports/members.go b/internal/ports/members.go index 4e8b1e2..296c718 100644 --- a/internal/ports/members.go +++ b/internal/ports/members.go @@ -44,6 +44,7 @@ func (h *MembersHandler) Routes() chi.Router { r.Get("/search/{name}", h.r.Wrap(h.GetMembersByName)) r.Delete("/{id}", h.r.Wrap(h.DeleteMember)) r.Put("/{id}", h.r.Wrap(h.UpdateMember)) + r.Get("/clearance/", h.r.Wrap(h.GetClearance)) return r } @@ -78,10 +79,10 @@ func (h *MembersHandler) GetAllMembers(w http.ResponseWriter, req *http.Request) h.logger.Infof("MembersHandler: GetAllMembers Authenticated: %v", checkResp.MemberID) - cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp) + cleaResp, err := h.members.GetClearance(context.Background(), checkResp) if err != nil { - h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err) + h.logger.Warnf("can't MembersHandler.GetClearance: %v", err) return handler.InternalServerErrorResponse() } @@ -138,10 +139,10 @@ func (h *MembersHandler) GetMember(w http.ResponseWriter, req *http.Request) han h.logger.Infof("MembersHandler: GetAllMembers Authenticated: %v", checkResp.MemberID) - cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp) + cleaResp, err := h.members.GetClearance(context.Background(), checkResp) if err != nil { - h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err) + h.logger.Warnf("can't MembersHandler.GetClearance: %v", err) return handler.InternalServerErrorResponse() } @@ -208,10 +209,10 @@ func (h *MembersHandler) GetMembersByName(w http.ResponseWriter, req *http.Reque h.logger.Infof("MembersHandler: GetMembersByName Authenticated: %v", checkResp.MemberID) - cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp) + cleaResp, err := h.members.GetClearance(context.Background(), checkResp) if err != nil { - h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err) + h.logger.Warnf("can't MembersHandler.GetClearance: %v", err) return handler.InternalServerErrorResponse() } @@ -278,10 +279,10 @@ func (h *MembersHandler) DeleteMember(w http.ResponseWriter, req *http.Request) h.logger.Infof("MembersHandler: DeleteMember Authenticated: %v", checkResp.MemberID) - cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp) + cleaResp, err := h.members.GetClearance(context.Background(), checkResp) if err != nil { - h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err) + h.logger.Warnf("can't MembersHandler.GetClearance: %v", err) return handler.InternalServerErrorResponse() } @@ -351,10 +352,10 @@ func (h *MembersHandler) UpdateMember(w http.ResponseWriter, req *http.Request) h.logger.Infof("MembersHandler: UpdateMember Authenticated: %v", checkResp.MemberID) - cleaResp, err := h.members.GetClearancePost(context.Background(), checkResp) + cleaResp, err := h.members.GetClearance(context.Background(), checkResp) if err != nil { - h.logger.Warnf("can't MembersHandler.GetClearancePost: %v", err) + h.logger.Warnf("can't MembersHandler.GetClearance: %v", err) return handler.InternalServerErrorResponse() } @@ -393,3 +394,42 @@ func (h *MembersHandler) UpdateMember(w http.ResponseWriter, req *http.Request) return handler.OkResponse(nil) } + +// GetClearance checks if the member is allowed to access members +// +// @Summary Check member`s clearance +// @Description Check if the member is allowed to access members +// @Tags auth.members +// @Success 200 {object} responses.GetClearance +// @Failure 401 +// @Failure 500 +// @Router /members/clearance/post/ [get] +// @Security Authorised +func (h *MembersHandler) GetClearance(w http.ResponseWriter, req *http.Request) handler.Response { + h.logger.Info("MembersHandler: got GetClearance request") + + accessToken, err := getAccessToken(req) + if err != nil { + h.logger.Warnf("can't get access token GetClearance: %v", err) + return handler.UnauthorizedResponse() + } + + checkResp, err := h.guard.Check(context.Background(), &requests.CheckRequest{AccessToken: accessToken}) + if err != nil || !checkResp.Valid { + h.logger.Warnf("can't GuardService.Check on GetClearance: %v", err) + return handler.UnauthorizedResponse() + } + + h.logger.Infof("MembersHandler: GetClearance Authenticated: %v", checkResp.MemberID) + + cleaResp, err := h.members.GetClearance(context.Background(), checkResp) + + if err != nil { + h.logger.Warnf("can't MembersService.GetClearance: %v", err) + return handler.InternalServerErrorResponse() + } + + h.logger.Info("MembersHandler: GetClearance Done") + + return handler.OkResponse(cleaResp) +}