修复 「1.2.2 用户管理-用户列表勾选单一用户会全选 」 close #216
This commit is contained in:
@ -1,56 +1,35 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"context"
|
||||
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/server/constant"
|
||||
"next-terminal/server/global/cache"
|
||||
"next-terminal/server/log"
|
||||
"next-terminal/server/model"
|
||||
"next-terminal/server/repository"
|
||||
"next-terminal/server/service"
|
||||
"next-terminal/server/utils"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
func UserCreateEndpoint(c echo.Context) (err error) {
|
||||
type UserApi struct{}
|
||||
|
||||
func (userApi UserApi) UserCreateEndpoint(c echo.Context) (err error) {
|
||||
var item model.User
|
||||
if err := c.Bind(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
if userRepository.ExistByUsername(item.Username) {
|
||||
return Fail(c, -1, "username is already in use")
|
||||
}
|
||||
|
||||
password := item.Password
|
||||
|
||||
var pass []byte
|
||||
if pass, err = utils.Encoder.Encode([]byte(password)); err != nil {
|
||||
return err
|
||||
}
|
||||
item.Password = string(pass)
|
||||
|
||||
item.ID = utils.UUID()
|
||||
item.Created = utils.NowJsonTime()
|
||||
item.Status = constant.StatusEnabled
|
||||
|
||||
if err := userRepository.Create(&item); err != nil {
|
||||
return err
|
||||
}
|
||||
err = storageService.CreateStorageByUser(&item)
|
||||
if err != nil {
|
||||
if err := service.UserService.CreateUser(item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if item.Mail != "" {
|
||||
go mailService.SendMail(item.Mail, "[Next Terminal] 注册通知", "你好,"+item.Nickname+"。管理员为你注册了账号:"+item.Username+" 密码:"+password)
|
||||
}
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func UserPagingEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserPagingEndpoint(c echo.Context) error {
|
||||
pageIndex, _ := strconv.Atoi(c.QueryParam("pageIndex"))
|
||||
pageSize, _ := strconv.Atoi(c.QueryParam("pageSize"))
|
||||
username := c.QueryParam("username")
|
||||
@ -60,19 +39,18 @@ func UserPagingEndpoint(c echo.Context) error {
|
||||
order := c.QueryParam("order")
|
||||
field := c.QueryParam("field")
|
||||
|
||||
account, _ := GetCurrentAccount(c)
|
||||
items, total, err := userRepository.Find(pageIndex, pageSize, username, nickname, mail, order, field, account)
|
||||
items, total, err := repository.UserRepository.Find(context.TODO(), pageIndex, pageSize, username, nickname, mail, order, field)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, H{
|
||||
return Success(c, Map{
|
||||
"total": total,
|
||||
"items": items,
|
||||
})
|
||||
}
|
||||
|
||||
func UserUpdateEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserUpdateEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
|
||||
account, _ := GetCurrentAccount(c)
|
||||
@ -86,14 +64,14 @@ func UserUpdateEndpoint(c echo.Context) error {
|
||||
}
|
||||
item.ID = id
|
||||
|
||||
if err := userRepository.Update(&item); err != nil {
|
||||
if err := repository.UserRepository.Update(context.TODO(), &item); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func UserUpdateStatusEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserUpdateStatusEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
status := c.QueryParam("status")
|
||||
account, _ := GetCurrentAccount(c)
|
||||
@ -101,14 +79,14 @@ func UserUpdateStatusEndpoint(c echo.Context) error {
|
||||
return Fail(c, -1, "不能操作自身账户")
|
||||
}
|
||||
|
||||
if err := userService.UpdateStatusById(id, status); err != nil {
|
||||
if err := service.UserService.UpdateStatusById(id, status); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func UserDeleteEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserDeleteEndpoint(c echo.Context) error {
|
||||
ids := c.Param("id")
|
||||
account, found := GetCurrentAccount(c)
|
||||
if !found {
|
||||
@ -120,16 +98,7 @@ func UserDeleteEndpoint(c echo.Context) error {
|
||||
if account.ID == userId {
|
||||
return Fail(c, -1, "不允许删除自身账户")
|
||||
}
|
||||
// 下线该用户
|
||||
if err := userService.LogoutById(userId); err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除用户
|
||||
if err := userRepository.DeleteById(userId); err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除用户的默认磁盘空间
|
||||
if err := storageService.DeleteStorageById(userId, true); err != nil {
|
||||
if err := service.UserService.DeleteUserById(userId); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -137,10 +106,10 @@ func UserDeleteEndpoint(c echo.Context) error {
|
||||
return Success(c, nil)
|
||||
}
|
||||
|
||||
func UserGetEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserGetEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
|
||||
item, err := userRepository.FindById(id)
|
||||
item, err := repository.UserRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -148,14 +117,14 @@ func UserGetEndpoint(c echo.Context) error {
|
||||
return Success(c, item)
|
||||
}
|
||||
|
||||
func UserChangePasswordEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserChangePasswordEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
password := c.FormValue("password")
|
||||
if password == "" {
|
||||
return Fail(c, -1, "请输入密码")
|
||||
}
|
||||
|
||||
user, err := userRepository.FindById(id)
|
||||
user, err := repository.UserRepository.FindById(context.TODO(), id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -168,61 +137,25 @@ func UserChangePasswordEndpoint(c echo.Context) error {
|
||||
Password: string(passwd),
|
||||
ID: id,
|
||||
}
|
||||
if err := userRepository.Update(u); err != nil {
|
||||
if err := repository.UserRepository.Update(context.TODO(), u); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if user.Mail != "" {
|
||||
go mailService.SendMail(user.Mail, "[Next Terminal] 密码修改通知", "你好,"+user.Nickname+"。管理员已将你的密码修改为:"+password)
|
||||
go service.MailService.SendMail(user.Mail, "[Next Terminal] 密码修改通知", "你好,"+user.Nickname+"。管理员已将你的密码修改为:"+password)
|
||||
}
|
||||
|
||||
return Success(c, "")
|
||||
}
|
||||
|
||||
func UserResetTotpEndpoint(c echo.Context) error {
|
||||
func (userApi UserApi) UserResetTotpEndpoint(c echo.Context) error {
|
||||
id := c.Param("id")
|
||||
u := &model.User{
|
||||
TOTPSecret: "-",
|
||||
ID: id,
|
||||
}
|
||||
if err := userRepository.Update(u); err != nil {
|
||||
if err := repository.UserRepository.Update(context.TODO(), u); err != nil {
|
||||
return err
|
||||
}
|
||||
return Success(c, "")
|
||||
}
|
||||
|
||||
func ReloadToken() error {
|
||||
loginLogs, err := loginLogRepository.FindAliveLoginLogs()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range loginLogs {
|
||||
loginLog := loginLogs[i]
|
||||
token := loginLog.ID
|
||||
user, err := userRepository.FindByUsername(loginLog.Username)
|
||||
if err != nil {
|
||||
if errors.Is(gorm.ErrRecordNotFound, err) {
|
||||
_ = loginLogRepository.DeleteById(token)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
authorization := Authorization{
|
||||
Token: token,
|
||||
Remember: loginLog.Remember,
|
||||
User: user,
|
||||
}
|
||||
|
||||
cacheKey := userService.BuildCacheKeyByToken(token)
|
||||
|
||||
if authorization.Remember {
|
||||
// 记住登录有效期两周
|
||||
cache.GlobalCache.Set(cacheKey, authorization, RememberEffectiveTime)
|
||||
} else {
|
||||
cache.GlobalCache.Set(cacheKey, authorization, NotRememberEffectiveTime)
|
||||
}
|
||||
log.Debugf("重新加载用户「%v」授权Token「%v」到缓存", user.Nickname, token)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user