mirror of https://github.com/Xhofe/alist
feat: account manage api
parent
cab498e376
commit
5783aa99f1
|
@ -33,20 +33,23 @@ func GetAccountByVirtualPath(virtualPath string) (driver.Driver, error) {
|
||||||
func CreateAccount(ctx context.Context, account model.Account) error {
|
func CreateAccount(ctx context.Context, account model.Account) error {
|
||||||
account.Modified = time.Now()
|
account.Modified = time.Now()
|
||||||
account.VirtualPath = utils.StandardizePath(account.VirtualPath)
|
account.VirtualPath = utils.StandardizePath(account.VirtualPath)
|
||||||
err := db.CreateAccount(&account)
|
var err error
|
||||||
if err != nil {
|
// check driver first
|
||||||
return errors.WithMessage(err, "failed create account in database")
|
|
||||||
}
|
|
||||||
// already has an id
|
|
||||||
driverName := account.Driver
|
driverName := account.Driver
|
||||||
driverNew, err := GetDriverNew(driverName)
|
driverNew, err := GetDriverNew(driverName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed get driver new")
|
return errors.WithMessage(err, "failed get driver new")
|
||||||
}
|
}
|
||||||
accountDriver := driverNew()
|
accountDriver := driverNew()
|
||||||
|
// insert account to database
|
||||||
|
err = db.CreateAccount(&account)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "failed create account in database")
|
||||||
|
}
|
||||||
|
// already has an id
|
||||||
err = accountDriver.Init(ctx, account)
|
err = accountDriver.Init(ctx, account)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed init account")
|
return errors.WithMessage(err, "failed init account but account is already created")
|
||||||
}
|
}
|
||||||
accountsMap.Store(account.VirtualPath, accountDriver)
|
accountsMap.Store(account.VirtualPath, accountDriver)
|
||||||
return nil
|
return nil
|
||||||
|
@ -60,6 +63,9 @@ func UpdateAccount(ctx context.Context, account model.Account) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.WithMessage(err, "failed get old account")
|
return errors.WithMessage(err, "failed get old account")
|
||||||
}
|
}
|
||||||
|
if oldAccount.Driver != account.Driver {
|
||||||
|
return errors.Errorf("driver cannot be changed")
|
||||||
|
}
|
||||||
account.Modified = time.Now()
|
account.Modified = time.Now()
|
||||||
account.VirtualPath = utils.StandardizePath(account.VirtualPath)
|
account.VirtualPath = utils.StandardizePath(account.VirtualPath)
|
||||||
err = db.UpdateAccount(&account)
|
err = db.UpdateAccount(&account)
|
||||||
|
@ -86,6 +92,28 @@ func UpdateAccount(ctx context.Context, account model.Account) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteAccountById(ctx context.Context, id uint) error {
|
||||||
|
account, err := db.GetAccountById(id)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "failed get account")
|
||||||
|
}
|
||||||
|
accountDriver, err := GetAccountByVirtualPath(account.VirtualPath)
|
||||||
|
if err != nil {
|
||||||
|
return errors.WithMessage(err, "failed get account driver")
|
||||||
|
}
|
||||||
|
// drop the account in the driver
|
||||||
|
if err := accountDriver.Drop(ctx); err != nil {
|
||||||
|
return errors.WithMessage(err, "failed drop account")
|
||||||
|
}
|
||||||
|
// delete the account in the database
|
||||||
|
if err := db.DeleteAccountById(id); err != nil {
|
||||||
|
return errors.WithMessage(err, "failed delete account in database")
|
||||||
|
}
|
||||||
|
// delete the account in the memory
|
||||||
|
accountsMap.Delete(account.VirtualPath)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// MustSaveDriverAccount call from specific driver
|
// MustSaveDriverAccount call from specific driver
|
||||||
func MustSaveDriverAccount(driver driver.Driver) {
|
func MustSaveDriverAccount(driver driver.Driver) {
|
||||||
err := saveDriverAccount(driver)
|
err := saveDriverAccount(driver)
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/alist-org/alist/v3/internal/db"
|
||||||
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
|
"github.com/alist-org/alist/v3/internal/operations"
|
||||||
|
"github.com/alist-org/alist/v3/server/common"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ListAccounts(c *gin.Context) {
|
||||||
|
var req common.PageReq
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
common.ErrorResp(c, err, 400, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
log.Debugf("%+v", req)
|
||||||
|
accounts, total, err := db.GetAccounts(req.PageIndex, req.PageSize)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorResp(c, err, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
common.SuccessResp(c, common.PageResp{
|
||||||
|
Content: accounts,
|
||||||
|
Total: total,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateAccount(c *gin.Context) {
|
||||||
|
var req model.Account
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
common.ErrorResp(c, err, 400, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := operations.CreateAccount(c, req); err != nil {
|
||||||
|
common.ErrorResp(c, err, 500)
|
||||||
|
} else {
|
||||||
|
common.SuccessResp(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func UpdateAccount(c *gin.Context) {
|
||||||
|
var req model.Account
|
||||||
|
if err := c.ShouldBind(&req); err != nil {
|
||||||
|
common.ErrorResp(c, err, 400, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := operations.UpdateAccount(c, req); err != nil {
|
||||||
|
common.ErrorResp(c, err, 500)
|
||||||
|
} else {
|
||||||
|
common.SuccessResp(c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteAccount(c *gin.Context) {
|
||||||
|
idStr := c.Query("id")
|
||||||
|
id, err := strconv.Atoi(idStr)
|
||||||
|
if err != nil {
|
||||||
|
common.ErrorResp(c, err, 400, true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if err := operations.DeleteAccountById(c, uint(id)); err != nil {
|
||||||
|
common.ErrorResp(c, err, 500)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
common.SuccessResp(c)
|
||||||
|
}
|
|
@ -52,7 +52,7 @@ func UpdateUser(c *gin.Context) {
|
||||||
}
|
}
|
||||||
user, err := db.GetUserById(req.ID)
|
user, err := db.GetUserById(req.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500, true)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if user.Role != req.Role {
|
if user.Role != req.Role {
|
||||||
|
|
|
@ -30,6 +30,12 @@ func Init(r *gin.Engine) {
|
||||||
user.POST("/create", controllers.CreateUser)
|
user.POST("/create", controllers.CreateUser)
|
||||||
user.POST("/update", controllers.UpdateUser)
|
user.POST("/update", controllers.UpdateUser)
|
||||||
user.POST("/delete", controllers.DeleteUser)
|
user.POST("/delete", controllers.DeleteUser)
|
||||||
|
|
||||||
|
account := admin.Group("/account")
|
||||||
|
account.GET("/list", controllers.ListAccounts)
|
||||||
|
account.POST("/create", controllers.CreateAccount)
|
||||||
|
account.POST("/update", controllers.UpdateAccount)
|
||||||
|
account.POST("/delete", controllers.DeleteAccount)
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cors(r *gin.Engine) {
|
func Cors(r *gin.Engine) {
|
||||||
|
|
Loading…
Reference in New Issue