2016-12-18 05:21:29 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2017-03-12 16:24:15 +00:00
|
|
|
"strconv"
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
"github.com/portainer/portainer"
|
|
|
|
|
|
|
|
"encoding/json"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2016-12-25 20:34:02 +00:00
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
|
|
|
"github.com/gorilla/mux"
|
2016-12-18 05:21:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// UserHandler represents an HTTP API handler for managing users.
|
|
|
|
type UserHandler struct {
|
|
|
|
*mux.Router
|
2017-03-12 16:24:15 +00:00
|
|
|
Logger *log.Logger
|
|
|
|
UserService portainer.UserService
|
|
|
|
ResourceControlService portainer.ResourceControlService
|
|
|
|
CryptoService portainer.CryptoService
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewUserHandler returns a new instance of UserHandler.
|
2017-03-12 16:24:15 +00:00
|
|
|
func NewUserHandler(mw *middleWareService) *UserHandler {
|
2016-12-18 05:21:29 +00:00
|
|
|
h := &UserHandler{
|
2017-03-12 16:24:15 +00:00
|
|
|
Router: mux.NewRouter(),
|
|
|
|
Logger: log.New(os.Stderr, "", log.LstdFlags),
|
|
|
|
}
|
|
|
|
h.Handle("/users",
|
|
|
|
mw.administrator(http.HandlerFunc(h.handlePostUsers))).Methods(http.MethodPost)
|
|
|
|
h.Handle("/users",
|
|
|
|
mw.administrator(http.HandlerFunc(h.handleGetUsers))).Methods(http.MethodGet)
|
|
|
|
h.Handle("/users/{id}",
|
|
|
|
mw.administrator(http.HandlerFunc(h.handleGetUser))).Methods(http.MethodGet)
|
|
|
|
h.Handle("/users/{id}",
|
|
|
|
mw.authenticated(http.HandlerFunc(h.handlePutUser))).Methods(http.MethodPut)
|
|
|
|
h.Handle("/users/{id}",
|
|
|
|
mw.administrator(http.HandlerFunc(h.handleDeleteUser))).Methods(http.MethodDelete)
|
|
|
|
h.Handle("/users/{id}/passwd",
|
|
|
|
mw.authenticated(http.HandlerFunc(h.handlePostUserPasswd)))
|
|
|
|
h.Handle("/users/{userId}/resources/{resourceType}",
|
|
|
|
mw.authenticated(http.HandlerFunc(h.handlePostUserResource))).Methods(http.MethodPost)
|
|
|
|
h.Handle("/users/{userId}/resources/{resourceType}/{resourceId}",
|
|
|
|
mw.authenticated(http.HandlerFunc(h.handleDeleteUserResource))).Methods(http.MethodDelete)
|
|
|
|
h.Handle("/users/admin/check",
|
|
|
|
mw.public(http.HandlerFunc(h.handleGetAdminCheck)))
|
|
|
|
h.Handle("/users/admin/init",
|
|
|
|
mw.public(http.HandlerFunc(h.handlePostAdminInit)))
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
return h
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePostUsers handles POST requests on /users
|
|
|
|
func (handler *UserHandler) handlePostUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req postUsersRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
Error(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := govalidator.ValidateStruct(req)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
var role portainer.UserRole
|
|
|
|
if req.Role == 1 {
|
|
|
|
role = portainer.AdministratorRole
|
|
|
|
} else {
|
|
|
|
role = portainer.StandardUserRole
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := handler.UserService.UserByUsername(req.Username)
|
|
|
|
if err != nil && err != portainer.ErrUserNotFound {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if user != nil {
|
|
|
|
Error(w, portainer.ErrUserAlreadyExists, http.StatusConflict, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user = &portainer.User{
|
2016-12-18 05:21:29 +00:00
|
|
|
Username: req.Username,
|
2017-03-12 16:24:15 +00:00
|
|
|
Role: role,
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
user.Password, err = handler.CryptoService.Hash(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, portainer.ErrCryptoHashFailure, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
err = handler.UserService.CreateUser(user)
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type postUsersRequest struct {
|
|
|
|
Username string `valid:"alphanum,required"`
|
|
|
|
Password string `valid:"required"`
|
2017-03-12 16:24:15 +00:00
|
|
|
Role int `valid:"required"`
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
// handleGetUsers handles GET requests on /users
|
|
|
|
func (handler *UserHandler) handleGetUsers(w http.ResponseWriter, r *http.Request) {
|
|
|
|
users, err := handler.UserService.Users()
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range users {
|
|
|
|
users[i].Password = ""
|
|
|
|
}
|
|
|
|
encodeJSON(w, users, handler.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePostUserPasswd handles POST requests on /users/:id/passwd
|
2016-12-18 05:21:29 +00:00
|
|
|
func (handler *UserHandler) handlePostUserPasswd(w http.ResponseWriter, r *http.Request) {
|
2016-12-25 20:34:02 +00:00
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
handleNotAllowed(w, []string{http.MethodPost})
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2017-03-12 16:24:15 +00:00
|
|
|
id := vars["id"]
|
|
|
|
|
|
|
|
userID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
2016-12-18 05:21:29 +00:00
|
|
|
|
|
|
|
var req postUserPasswdRequest
|
2017-03-12 16:24:15 +00:00
|
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2016-12-18 05:21:29 +00:00
|
|
|
Error(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
_, err = govalidator.ValidateStruct(req)
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var password = req.Password
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
u, err := handler.UserService.User(portainer.UserID(userID))
|
2016-12-18 05:21:29 +00:00
|
|
|
if err == portainer.ErrUserNotFound {
|
|
|
|
Error(w, err, http.StatusNotFound, handler.Logger)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
valid := true
|
|
|
|
err = handler.CryptoService.CompareHashAndData(u.Password, password)
|
|
|
|
if err != nil {
|
|
|
|
valid = false
|
|
|
|
}
|
|
|
|
|
|
|
|
encodeJSON(w, &postUserPasswdResponse{Valid: valid}, handler.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
type postUserPasswdRequest struct {
|
|
|
|
Password string `valid:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type postUserPasswdResponse struct {
|
|
|
|
Valid bool `json:"valid"`
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
// handleGetUser handles GET requests on /users/:id
|
2016-12-18 05:21:29 +00:00
|
|
|
func (handler *UserHandler) handleGetUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
2017-03-12 16:24:15 +00:00
|
|
|
id := vars["id"]
|
2016-12-18 05:21:29 +00:00
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
userID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := handler.UserService.User(portainer.UserID(userID))
|
2016-12-18 05:21:29 +00:00
|
|
|
if err == portainer.ErrUserNotFound {
|
|
|
|
Error(w, err, http.StatusNotFound, handler.Logger)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user.Password = ""
|
|
|
|
encodeJSON(w, &user, handler.Logger)
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
// handlePutUser handles PUT requests on /users/:id
|
2016-12-18 05:21:29 +00:00
|
|
|
func (handler *UserHandler) handlePutUser(w http.ResponseWriter, r *http.Request) {
|
2017-03-12 16:24:15 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
|
|
|
|
userID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenData, err := extractTokenDataFromRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
}
|
|
|
|
|
|
|
|
if tokenData.Role != portainer.AdministratorRole && tokenData.ID != portainer.UserID(userID) {
|
|
|
|
Error(w, portainer.ErrUnauthorized, http.StatusForbidden, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-18 05:21:29 +00:00
|
|
|
var req putUserRequest
|
2017-03-12 16:24:15 +00:00
|
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
2016-12-18 05:21:29 +00:00
|
|
|
Error(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
_, err = govalidator.ValidateStruct(req)
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
if req.Password == "" && req.Role == 0 {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
|
|
|
|
user, err := handler.UserService.User(portainer.UserID(userID))
|
|
|
|
if err == portainer.ErrUserNotFound {
|
|
|
|
Error(w, err, http.StatusNotFound, handler.Logger)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
if req.Password != "" {
|
|
|
|
user.Password, err = handler.CryptoService.Hash(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, portainer.ErrCryptoHashFailure, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if req.Role != 0 {
|
|
|
|
if tokenData.Role != portainer.AdministratorRole {
|
|
|
|
Error(w, portainer.ErrUnauthorized, http.StatusForbidden, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if req.Role == 1 {
|
|
|
|
user.Role = portainer.AdministratorRole
|
|
|
|
} else {
|
|
|
|
user.Role = portainer.StandardUserRole
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.UserService.UpdateUser(user.ID, user)
|
2016-12-18 05:21:29 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type putUserRequest struct {
|
2017-03-12 16:24:15 +00:00
|
|
|
Password string `valid:"-"`
|
|
|
|
Role int `valid:"-"`
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlePostAdminInit handles GET requests on /users/admin/check
|
|
|
|
func (handler *UserHandler) handleGetAdminCheck(w http.ResponseWriter, r *http.Request) {
|
2016-12-25 20:34:02 +00:00
|
|
|
if r.Method != http.MethodGet {
|
|
|
|
handleNotAllowed(w, []string{http.MethodGet})
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
users, err := handler.UserService.UsersByRole(portainer.AdministratorRole)
|
|
|
|
if err != nil {
|
2016-12-18 05:21:29 +00:00
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
if len(users) == 0 {
|
|
|
|
Error(w, portainer.ErrUserNotFound, http.StatusNotFound, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
2016-12-18 05:21:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handlePostAdminInit handles POST requests on /users/admin/init
|
|
|
|
func (handler *UserHandler) handlePostAdminInit(w http.ResponseWriter, r *http.Request) {
|
2016-12-25 20:34:02 +00:00
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
handleNotAllowed(w, []string{http.MethodPost})
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req postAdminInitRequest
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
Error(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := govalidator.ValidateStruct(req)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
user, err := handler.UserService.UserByUsername("admin")
|
2017-01-12 05:17:28 +00:00
|
|
|
if err == portainer.ErrUserNotFound {
|
|
|
|
user := &portainer.User{
|
|
|
|
Username: "admin",
|
2017-03-12 16:24:15 +00:00
|
|
|
Role: portainer.AdministratorRole,
|
2017-01-12 05:17:28 +00:00
|
|
|
}
|
|
|
|
user.Password, err = handler.CryptoService.Hash(req.Password)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, portainer.ErrCryptoHashFailure, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:24:15 +00:00
|
|
|
err = handler.UserService.CreateUser(user)
|
2017-01-12 05:17:28 +00:00
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
2017-01-12 05:17:28 +00:00
|
|
|
if user != nil {
|
|
|
|
Error(w, portainer.ErrAdminAlreadyInitialized, http.StatusForbidden, handler.Logger)
|
2016-12-18 05:21:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type postAdminInitRequest struct {
|
|
|
|
Password string `valid:"required"`
|
|
|
|
}
|
2017-03-12 16:24:15 +00:00
|
|
|
|
|
|
|
// handleDeleteUser handles DELETE requests on /users/:id
|
|
|
|
func (handler *UserHandler) handleDeleteUser(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
id := vars["id"]
|
|
|
|
|
|
|
|
userID, err := strconv.Atoi(id)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = handler.UserService.User(portainer.UserID(userID))
|
|
|
|
|
|
|
|
if err == portainer.ErrUserNotFound {
|
|
|
|
Error(w, err, http.StatusNotFound, handler.Logger)
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.UserService.DeleteUser(portainer.UserID(userID))
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// handlePostUserResource handles POST requests on /users/:userId/resources/:resourceType
|
|
|
|
func (handler *UserHandler) handlePostUserResource(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userID := vars["userId"]
|
|
|
|
resourceType := vars["resourceType"]
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(userID)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rcType portainer.ResourceControlType
|
|
|
|
if resourceType == "container" {
|
|
|
|
rcType = portainer.ContainerResourceControl
|
|
|
|
} else if resourceType == "service" {
|
|
|
|
rcType = portainer.ServiceResourceControl
|
|
|
|
} else if resourceType == "volume" {
|
|
|
|
rcType = portainer.VolumeResourceControl
|
|
|
|
} else {
|
|
|
|
Error(w, ErrInvalidQueryFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenData, err := extractTokenDataFromRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
}
|
|
|
|
if tokenData.ID != portainer.UserID(uid) {
|
|
|
|
Error(w, portainer.ErrResourceAccessDenied, http.StatusForbidden, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var req postUserResourceRequest
|
|
|
|
if err = json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
Error(w, ErrInvalidJSON, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = govalidator.ValidateStruct(req)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resource := portainer.ResourceControl{
|
|
|
|
OwnerID: portainer.UserID(uid),
|
|
|
|
ResourceID: req.ResourceID,
|
|
|
|
AccessLevel: portainer.RestrictedResourceAccessLevel,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.ResourceControlService.CreateResourceControl(req.ResourceID, &resource, rcType)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, ErrInvalidRequestFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type postUserResourceRequest struct {
|
|
|
|
ResourceID string `valid:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleDeleteUserResource handles DELETE requests on /users/:userId/resources/:resourceType/:resourceId
|
|
|
|
func (handler *UserHandler) handleDeleteUserResource(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
userID := vars["userId"]
|
|
|
|
resourceID := vars["resourceId"]
|
|
|
|
resourceType := vars["resourceType"]
|
|
|
|
|
|
|
|
uid, err := strconv.Atoi(userID)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var rcType portainer.ResourceControlType
|
|
|
|
if resourceType == "container" {
|
|
|
|
rcType = portainer.ContainerResourceControl
|
|
|
|
} else if resourceType == "service" {
|
|
|
|
rcType = portainer.ServiceResourceControl
|
|
|
|
} else if resourceType == "volume" {
|
|
|
|
rcType = portainer.VolumeResourceControl
|
|
|
|
} else {
|
|
|
|
Error(w, ErrInvalidQueryFormat, http.StatusBadRequest, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenData, err := extractTokenDataFromRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
}
|
|
|
|
if tokenData.Role != portainer.AdministratorRole && tokenData.ID != portainer.UserID(uid) {
|
|
|
|
Error(w, portainer.ErrResourceAccessDenied, http.StatusForbidden, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = handler.ResourceControlService.DeleteResourceControl(resourceID, rcType)
|
|
|
|
if err != nil {
|
|
|
|
Error(w, err, http.StatusInternalServerError, handler.Logger)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|