addressBookCollectionRule.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package my
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "github.com/lejianwen/rustdesk-api/global"
  5. "github.com/lejianwen/rustdesk-api/http/request/admin"
  6. "github.com/lejianwen/rustdesk-api/http/response"
  7. "github.com/lejianwen/rustdesk-api/model"
  8. "github.com/lejianwen/rustdesk-api/service"
  9. "gorm.io/gorm"
  10. )
  11. type AddressBookCollectionRule struct {
  12. }
  13. // List 列表
  14. // @Tags 我的地址簿规则
  15. // @Summary 地址簿规则列表
  16. // @Description 地址簿规则列表
  17. // @Accept json
  18. // @Produce json
  19. // @Param page query int false "页码"
  20. // @Param page_size query int false "页大小"
  21. // @Param is_my query int false "是否是我的"
  22. // @Param user_id query int false "用户id"
  23. // @Param collection_id query int false "地址簿集合id"
  24. // @Success 200 {object} response.Response{data=model.AddressBookCollectionList}
  25. // @Failure 500 {object} response.Response
  26. // @Router /admin/my/address_book_collection_rule/list [get]
  27. // @Security token
  28. func (abcr *AddressBookCollectionRule) List(c *gin.Context) {
  29. query := &admin.AddressBookCollectionRuleQuery{}
  30. if err := c.ShouldBindQuery(query); err != nil {
  31. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  32. return
  33. }
  34. u := service.AllService.UserService.CurUser(c)
  35. query.UserId = int(u.Id)
  36. res := service.AllService.AddressBookService.ListRules(query.Page, query.PageSize, func(tx *gorm.DB) {
  37. tx.Where("user_id = ?", query.UserId)
  38. if query.CollectionId > 0 {
  39. tx.Where("collection_id = ?", query.CollectionId)
  40. }
  41. })
  42. response.Success(c, res)
  43. }
  44. // Create 创建地址簿规则
  45. // @Tags 我的地址簿规则
  46. // @Summary 创建地址簿规则
  47. // @Description 创建地址簿规则
  48. // @Accept json
  49. // @Produce json
  50. // @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
  51. // @Success 200 {object} response.Response{data=model.AddressBookCollection}
  52. // @Failure 500 {object} response.Response
  53. // @Router /admin/my/address_book_collection_rule/create [post]
  54. // @Security token
  55. func (abcr *AddressBookCollectionRule) Create(c *gin.Context) {
  56. f := &model.AddressBookCollectionRule{}
  57. if err := c.ShouldBindJSON(f); err != nil {
  58. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  59. return
  60. }
  61. errList := global.Validator.ValidStruct(c, f)
  62. if len(errList) > 0 {
  63. response.Fail(c, 101, errList[0])
  64. return
  65. }
  66. if f.Type != model.ShareAddressBookRuleTypePersonal && f.Type != model.ShareAddressBookRuleTypeGroup {
  67. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  68. return
  69. }
  70. //t := f.ToAddressBookCollection()
  71. t := f
  72. u := service.AllService.UserService.CurUser(c)
  73. t.UserId = u.Id
  74. msg, res := abcr.CheckForm(u, t)
  75. if !res {
  76. response.Fail(c, 101, response.TranslateMsg(c, msg))
  77. return
  78. }
  79. err := service.AllService.AddressBookService.CreateRule(t)
  80. if err != nil {
  81. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  82. return
  83. }
  84. response.Success(c, nil)
  85. }
  86. func (abcr *AddressBookCollectionRule) CheckForm(u *model.User, t *model.AddressBookCollectionRule) (string, bool) {
  87. if t.UserId != u.Id {
  88. return "NoAccess", false
  89. }
  90. if t.CollectionId > 0 && !service.AllService.AddressBookService.CheckCollectionOwner(t.UserId, t.CollectionId) {
  91. return "ParamsError", false
  92. }
  93. //check to_id
  94. if t.Type == model.ShareAddressBookRuleTypePersonal {
  95. if t.ToId == t.UserId {
  96. return "ParamsError", false
  97. }
  98. tou := service.AllService.UserService.InfoById(t.ToId)
  99. if tou.Id == 0 {
  100. return "ItemNotFound", false
  101. }
  102. //非管理员不能分享给非本组织用户
  103. if tou.GroupId != u.GroupId {
  104. return "NoAccess", false
  105. }
  106. } else if t.Type == model.ShareAddressBookRuleTypeGroup {
  107. //非管理员不能分享给其他组
  108. if t.ToId != u.GroupId {
  109. return "NoAccess", false
  110. }
  111. tog := service.AllService.GroupService.InfoById(t.ToId)
  112. if tog.Id == 0 {
  113. return "ItemNotFound", false
  114. }
  115. } else {
  116. return "ParamsError", false
  117. }
  118. // 重复检查
  119. ex := service.AllService.AddressBookService.RulePersonalInfoByToIdAndCid(t.ToId, t.CollectionId)
  120. if t.Id == 0 && ex.Id > 0 {
  121. return "ItemExists", false
  122. }
  123. if t.Id > 0 && ex.Id > 0 && t.Id != ex.Id {
  124. return "ItemExists", false
  125. }
  126. return "", true
  127. }
  128. // Update 编辑
  129. // @Tags 我的地址簿规则
  130. // @Summary 地址簿规则编辑
  131. // @Description 地址簿规则编辑
  132. // @Accept json
  133. // @Produce json
  134. // @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
  135. // @Success 200 {object} response.Response{data=model.AddressBookCollection}
  136. // @Failure 500 {object} response.Response
  137. // @Router /admin/my/address_book_collection_rule/update [post]
  138. // @Security token
  139. func (abcr *AddressBookCollectionRule) Update(c *gin.Context) {
  140. f := &model.AddressBookCollectionRule{}
  141. if err := c.ShouldBindJSON(f); err != nil {
  142. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  143. return
  144. }
  145. errList := global.Validator.ValidStruct(c, f)
  146. if len(errList) > 0 {
  147. response.Fail(c, 101, errList[0])
  148. return
  149. }
  150. if f.Id == 0 {
  151. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError"))
  152. return
  153. }
  154. u := service.AllService.UserService.CurUser(c)
  155. ex := service.AllService.AddressBookService.RuleInfoById(f.Id)
  156. if ex.Id == 0 {
  157. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  158. return
  159. }
  160. if ex.UserId != u.Id {
  161. response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
  162. return
  163. }
  164. t := f
  165. msg, res := abcr.CheckForm(u, t)
  166. if !res {
  167. response.Fail(c, 101, response.TranslateMsg(c, msg))
  168. return
  169. }
  170. err := service.AllService.AddressBookService.UpdateRule(t)
  171. if err != nil {
  172. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  173. return
  174. }
  175. response.Success(c, nil)
  176. }
  177. // Delete 删除
  178. // @Tags 我的地址簿规则
  179. // @Summary 地址簿规则删除
  180. // @Description 地址簿规则删除
  181. // @Accept json
  182. // @Produce json
  183. // @Param body body model.AddressBookCollectionRule true "地址簿规则信息"
  184. // @Success 200 {object} response.Response
  185. // @Failure 500 {object} response.Response
  186. // @Router /admin/my/address_book_collection_rule/delete [post]
  187. // @Security token
  188. func (abcr *AddressBookCollectionRule) Delete(c *gin.Context) {
  189. f := &model.AddressBookCollectionRule{}
  190. if err := c.ShouldBindJSON(f); err != nil {
  191. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  192. return
  193. }
  194. id := f.Id
  195. errList := global.Validator.ValidVar(c, id, "required,gt=0")
  196. if len(errList) > 0 {
  197. response.Fail(c, 101, errList[0])
  198. return
  199. }
  200. ex := service.AllService.AddressBookService.RuleInfoById(f.Id)
  201. if ex.Id == 0 {
  202. response.Fail(c, 101, response.TranslateMsg(c, "ItemNotFound"))
  203. return
  204. }
  205. u := service.AllService.UserService.CurUser(c)
  206. if ex.UserId != u.Id {
  207. response.Fail(c, 101, response.TranslateMsg(c, "NoAccess"))
  208. return
  209. }
  210. err := service.AllService.AddressBookService.DeleteRule(ex)
  211. if err == nil {
  212. response.Success(c, nil)
  213. return
  214. }
  215. response.Fail(c, 101, response.TranslateMsg(c, "OperationFailed")+err.Error())
  216. }