ab.go 19 KB

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