From 12cd73367871874376a28de2acf360cbd6577728 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Wed, 20 Dec 2017 16:06:28 +0800 Subject: [PATCH] Remove useless code from UI router and API Some URLs are not used on UI, so they are removed. And the validation code of API is removed as we use the security context approach. fix test issue --- src/common/api/base.go | 58 +----------------------------------------- src/ui/router.go | 4 --- 2 files changed, 1 insertion(+), 61 deletions(-) diff --git a/src/common/api/base.go b/src/common/api/base.go index 57147d104c..8d6de31fc2 100644 --- a/src/common/api/base.go +++ b/src/common/api/base.go @@ -21,11 +21,8 @@ import ( "strconv" "github.com/astaxie/beego/validation" - "github.com/vmware/harbor/src/common/dao" - "github.com/vmware/harbor/src/common/models" http_error "github.com/vmware/harbor/src/common/utils/error" "github.com/vmware/harbor/src/common/utils/log" - "github.com/vmware/harbor/src/ui/auth" "github.com/astaxie/beego" ) @@ -122,7 +119,7 @@ func (b *BaseAPI) DecodeJSONReq(v interface{}) { err := json.Unmarshal(b.Ctx.Input.CopyBody(1<<32), v) if err != nil { log.Errorf("Error while decoding the json request, error: %v, %v", - err, string(b.Ctx.Input.CopyBody(1<<32)[:])) + err, string(b.Ctx.Input.CopyBody(1 << 32)[:])) b.CustomAbort(http.StatusBadRequest, "Invalid json request") } } @@ -151,59 +148,6 @@ func (b *BaseAPI) DecodeJSONReqAndValidate(v interface{}) { b.Validate(v) } -// ValidateUser checks if the request triggered by a valid user -// TODO remove -func (b *BaseAPI) ValidateUser() int { - userID, needsCheck, ok := b.GetUserIDForRequest() - if !ok { - log.Warning("No user id in session, canceling request") - b.CustomAbort(http.StatusUnauthorized, "") - } - if needsCheck { - u, err := dao.GetUser(models.User{UserID: userID}) - if err != nil { - log.Errorf("Error occurred in GetUser, error: %v", err) - b.CustomAbort(http.StatusInternalServerError, "Internal error.") - } - if u == nil { - log.Warningf("User was deleted already, user id: %d, canceling request.", userID) - b.CustomAbort(http.StatusUnauthorized, "") - } - } - return userID -} - -// GetUserIDForRequest tries to get user ID from basic auth header and session. -// It returns the user ID, whether need further verification(when the id is from session) and if the action is successful -// TODO remove -func (b *BaseAPI) GetUserIDForRequest() (int, bool, bool) { - username, password, ok := b.Ctx.Request.BasicAuth() - if ok { - log.Infof("Requst with Basic Authentication header, username: %s", username) - user, err := auth.Login(models.AuthModel{ - Principal: username, - Password: password, - }) - if err != nil { - log.Errorf("Error while trying to login, username: %s, error: %v", username, err) - user = nil - } - if user != nil { - b.SetSession("userId", user.UserID) - b.SetSession("username", user.Username) - // User login successfully no further check required. - return user.UserID, false, true - } - } - sessionUserID, ok := b.GetSession("userId").(int) - if ok { - // The ID is from session - return sessionUserID, true, true - } - log.Debug("No valid user id in session.") - return 0, false, false -} - // Redirect does redirection to resource URI with http header status code. func (b *BaseAPI) Redirect(statusCode int, resouceID string) { requestURI := b.Ctx.Request.RequestURI diff --git a/src/ui/router.go b/src/ui/router.go index 105226f65d..51c031653b 100644 --- a/src/ui/router.go +++ b/src/ui/router.go @@ -32,15 +32,11 @@ func initRouters() { //Page Controllers: beego.Router("/", &controllers.IndexController{}) - beego.Router("/sign-in", &controllers.IndexController{}) - beego.Router("/sign-up", &controllers.IndexController{}) beego.Router("/reset_password", &controllers.IndexController{}) beego.Router("/harbor", &controllers.IndexController{}) beego.Router("/harbor/sign-in", &controllers.IndexController{}) - beego.Router("/harbor/sign-up", &controllers.IndexController{}) - beego.Router("/harbor/dashboard", &controllers.IndexController{}) beego.Router("/harbor/projects", &controllers.IndexController{}) beego.Router("/harbor/projects/:id/repositories", &controllers.IndexController{}) beego.Router("/harbor/projects/:id/repositories/*", &controllers.IndexController{})