2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// 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.
|
|
|
|
|
2021-09-22 18:48:01 +00:00
|
|
|
package api
|
2015-04-08 22:43:59 +00:00
|
|
|
|
|
|
|
import (
|
2016-05-03 00:47:58 +00:00
|
|
|
"encoding/base32"
|
2015-09-30 01:21:17 +00:00
|
|
|
"net/http"
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-03 00:47:58 +00:00
|
|
|
"github.com/gorilla/securecookie"
|
2023-03-19 17:32:19 +00:00
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/router/middleware/session"
|
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/store"
|
2015-04-08 22:43:59 +00:00
|
|
|
)
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetUsers
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary List users
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Description Returns all registered, active users in the system. Requires admin rights.
|
|
|
|
// @Router /users [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {array} User
|
|
|
|
// @Tags Users
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param page query int false "for response pagination, page offset number" default(1)
|
|
|
|
// @Param perPage query int false "for response pagination, max items per page" default(50)
|
2015-04-08 22:43:59 +00:00
|
|
|
func GetUsers(c *gin.Context) {
|
2023-04-30 01:40:13 +00:00
|
|
|
users, err := store.FromContext(c).GetUserList(session.Pagination(c))
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2023-09-02 11:31:10 +00:00
|
|
|
c.String(http.StatusInternalServerError, "Error getting user list. %s", err)
|
2016-05-03 00:47:58 +00:00
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2023-09-02 11:31:10 +00:00
|
|
|
c.JSON(http.StatusOK, users)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// GetUser
|
|
|
|
//
|
|
|
|
// @Summary Get a user
|
|
|
|
// @Description Returns a user with the specified login name. Requires admin rights.
|
|
|
|
// @Router /users/{login} [get]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} User
|
|
|
|
// @Tags Users
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param login path string true "the user's login name"
|
2015-04-08 22:43:59 +00:00
|
|
|
func GetUser(c *gin.Context) {
|
2021-10-28 09:12:58 +00:00
|
|
|
user, err := store.FromContext(c).GetUserLogin(c.Param("login"))
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, err)
|
2016-05-02 19:21:25 +00:00
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2023-09-02 11:31:10 +00:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PatchUser
|
|
|
|
//
|
2024-05-01 09:50:41 +00:00
|
|
|
// @Summary Update a user
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Description Changes the data of an existing user. Requires admin rights.
|
|
|
|
// @Router /users/{login} [patch]
|
|
|
|
// @Produce json
|
|
|
|
// @Accept json
|
|
|
|
// @Success 200 {object} User
|
|
|
|
// @Tags Users
|
2023-12-24 14:50:01 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param login path string true "the user's login name"
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param user body User true "the user's data"
|
2015-09-30 01:21:17 +00:00
|
|
|
func PatchUser(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
user, err := _store.GetUserLogin(c.Param("login"))
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2023-03-18 20:21:20 +00:00
|
|
|
|
|
|
|
// TODO: allow to change login (currently used as primary key)
|
|
|
|
// TODO: disallow to change login, email, avatar if the user is using oauth
|
|
|
|
user.Email = in.Email
|
|
|
|
user.Avatar = in.Avatar
|
|
|
|
user.Admin = in.Admin
|
2015-09-30 01:21:17 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
err = _store.UpdateUser(user)
|
2015-09-30 01:21:17 +00:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 20:15:28 +00:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
2015-04-08 22:43:59 +00:00
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// PostUser
|
|
|
|
//
|
|
|
|
// @Summary Create a user
|
|
|
|
// @Description Creates a new user account with the specified external login. Requires admin rights.
|
|
|
|
// @Router /users [post]
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} User
|
|
|
|
// @Tags Users
|
2023-12-24 14:50:01 +00:00
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Param user body User true "the user's data"
|
2015-09-30 01:21:17 +00:00
|
|
|
func PostUser(c *gin.Context) {
|
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
|
|
|
if err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2016-05-02 19:21:25 +00:00
|
|
|
user := &model.User{
|
|
|
|
Login: in.Login,
|
|
|
|
Email: in.Email,
|
|
|
|
Avatar: in.Avatar,
|
2016-05-03 00:47:58 +00:00
|
|
|
Hash: base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
),
|
2024-04-16 06:04:55 +00:00
|
|
|
ForgeID: 1, // TODO: replace with forge id when multiple forges are supported
|
|
|
|
ForgeRemoteID: model.ForgeRemoteID("0"), // TODO: search for the user in the forge and get the remote id
|
2016-05-02 19:21:25 +00:00
|
|
|
}
|
2017-07-17 04:01:35 +00:00
|
|
|
if err = user.Validate(); err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-10-28 09:12:58 +00:00
|
|
|
if err = store.FromContext(c).CreateUser(user); err != nil {
|
2015-09-30 01:21:17 +00:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2016-03-30 20:15:28 +00:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
|
|
|
|
2023-06-03 19:38:36 +00:00
|
|
|
// DeleteUser
|
|
|
|
//
|
|
|
|
// @Summary Delete a user
|
|
|
|
// @Description Deletes the given user. Requires admin rights.
|
|
|
|
// @Router /users/{login} [delete]
|
2023-09-02 11:31:10 +00:00
|
|
|
// @Produce plain
|
|
|
|
// @Success 204
|
2023-06-03 19:38:36 +00:00
|
|
|
// @Tags Users
|
|
|
|
// @Param Authorization header string true "Insert your personal access token" default(Bearer <personal access token>)
|
|
|
|
// @Param login path string true "the user's login name"
|
2015-04-08 22:43:59 +00:00
|
|
|
func DeleteUser(c *gin.Context) {
|
2021-12-01 13:22:06 +00:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 09:12:58 +00:00
|
|
|
|
2021-12-01 13:22:06 +00:00
|
|
|
user, err := _store.GetUserLogin(c.Param("login"))
|
2015-04-08 22:43:59 +00:00
|
|
|
if err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, err)
|
2015-04-08 22:43:59 +00:00
|
|
|
return
|
|
|
|
}
|
2021-12-01 13:22:06 +00:00
|
|
|
if err = _store.DeleteUser(user); err != nil {
|
2024-01-10 21:56:42 +00:00
|
|
|
handleDBError(c, err)
|
2016-05-02 19:21:25 +00:00
|
|
|
return
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|
2023-09-02 11:31:10 +00:00
|
|
|
c.Status(http.StatusNoContent)
|
2015-04-08 22:43:59 +00:00
|
|
|
}
|