ab.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. package api
  2. import (
  3. "Gwen/global"
  4. requstform "Gwen/http/request/api"
  5. "Gwen/http/response"
  6. "Gwen/http/response/api"
  7. "Gwen/model"
  8. "Gwen/service"
  9. "Gwen/utils"
  10. "encoding/json"
  11. "errors"
  12. "github.com/gin-gonic/gin"
  13. "net/http"
  14. "strconv"
  15. "strings"
  16. )
  17. type Ab struct {
  18. }
  19. // Ab
  20. // @Tags 地址
  21. // @Summary 地址列表
  22. // @Description 地址列表
  23. // @Accept json
  24. // @Produce json
  25. // @Success 200 {object} response.Response
  26. // @Failure 500 {object} response.ErrorResponse
  27. // @Router /ab [get]
  28. // @Security BearerAuth
  29. func (a *Ab) Ab(c *gin.Context) {
  30. user := service.AllService.UserService.CurUser(c)
  31. al := service.AllService.AddressBookService.ListByUserId(user.Id, 1, 1000)
  32. tags := service.AllService.TagService.ListByUserId(user.Id)
  33. tagColors := map[string]uint{}
  34. //将tags中的name转成一个以逗号分割的字符串
  35. var tagNames []string
  36. for _, tag := range tags.Tags {
  37. tagNames = append(tagNames, tag.Name)
  38. tagColors[tag.Name] = tag.Color
  39. }
  40. tgc, _ := json.Marshal(tagColors)
  41. res := &api.AbList{
  42. Peers: al.AddressBooks,
  43. Tags: tagNames,
  44. TagColors: string(tgc),
  45. }
  46. data, _ := json.Marshal(res)
  47. c.JSON(http.StatusOK, gin.H{
  48. "data": string(data),
  49. //"licensed_devices": 999,
  50. })
  51. }
  52. // UpAb
  53. // @Tags 地址
  54. // @Summary 地址更新
  55. // @Description 地址更新
  56. // @Accept json
  57. // @Produce json
  58. // @Param body body requstform.AddressBookForm true "地址表单"
  59. // @Success 200 {string} string "null"
  60. // @Failure 500 {object} response.ErrorResponse
  61. // @Router /ab [post]
  62. // @Security BearerAuth
  63. func (a *Ab) UpAb(c *gin.Context) {
  64. abf := &requstform.AddressBookForm{}
  65. err := c.ShouldBindJSON(&abf)
  66. if err != nil {
  67. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  68. return
  69. }
  70. abd := &requstform.AddressBookFormData{}
  71. err = json.Unmarshal([]byte(abf.Data), abd)
  72. if err != nil {
  73. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  74. return
  75. }
  76. tc := map[string]uint{}
  77. err = json.Unmarshal([]byte(abd.TagColors), &tc)
  78. if err != nil {
  79. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  80. return
  81. }
  82. user := service.AllService.UserService.CurUser(c)
  83. err = service.AllService.AddressBookService.UpdateAddressBook(abd.Peers, user.Id)
  84. if err != nil {
  85. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  86. return
  87. }
  88. service.AllService.TagService.UpdateTags(user.Id, tc)
  89. c.JSON(http.StatusOK, nil)
  90. }
  91. // Tags
  92. // @Tags 地址
  93. // @Summary 标签
  94. // @Description 标签
  95. // @Accept json
  96. // @Produce json
  97. // @Success 200 {object} []model.Tag
  98. // @Failure 500 {object} response.ErrorResponse
  99. // @Router /tags [post]
  100. // @Security BearerAuth
  101. func (a *Ab) Tags(c *gin.Context) {
  102. user := service.AllService.UserService.CurUser(c)
  103. tags := service.AllService.TagService.ListByUserId(user.Id)
  104. c.JSON(http.StatusOK, tags.Tags)
  105. }
  106. // PTags
  107. // @Tags 地址[Personal]
  108. // @Summary 标签
  109. // @Description 标签
  110. // @Accept json
  111. // @Produce json
  112. // @Param guid path string true "guid"
  113. // @Success 200 {object} model.TagList
  114. // @Failure 500 {object} response.ErrorResponse
  115. // @Router /ab/tags/{guid} [post]
  116. // @Security BearerAuth
  117. func (a *Ab) PTags(c *gin.Context) {
  118. u := service.AllService.UserService.CurUser(c)
  119. guid := c.Param("guid")
  120. _, uid, cid, err := a.CheckGuid(u, guid)
  121. if err != nil {
  122. response.Error(c, response.TranslateMsg(c, err.Error()))
  123. return
  124. }
  125. //check privileges
  126. if !service.AllService.AddressBookService.CheckUserReadPrivilege(u, uid, cid) {
  127. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  128. return
  129. }
  130. tags := service.AllService.TagService.ListByUserIdAndCollectionId(uid, cid)
  131. c.JSON(http.StatusOK, tags.Tags)
  132. }
  133. // TagAdd
  134. // @Tags 地址[Personal]
  135. // @Summary 标签添加
  136. // @Description 标签
  137. // @Accept json
  138. // @Produce json
  139. // @Param guid path string true "guid"
  140. // @Success 200 {string} string
  141. // @Failure 500 {object} response.ErrorResponse
  142. // @Router /ab/tag/add/{guid} [post]
  143. // @Security BearerAuth
  144. func (a *Ab) TagAdd(c *gin.Context) {
  145. t := &model.Tag{}
  146. err := c.ShouldBindJSON(t)
  147. if err != nil {
  148. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  149. return
  150. }
  151. u := service.AllService.UserService.CurUser(c)
  152. guid := c.Param("guid")
  153. _, uid, cid, err := a.CheckGuid(u, guid)
  154. if err != nil {
  155. response.Error(c, response.TranslateMsg(c, err.Error()))
  156. return
  157. }
  158. //check privileges
  159. if !service.AllService.AddressBookService.CheckUserWritePrivilege(u, uid, cid) {
  160. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  161. return
  162. }
  163. tag := service.AllService.TagService.InfoByUserIdAndNameAndCollectionId(uid, t.Name, cid)
  164. if tag != nil && tag.Id != 0 {
  165. response.Error(c, response.TranslateMsg(c, "ItemExists"))
  166. return
  167. }
  168. t.UserId = uid
  169. t.CollectionId = cid
  170. err = service.AllService.TagService.Create(t)
  171. if err != nil {
  172. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  173. return
  174. }
  175. c.String(http.StatusOK, "")
  176. }
  177. // TagRename
  178. // @Tags 地址[Personal]
  179. // @Summary 标签重命名
  180. // @Description 标签
  181. // @Accept json
  182. // @Produce json
  183. // @Param guid path string true "guid"
  184. // @Success 200 {string} string
  185. // @Failure 500 {object} response.ErrorResponse
  186. // @Router /ab/tag/rename/{guid} [put]
  187. // @Security BearerAuth
  188. func (a *Ab) TagRename(c *gin.Context) {
  189. t := &requstform.TagRenameForm{}
  190. err := c.ShouldBindJSON(t)
  191. if err != nil {
  192. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  193. return
  194. }
  195. u := service.AllService.UserService.CurUser(c)
  196. guid := c.Param("guid")
  197. _, uid, cid, err := a.CheckGuid(u, guid)
  198. if err != nil {
  199. response.Error(c, response.TranslateMsg(c, err.Error()))
  200. return
  201. }
  202. //check privileges
  203. if !service.AllService.AddressBookService.CheckUserWritePrivilege(u, uid, cid) {
  204. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  205. return
  206. }
  207. tag := service.AllService.TagService.InfoByUserIdAndNameAndCollectionId(uid, t.Old, cid)
  208. if tag == nil || tag.Id == 0 {
  209. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  210. return
  211. }
  212. ntag := service.AllService.TagService.InfoByUserIdAndNameAndCollectionId(uid, t.New, cid)
  213. if ntag != nil && ntag.Id != 0 {
  214. response.Error(c, response.TranslateMsg(c, "ItemExists"))
  215. return
  216. }
  217. tag.Name = t.New
  218. err = service.AllService.TagService.Update(tag)
  219. if err != nil {
  220. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  221. return
  222. }
  223. c.String(http.StatusOK, "")
  224. }
  225. // TagUpdate
  226. // @Tags 地址[Personal]
  227. // @Summary 标签修改颜色
  228. // @Description 标签
  229. // @Accept json
  230. // @Produce json
  231. // @Param guid path string true "guid"
  232. // @Success 200 {string} string
  233. // @Failure 500 {object} response.ErrorResponse
  234. // @Router /ab/tag/update/{guid} [put]
  235. // @Security BearerAuth
  236. func (a *Ab) TagUpdate(c *gin.Context) {
  237. t := &requstform.TagColorForm{}
  238. err := c.ShouldBindJSON(t)
  239. if err != nil {
  240. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  241. return
  242. }
  243. u := service.AllService.UserService.CurUser(c)
  244. guid := c.Param("guid")
  245. _, uid, cid, err := a.CheckGuid(u, guid)
  246. if err != nil {
  247. response.Error(c, response.TranslateMsg(c, err.Error()))
  248. return
  249. }
  250. //check privileges
  251. if !service.AllService.AddressBookService.CheckUserWritePrivilege(u, uid, cid) {
  252. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  253. return
  254. }
  255. tag := service.AllService.TagService.InfoByUserIdAndNameAndCollectionId(uid, t.Name, cid)
  256. if tag == nil || tag.Id == 0 {
  257. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  258. return
  259. }
  260. tag.Color = t.Color
  261. err = service.AllService.TagService.Update(tag)
  262. if err != nil {
  263. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  264. return
  265. }
  266. c.String(http.StatusOK, "")
  267. }
  268. // TagDel
  269. // @Tags 地址[Personal]
  270. // @Summary 标签删除
  271. // @Description 标签
  272. // @Accept json
  273. // @Produce json
  274. // @Param guid path string true "guid"
  275. // @Success 200 {string} string
  276. // @Failure 500 {object} response.ErrorResponse
  277. // @Router /ab/tag/{guid} [delete]
  278. // @Security BearerAuth
  279. func (a *Ab) TagDel(c *gin.Context) {
  280. t := &[]string{}
  281. err := c.ShouldBind(t)
  282. if err != nil {
  283. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  284. return
  285. }
  286. //fmt.Println(t)
  287. u := service.AllService.UserService.CurUser(c)
  288. guid := c.Param("guid")
  289. _, uid, cid, err := a.CheckGuid(u, guid)
  290. if err != nil {
  291. response.Error(c, response.TranslateMsg(c, err.Error()))
  292. return
  293. }
  294. //check privileges
  295. if !service.AllService.AddressBookService.CheckUserFullControlPrivilege(u, uid, cid) {
  296. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  297. return
  298. }
  299. for _, name := range *t {
  300. tag := service.AllService.TagService.InfoByUserIdAndNameAndCollectionId(uid, name, cid)
  301. if tag == nil || tag.Id == 0 {
  302. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  303. return
  304. }
  305. err = service.AllService.TagService.Delete(tag)
  306. if err != nil {
  307. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  308. return
  309. }
  310. }
  311. c.String(http.StatusOK, "")
  312. }
  313. // Personal
  314. // @Tags 地址[Personal]
  315. // @Summary 个人地址
  316. // @Description 个人地址
  317. // @Accept json
  318. // @Produce json
  319. // @Param string body string false "string valid"
  320. // @Success 200 {object} response.Response
  321. // @Failure 500 {object} response.Response
  322. // @Router /ab/personal [post]
  323. // @Security BearerAuth
  324. func (a *Ab) Personal(c *gin.Context) {
  325. user := service.AllService.UserService.CurUser(c)
  326. /**
  327. guid = json['guid'] ?? '',
  328. name = json['name'] ?? '',
  329. owner = json['owner'] ?? '',
  330. note = json['note'] ?? '',
  331. rule = json['rule'] ?? 0;
  332. */
  333. if global.Config.Rustdesk.Personal == 1 {
  334. guid := a.ComposeGuid(user.GroupId, user.Id, 0)
  335. //如果返回了guid,后面的请求会有变化
  336. c.JSON(http.StatusOK, gin.H{
  337. "guid": guid,
  338. "name": user.Username,
  339. "rule": 3,
  340. })
  341. } else {
  342. c.JSON(http.StatusOK, nil)
  343. }
  344. }
  345. // Settings
  346. // @Tags 地址[Personal]
  347. // @Summary 设置
  348. // @Description 设置
  349. // @Accept json
  350. // @Produce json
  351. // @Param string body string false "string valid"
  352. // @Success 200 {object} response.Response
  353. // @Failure 500 {object} response.Response
  354. // @Router /ab/settings [post]
  355. // @Security BearerAuth
  356. func (a *Ab) Settings(c *gin.Context) {
  357. c.JSON(http.StatusOK, gin.H{
  358. "max_peer_one_ab": 0, //最大peer数,0表示不限制
  359. })
  360. }
  361. // SharedProfiles
  362. // @Tags 地址[Personal]
  363. // @Summary 共享地址簿
  364. // @Description 共享
  365. // @Accept json
  366. // @Produce json
  367. // @Param current query int false "页码"
  368. // @Param pageSize query int false "每页数量"
  369. // @Success 200 {object} response.Response
  370. // @Failure 500 {object} response.Response
  371. // @Router /ab/shared/profiles [post]
  372. // @Security BearerAuth
  373. func (a *Ab) SharedProfiles(c *gin.Context) {
  374. var res []*api.SharedProfilesPayload
  375. user := service.AllService.UserService.CurUser(c)
  376. myAbCollectionList := service.AllService.AddressBookService.ListCollectionByUserId(user.Id)
  377. for _, ab := range myAbCollectionList.AddressBookCollection {
  378. res = append(res, &api.SharedProfilesPayload{
  379. Guid: a.ComposeGuid(user.GroupId, user.Id, ab.Id),
  380. Name: ab.Name,
  381. Owner: user.Username,
  382. Rule: model.ShareAddressBookRuleRuleFullControl,
  383. })
  384. }
  385. allAbIds := make(map[uint]int) //用map去重,并保留最大Rule
  386. allUserIds := make(map[uint]*model.User)
  387. rules := service.AllService.AddressBookService.CollectionReadRules(user)
  388. for _, rule := range rules {
  389. //先判断是否存在
  390. r, ok := allAbIds[rule.CollectionId]
  391. if ok {
  392. //再判断权限大小
  393. if r < rule.Rule {
  394. allAbIds[rule.CollectionId] = rule.Rule
  395. }
  396. } else {
  397. allAbIds[rule.CollectionId] = rule.Rule
  398. allUserIds[rule.UserId] = nil
  399. }
  400. }
  401. abids := utils.Keys(allAbIds)
  402. collections := service.AllService.AddressBookService.ListCollectionByIds(abids)
  403. ids := utils.Keys(allUserIds)
  404. allUsers := service.AllService.UserService.ListByIds(ids)
  405. for _, u := range allUsers {
  406. allUserIds[u.Id] = u
  407. }
  408. for _, collection := range collections {
  409. _u, ok := allUserIds[collection.UserId]
  410. if !ok {
  411. continue
  412. }
  413. res = append(res, &api.SharedProfilesPayload{
  414. Guid: a.ComposeGuid(_u.GroupId, _u.Id, collection.Id),
  415. Name: collection.Name,
  416. Owner: _u.Username,
  417. Rule: allAbIds[collection.Id],
  418. })
  419. }
  420. c.JSON(http.StatusOK, gin.H{
  421. "total": 0, //len(res),
  422. "data": res,
  423. })
  424. }
  425. // ParseGuid
  426. func (a *Ab) ParseGuid(guid string) (gid, uid, cid uint) {
  427. //用-切割 guid
  428. guids := strings.Split(guid, "-")
  429. if len(guids) < 2 {
  430. return 0, 0, 0
  431. }
  432. if len(guids) != 3 {
  433. cid = 0
  434. } else {
  435. s, err := strconv.Atoi(guids[2])
  436. if err != nil {
  437. return 0, 0, 0
  438. }
  439. cid = uint(s)
  440. }
  441. g, err := strconv.Atoi(guids[0])
  442. if err != nil {
  443. return 0, 0, 0
  444. }
  445. gid = uint(g)
  446. u, err := strconv.Atoi(guids[1])
  447. if err != nil {
  448. return 0, 0, 0
  449. }
  450. uid = uint(u)
  451. return
  452. }
  453. // ComposeGuid
  454. func (a *Ab) ComposeGuid(gid, uid, cid uint) string {
  455. return strconv.Itoa(int(gid)) + "-" + strconv.Itoa(int(uid)) + "-" + strconv.Itoa(int(cid))
  456. }
  457. // CheckGuid
  458. func (a *Ab) CheckGuid(cu *model.User, guid string) (gid, uid, cid uint, err error) {
  459. gid, uid, cid = a.ParseGuid(guid)
  460. err = nil
  461. if gid == 0 || uid == 0 {
  462. err = errors.New("ParamsError")
  463. return
  464. }
  465. u := &model.User{}
  466. if cu.Id == uid {
  467. u = cu
  468. } else {
  469. u = service.AllService.UserService.InfoById(uid)
  470. }
  471. if u == nil || u.Id == 0 {
  472. err = errors.New("ParamsError")
  473. return
  474. }
  475. if u.GroupId != gid {
  476. err = errors.New("ParamsError")
  477. return
  478. }
  479. if cid == 0 && cu.Id != uid {
  480. err = errors.New("ParamsError")
  481. return
  482. }
  483. if cid > 0 {
  484. c := service.AllService.AddressBookService.CollectionInfoById(cid)
  485. if c == nil || c.Id == 0 {
  486. err = errors.New("ParamsError")
  487. return
  488. }
  489. if c.UserId != uid {
  490. err = errors.New("ParamsError")
  491. return
  492. }
  493. }
  494. return
  495. }
  496. // Peers
  497. // @Tags 地址[Personal]
  498. // @Summary 地址列表
  499. // @Description 地址
  500. // @Accept json
  501. // @Produce json
  502. // @Param current query int false "页码"
  503. // @Param pageSize query int false "每页数量"
  504. // @Param ab query string false "guid"
  505. // @Success 200 {object} response.Response
  506. // @Failure 500 {object} response.Response
  507. // @Router /ab/peers [post]
  508. // @Security BearerAuth
  509. func (a *Ab) Peers(c *gin.Context) {
  510. u := service.AllService.UserService.CurUser(c)
  511. guid := c.Query("ab")
  512. _, uid, cid, err := a.CheckGuid(u, guid)
  513. if err != nil {
  514. response.Error(c, response.TranslateMsg(c, err.Error()))
  515. return
  516. }
  517. //check privileges
  518. if !service.AllService.AddressBookService.CheckUserReadPrivilege(u, uid, cid) {
  519. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  520. return
  521. }
  522. al := service.AllService.AddressBookService.ListByUserIdAndCollectionId(uid, cid, 1, 1000)
  523. c.JSON(http.StatusOK, gin.H{
  524. "total": al.Total,
  525. "data": al.AddressBooks,
  526. "licensed_devices": 99999,
  527. })
  528. }
  529. // PeerAdd
  530. // @Tags 地址[Personal]
  531. // @Summary 添加地址
  532. // @Description 添加地址
  533. // @Accept json
  534. // @Produce json
  535. // @Param guid path string true "guid"
  536. // @Success 200 {string} string
  537. // @Failure 500 {object} response.ErrorResponse
  538. // @Router /ab/peer/add/{guid} [post]
  539. // @Security BearerAuth
  540. func (a *Ab) PeerAdd(c *gin.Context) {
  541. // forceAlwaysRelay永远是字符串"false"
  542. //f := &gin.H{}
  543. f := &requstform.PersonalAddressBookForm{}
  544. err := c.ShouldBindJSON(f)
  545. if err != nil {
  546. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  547. return
  548. }
  549. u := service.AllService.UserService.CurUser(c)
  550. guid := c.Param("guid")
  551. _, uid, cid, err := a.CheckGuid(u, guid)
  552. if err != nil {
  553. response.Error(c, response.TranslateMsg(c, err.Error()))
  554. return
  555. }
  556. //check privileges
  557. if !service.AllService.AddressBookService.CheckUserWritePrivilege(u, uid, cid) {
  558. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  559. return
  560. }
  561. //fmt.Println(f)
  562. f.UserId = uid
  563. ab := f.ToAddressBook()
  564. ab.CollectionId = cid
  565. if ab.Platform == "" || ab.Username == "" || ab.Hostname == "" {
  566. peer := service.AllService.PeerService.FindById(ab.Id)
  567. if peer.RowId != 0 {
  568. ab.Platform = service.AllService.AddressBookService.PlatformFromOs(peer.Os)
  569. ab.Username = peer.Username
  570. ab.Hostname = peer.Hostname
  571. }
  572. }
  573. err = service.AllService.AddressBookService.AddAddressBook(ab)
  574. if err != nil {
  575. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  576. return
  577. }
  578. c.String(http.StatusOK, "")
  579. }
  580. // PeerDel
  581. // @Tags 地址[Personal]
  582. // @Summary 删除地址
  583. // @Description 删除地址
  584. // @Accept json
  585. // @Produce json
  586. // @Param guid path string true "guid"
  587. // @Success 200 {string} string
  588. // @Failure 500 {object} response.ErrorResponse
  589. // @Router /ab/peer/add/{guid} [delete]
  590. // @Security BearerAuth
  591. func (a *Ab) PeerDel(c *gin.Context) {
  592. f := &[]string{}
  593. err := c.ShouldBind(f)
  594. if err != nil {
  595. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  596. return
  597. }
  598. u := service.AllService.UserService.CurUser(c)
  599. guid := c.Param("guid")
  600. _, uid, cid, err := a.CheckGuid(u, guid)
  601. if err != nil {
  602. response.Error(c, response.TranslateMsg(c, err.Error()))
  603. return
  604. }
  605. //check privileges
  606. if !service.AllService.AddressBookService.CheckUserFullControlPrivilege(u, uid, cid) {
  607. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  608. return
  609. }
  610. for _, id := range *f {
  611. ab := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(uid, id, cid)
  612. if ab == nil || ab.RowId == 0 {
  613. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  614. return
  615. }
  616. err = service.AllService.AddressBookService.Delete(ab)
  617. if err != nil {
  618. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  619. return
  620. }
  621. }
  622. c.String(http.StatusOK, "")
  623. }
  624. // PeerUpdate
  625. // @Tags 地址[Personal]
  626. // @Summary 更新地址
  627. // @Description 更新地址
  628. // @Accept json
  629. // @Produce json
  630. // @Param guid path string true "guid"
  631. // @Success 200 {string} string
  632. // @Failure 500 {object} response.ErrorResponse
  633. // @Router /ab/peer/update/{guid} [put]
  634. // @Security BearerAuth
  635. func (a *Ab) PeerUpdate(c *gin.Context) {
  636. f := gin.H{}
  637. //f := &requstform.PersonalAddressBookForm{}
  638. err := c.ShouldBindJSON(&f)
  639. if err != nil {
  640. response.Error(c, response.TranslateMsg(c, "ParamsError")+err.Error())
  641. return
  642. }
  643. u := service.AllService.UserService.CurUser(c)
  644. guid := c.Param("guid")
  645. _, uid, cid, err := a.CheckGuid(u, guid)
  646. if err != nil {
  647. response.Error(c, response.TranslateMsg(c, err.Error()))
  648. return
  649. }
  650. //check privileges
  651. if !service.AllService.AddressBookService.CheckUserWritePrivilege(u, uid, cid) {
  652. response.Error(c, response.TranslateMsg(c, "NoAccess"))
  653. return
  654. }
  655. //fmt.Println(f)
  656. //判断f["Id"]是否存在
  657. fid, ok := f["id"]
  658. if !ok {
  659. response.Error(c, response.TranslateMsg(c, "ParamsError"))
  660. return
  661. }
  662. fidstr := fid.(string)
  663. ab := service.AllService.AddressBookService.InfoByUserIdAndIdAndCid(uid, fidstr, cid)
  664. if ab == nil || ab.RowId == 0 {
  665. response.Error(c, response.TranslateMsg(c, "ItemNotFound"))
  666. return
  667. }
  668. //允许的字段
  669. allowUp := []string{"password", "hash", "tags", "alias"}
  670. //f中的字段如果不在allowUp中,就删除
  671. for k := range f {
  672. if !utils.InArray(k, allowUp) {
  673. delete(f, k)
  674. }
  675. }
  676. //fmt.Println(f)
  677. if tags, _ok := f["tags"]; _ok {
  678. f["tags"], _ = json.Marshal(tags)
  679. }
  680. err = service.AllService.AddressBookService.UpdateByMap(ab, f)
  681. if err != nil {
  682. response.Error(c, response.TranslateMsg(c, "OperationFailed")+err.Error())
  683. return
  684. }
  685. c.String(http.StatusOK, "")
  686. }