ability to transfer vehicle and disable users

This commit is contained in:
Akhil Gupta
2021-06-24 10:24:20 +05:30
parent b111e23dea
commit 2bd8481670
15 changed files with 186 additions and 16 deletions

View File

@@ -102,6 +102,11 @@ func userLogin(c *gin.Context) {
c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password")))
return
}
if user.IsDisabled {
c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Your user has been disabled by the admin. Please contact them to get it re-enabled.")))
return
}
UpdateContextUserModel(c, user.ID)
token, refreshToken := common.GenToken(user.ID, user.Role)
response := models.LoginResponse{

View File

@@ -3,12 +3,17 @@ package controllers
import (
"net/http"
"github.com/akhilrex/hammond/common"
"github.com/akhilrex/hammond/db"
"github.com/akhilrex/hammond/models"
"github.com/akhilrex/hammond/service"
"github.com/gin-gonic/gin"
)
func RegisterUserController(router *gin.RouterGroup) {
router.GET("/users", allUsers)
router.POST("/users/:id/enable", ShouldBeAdmin(), enableUser)
router.POST("/users/:id/disable", ShouldBeAdmin(), disableUser)
}
func allUsers(c *gin.Context) {
@@ -20,3 +25,31 @@ func allUsers(c *gin.Context) {
c.JSON(http.StatusOK, users)
}
func enableUser(c *gin.Context) {
var searchByIdQuery models.SearchByIdQuery
if err := c.ShouldBindUri(&searchByIdQuery); err == nil {
err := service.SetDisabledStatusForUser(searchByIdQuery.Id, false)
if err != nil {
c.JSON(http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, gin.H{})
} else {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
}
}
func disableUser(c *gin.Context) {
var searchByIdQuery models.SearchByIdQuery
if err := c.ShouldBindUri(&searchByIdQuery); err == nil {
err := service.SetDisabledStatusForUser(searchByIdQuery.Id, true)
if err != nil {
c.JSON(http.StatusBadRequest, err)
return
}
c.JSON(http.StatusOK, gin.H{})
} else {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
}
}

View File

@@ -20,6 +20,7 @@ func RegisterVehicleController(router *gin.RouterGroup) {
router.GET("/vehicles/:id/users", getVehicleUsers)
router.POST("/vehicles/:id/users/:subId", shareVehicle)
router.DELETE("/vehicles/:id/users/:subId", unshareVehicle)
router.POST("/vehicles/:id/users/:subId/transfer", transferVehicle)
router.GET("/me/vehicles", getMyVehicles)
router.GET("/me/stats", getMystats)
@@ -409,6 +410,22 @@ func shareVehicle(c *gin.Context) {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
}
}
func transferVehicle(c *gin.Context) {
var searchByIdQuery models.SubItemQuery
if err := c.ShouldBindUri(&searchByIdQuery); err == nil {
err := service.TransferVehicle(searchByIdQuery.Id, c.MustGet("userId").(string), searchByIdQuery.SubId)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("shareVehicle", err))
return
}
c.JSON(http.StatusOK, gin.H{})
} else {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
}
}
func unshareVehicle(c *gin.Context) {
var searchByIdQuery models.SubItemQuery