group.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  1. // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
  2. // See LICENSE.txt for license information.
  3. package api4
  4. import (
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. "github.com/mattermost/mattermost-server/v5/audit"
  12. "github.com/mattermost/mattermost-server/v5/model"
  13. )
  14. func (api *API) InitGroup() {
  15. // GET /api/v4/groups
  16. api.BaseRoutes.Groups.Handle("", api.ApiSessionRequired(getGroups)).Methods("GET")
  17. // GET /api/v4/groups/:group_id
  18. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}",
  19. api.ApiSessionRequired(getGroup)).Methods("GET")
  20. // PUT /api/v4/groups/:group_id/patch
  21. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/patch",
  22. api.ApiSessionRequired(patchGroup)).Methods("PUT")
  23. // POST /api/v4/groups/:group_id/teams/:team_id/link
  24. // POST /api/v4/groups/:group_id/channels/:channel_id/link
  25. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/link",
  26. api.ApiSessionRequired(linkGroupSyncable)).Methods("POST")
  27. // DELETE /api/v4/groups/:group_id/teams/:team_id/link
  28. // DELETE /api/v4/groups/:group_id/channels/:channel_id/link
  29. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/link",
  30. api.ApiSessionRequired(unlinkGroupSyncable)).Methods("DELETE")
  31. // GET /api/v4/groups/:group_id/teams/:team_id
  32. // GET /api/v4/groups/:group_id/channels/:channel_id
  33. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}",
  34. api.ApiSessionRequired(getGroupSyncable)).Methods("GET")
  35. // GET /api/v4/groups/:group_id/teams
  36. // GET /api/v4/groups/:group_id/channels
  37. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}",
  38. api.ApiSessionRequired(getGroupSyncables)).Methods("GET")
  39. // PUT /api/v4/groups/:group_id/teams/:team_id/patch
  40. // PUT /api/v4/groups/:group_id/channels/:channel_id/patch
  41. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/patch",
  42. api.ApiSessionRequired(patchGroupSyncable)).Methods("PUT")
  43. // GET /api/v4/groups/:group_id/stats
  44. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/stats",
  45. api.ApiSessionRequired(getGroupStats)).Methods("GET")
  46. // GET /api/v4/groups/:group_id/members?page=0&per_page=100
  47. api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/members",
  48. api.ApiSessionRequired(getGroupMembers)).Methods("GET")
  49. // GET /api/v4/users/:user_id/groups?page=0&per_page=100
  50. api.BaseRoutes.Users.Handle("/{user_id:[A-Za-z0-9]+}/groups",
  51. api.ApiSessionRequired(getGroupsByUserId)).Methods("GET")
  52. // GET /api/v4/channels/:channel_id/groups?page=0&per_page=100
  53. api.BaseRoutes.Channels.Handle("/{channel_id:[A-Za-z0-9]+}/groups",
  54. api.ApiSessionRequired(getGroupsByChannel)).Methods("GET")
  55. // GET /api/v4/teams/:team_id/groups?page=0&per_page=100
  56. api.BaseRoutes.Teams.Handle("/{team_id:[A-Za-z0-9]+}/groups",
  57. api.ApiSessionRequired(getGroupsByTeam)).Methods("GET")
  58. // GET /api/v4/teams/:team_id/groups_by_channels?page=0&per_page=100
  59. api.BaseRoutes.Teams.Handle("/{team_id:[A-Za-z0-9]+}/groups_by_channels",
  60. api.ApiSessionRequired(getGroupsAssociatedToChannelsByTeam)).Methods("GET")
  61. }
  62. func getGroup(c *Context, w http.ResponseWriter, r *http.Request) {
  63. c.RequireGroupId()
  64. if c.Err != nil {
  65. return
  66. }
  67. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  68. c.Err = model.NewAppError("Api4.getGroup", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  69. return
  70. }
  71. if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  72. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  73. return
  74. }
  75. group, err := c.App.GetGroup(c.Params.GroupId)
  76. if err != nil {
  77. c.Err = err
  78. return
  79. }
  80. b, marshalErr := json.Marshal(group)
  81. if marshalErr != nil {
  82. c.Err = model.NewAppError("Api4.getGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  83. return
  84. }
  85. w.Write(b)
  86. }
  87. func patchGroup(c *Context, w http.ResponseWriter, r *http.Request) {
  88. c.RequireGroupId()
  89. if c.Err != nil {
  90. return
  91. }
  92. groupPatch := model.GroupPatchFromJson(r.Body)
  93. if groupPatch == nil {
  94. c.SetInvalidParam("group")
  95. return
  96. }
  97. auditRec := c.MakeAuditRecord("patchGroup", audit.Fail)
  98. defer c.LogAuditRec(auditRec)
  99. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  100. c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  101. return
  102. }
  103. if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  104. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  105. return
  106. }
  107. group, err := c.App.GetGroup(c.Params.GroupId)
  108. if err != nil {
  109. c.Err = err
  110. return
  111. }
  112. auditRec.AddMeta("group", group)
  113. if groupPatch.AllowReference != nil && *groupPatch.AllowReference {
  114. if groupPatch.Name == nil {
  115. tmp := strings.ReplaceAll(strings.ToLower(group.DisplayName), " ", "-")
  116. groupPatch.Name = &tmp
  117. } else {
  118. if *groupPatch.Name == model.USER_NOTIFY_ALL || *groupPatch.Name == model.CHANNEL_MENTIONS_NOTIFY_PROP || *groupPatch.Name == model.USER_NOTIFY_HERE {
  119. c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.existing_reserved_name_error", nil, "", http.StatusNotImplemented)
  120. return
  121. }
  122. //check if a user already has this group name
  123. user, _ := c.App.GetUserByUsername(*groupPatch.Name)
  124. if user != nil {
  125. c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.existing_user_name_error", nil, "", http.StatusNotImplemented)
  126. return
  127. }
  128. //check if a mentionable group already has this name
  129. searchOpts := model.GroupSearchOpts{
  130. FilterAllowReference: true,
  131. }
  132. existingGroup, _ := c.App.GetGroupByName(*groupPatch.Name, searchOpts)
  133. if existingGroup != nil {
  134. c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.existing_group_name_error", nil, "", http.StatusNotImplemented)
  135. return
  136. }
  137. }
  138. }
  139. group.Patch(groupPatch)
  140. group, err = c.App.UpdateGroup(group)
  141. if err != nil {
  142. c.Err = err
  143. return
  144. }
  145. auditRec.AddMeta("patch", group)
  146. b, marshalErr := json.Marshal(group)
  147. if marshalErr != nil {
  148. c.Err = model.NewAppError("Api4.patchGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  149. return
  150. }
  151. auditRec.Success()
  152. w.Write(b)
  153. }
  154. func linkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
  155. c.RequireGroupId()
  156. if c.Err != nil {
  157. return
  158. }
  159. c.RequireSyncableId()
  160. if c.Err != nil {
  161. return
  162. }
  163. syncableID := c.Params.SyncableId
  164. c.RequireSyncableType()
  165. if c.Err != nil {
  166. return
  167. }
  168. syncableType := c.Params.SyncableType
  169. body, err := ioutil.ReadAll(r.Body)
  170. if err != nil {
  171. c.Err = model.NewAppError("Api4.createGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
  172. return
  173. }
  174. auditRec := c.MakeAuditRecord("linkGroupSyncable", audit.Fail)
  175. defer c.LogAuditRec(auditRec)
  176. auditRec.AddMeta("group_id", c.Params.GroupId)
  177. auditRec.AddMeta("syncable_id", syncableID)
  178. auditRec.AddMeta("syncable_type", syncableType)
  179. var patch *model.GroupSyncablePatch
  180. err = json.Unmarshal(body, &patch)
  181. if err != nil || patch == nil {
  182. c.SetInvalidParam(fmt.Sprintf("Group%s", syncableType.String()))
  183. return
  184. }
  185. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  186. c.Err = model.NewAppError("Api4.createGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  187. return
  188. }
  189. appErr := verifyLinkUnlinkPermission(c, syncableType, syncableID)
  190. if appErr != nil {
  191. c.Err = appErr
  192. return
  193. }
  194. groupSyncable := &model.GroupSyncable{
  195. GroupId: c.Params.GroupId,
  196. SyncableId: syncableID,
  197. Type: syncableType,
  198. }
  199. groupSyncable.Patch(patch)
  200. groupSyncable, appErr = c.App.UpsertGroupSyncable(groupSyncable)
  201. if appErr != nil {
  202. c.Err = appErr
  203. return
  204. }
  205. c.App.Srv().Go(func() {
  206. c.App.SyncRolesAndMembership(syncableID, syncableType)
  207. })
  208. w.WriteHeader(http.StatusCreated)
  209. b, marshalErr := json.Marshal(groupSyncable)
  210. if marshalErr != nil {
  211. c.Err = model.NewAppError("Api4.createGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  212. return
  213. }
  214. auditRec.Success()
  215. w.Write(b)
  216. }
  217. func getGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
  218. c.RequireGroupId()
  219. if c.Err != nil {
  220. return
  221. }
  222. c.RequireSyncableId()
  223. if c.Err != nil {
  224. return
  225. }
  226. syncableID := c.Params.SyncableId
  227. c.RequireSyncableType()
  228. if c.Err != nil {
  229. return
  230. }
  231. syncableType := c.Params.SyncableType
  232. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  233. c.Err = model.NewAppError("Api4.getGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  234. return
  235. }
  236. if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  237. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  238. return
  239. }
  240. groupSyncable, err := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
  241. if err != nil {
  242. c.Err = err
  243. return
  244. }
  245. b, marshalErr := json.Marshal(groupSyncable)
  246. if marshalErr != nil {
  247. c.Err = model.NewAppError("Api4.getGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  248. return
  249. }
  250. w.Write(b)
  251. }
  252. func getGroupSyncables(c *Context, w http.ResponseWriter, r *http.Request) {
  253. c.RequireGroupId()
  254. if c.Err != nil {
  255. return
  256. }
  257. c.RequireSyncableType()
  258. if c.Err != nil {
  259. return
  260. }
  261. syncableType := c.Params.SyncableType
  262. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  263. c.Err = model.NewAppError("Api4.getGroupSyncables", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  264. return
  265. }
  266. if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  267. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  268. return
  269. }
  270. groupSyncables, err := c.App.GetGroupSyncables(c.Params.GroupId, syncableType)
  271. if err != nil {
  272. c.Err = err
  273. return
  274. }
  275. b, marshalErr := json.Marshal(groupSyncables)
  276. if marshalErr != nil {
  277. c.Err = model.NewAppError("Api4.getGroupSyncables", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  278. return
  279. }
  280. w.Write(b)
  281. }
  282. func patchGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
  283. c.RequireGroupId()
  284. if c.Err != nil {
  285. return
  286. }
  287. c.RequireSyncableId()
  288. if c.Err != nil {
  289. return
  290. }
  291. syncableID := c.Params.SyncableId
  292. c.RequireSyncableType()
  293. if c.Err != nil {
  294. return
  295. }
  296. syncableType := c.Params.SyncableType
  297. body, err := ioutil.ReadAll(r.Body)
  298. if err != nil {
  299. c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
  300. return
  301. }
  302. auditRec := c.MakeAuditRecord("patchGroupSyncable", audit.Fail)
  303. defer c.LogAuditRec(auditRec)
  304. auditRec.AddMeta("group_id", c.Params.GroupId)
  305. auditRec.AddMeta("old_syncable_id", syncableID)
  306. auditRec.AddMeta("old_syncable_type", syncableType)
  307. var patch *model.GroupSyncablePatch
  308. err = json.Unmarshal(body, &patch)
  309. if err != nil || patch == nil {
  310. c.SetInvalidParam(fmt.Sprintf("Group[%s]Patch", syncableType.String()))
  311. return
  312. }
  313. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  314. c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.ldap_groups.license_error", nil, "",
  315. http.StatusNotImplemented)
  316. return
  317. }
  318. appErr := verifyLinkUnlinkPermission(c, syncableType, syncableID)
  319. if appErr != nil {
  320. c.Err = appErr
  321. return
  322. }
  323. groupSyncable, appErr := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
  324. if appErr != nil {
  325. c.Err = appErr
  326. return
  327. }
  328. groupSyncable.Patch(patch)
  329. groupSyncable, appErr = c.App.UpdateGroupSyncable(groupSyncable)
  330. if appErr != nil {
  331. c.Err = appErr
  332. return
  333. }
  334. auditRec.AddMeta("new_syncable_id", groupSyncable.SyncableId)
  335. auditRec.AddMeta("new_syncable_type", groupSyncable.Type)
  336. c.App.Srv().Go(func() {
  337. c.App.SyncRolesAndMembership(syncableID, syncableType)
  338. })
  339. b, marshalErr := json.Marshal(groupSyncable)
  340. if marshalErr != nil {
  341. c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  342. return
  343. }
  344. auditRec.Success()
  345. w.Write(b)
  346. }
  347. func unlinkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
  348. c.RequireGroupId()
  349. if c.Err != nil {
  350. return
  351. }
  352. c.RequireSyncableId()
  353. if c.Err != nil {
  354. return
  355. }
  356. syncableID := c.Params.SyncableId
  357. c.RequireSyncableType()
  358. if c.Err != nil {
  359. return
  360. }
  361. syncableType := c.Params.SyncableType
  362. auditRec := c.MakeAuditRecord("unlinkGroupSyncable", audit.Fail)
  363. defer c.LogAuditRec(auditRec)
  364. auditRec.AddMeta("group_id", c.Params.GroupId)
  365. auditRec.AddMeta("syncable_id", syncableID)
  366. auditRec.AddMeta("syncable_type", syncableType)
  367. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  368. c.Err = model.NewAppError("Api4.unlinkGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  369. return
  370. }
  371. err := verifyLinkUnlinkPermission(c, syncableType, syncableID)
  372. if err != nil {
  373. c.Err = err
  374. return
  375. }
  376. _, err = c.App.DeleteGroupSyncable(c.Params.GroupId, syncableID, syncableType)
  377. if err != nil {
  378. c.Err = err
  379. return
  380. }
  381. c.App.Srv().Go(func() {
  382. c.App.SyncRolesAndMembership(syncableID, syncableType)
  383. })
  384. auditRec.Success()
  385. ReturnStatusOK(w)
  386. }
  387. func verifyLinkUnlinkPermission(c *Context, syncableType model.GroupSyncableType, syncableID string) *model.AppError {
  388. switch syncableType {
  389. case model.GroupSyncableTypeTeam:
  390. if !c.App.SessionHasPermissionToTeam(*c.App.Session(), syncableID, model.PERMISSION_MANAGE_TEAM) {
  391. return c.App.MakePermissionError(model.PERMISSION_MANAGE_TEAM)
  392. }
  393. case model.GroupSyncableTypeChannel:
  394. channel, err := c.App.GetChannel(syncableID)
  395. if err != nil {
  396. return err
  397. }
  398. var permission *model.Permission
  399. if channel.Type == model.CHANNEL_PRIVATE {
  400. permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
  401. } else {
  402. permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
  403. }
  404. if !c.App.SessionHasPermissionToChannel(*c.App.Session(), syncableID, permission) {
  405. return c.App.MakePermissionError(permission)
  406. }
  407. }
  408. return nil
  409. }
  410. func getGroupMembers(c *Context, w http.ResponseWriter, r *http.Request) {
  411. c.RequireGroupId()
  412. if c.Err != nil {
  413. return
  414. }
  415. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  416. c.Err = model.NewAppError("Api4.getGroupMembers", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  417. return
  418. }
  419. if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  420. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  421. return
  422. }
  423. members, count, err := c.App.GetGroupMemberUsersPage(c.Params.GroupId, c.Params.Page, c.Params.PerPage)
  424. if err != nil {
  425. c.Err = err
  426. return
  427. }
  428. b, marshalErr := json.Marshal(struct {
  429. Members []*model.User `json:"members"`
  430. Count int `json:"total_member_count"`
  431. }{
  432. Members: members,
  433. Count: count,
  434. })
  435. if marshalErr != nil {
  436. c.Err = model.NewAppError("Api4.getGroupMembers", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  437. return
  438. }
  439. w.Write(b)
  440. }
  441. func getGroupStats(c *Context, w http.ResponseWriter, r *http.Request) {
  442. c.RequireGroupId()
  443. if c.Err != nil {
  444. return
  445. }
  446. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  447. c.Err = model.NewAppError("Api4.getGroupStats", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  448. return
  449. }
  450. if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  451. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  452. return
  453. }
  454. groupID := c.Params.GroupId
  455. count, err := c.App.GetGroupMemberCount(groupID)
  456. if err != nil {
  457. c.Err = err
  458. return
  459. }
  460. b, marshalErr := json.Marshal(model.GroupStats{
  461. GroupID: groupID,
  462. TotalMemberCount: count,
  463. })
  464. if marshalErr != nil {
  465. c.Err = model.NewAppError("Api4.getGroupStats", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  466. return
  467. }
  468. w.Write(b)
  469. }
  470. func getGroupsByUserId(c *Context, w http.ResponseWriter, r *http.Request) {
  471. c.RequireUserId()
  472. if c.Err != nil {
  473. return
  474. }
  475. if c.App.Session().UserId != c.Params.UserId && !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
  476. c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
  477. return
  478. }
  479. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  480. c.Err = model.NewAppError("Api4.getGroupsByUserId", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  481. return
  482. }
  483. groups, err := c.App.GetGroupsByUserId(c.Params.UserId)
  484. if err != nil {
  485. c.Err = err
  486. return
  487. }
  488. b, marshalErr := json.Marshal(groups)
  489. if marshalErr != nil {
  490. c.Err = model.NewAppError("Api4.getGroupsByUserId", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  491. return
  492. }
  493. w.Write(b)
  494. }
  495. func getGroupsByChannel(c *Context, w http.ResponseWriter, r *http.Request) {
  496. c.RequireChannelId()
  497. if c.Err != nil {
  498. return
  499. }
  500. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  501. c.Err = model.NewAppError("Api4.getGroupsByChannel", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  502. return
  503. }
  504. var permission *model.Permission
  505. channel, err := c.App.GetChannel(c.Params.ChannelId)
  506. if err != nil {
  507. c.Err = err
  508. return
  509. }
  510. if channel.Type == model.CHANNEL_PRIVATE {
  511. permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
  512. } else {
  513. permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
  514. }
  515. if !c.App.SessionHasPermissionToChannel(*c.App.Session(), c.Params.ChannelId, permission) {
  516. c.SetPermissionError(permission)
  517. return
  518. }
  519. opts := model.GroupSearchOpts{
  520. Q: c.Params.Q,
  521. IncludeMemberCount: c.Params.IncludeMemberCount,
  522. FilterAllowReference: c.Params.FilterAllowReference,
  523. }
  524. if c.Params.Paginate == nil || *c.Params.Paginate {
  525. opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
  526. }
  527. groups, totalCount, err := c.App.GetGroupsByChannel(c.Params.ChannelId, opts)
  528. if err != nil {
  529. c.Err = err
  530. return
  531. }
  532. b, marshalErr := json.Marshal(struct {
  533. Groups []*model.GroupWithSchemeAdmin `json:"groups"`
  534. Count int `json:"total_group_count"`
  535. }{
  536. Groups: groups,
  537. Count: totalCount,
  538. })
  539. if marshalErr != nil {
  540. c.Err = model.NewAppError("Api4.getGroupsByChannel", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  541. return
  542. }
  543. w.Write(b)
  544. }
  545. func getGroupsByTeam(c *Context, w http.ResponseWriter, r *http.Request) {
  546. c.RequireTeamId()
  547. if c.Err != nil {
  548. return
  549. }
  550. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  551. c.Err = model.NewAppError("Api4.getGroupsByTeam", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  552. return
  553. }
  554. opts := model.GroupSearchOpts{
  555. Q: c.Params.Q,
  556. IncludeMemberCount: c.Params.IncludeMemberCount,
  557. FilterAllowReference: c.Params.FilterAllowReference,
  558. }
  559. if c.Params.Paginate == nil || *c.Params.Paginate {
  560. opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
  561. }
  562. groups, totalCount, err := c.App.GetGroupsByTeam(c.Params.TeamId, opts)
  563. if err != nil {
  564. c.Err = err
  565. return
  566. }
  567. b, marshalErr := json.Marshal(struct {
  568. Groups []*model.GroupWithSchemeAdmin `json:"groups"`
  569. Count int `json:"total_group_count"`
  570. }{
  571. Groups: groups,
  572. Count: totalCount,
  573. })
  574. if marshalErr != nil {
  575. c.Err = model.NewAppError("Api4.getGroupsByTeam", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  576. return
  577. }
  578. w.Write(b)
  579. }
  580. func getGroupsAssociatedToChannelsByTeam(c *Context, w http.ResponseWriter, r *http.Request) {
  581. c.RequireTeamId()
  582. if c.Err != nil {
  583. return
  584. }
  585. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  586. c.Err = model.NewAppError("Api4.getGroupsAssociatedToChannelsByTeam", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  587. return
  588. }
  589. opts := model.GroupSearchOpts{
  590. Q: c.Params.Q,
  591. IncludeMemberCount: c.Params.IncludeMemberCount,
  592. FilterAllowReference: c.Params.FilterAllowReference,
  593. }
  594. if c.Params.Paginate == nil || *c.Params.Paginate {
  595. opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
  596. }
  597. groupsAssociatedByChannelID, err := c.App.GetGroupsAssociatedToChannelsByTeam(c.Params.TeamId, opts)
  598. if err != nil {
  599. c.Err = err
  600. return
  601. }
  602. b, marshalErr := json.Marshal(struct {
  603. GroupsAssociatedToChannels map[string][]*model.GroupWithSchemeAdmin `json:"groups"`
  604. }{
  605. GroupsAssociatedToChannels: groupsAssociatedByChannelID,
  606. })
  607. if marshalErr != nil {
  608. c.Err = model.NewAppError("Api4.getGroupsAssociatedToChannelsByTeam", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  609. return
  610. }
  611. w.Write(b)
  612. }
  613. func getGroups(c *Context, w http.ResponseWriter, r *http.Request) {
  614. if c.App.Srv().License() == nil || !*c.App.Srv().License().Features.LDAPGroups {
  615. c.Err = model.NewAppError("Api4.getGroups", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
  616. return
  617. }
  618. var teamID, channelID string
  619. if id := c.Params.NotAssociatedToTeam; model.IsValidId(id) {
  620. teamID = id
  621. }
  622. if id := c.Params.NotAssociatedToChannel; model.IsValidId(id) {
  623. channelID = id
  624. }
  625. opts := model.GroupSearchOpts{
  626. Q: c.Params.Q,
  627. IncludeMemberCount: c.Params.IncludeMemberCount,
  628. FilterAllowReference: c.Params.FilterAllowReference,
  629. FilterParentTeamPermitted: c.Params.FilterParentTeamPermitted,
  630. }
  631. if teamID != "" {
  632. _, err := c.App.GetTeam(teamID)
  633. if err != nil {
  634. c.Err = err
  635. return
  636. }
  637. opts.NotAssociatedToTeam = teamID
  638. }
  639. if channelID != "" {
  640. channel, err := c.App.GetChannel(channelID)
  641. if err != nil {
  642. c.Err = err
  643. return
  644. }
  645. var permission *model.Permission
  646. if channel.Type == model.CHANNEL_PRIVATE {
  647. permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
  648. } else {
  649. permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
  650. }
  651. if !c.App.SessionHasPermissionToChannel(*c.App.Session(), channelID, permission) {
  652. c.SetPermissionError(permission)
  653. return
  654. }
  655. opts.NotAssociatedToChannel = channelID
  656. }
  657. sinceString := r.URL.Query().Get("since")
  658. if len(sinceString) > 0 {
  659. since, parseError := strconv.ParseInt(sinceString, 10, 64)
  660. if parseError != nil {
  661. c.SetInvalidParam("since")
  662. return
  663. }
  664. opts.Since = since
  665. }
  666. groups, err := c.App.GetGroups(c.Params.Page, c.Params.PerPage, opts)
  667. if err != nil {
  668. c.Err = err
  669. return
  670. }
  671. b, marshalErr := json.Marshal(groups)
  672. if marshalErr != nil {
  673. c.Err = model.NewAppError("Api4.getGroups", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
  674. return
  675. }
  676. w.Write(b)
  677. }