change register to api/users

This commit is contained in:
wy65701436 2016-04-01 04:42:13 -07:00
parent fedbb1b274
commit 948e5ad1c5
2 changed files with 90 additions and 1 deletions

View File

@ -16,8 +16,10 @@
package api
import (
"fmt"
"net/http"
"strconv"
"strings"
"github.com/vmware/harbor/dao"
"github.com/vmware/harbor/models"
@ -31,9 +33,18 @@ type UserAPI struct {
userID int
}
const userNameMaxLen int = 20
const passwordMaxLen int = 20
const realNameMaxLen int = 20
const commentsMaxLen int = 20
// Prepare validates the URL and parms
func (ua *UserAPI) Prepare() {
if ua.Ctx.Input.IsPost() {
return
}
ua.currentUserID = ua.ValidateUser()
id := ua.Ctx.Input.Param(":id")
if id == "current" {
@ -117,6 +128,44 @@ func (ua *UserAPI) Put() { //currently only for toggle admin, so no request body
dao.ToggleUserAdminRole(userQuery)
}
// Post ...
func (ua *UserAPI) Post() {
username := strings.TrimSpace(ua.GetString("username"))
password := strings.TrimSpace(ua.GetString("password"))
email := strings.TrimSpace(ua.GetString("email"))
realname := strings.TrimSpace(ua.GetString("realname"))
comment := strings.TrimSpace(ua.GetString("comment"))
err := validateUserReq(ua)
if err != nil {
log.Errorf("Invalid user request, error: %v", err)
ua.RenderError(http.StatusBadRequest, "Invalid request for creating user")
return
}
user := models.User{Username: username, Email: email, Realname: realname, Password: password, Comment: comment}
exist, err := dao.UserExists(user, "email")
if err != nil {
log.Errorf("Error occurred in UserExists:", err)
}
if exist {
ua.RenderError(http.StatusConflict, "")
return
}
userID, err := dao.Register(user)
if err != nil {
log.Errorf("Error occurred in Register:", err)
ua.RenderError(http.StatusInternalServerError, "Internal error.")
return
}
if userID == 0 {
log.Errorf("Error happened on registing new user in db.")
ua.RenderError(http.StatusInternalServerError, "Internal error.")
}
}
// Delete ...
func (ua *UserAPI) Delete() {
exist, err := dao.IsAdminRole(ua.currentUserID)
@ -136,3 +185,42 @@ func (ua *UserAPI) Delete() {
return
}
}
func validateUserReq(ua *UserAPI) error {
userName := ua.GetString("username")
if len(userName) == 0 {
return fmt.Errorf("User name can not be empty")
}
if len(userName) > userNameMaxLen {
return fmt.Errorf("User name is too long")
}
password := ua.GetString("password")
if len(password) == 0 {
return fmt.Errorf("Password can not be empty")
}
if len(password) >= passwordMaxLen {
return fmt.Errorf("Password can is too long")
}
realName := ua.GetString("realname")
if len(realName) == 0 {
return fmt.Errorf("Real name can not be empty")
}
if len(realName) >= realNameMaxLen {
return fmt.Errorf("Real name is too long")
}
email := ua.GetString("email")
if len(email) == 0 {
return fmt.Errorf("Email can not be empty")
}
comments := ua.GetString("comment")
if len(comments) != 0 {
if len(comments) >= commentsMaxLen {
return fmt.Errorf("Comments is too long")
}
}
return nil
}

View File

@ -32,7 +32,7 @@ func init() {
beego.Router("/login", &controllers.CommonController{}, "post:Login")
beego.Router("/logout", &controllers.CommonController{}, "get:Logout")
beego.Router("/language", &controllers.CommonController{}, "get:SwitchLanguage")
beego.Router("/signUp", &controllers.CommonController{}, "post:SignUp")
// beego.Router("/signUp", &controllers.CommonController{}, "post:SignUp")
beego.Router("/userExists", &controllers.CommonController{}, "post:UserExists")
beego.Router("/reset", &controllers.CommonController{}, "post:ResetPassword")
beego.Router("/sendEmail", &controllers.CommonController{}, "get:SendEmail")
@ -55,6 +55,7 @@ func init() {
beego.Router("/api/projects/:pid/members/?:mid", &api.ProjectMemberAPI{})
beego.Router("/api/projects/?:id", &api.ProjectAPI{})
beego.Router("/api/projects/:id/logs/filter", &api.ProjectAPI{}, "post:FilterAccessLog")
beego.Router("/api/users", &api.UserAPI{})
beego.Router("/api/users/?:id", &api.UserAPI{})
beego.Router("/api/repositories", &api.RepositoryAPI{})
beego.Router("/api/repositories/tags", &api.RepositoryAPI{}, "get:GetTags")