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/{}
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 ...

View File

@ -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")

View File

@ -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();
});
});
});