diff --git a/api/user.go b/api/user.go index 4a8101ba8..4e6136214 100644 --- a/api/user.go +++ b/api/user.go @@ -16,7 +16,6 @@ package api import ( - "fmt" "net/http" "strconv" @@ -32,19 +31,6 @@ type UserAPI struct { userID int } -type userReq struct { - UserName string `json:"username"` - Password string `json:"password"` - Realname string `json:"realname"` - Email string `json:"email"` - Comment string `json:"comment"` -} - -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() { @@ -137,37 +123,15 @@ func (ua *UserAPI) Put() { //currently only for toggle admin, so no request body // Post ... func (ua *UserAPI) Post() { - var req userReq - ua.DecodeJSONReq(&req) + user := models.User{} + ua.DecodeJSONReq(&user) - err := validateUserReq(req) - 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: req.UserName, Email: req.Email, Realname: req.Realname, Password: req.Password, Comment: req.Comment} - exist, err := dao.UserExists(user, "email") - if err != nil { - log.Errorf("Error occurred in UserExists: %v", err) - } - if exist { - ua.RenderError(http.StatusConflict, "") - return - } - - userID, err := dao.Register(user) + _, err := dao.Register(user) if err != nil { log.Errorf("Error occurred in Register: %v", 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 ... @@ -189,36 +153,3 @@ func (ua *UserAPI) Delete() { return } } - -func validateUserReq(req userReq) error { - userName := req.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 := req.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 := req.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 := req.Email - if len(email) == 0 { - return fmt.Errorf("Email can not be empty") - } - - return nil -} diff --git a/models/user.go b/models/user.go index 82b8bc221..29fb41c13 100644 --- a/models/user.go +++ b/models/user.go @@ -22,11 +22,11 @@ import ( // User holds the details of a user. type User struct { UserID int `orm:"column(user_id)" json:"UserId"` - Username string `orm:"column(username)"` - Email string `orm:"column(email)"` - Password string `orm:"column(password)"` - Realname string `orm:"column(realname)"` - Comment string `orm:"column(comment)"` + Username string `orm:"column(username)" json:"username"` + Email string `orm:"column(email)" json:"email"` + Password string `orm:"column(password)" json:"password"` + Realname string `orm:"column(realname)" json:"realname"` + Comment string `orm:"column(comment)" json:"comment"` Deleted int `orm:"column(deleted)"` Rolename string RoleID int `json:"RoleId"`