From 7165156f2d935a879859770bda8b4611ad252864 Mon Sep 17 00:00:00 2001 From: wy65701436 Date: Thu, 14 Apr 2016 21:33:48 -0700 Subject: [PATCH] update per comments, add support for basic auth --- api/user.go | 21 ++++++++++++++++----- controllers/register.go | 28 ---------------------------- static/resources/js/register.js | 33 +++++++++++++++------------------ 3 files changed, 31 insertions(+), 51 deletions(-) diff --git a/api/user.go b/api/user.go index ed433397a..d4c0c1851 100644 --- a/api/user.go +++ b/api/user.go @@ -29,11 +29,14 @@ import ( // UserAPI handles request to /api/users/{} type UserAPI struct { BaseAPI - currentUserID int - userID int - SelfRegistration bool - IsAdmin bool - AuthMode string + currentUserID int + userID int + SelfRegistration bool + IsAdmin bool + AuthMode string + IsBasicAuth bool + UserNameInBasicAuth string + PasswordInBasicAuth string } // Prepare validates the URL and parms @@ -51,6 +54,8 @@ func (ua *UserAPI) Prepare() { } if ua.Ctx.Input.IsPost() { + ua.UserNameInBasicAuth, ua.PasswordInBasicAuth, ua.IsBasicAuth = ua.Ctx.Request.BasicAuth() + sessionUserID := ua.GetSession("userId") if sessionUserID == nil { return @@ -151,12 +156,18 @@ func (ua *UserAPI) Post() { user := models.User{} ua.DecodeJSONReq(&user) + if ua.IsBasicAuth { + user.Username = ua.UserNameInBasicAuth + user.Password = ua.PasswordInBasicAuth + } + _, err := dao.Register(user) if err != nil { log.Errorf("Error occurred in Register: %v", err) ua.RenderError(http.StatusInternalServerError, "Internal error.") return } + } // Delete ... diff --git a/controllers/register.go b/controllers/register.go index dd1a80eab..d8ed05715 100644 --- a/controllers/register.go +++ b/controllers/register.go @@ -17,7 +17,6 @@ package controllers import ( "net/http" - "strings" "github.com/vmware/harbor/dao" "github.com/vmware/harbor/models" @@ -65,33 +64,6 @@ func (ac *AddUserController) Get() { } } -// SignUp insert data into DB based on data in form. -func (cc *CommonController) SignUp() { - - if !(cc.AuthMode == "db_auth") { - cc.CustomAbort(http.StatusForbidden, "") - } - - if !(cc.SelfRegistration || cc.IsAdmin) { - log.Warning("Registration can only be used by admin role user when self-registration is off.") - cc.CustomAbort(http.StatusForbidden, "") - } - - username := strings.TrimSpace(cc.GetString("username")) - email := strings.TrimSpace(cc.GetString("email")) - realname := strings.TrimSpace(cc.GetString("realname")) - password := strings.TrimSpace(cc.GetString("password")) - comment := strings.TrimSpace(cc.GetString("comment")) - - user := models.User{Username: username, Email: email, Realname: realname, Password: password, Comment: comment} - - _, err := dao.Register(user) - if err != nil { - log.Errorf("Error occurred in Register: %v", err) - cc.CustomAbort(http.StatusInternalServerError, "Internal error.") - } -} - // UserExists checks if user exists when user input value in sign in form. func (cc *CommonController) UserExists() { target := cc.GetString("target") diff --git a/static/resources/js/register.js b/static/resources/js/register.js index ebfe60dd0..8f970946f 100644 --- a/static/resources/js/register.js +++ b/static/resources/js/register.js @@ -38,15 +38,25 @@ jQuery(function(){ var comment = $.trim($("#Comment").val()); var isAdmin = $("#isAdmin").val(); - $.ajax({ + new AjaxUtil({ url : "/api/users", - data: JSON.stringify({username: username, password: password, realname: realname, comment: comment, email: email}), + data: {"username": username, "password": password, "realname": realname, "comment": comment, "email": email}, type: "POST", - contentType: "application/json; charset=UTF-8", beforeSend: function(e){ $("#btnPageSignUp").prop("disabled", true); }, - success: function(data, status, xhr){ + error:function(jqxhr, status, error){ + $("#dlgModal") + .dialogModal({ + "title": i18n.getMessage("title_sign_up"), + "content": i18n.getMessage("internal_error"), + "callback": function(){ + return; + } + }); + }, + complete: function(xhr, status){ + $("#btnPageSignUp").prop("disabled", false); if(xhr && xhr.status == 200){ $("#dlgModal") .dialogModal({ @@ -61,21 +71,8 @@ jQuery(function(){ } }); } - }, - error:function(jqxhr, status, error){ - $("#dlgModal") - .dialogModal({ - "title": i18n.getMessage("title_sign_up"), - "content": i18n.getMessage("internal_error"), - "callback": function(){ - return; - } - }); - }, - complete: function(){ - $("#btnPageSignUp").prop("disabled", false); } - }); + }).exec(); }); }); }); \ No newline at end of file