2016-02-01 12:59:10 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
2016-02-26 11:54:14 +01:00
|
|
|
|
2016-02-01 12:59:10 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2016-04-01 13:42:13 +02:00
|
|
|
"fmt"
|
2016-02-24 07:31:52 +01:00
|
|
|
"net/http"
|
2016-02-01 12:59:10 +01:00
|
|
|
"strconv"
|
2016-04-01 13:42:13 +02:00
|
|
|
"strings"
|
2016-02-01 12:59:10 +01:00
|
|
|
|
|
|
|
"github.com/vmware/harbor/dao"
|
|
|
|
"github.com/vmware/harbor/models"
|
2016-03-28 02:50:09 +02:00
|
|
|
"github.com/vmware/harbor/utils/log"
|
2016-02-01 12:59:10 +01:00
|
|
|
)
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// UserAPI handles request to /api/users/{}
|
2016-02-01 12:59:10 +01:00
|
|
|
type UserAPI struct {
|
|
|
|
BaseAPI
|
2016-02-25 06:40:08 +01:00
|
|
|
currentUserID int
|
|
|
|
userID int
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-04-01 13:42:13 +02:00
|
|
|
const userNameMaxLen int = 20
|
|
|
|
const passwordMaxLen int = 20
|
|
|
|
const realNameMaxLen int = 20
|
|
|
|
const commentsMaxLen int = 20
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Prepare validates the URL and parms
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ua *UserAPI) Prepare() {
|
|
|
|
|
2016-04-01 13:42:13 +02:00
|
|
|
if ua.Ctx.Input.IsPost() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
ua.currentUserID = ua.ValidateUser()
|
2016-02-01 12:59:10 +01:00
|
|
|
id := ua.Ctx.Input.Param(":id")
|
|
|
|
if id == "current" {
|
2016-02-25 06:40:08 +01:00
|
|
|
ua.userID = ua.currentUserID
|
2016-02-01 12:59:10 +01:00
|
|
|
} else if len(id) > 0 {
|
|
|
|
var err error
|
2016-02-25 06:40:08 +01:00
|
|
|
ua.userID, err = strconv.Atoi(id)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Invalid user id, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusBadRequest, "Invalid user Id")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
2016-02-26 03:15:01 +01:00
|
|
|
userQuery := models.User{UserID: ua.userID}
|
2016-02-01 12:59:10 +01:00
|
|
|
u, err := dao.GetUser(userQuery)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in GetUser, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if u == nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("User with Id: %d does not exist", ua.userID)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusNotFound, "")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Get ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ua *UserAPI) Get() {
|
2016-02-25 06:40:08 +01:00
|
|
|
exist, err := dao.IsAdminRole(ua.currentUserID)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
if ua.userID == 0 { //list users
|
2016-02-01 12:59:10 +01:00
|
|
|
if !exist {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Current user, id: %d does not have admin role, can not list users", ua.currentUserID)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
username := ua.GetString("username")
|
|
|
|
userQuery := models.User{}
|
|
|
|
if len(username) > 0 {
|
|
|
|
userQuery.Username = "%" + username + "%"
|
|
|
|
}
|
|
|
|
userList, err := dao.ListUsers(userQuery)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Failed to get data from database, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.RenderError(http.StatusInternalServerError, "Failed to query from database")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ua.Data["json"] = userList
|
|
|
|
|
2016-02-25 06:40:08 +01:00
|
|
|
} else if ua.userID == ua.currentUserID || exist {
|
2016-02-26 03:15:01 +01:00
|
|
|
userQuery := models.User{UserID: ua.userID}
|
2016-02-01 12:59:10 +01:00
|
|
|
u, err := dao.GetUser(userQuery)
|
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in GetUser, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
ua.Data["json"] = u
|
|
|
|
} else {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Current user, id: %d does not have admin role, can not view other user's detail", ua.currentUserID)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
ua.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Put ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ua *UserAPI) Put() { //currently only for toggle admin, so no request body
|
2016-02-25 06:40:08 +01:00
|
|
|
exist, err := dao.IsAdminRole(ua.currentUserID)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if !exist {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Warningf("current user, id: %d does not have admin role, can not update other user's role", ua.currentUserID)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-26 03:15:01 +01:00
|
|
|
userQuery := models.User{UserID: ua.userID}
|
2016-02-01 12:59:10 +01:00
|
|
|
dao.ToggleUserAdminRole(userQuery)
|
|
|
|
}
|
|
|
|
|
2016-04-01 13:42:13 +02:00
|
|
|
// 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.")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-26 11:35:55 +01:00
|
|
|
// Delete ...
|
2016-02-01 12:59:10 +01:00
|
|
|
func (ua *UserAPI) Delete() {
|
2016-02-25 06:40:08 +01:00
|
|
|
exist, err := dao.IsAdminRole(ua.currentUserID)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Error occurred in IsAdminRole, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.CustomAbort(http.StatusInternalServerError, "Internal error.")
|
2016-02-01 12:59:10 +01:00
|
|
|
}
|
|
|
|
if !exist {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Warningf("current user, id: %d does not have admin role, can not remove user", ua.currentUserID)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.RenderError(http.StatusForbidden, "User does not have admin role")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
2016-02-25 06:40:08 +01:00
|
|
|
err = dao.DeleteUser(ua.userID)
|
2016-02-01 12:59:10 +01:00
|
|
|
if err != nil {
|
2016-03-28 02:50:09 +02:00
|
|
|
log.Errorf("Failed to delete data from database, error: %v", err)
|
2016-02-24 07:31:52 +01:00
|
|
|
ua.RenderError(http.StatusInternalServerError, "Failed to delete User")
|
2016-02-01 12:59:10 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-01 13:42:13 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|