更新
This commit is contained in:
1 parent
012f8edf8b
commit
36ff960fbd
33 files changed
+1644
-344
No files matched your search
@@ -1,13 +1,16 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/middleware"
|
||||
"server/models"
|
||||
"server/pkg/identity"
|
||||
"server/pkg/jwtutil"
|
||||
"server/services/gueststore"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
@@ -24,12 +27,18 @@ type AppPasswordStoreController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// 只放行移动端与租户端令牌:/api/** 仅被 uniapp 调用,平台令牌不该能打移动端接口。
|
||||
// 放行移动端、租户端与游客令牌:游客 tid=0 合法,数据进 gueststore 而非业务表。
|
||||
func (c *AppPasswordStoreController) appPwdClaims() (*jwtutil.Claims, error) {
|
||||
claims, err := identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndApp, identity.EndBackend)
|
||||
claims, err := identity.Claims(c.Ctx.Request.Header.Get("Authorization"), identity.EndApp, identity.EndBackend, identity.EndGuest)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if identity.IsGuest(claims) {
|
||||
if claims.UserID <= 0 {
|
||||
return nil, orm.ErrNoRows
|
||||
}
|
||||
return claims, nil
|
||||
}
|
||||
if claims.UserID <= 0 || claims.TenantId <= 0 {
|
||||
return nil, orm.ErrNoRows
|
||||
}
|
||||
@@ -43,6 +52,10 @@ func (c *AppPasswordStoreController) GetList() {
|
||||
passwordReply(&c.Controller, 401, "未登录或无权限", nil)
|
||||
return
|
||||
}
|
||||
if identity.IsGuest(cl) {
|
||||
c.guestList(cl, strings.TrimSpace(c.GetString("keyword")))
|
||||
return
|
||||
}
|
||||
qs := models.Orm.QueryTable(new(models.BackendPasswordStore)).
|
||||
Filter("is_deleted", 0).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID)
|
||||
if k := strings.TrimSpace(c.GetString("keyword")); k != "" {
|
||||
@@ -82,6 +95,10 @@ func (c *AppPasswordStoreController) GetDetail() {
|
||||
passwordReply(&c.Controller, 400, "参数错误", nil)
|
||||
return
|
||||
}
|
||||
if identity.IsGuest(cl) {
|
||||
c.guestDetail(cl, uint64(id))
|
||||
return
|
||||
}
|
||||
var m models.BackendPasswordStore
|
||||
e = models.Orm.QueryTable(new(models.BackendPasswordStore)).
|
||||
Filter("id", id).Filter("is_deleted", 0).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID).One(&m)
|
||||
@@ -121,6 +138,10 @@ func (c *AppPasswordStoreController) Create() {
|
||||
passwordReply(&c.Controller, 400, e.Error(), nil)
|
||||
return
|
||||
}
|
||||
if identity.IsGuest(cl) {
|
||||
c.guestCreate(cl, p)
|
||||
return
|
||||
}
|
||||
qs := models.Orm.QueryTable(new(models.BackendPasswordStore)).
|
||||
Filter("is_deleted", 0).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID)
|
||||
if existing, e := findSamePlatformRecord(qs, 0, p.Platform, p.URL); e != nil {
|
||||
@@ -172,6 +193,10 @@ func (c *AppPasswordStoreController) Update() {
|
||||
passwordReply(&c.Controller, 400, e.Error(), nil)
|
||||
return
|
||||
}
|
||||
if identity.IsGuest(cl) {
|
||||
c.guestUpdate(cl, uint64(id), p)
|
||||
return
|
||||
}
|
||||
var m models.BackendPasswordStore
|
||||
e = models.Orm.QueryTable(new(models.BackendPasswordStore)).
|
||||
Filter("id", id).Filter("is_deleted", 0).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID).One(&m)
|
||||
@@ -221,6 +246,18 @@ func (c *AppPasswordStoreController) Delete() {
|
||||
passwordReply(&c.Controller, 400, "参数错误", nil)
|
||||
return
|
||||
}
|
||||
if identity.IsGuest(cl) {
|
||||
if err := gueststore.Delete(uint64(cl.UserID), guestModulePasswordStore, uint64(id)); err != nil {
|
||||
if err == gueststore.ErrNotFound {
|
||||
passwordReply(&c.Controller, 404, "记录不存在", nil)
|
||||
return
|
||||
}
|
||||
passwordReply(&c.Controller, 500, "删除失败", nil)
|
||||
return
|
||||
}
|
||||
passwordReply(&c.Controller, 200, "success", nil)
|
||||
return
|
||||
}
|
||||
var m models.BackendPasswordStore
|
||||
e = models.Orm.QueryTable(new(models.BackendPasswordStore)).
|
||||
Filter("id", id).Filter("is_deleted", 0).Filter("tid", cl.TenantId).Filter("user_id", cl.UserID).One(&m)
|
||||
@@ -241,3 +278,125 @@ func (c *AppPasswordStoreController) Delete() {
|
||||
middleware.WriteDeleteLog(uint64(cl.UserID), cl.TenantId, "passwordStore", m.Platform)
|
||||
passwordReply(&c.Controller, 200, "success", nil)
|
||||
}
|
||||
|
||||
// ---------- 游客分支(免登录态):数据存 yz_guest_data,响应结构与登录态一致 ----------
|
||||
|
||||
func guestPwdFull(r gueststore.Row) PasswordStoreItemDTO {
|
||||
var it PasswordStoreItemDTO
|
||||
if json.Unmarshal(r.Payload, &it) != nil {
|
||||
it = PasswordStoreItemDTO{}
|
||||
}
|
||||
it.ID = r.ID
|
||||
it.Tid = 0
|
||||
it.UserID = 0
|
||||
it.CreateTime = r.CreateTime
|
||||
it.UpdateTime = r.UpdateTime
|
||||
if it.Accounts == nil {
|
||||
it.Accounts = []models.PasswordAccountItem{}
|
||||
}
|
||||
return it
|
||||
}
|
||||
|
||||
// guestPwdSamePlatform 与登录态 findSamePlatformRecord 同语义:平台+网址重复。
|
||||
func guestPwdSamePlatform(it PasswordStoreItemDTO, platform, url string, excludeID uint64) bool {
|
||||
if it.ID == excludeID {
|
||||
return false
|
||||
}
|
||||
return strings.EqualFold(strings.TrimSpace(it.Platform), strings.TrimSpace(platform)) &&
|
||||
strings.TrimSpace(it.URL) == strings.TrimSpace(url)
|
||||
}
|
||||
|
||||
func guestPwdKeywordHit(it PasswordStoreItemDTO, k string) bool {
|
||||
k = strings.ToLower(k)
|
||||
if strings.Contains(strings.ToLower(it.Platform), k) ||
|
||||
strings.Contains(strings.ToLower(it.URL), k) ||
|
||||
strings.Contains(strings.ToLower(it.Remark), k) {
|
||||
return true
|
||||
}
|
||||
for _, a := range it.Accounts {
|
||||
if strings.Contains(strings.ToLower(a.Username), k) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (c *AppPasswordStoreController) guestList(cl *jwtutil.Claims, keyword string) {
|
||||
rows, err := gueststore.List(uint64(cl.UserID), guestModulePasswordStore)
|
||||
if err != nil {
|
||||
passwordReply(&c.Controller, 500, "查询失败", nil)
|
||||
return
|
||||
}
|
||||
res := make([]PasswordStoreItemDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
it := guestPwdFull(r)
|
||||
if keyword != "" && !guestPwdKeywordHit(it, keyword) {
|
||||
continue
|
||||
}
|
||||
res = append(res, it)
|
||||
}
|
||||
passwordReply(&c.Controller, 200, "success", map[string]interface{}{"list": res, "total": len(res)})
|
||||
}
|
||||
|
||||
func (c *AppPasswordStoreController) guestDetail(cl *jwtutil.Claims, id uint64) {
|
||||
r, err := gueststore.Get(uint64(cl.UserID), guestModulePasswordStore, id)
|
||||
if err != nil {
|
||||
passwordReply(&c.Controller, 404, "记录不存在", nil)
|
||||
return
|
||||
}
|
||||
passwordReply(&c.Controller, 200, "success", guestPwdFull(r))
|
||||
}
|
||||
|
||||
func (c *AppPasswordStoreController) guestCreate(cl *jwtutil.Claims, p passwordPayload) {
|
||||
rows, err := gueststore.List(uint64(cl.UserID), guestModulePasswordStore)
|
||||
if err != nil {
|
||||
passwordReply(&c.Controller, 500, "查询失败", nil)
|
||||
return
|
||||
}
|
||||
for _, r := range rows {
|
||||
if guestPwdSamePlatform(guestPwdFull(r), p.Platform, p.URL, 0) {
|
||||
passwordReply(&c.Controller, 400, "同平台同网址记录已存在,请勿重复添加", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
it := PasswordStoreItemDTO{
|
||||
Platform: p.Platform, URL: p.URL, Accounts: p.Accounts, Remark: p.Remark,
|
||||
CreateTime: time.Now(),
|
||||
}
|
||||
newID, err := gueststore.Insert(uint64(cl.UserID), guestModulePasswordStore, it)
|
||||
if err != nil {
|
||||
passwordReply(&c.Controller, 500, "创建失败", nil)
|
||||
return
|
||||
}
|
||||
it.ID = newID
|
||||
passwordReply(&c.Controller, 200, "success", it)
|
||||
}
|
||||
|
||||
func (c *AppPasswordStoreController) guestUpdate(cl *jwtutil.Claims, id uint64, p passwordPayload) {
|
||||
r, err := gueststore.Get(uint64(cl.UserID), guestModulePasswordStore, id)
|
||||
if err != nil {
|
||||
passwordReply(&c.Controller, 404, "记录不存在", nil)
|
||||
return
|
||||
}
|
||||
rows, err := gueststore.List(uint64(cl.UserID), guestModulePasswordStore)
|
||||
if err != nil {
|
||||
passwordReply(&c.Controller, 500, "查询失败", nil)
|
||||
return
|
||||
}
|
||||
for _, rr := range rows {
|
||||
if guestPwdSamePlatform(guestPwdFull(rr), p.Platform, p.URL, id) {
|
||||
passwordReply(&c.Controller, 400, "同平台同网址记录已存在,请勿重复添加", nil)
|
||||
return
|
||||
}
|
||||
}
|
||||
it := guestPwdFull(r)
|
||||
it.Platform = p.Platform
|
||||
it.URL = p.URL
|
||||
it.Accounts = p.Accounts
|
||||
it.Remark = p.Remark
|
||||
if err = gueststore.Update(uint64(cl.UserID), guestModulePasswordStore, id, it); err != nil {
|
||||
passwordReply(&c.Controller, 500, "更新失败", nil)
|
||||
return
|
||||
}
|
||||
passwordReply(&c.Controller, 200, "success", it)
|
||||
}
|
||||
Reference in new issue
Block a user