2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 20:48:01 +02:00
|
|
|
package api
|
2015-04-09 00:43:59 +02:00
|
|
|
|
|
|
|
import (
|
2016-05-03 02:47:58 +02:00
|
|
|
"encoding/base32"
|
2015-09-30 03:21:17 +02:00
|
|
|
"net/http"
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
"github.com/gin-gonic/gin"
|
2016-05-03 02:47:58 +02:00
|
|
|
"github.com/gorilla/securecookie"
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2021-09-27 19:51:55 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/model"
|
2021-09-23 13:33:59 +02:00
|
|
|
"github.com/woodpecker-ci/woodpecker/server/store"
|
2015-04-09 00:43:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func GetUsers(c *gin.Context) {
|
2021-10-28 11:12:58 +02:00
|
|
|
users, err := store.FromContext(c).GetUserList()
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2016-03-30 22:15:28 +02:00
|
|
|
c.String(500, "Error getting user list. %s", err)
|
2016-05-03 02:47:58 +02:00
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2016-05-03 02:47:58 +02:00
|
|
|
c.JSON(200, users)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetUser(c *gin.Context) {
|
2021-10-28 11:12:58 +02:00
|
|
|
user, err := store.FromContext(c).GetUserLogin(c.Param("login"))
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2016-03-30 22:15:28 +02:00
|
|
|
c.String(404, "Cannot find user. %s", err)
|
2016-05-02 21:21:25 +02:00
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2016-05-02 21:21:25 +02:00
|
|
|
c.JSON(200, user)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
func PatchUser(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 11:12:58 +02:00
|
|
|
|
2015-09-30 03:21:17 +02:00
|
|
|
in := &model.User{}
|
|
|
|
err := c.Bind(in)
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.AbortWithStatus(http.StatusBadRequest)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
user, err := _store.GetUserLogin(c.Param("login"))
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusNotFound)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
user.Active = in.Active
|
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
err = _store.UpdateUser(user)
|
2015-09-30 03:21:17 +02:00
|
|
|
if err != nil {
|
|
|
|
c.AbortWithStatus(http.StatusConflict)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-03-30 22:15:28 +02:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2015-04-09 00:43:59 +02:00
|
|
|
|
2015-09-30 03:21:17 +02: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-09 00:43:59 +02:00
|
|
|
}
|
2016-05-02 21:21:25 +02:00
|
|
|
user := &model.User{
|
|
|
|
Active: true,
|
|
|
|
Login: in.Login,
|
|
|
|
Email: in.Email,
|
|
|
|
Avatar: in.Avatar,
|
2016-05-03 02:47:58 +02:00
|
|
|
Hash: base32.StdEncoding.EncodeToString(
|
|
|
|
securecookie.GenerateRandomKey(32),
|
|
|
|
),
|
2016-05-02 21:21:25 +02:00
|
|
|
}
|
2017-07-17 06:01:35 +02:00
|
|
|
if err = user.Validate(); err != nil {
|
|
|
|
c.String(http.StatusBadRequest, err.Error())
|
|
|
|
return
|
|
|
|
}
|
2021-10-28 11:12:58 +02:00
|
|
|
if err = store.FromContext(c).CreateUser(user); err != nil {
|
2015-09-30 03:21:17 +02:00
|
|
|
c.String(http.StatusInternalServerError, err.Error())
|
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2016-03-30 22:15:28 +02:00
|
|
|
c.JSON(http.StatusOK, user)
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteUser(c *gin.Context) {
|
2021-12-01 15:22:06 +02:00
|
|
|
_store := store.FromContext(c)
|
2021-10-28 11:12:58 +02:00
|
|
|
|
2021-12-01 15:22:06 +02:00
|
|
|
user, err := _store.GetUserLogin(c.Param("login"))
|
2015-04-09 00:43:59 +02:00
|
|
|
if err != nil {
|
2016-03-30 22:15:28 +02:00
|
|
|
c.String(404, "Cannot find user. %s", err)
|
2015-04-09 00:43:59 +02:00
|
|
|
return
|
|
|
|
}
|
2021-12-01 15:22:06 +02:00
|
|
|
if err = _store.DeleteUser(user); err != nil {
|
2016-03-30 22:15:28 +02:00
|
|
|
c.String(500, "Error deleting user. %s", err)
|
2016-05-02 21:21:25 +02:00
|
|
|
return
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|
2016-05-02 21:21:25 +02:00
|
|
|
c.String(200, "")
|
2015-04-09 00:43:59 +02:00
|
|
|
}
|