2023-11-20 07:35:03 +00:00
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
portainer "github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/http/security"
|
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CurrentUserInspectResponse struct {
|
|
|
|
*portainer.User
|
|
|
|
ForceChangePassword bool `json:"forceChangePassword"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// @id CurrentUserInspect
|
|
|
|
// @summary Inspect the current user user
|
|
|
|
// @description Retrieve details about the current user.
|
|
|
|
// @description User passwords are filtered out, and should never be accessible.
|
|
|
|
// @description **Access policy**: authenticated
|
|
|
|
// @tags users
|
|
|
|
// @security ApiKeyAuth
|
|
|
|
// @security jwt
|
|
|
|
// @produce json
|
|
|
|
// @success 200 {object} portainer.User "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 403 "Permission denied"
|
|
|
|
// @failure 404 "User not found"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /users/me [get]
|
|
|
|
func (handler *Handler) userInspectMe(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2023-11-30 04:46:57 +00:00
|
|
|
tokenData, err := security.RetrieveTokenData(r)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to retrieve user authentication token", err)
|
|
|
|
}
|
|
|
|
|
2023-11-20 07:35:03 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to retrieve info from request context", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := handler.DataStore.User().Read(securityContext.UserID)
|
|
|
|
if handler.DataStore.IsErrObjectNotFound(err) {
|
|
|
|
return httperror.NotFound("Unable to find a user with the specified identifier inside the database", err)
|
|
|
|
} else if err != nil {
|
|
|
|
return httperror.InternalServerError("Unable to find a user with the specified identifier inside the database", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hideFields(user)
|
2023-11-30 04:46:57 +00:00
|
|
|
return response.JSON(
|
|
|
|
w,
|
|
|
|
&CurrentUserInspectResponse{
|
|
|
|
User: user,
|
|
|
|
ForceChangePassword: tokenData.ForceChangePassword,
|
|
|
|
},
|
|
|
|
)
|
2023-11-20 07:35:03 +00:00
|
|
|
}
|