ability to transfer vehicle and disable users
This commit is contained in:
@@ -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{
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@ type User struct {
|
||||
Role Role `json:"role"`
|
||||
Name string `json:"name"`
|
||||
Vehicles []Vehicle `gorm:"many2many:user_vehicles;" json:"vehicles"`
|
||||
IsDisabled bool `json:"isDisabled"`
|
||||
}
|
||||
|
||||
func (b *User) MarshalJSON() ([]byte, error) {
|
||||
|
||||
@@ -33,6 +33,11 @@ func FindOneUser(condition interface{}) (User, error) {
|
||||
err := DB.Where(condition).First(&model).Error
|
||||
return model, err
|
||||
}
|
||||
func SetDisabledStatusForUser(userId string, isDisabled bool) error {
|
||||
//Cannot do this for admin
|
||||
tx := DB.Debug().Model(&User{}).Where("id= ? and role=?", userId, USER).Update("is_disabled", isDisabled)
|
||||
return tx.Error
|
||||
}
|
||||
func GetAllUsers() (*[]User, error) {
|
||||
|
||||
sorting := "created_at desc"
|
||||
@@ -92,6 +97,16 @@ func ShareVehicle(vehicleId, userId string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func TransferVehicle(vehicleId, ownerId, newUserID string) error {
|
||||
|
||||
tx := DB.Model(&UserVehicle{}).Where("vehicle_id = ? AND user_id = ?", vehicleId, ownerId).Update("is_owner", false)
|
||||
if tx.Error != nil {
|
||||
return tx.Error
|
||||
}
|
||||
tx = DB.Model(&UserVehicle{}).Where("vehicle_id = ? AND user_id = ?", vehicleId, newUserID).Update("is_owner", true)
|
||||
|
||||
return tx.Error
|
||||
}
|
||||
|
||||
func UnshareVehicle(vehicleId, userId string) error {
|
||||
var mapping UserVehicle
|
||||
|
||||
@@ -14,10 +14,10 @@ type localMigration struct {
|
||||
}
|
||||
|
||||
var migrations = []localMigration{
|
||||
// {
|
||||
// Name: "2020_11_03_04_42_SetDefaultDownloadStatus",
|
||||
// Query: "update podcast_items set download_status=2 where download_path!='' and download_status=0",
|
||||
// },
|
||||
{
|
||||
Name: "2021_06_24_04_42_SetUserDisabledFalse",
|
||||
Query: "update users set is_disabled=0",
|
||||
},
|
||||
}
|
||||
|
||||
func RunMigrations() {
|
||||
|
||||
@@ -45,3 +45,6 @@ func UpdatePassword(id, password string) (bool, error) {
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
func SetDisabledStatusForUser(userId string, isDisabled bool) error {
|
||||
return db.SetDisabledStatusForUser(userId, isDisabled)
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/akhilrex/hammond/db"
|
||||
"github.com/akhilrex/hammond/models"
|
||||
"gorm.io/gorm/clause"
|
||||
@@ -59,6 +61,17 @@ func DeleteVehicle(vehicleId string) error {
|
||||
func ShareVehicle(vehicleId, userId string) error {
|
||||
return db.ShareVehicle(vehicleId, userId)
|
||||
}
|
||||
func TransferVehicle(vehicleId, ownerId, newUserID string) error {
|
||||
vehicleOwnerId, err := GetVehicleOwner(vehicleId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if vehicleOwnerId != ownerId {
|
||||
return fmt.Errorf("Only vehicle owner can transfer the vehicle")
|
||||
}
|
||||
|
||||
return db.TransferVehicle(vehicleId, ownerId, newUserID)
|
||||
}
|
||||
func UnshareVehicle(vehicleId, userId string) error {
|
||||
return db.UnshareVehicle(vehicleId, userId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user