github.com/infraboard/keyauth@v0.8.1/apps/application/http/application.go (about) 1 package http 2 3 import ( 4 "net/http" 5 6 "github.com/infraboard/mcube/exception" 7 "github.com/infraboard/mcube/http/context" 8 "github.com/infraboard/mcube/http/request" 9 "github.com/infraboard/mcube/http/response" 10 11 "github.com/infraboard/keyauth/apps/application" 12 "github.com/infraboard/keyauth/apps/token" 13 ) 14 15 func (h *handler) QueryApplication(w http.ResponseWriter, r *http.Request) { 16 ctx := context.GetContext(r) 17 tk := ctx.AuthInfo.(*token.Token) 18 19 page := request.NewPageRequestFromHTTP(r) 20 req := application.NewQueryApplicationRequest(page) 21 req.Account = tk.Account 22 23 apps, err := h.service.QueryApplication(r.Context(), req) 24 if err != nil { 25 response.Failed(w, err) 26 return 27 } 28 29 response.Success(w, apps) 30 } 31 32 // CreateApplication 创建主账号 33 func (h *handler) CreateApplication(w http.ResponseWriter, r *http.Request) { 34 ctx := context.GetContext(r) 35 tk := ctx.AuthInfo.(*token.Token) 36 37 req := application.NewCreateApplicatonRequest() 38 if err := request.GetDataFromRequest(r, req); err != nil { 39 response.Failed(w, err) 40 return 41 } 42 req.UpdateOwner(tk) 43 44 d, err := h.service.CreateApplication(r.Context(), req) 45 if err != nil { 46 response.Failed(w, err) 47 return 48 } 49 50 response.Success(w, d) 51 } 52 53 func (h *handler) GetApplication(w http.ResponseWriter, r *http.Request) { 54 ctx := context.GetContext(r) 55 tk := ctx.AuthInfo.(*token.Token) 56 57 req := application.NewDescriptApplicationRequest() 58 req.Id = ctx.PS.ByName("id") 59 60 ins, err := h.service.DescribeApplication(r.Context(), req) 61 if err != nil { 62 response.Failed(w, err) 63 return 64 } 65 66 if !ins.IsOwner(tk.Account) { 67 response.Failed(w, exception.NewPermissionDeny("this application is not yours")) 68 return 69 } 70 71 response.Success(w, ins) 72 } 73 74 // DestroyPrimaryAccount 注销账号 75 func (h *handler) DestroyApplication(w http.ResponseWriter, r *http.Request) { 76 ctx := context.GetContext(r) 77 tk := ctx.AuthInfo.(*token.Token) 78 79 descReq := application.NewDescriptApplicationRequest() 80 descReq.Id = ctx.PS.ByName("id") 81 82 ins, err := h.service.DescribeApplication(r.Context(), descReq) 83 84 if err != nil { 85 response.Failed(w, err) 86 return 87 } 88 89 if !ins.IsOwner(tk.Account) { 90 response.Failed(w, exception.NewPermissionDeny("this application is not yours")) 91 return 92 } 93 94 req := application.NewDeleteApplicationRequestWithID(ctx.PS.ByName("id")) 95 96 _, err = h.service.DeleteApplication(r.Context(), req) 97 98 if err != nil { 99 response.Failed(w, err) 100 return 101 } 102 103 response.Success(w, "delete ok") 104 }