update per comments, add support for basic auth

This commit is contained in:
wy65701436 2016-04-14 21:33:48 -07:00
parent 943d6ebd4c
commit 7165156f2d
3 changed files with 31 additions and 51 deletions

View File

@ -29,11 +29,14 @@ import (
// UserAPI handles request to /api/users/{} // UserAPI handles request to /api/users/{}
type UserAPI struct { type UserAPI struct {
BaseAPI BaseAPI
currentUserID int currentUserID int
userID int userID int
SelfRegistration bool SelfRegistration bool
IsAdmin bool IsAdmin bool
AuthMode string AuthMode string
IsBasicAuth bool
UserNameInBasicAuth string
PasswordInBasicAuth string
} }
// Prepare validates the URL and parms // Prepare validates the URL and parms
@ -51,6 +54,8 @@ func (ua *UserAPI) Prepare() {
} }
if ua.Ctx.Input.IsPost() { if ua.Ctx.Input.IsPost() {
ua.UserNameInBasicAuth, ua.PasswordInBasicAuth, ua.IsBasicAuth = ua.Ctx.Request.BasicAuth()
sessionUserID := ua.GetSession("userId") sessionUserID := ua.GetSession("userId")
if sessionUserID == nil { if sessionUserID == nil {
return return
@ -151,12 +156,18 @@ func (ua *UserAPI) Post() {
user := models.User{} user := models.User{}
ua.DecodeJSONReq(&user) ua.DecodeJSONReq(&user)
if ua.IsBasicAuth {
user.Username = ua.UserNameInBasicAuth
user.Password = ua.PasswordInBasicAuth
}
_, err := dao.Register(user) _, err := dao.Register(user)
if err != nil { if err != nil {
log.Errorf("Error occurred in Register: %v", err) log.Errorf("Error occurred in Register: %v", err)
ua.RenderError(http.StatusInternalServerError, "Internal error.") ua.RenderError(http.StatusInternalServerError, "Internal error.")
return return
} }
} }
// Delete ... // Delete ...

View File

@ -17,7 +17,6 @@ package controllers
import ( import (
"net/http" "net/http"
"strings"
"github.com/vmware/harbor/dao" "github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models" "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. // UserExists checks if user exists when user input value in sign in form.
func (cc *CommonController) UserExists() { func (cc *CommonController) UserExists() {
target := cc.GetString("target") target := cc.GetString("target")

View File

@ -38,15 +38,25 @@ jQuery(function(){
var comment = $.trim($("#Comment").val()); var comment = $.trim($("#Comment").val());
var isAdmin = $("#isAdmin").val(); var isAdmin = $("#isAdmin").val();
$.ajax({ new AjaxUtil({
url : "/api/users", 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", type: "POST",
contentType: "application/json; charset=UTF-8",
beforeSend: function(e){ beforeSend: function(e){
$("#btnPageSignUp").prop("disabled", true); $("#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){ if(xhr && xhr.status == 200){
$("#dlgModal") $("#dlgModal")
.dialogModal({ .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();
}); });
}); });
}); });