2015-01-15 05:16:54 -06:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2020-08-13 07:38:54 -05:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-11-29 03:18:01 -06:00
|
|
|
"net/http"
|
2022-01-14 10:55:57 -06:00
|
|
|
"strconv"
|
2020-08-13 07:38:54 -05:00
|
|
|
|
2015-02-10 08:36:51 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/dtos"
|
2021-01-15 07:43:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/response"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2019-05-21 06:52:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2015-02-23 04:24:22 -06:00
|
|
|
"github.com/grafana/grafana/pkg/util"
|
2021-11-29 03:18:01 -06:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2015-01-15 05:16:54 -06:00
|
|
|
)
|
|
|
|
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AdminCreateUser(c *models.ReqContext) response.Response {
|
|
|
|
form := dtos.AdminCreateUserForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2019-05-21 06:52:49 -05:00
|
|
|
cmd := models.CreateUserCommand{
|
2015-02-10 08:36:51 -06:00
|
|
|
Login: form.Login,
|
|
|
|
Email: form.Email,
|
|
|
|
Password: form.Password,
|
|
|
|
Name: form.Name,
|
2020-04-15 04:11:45 -05:00
|
|
|
OrgId: form.OrgId,
|
2015-02-10 08:36:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmd.Login) == 0 {
|
|
|
|
cmd.Login = cmd.Email
|
|
|
|
if len(cmd.Login) == 0 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Validation error, need specify either username or email", nil)
|
2015-02-10 08:36:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cmd.Password) < 4 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "Password is missing or too short", nil)
|
2015-02-10 08:36:51 -06:00
|
|
|
}
|
|
|
|
|
2021-03-18 11:16:56 -05:00
|
|
|
user, err := hs.Login.CreateUser(cmd)
|
|
|
|
if err != nil {
|
2020-08-13 07:38:54 -05:00
|
|
|
if errors.Is(err, models.ErrOrgNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, err.Error(), nil)
|
2020-08-13 07:38:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors.Is(err, models.ErrUserAlreadyExists) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(412, fmt.Sprintf("User with email '%s' or username '%s' already exists", form.Email, form.Login), err)
|
2020-04-15 04:11:45 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "failed to create user", err)
|
2015-02-10 08:36:51 -06:00
|
|
|
}
|
|
|
|
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiAdminUserCreate.Inc()
|
2015-03-22 14:14:00 -05:00
|
|
|
|
2019-05-21 06:52:49 -05:00
|
|
|
result := models.UserIdDTO{
|
2015-11-16 09:28:38 -06:00
|
|
|
Message: "User created",
|
|
|
|
Id: user.Id,
|
|
|
|
}
|
2015-11-16 08:55:02 -06:00
|
|
|
|
2022-04-15 07:01:58 -05:00
|
|
|
return response.JSON(http.StatusOK, result)
|
2015-02-10 08:36:51 -06:00
|
|
|
}
|
|
|
|
|
2022-02-04 12:45:42 -06:00
|
|
|
func (hs *HTTPServer) AdminUpdateUserPassword(c *models.ReqContext) response.Response {
|
2021-11-29 03:18:01 -06:00
|
|
|
form := dtos.AdminUpdateUserPasswordForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
|
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2015-02-23 04:24:22 -06:00
|
|
|
|
|
|
|
if len(form.Password) < 4 {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "New password too short", nil)
|
2015-02-23 04:24:22 -06:00
|
|
|
}
|
|
|
|
|
2019-05-21 06:52:49 -05:00
|
|
|
userQuery := models.GetUserByIdQuery{Id: userID}
|
2015-02-23 04:24:22 -06:00
|
|
|
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.SQLStore.GetUserById(c.Req.Context(), &userQuery); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not read user from database", err)
|
2015-02-23 04:24:22 -06:00
|
|
|
}
|
|
|
|
|
2019-10-23 03:40:12 -05:00
|
|
|
passwordHashed, err := util.EncodePassword(form.Password, userQuery.Result.Salt)
|
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not encode password", err)
|
2019-10-23 03:40:12 -05:00
|
|
|
}
|
2015-02-23 04:24:22 -06:00
|
|
|
|
2019-05-21 06:52:49 -05:00
|
|
|
cmd := models.ChangeUserPasswordCommand{
|
2018-03-22 06:37:35 -05:00
|
|
|
UserId: userID,
|
2015-02-23 04:24:22 -06:00
|
|
|
NewPassword: passwordHashed,
|
|
|
|
}
|
|
|
|
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.SQLStore.ChangeUserPassword(c.Req.Context(), &cmd); err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update user password", err)
|
2015-02-23 04:24:22 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User password updated")
|
2015-02-23 04:24:22 -06:00
|
|
|
}
|
|
|
|
|
2018-12-01 11:28:10 -06:00
|
|
|
// PUT /api/admin/users/:id/permissions
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AdminUpdateUserPermissions(c *models.ReqContext) response.Response {
|
|
|
|
form := dtos.AdminUpdateUserPermissionsForm{}
|
|
|
|
if err := web.Bind(c.Req, &form); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2015-02-26 08:43:48 -06:00
|
|
|
|
2022-02-03 02:20:20 -06:00
|
|
|
err = hs.SQLStore.UpdateUserPermissions(userID, form.IsGrafanaAdmin)
|
2021-03-19 03:14:14 -05:00
|
|
|
if err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrLastGrafanaAdmin) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, models.ErrLastGrafanaAdmin.Error(), nil)
|
2018-12-01 11:28:10 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to update user permissions", err)
|
2015-02-26 08:43:48 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User permissions updated")
|
2015-02-26 08:43:48 -06:00
|
|
|
}
|
|
|
|
|
2022-02-04 12:45:42 -06:00
|
|
|
func (hs *HTTPServer) AdminDeleteUser(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2015-02-11 09:47:22 -06:00
|
|
|
|
2019-05-21 06:52:49 -05:00
|
|
|
cmd := models.DeleteUserCommand{UserId: userID}
|
2015-02-11 09:47:22 -06:00
|
|
|
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.SQLStore.DeleteUser(c.Req.Context(), &cmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrUserNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, models.ErrUserNotFound.Error(), nil)
|
2020-01-10 04:43:44 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to delete user", err)
|
2015-02-11 09:47:22 -06:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User deleted")
|
2015-02-11 09:47:22 -06:00
|
|
|
}
|
2019-03-08 08:15:38 -06:00
|
|
|
|
2019-05-21 06:52:49 -05:00
|
|
|
// POST /api/admin/users/:id/disable
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) AdminDisableUser(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2019-05-21 06:52:49 -05:00
|
|
|
|
|
|
|
// External users shouldn't be disabled from API
|
|
|
|
authInfoQuery := &models.GetAuthInfoQuery{UserId: userID}
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.authInfoService.GetAuthInfo(c.Req.Context(), authInfoQuery); !errors.Is(err, models.ErrUserNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not disable external user", nil)
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
disableCmd := models.DisableUserCommand{UserId: userID, IsDisabled: true}
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.SQLStore.DisableUser(c.Req.Context(), &disableCmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrUserNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, models.ErrUserNotFound.Error(), nil)
|
2020-01-10 04:43:44 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to disable user", err)
|
2019-05-23 07:54:47 -05:00
|
|
|
}
|
|
|
|
|
2022-01-14 10:55:57 -06:00
|
|
|
err = hs.AuthTokenService.RevokeAllUserTokens(c.Req.Context(), userID)
|
2019-05-23 07:54:47 -05:00
|
|
|
if err != nil {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to disable user", err)
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User disabled")
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/admin/users/:id/enable
|
2022-02-04 12:45:42 -06:00
|
|
|
func (hs *HTTPServer) AdminEnableUser(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2019-05-21 06:52:49 -05:00
|
|
|
|
|
|
|
// External users shouldn't be disabled from API
|
|
|
|
authInfoQuery := &models.GetAuthInfoQuery{UserId: userID}
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.authInfoService.GetAuthInfo(c.Req.Context(), authInfoQuery); !errors.Is(err, models.ErrUserNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Could not enable external user", nil)
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
disableCmd := models.DisableUserCommand{UserId: userID, IsDisabled: false}
|
2022-02-04 12:45:42 -06:00
|
|
|
if err := hs.SQLStore.DisableUser(c.Req.Context(), &disableCmd); err != nil {
|
2020-11-19 06:34:28 -06:00
|
|
|
if errors.Is(err, models.ErrUserNotFound) {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(404, models.ErrUserNotFound.Error(), nil)
|
2020-01-10 04:43:44 -06:00
|
|
|
}
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(500, "Failed to enable user", err)
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Success("User enabled")
|
2019-05-21 06:52:49 -05:00
|
|
|
}
|
|
|
|
|
2019-03-08 08:15:38 -06:00
|
|
|
// POST /api/admin/users/:id/logout
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) AdminLogoutUser(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2019-03-08 08:15:38 -06:00
|
|
|
|
|
|
|
if c.UserId == userID {
|
2021-01-15 07:43:20 -06:00
|
|
|
return response.Error(400, "You cannot logout yourself", nil)
|
2019-03-08 08:15:38 -06:00
|
|
|
}
|
|
|
|
|
2020-11-05 08:37:11 -06:00
|
|
|
return hs.logoutUserFromAllDevicesInternal(c.Req.Context(), userID)
|
2019-03-08 08:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// GET /api/admin/users/:id/auth-tokens
|
2021-01-15 07:43:20 -06:00
|
|
|
func (hs *HTTPServer) AdminGetUserAuthTokens(c *models.ReqContext) response.Response {
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2020-11-05 08:37:11 -06:00
|
|
|
return hs.getUserAuthTokensInternal(c, userID)
|
2019-03-08 08:15:38 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// POST /api/admin/users/:id/revoke-auth-token
|
2021-11-29 03:18:01 -06:00
|
|
|
func (hs *HTTPServer) AdminRevokeUserAuthToken(c *models.ReqContext) response.Response {
|
|
|
|
cmd := models.RevokeAuthTokenCmd{}
|
|
|
|
if err := web.Bind(c.Req, &cmd); err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "bad request data", err)
|
|
|
|
}
|
2022-01-14 10:55:57 -06:00
|
|
|
userID, err := strconv.ParseInt(web.Params(c.Req)[":id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return response.Error(http.StatusBadRequest, "id is invalid", err)
|
|
|
|
}
|
2020-11-05 08:37:11 -06:00
|
|
|
return hs.revokeUserAuthTokenInternal(c, userID, cmd)
|
2019-03-08 08:15:38 -06:00
|
|
|
}
|