2019-11-29 12:59:40 +01:00
|
|
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See LICENSE.txt for license information.
|
2019-01-10 15:17:31 -05:00
|
|
|
|
|
|
|
|
package api4
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"net/http"
|
2020-05-07 14:35:09 -06:00
|
|
|
"strconv"
|
2020-04-13 11:37:29 -07:00
|
|
|
"strings"
|
2019-01-10 15:17:31 -05:00
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
"github.com/mattermost/mattermost-server/v5/audit"
|
2019-11-28 14:39:38 +01:00
|
|
|
"github.com/mattermost/mattermost-server/v5/model"
|
2019-01-10 15:17:31 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (api *API) InitGroup() {
|
2019-05-10 11:47:21 -04:00
|
|
|
// GET /api/v4/groups
|
|
|
|
|
api.BaseRoutes.Groups.Handle("", api.ApiSessionRequired(getGroups)).Methods("GET")
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
// GET /api/v4/groups/:group_id
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}",
|
|
|
|
|
api.ApiSessionRequired(getGroup)).Methods("GET")
|
|
|
|
|
|
|
|
|
|
// PUT /api/v4/groups/:group_id/patch
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/patch",
|
|
|
|
|
api.ApiSessionRequired(patchGroup)).Methods("PUT")
|
|
|
|
|
|
|
|
|
|
// POST /api/v4/groups/:group_id/teams/:team_id/link
|
|
|
|
|
// POST /api/v4/groups/:group_id/channels/:channel_id/link
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/link",
|
|
|
|
|
api.ApiSessionRequired(linkGroupSyncable)).Methods("POST")
|
|
|
|
|
|
|
|
|
|
// DELETE /api/v4/groups/:group_id/teams/:team_id/link
|
|
|
|
|
// DELETE /api/v4/groups/:group_id/channels/:channel_id/link
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/link",
|
|
|
|
|
api.ApiSessionRequired(unlinkGroupSyncable)).Methods("DELETE")
|
|
|
|
|
|
|
|
|
|
// GET /api/v4/groups/:group_id/teams/:team_id
|
|
|
|
|
// GET /api/v4/groups/:group_id/channels/:channel_id
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}",
|
|
|
|
|
api.ApiSessionRequired(getGroupSyncable)).Methods("GET")
|
|
|
|
|
|
|
|
|
|
// GET /api/v4/groups/:group_id/teams
|
|
|
|
|
// GET /api/v4/groups/:group_id/channels
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}",
|
|
|
|
|
api.ApiSessionRequired(getGroupSyncables)).Methods("GET")
|
|
|
|
|
|
|
|
|
|
// PUT /api/v4/groups/:group_id/teams/:team_id/patch
|
|
|
|
|
// PUT /api/v4/groups/:group_id/channels/:channel_id/patch
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/patch",
|
|
|
|
|
api.ApiSessionRequired(patchGroupSyncable)).Methods("PUT")
|
|
|
|
|
|
|
|
|
|
// GET /api/v4/groups/:group_id/members?page=0&per_page=100
|
|
|
|
|
api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/members",
|
|
|
|
|
api.ApiSessionRequired(getGroupMembers)).Methods("GET")
|
2019-04-02 21:02:51 +01:00
|
|
|
|
2020-05-07 13:35:56 -06:00
|
|
|
// GET /api/v4/users/:user_id/groups?page=0&per_page=100
|
|
|
|
|
api.BaseRoutes.Users.Handle("/{user_id:[A-Za-z0-9]+}/groups",
|
|
|
|
|
api.ApiSessionRequired(getGroupsByUserId)).Methods("GET")
|
|
|
|
|
|
2019-04-02 21:02:51 +01:00
|
|
|
// GET /api/v4/channels/:channel_id/groups?page=0&per_page=100
|
|
|
|
|
api.BaseRoutes.Channels.Handle("/{channel_id:[A-Za-z0-9]+}/groups",
|
|
|
|
|
api.ApiSessionRequired(getGroupsByChannel)).Methods("GET")
|
|
|
|
|
|
|
|
|
|
// GET /api/v4/teams/:team_id/groups?page=0&per_page=100
|
|
|
|
|
api.BaseRoutes.Teams.Handle("/{team_id:[A-Za-z0-9]+}/groups",
|
|
|
|
|
api.ApiSessionRequired(getGroupsByTeam)).Methods("GET")
|
2020-04-13 11:37:29 -07:00
|
|
|
|
|
|
|
|
// GET /api/v4/teams/:team_id/groups_by_channels?page=0&per_page=100
|
|
|
|
|
api.BaseRoutes.Teams.Handle("/{team_id:[A-Za-z0-9]+}/groups_by_channels",
|
|
|
|
|
api.ApiSessionRequired(getGroupsAssociatedToChannelsByTeam)).Methods("GET")
|
2019-01-10 15:17:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getGroup(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroup", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
|
2019-01-10 15:17:31 -05:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group, err := c.App.GetGroup(c.Params.GroupId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(group)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func patchGroup(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupPatch := model.GroupPatchFromJson(r.Body)
|
|
|
|
|
if groupPatch == nil {
|
|
|
|
|
c.SetInvalidParam("group")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec := c.MakeAuditRecord("patchGroup", audit.Fail)
|
|
|
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
|
2019-01-10 15:17:31 -05:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group, err := c.App.GetGroup(c.Params.GroupId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-08 00:52:30 -04:00
|
|
|
auditRec.AddMeta("group", group)
|
2019-01-10 15:17:31 -05:00
|
|
|
|
2020-04-13 11:37:29 -07:00
|
|
|
if groupPatch.AllowReference != nil && *groupPatch.AllowReference {
|
|
|
|
|
if groupPatch.Name == nil {
|
2020-05-12 08:35:03 -07:00
|
|
|
tmp := strings.ReplaceAll(strings.ToLower(group.DisplayName), " ", "-")
|
|
|
|
|
groupPatch.Name = &tmp
|
|
|
|
|
} else {
|
|
|
|
|
if *groupPatch.Name == model.USER_NOTIFY_ALL || *groupPatch.Name == model.CHANNEL_MENTIONS_NOTIFY_PROP || *groupPatch.Name == model.USER_NOTIFY_HERE {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.existing_reserved_name_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//check if a user already has this group name
|
|
|
|
|
user, _ := c.App.GetUserByUsername(*groupPatch.Name)
|
|
|
|
|
if user != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.existing_user_name_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
//check if a mentionable group already has this name
|
|
|
|
|
searchOpts := model.GroupSearchOpts{
|
|
|
|
|
FilterAllowReference: true,
|
|
|
|
|
}
|
|
|
|
|
existingGroup, _ := c.App.GetGroupByName(*groupPatch.Name, searchOpts)
|
|
|
|
|
if existingGroup != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.existing_group_name_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-13 11:37:29 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-05-12 08:35:03 -07:00
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
group.Patch(groupPatch)
|
|
|
|
|
|
|
|
|
|
group, err = c.App.UpdateGroup(group)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-08 00:52:30 -04:00
|
|
|
auditRec.AddMeta("patch", group)
|
2020-03-12 15:50:21 -04:00
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
b, marshalErr := json.Marshal(group)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec.Success()
|
2019-01-10 15:17:31 -05:00
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func linkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableID := c.Params.SyncableId
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableType()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableType := c.Params.SyncableType
|
|
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.createGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec := c.MakeAuditRecord("linkGroupSyncable", audit.Fail)
|
|
|
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
|
auditRec.AddMeta("group_id", c.Params.GroupId)
|
|
|
|
|
auditRec.AddMeta("syncable_id", syncableID)
|
|
|
|
|
auditRec.AddMeta("syncable_type", syncableType)
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
var patch *model.GroupSyncablePatch
|
|
|
|
|
err = json.Unmarshal(body, &patch)
|
|
|
|
|
if err != nil || patch == nil {
|
|
|
|
|
c.SetInvalidParam(fmt.Sprintf("Group%s", syncableType.String()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.createGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
appErr := verifyLinkUnlinkPermission(c, syncableType, syncableID)
|
|
|
|
|
if appErr != nil {
|
|
|
|
|
c.Err = appErr
|
2019-01-10 15:17:31 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-18 08:54:09 -05:00
|
|
|
groupSyncable := &model.GroupSyncable{
|
|
|
|
|
GroupId: c.Params.GroupId,
|
|
|
|
|
SyncableId: syncableID,
|
|
|
|
|
Type: syncableType,
|
|
|
|
|
}
|
|
|
|
|
groupSyncable.Patch(patch)
|
|
|
|
|
groupSyncable, appErr = c.App.UpsertGroupSyncable(groupSyncable)
|
|
|
|
|
if appErr != nil {
|
2019-01-10 15:17:31 -05:00
|
|
|
c.Err = appErr
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 11:45:54 -04:00
|
|
|
c.App.Srv().Go(func() {
|
|
|
|
|
c.App.SyncRolesAndMembership(syncableID, syncableType)
|
|
|
|
|
})
|
2020-01-20 09:10:25 -05:00
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(groupSyncable)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.createGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec.Success()
|
2019-01-10 15:17:31 -05:00
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableID := c.Params.SyncableId
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableType()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableType := c.Params.SyncableType
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
|
2019-01-10 15:17:31 -05:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupSyncable, err := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(groupSyncable)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getGroupSyncables(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableType()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableType := c.Params.SyncableType
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupSyncables", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
|
2019-01-10 15:17:31 -05:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupSyncables, err := c.App.GetGroupSyncables(c.Params.GroupId, syncableType)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(groupSyncables)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupSyncables", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func patchGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableID := c.Params.SyncableId
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableType()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableType := c.Params.SyncableType
|
|
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec := c.MakeAuditRecord("patchGroupSyncable", audit.Fail)
|
|
|
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
|
auditRec.AddMeta("group_id", c.Params.GroupId)
|
|
|
|
|
auditRec.AddMeta("old_syncable_id", syncableID)
|
|
|
|
|
auditRec.AddMeta("old_syncable_type", syncableType)
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
var patch *model.GroupSyncablePatch
|
|
|
|
|
err = json.Unmarshal(body, &patch)
|
|
|
|
|
if err != nil || patch == nil {
|
|
|
|
|
c.SetInvalidParam(fmt.Sprintf("Group[%s]Patch", syncableType.String()))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.ldap_groups.license_error", nil, "",
|
|
|
|
|
http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-10 12:19:39 -05:00
|
|
|
appErr := verifyLinkUnlinkPermission(c, syncableType, syncableID)
|
|
|
|
|
if appErr != nil {
|
|
|
|
|
c.Err = appErr
|
2019-01-10 15:17:31 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupSyncable, appErr := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
|
|
|
|
|
if appErr != nil {
|
|
|
|
|
c.Err = appErr
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupSyncable.Patch(patch)
|
|
|
|
|
|
|
|
|
|
groupSyncable, appErr = c.App.UpdateGroupSyncable(groupSyncable)
|
|
|
|
|
if appErr != nil {
|
|
|
|
|
c.Err = appErr
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec.AddMeta("new_syncable_id", groupSyncable.SyncableId)
|
|
|
|
|
auditRec.AddMeta("new_syncable_type", groupSyncable.Type)
|
|
|
|
|
|
2020-05-11 11:45:54 -04:00
|
|
|
c.App.Srv().Go(func() {
|
|
|
|
|
c.App.SyncRolesAndMembership(syncableID, syncableType)
|
|
|
|
|
})
|
2020-01-20 09:10:25 -05:00
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
b, marshalErr := json.Marshal(groupSyncable)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec.Success()
|
2019-01-10 15:17:31 -05:00
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func unlinkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableID := c.Params.SyncableId
|
|
|
|
|
|
|
|
|
|
c.RequireSyncableType()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
syncableType := c.Params.SyncableType
|
|
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec := c.MakeAuditRecord("unlinkGroupSyncable", audit.Fail)
|
|
|
|
|
defer c.LogAuditRec(auditRec)
|
|
|
|
|
auditRec.AddMeta("group_id", c.Params.GroupId)
|
|
|
|
|
auditRec.AddMeta("syncable_id", syncableID)
|
|
|
|
|
auditRec.AddMeta("syncable_type", syncableType)
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.unlinkGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
err := verifyLinkUnlinkPermission(c, syncableType, syncableID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
2019-01-10 15:17:31 -05:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
_, err = c.App.DeleteGroupSyncable(c.Params.GroupId, syncableID, syncableType)
|
2019-01-10 15:17:31 -05:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-11 11:45:54 -04:00
|
|
|
c.App.Srv().Go(func() {
|
|
|
|
|
c.App.SyncRolesAndMembership(syncableID, syncableType)
|
|
|
|
|
})
|
2020-01-20 09:10:25 -05:00
|
|
|
|
2020-03-12 15:50:21 -04:00
|
|
|
auditRec.Success()
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
ReturnStatusOK(w)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
func verifyLinkUnlinkPermission(c *Context, syncableType model.GroupSyncableType, syncableID string) *model.AppError {
|
|
|
|
|
switch syncableType {
|
|
|
|
|
case model.GroupSyncableTypeTeam:
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionToTeam(*c.App.Session(), syncableID, model.PERMISSION_MANAGE_TEAM) {
|
2019-05-10 11:47:21 -04:00
|
|
|
return c.App.MakePermissionError(model.PERMISSION_MANAGE_TEAM)
|
|
|
|
|
}
|
|
|
|
|
case model.GroupSyncableTypeChannel:
|
|
|
|
|
channel, err := c.App.GetChannel(syncableID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var permission *model.Permission
|
|
|
|
|
if channel.Type == model.CHANNEL_PRIVATE {
|
|
|
|
|
permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
|
|
|
|
|
} else {
|
|
|
|
|
permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionToChannel(*c.App.Session(), syncableID, permission) {
|
2019-05-10 11:47:21 -04:00
|
|
|
return c.App.MakePermissionError(permission)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 15:17:31 -05:00
|
|
|
func getGroupMembers(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireGroupId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupMembers", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
|
2019-01-10 15:17:31 -05:00
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
members, count, err := c.App.GetGroupMemberUsersPage(c.Params.GroupId, c.Params.Page, c.Params.PerPage)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(struct {
|
|
|
|
|
Members []*model.User `json:"members"`
|
|
|
|
|
Count int `json:"total_member_count"`
|
|
|
|
|
}{
|
|
|
|
|
Members: members,
|
|
|
|
|
Count: count,
|
|
|
|
|
})
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupMembers", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
2019-04-02 21:02:51 +01:00
|
|
|
|
2020-05-07 13:35:56 -06:00
|
|
|
func getGroupsByUserId(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireUserId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.Session().UserId != c.Params.UserId && !c.App.SessionHasPermissionTo(*c.App.Session(), model.PERMISSION_MANAGE_SYSTEM) {
|
|
|
|
|
c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsByUserId", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groups, err := c.App.GetGroupsByUserId(c.Params.UserId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(groups)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsByUserId", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-02 21:02:51 +01:00
|
|
|
func getGroupsByChannel(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireChannelId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsByChannel", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 12:03:47 -04:00
|
|
|
var permission *model.Permission
|
|
|
|
|
channel, err := c.App.GetChannel(c.Params.ChannelId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if channel.Type == model.CHANNEL_PRIVATE {
|
|
|
|
|
permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
|
|
|
|
|
} else {
|
|
|
|
|
permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
|
|
|
|
|
}
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionToChannel(*c.App.Session(), c.Params.ChannelId, permission) {
|
2019-05-15 12:03:47 -04:00
|
|
|
c.SetPermissionError(permission)
|
2019-04-02 21:02:51 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 12:03:47 -04:00
|
|
|
opts := model.GroupSearchOpts{
|
2020-04-13 11:37:29 -07:00
|
|
|
Q: c.Params.Q,
|
|
|
|
|
IncludeMemberCount: c.Params.IncludeMemberCount,
|
|
|
|
|
FilterAllowReference: c.Params.FilterAllowReference,
|
2019-05-15 12:03:47 -04:00
|
|
|
}
|
|
|
|
|
if c.Params.Paginate == nil || *c.Params.Paginate {
|
|
|
|
|
opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groups, totalCount, err := c.App.GetGroupsByChannel(c.Params.ChannelId, opts)
|
2019-04-02 21:02:51 +01:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-15 12:03:47 -04:00
|
|
|
b, marshalErr := json.Marshal(struct {
|
2020-01-10 12:19:39 -05:00
|
|
|
Groups []*model.GroupWithSchemeAdmin `json:"groups"`
|
|
|
|
|
Count int `json:"total_group_count"`
|
2019-05-15 12:03:47 -04:00
|
|
|
}{
|
|
|
|
|
Groups: groups,
|
|
|
|
|
Count: totalCount,
|
|
|
|
|
})
|
|
|
|
|
|
2019-04-02 21:02:51 +01:00
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsByChannel", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getGroupsByTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireTeamId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsByTeam", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
opts := model.GroupSearchOpts{
|
2020-04-13 11:37:29 -07:00
|
|
|
Q: c.Params.Q,
|
|
|
|
|
IncludeMemberCount: c.Params.IncludeMemberCount,
|
|
|
|
|
FilterAllowReference: c.Params.FilterAllowReference,
|
2019-05-10 11:47:21 -04:00
|
|
|
}
|
|
|
|
|
if c.Params.Paginate == nil || *c.Params.Paginate {
|
|
|
|
|
opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groups, totalCount, err := c.App.GetGroupsByTeam(c.Params.TeamId, opts)
|
2019-04-02 21:02:51 +01:00
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
b, marshalErr := json.Marshal(struct {
|
2020-01-10 12:19:39 -05:00
|
|
|
Groups []*model.GroupWithSchemeAdmin `json:"groups"`
|
|
|
|
|
Count int `json:"total_group_count"`
|
2019-05-10 11:47:21 -04:00
|
|
|
}{
|
|
|
|
|
Groups: groups,
|
|
|
|
|
Count: totalCount,
|
|
|
|
|
})
|
|
|
|
|
|
2019-04-02 21:02:51 +01:00
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsByTeam", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
2019-05-10 11:47:21 -04:00
|
|
|
|
2020-04-13 11:37:29 -07:00
|
|
|
func getGroupsAssociatedToChannelsByTeam(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
c.RequireTeamId()
|
|
|
|
|
if c.Err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsAssociatedToChannelsByTeam", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := model.GroupSearchOpts{
|
|
|
|
|
Q: c.Params.Q,
|
|
|
|
|
IncludeMemberCount: c.Params.IncludeMemberCount,
|
|
|
|
|
FilterAllowReference: c.Params.FilterAllowReference,
|
|
|
|
|
}
|
|
|
|
|
if c.Params.Paginate == nil || *c.Params.Paginate {
|
|
|
|
|
opts.PageOpts = &model.PageOpts{Page: c.Params.Page, PerPage: c.Params.PerPage}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groupsAssociatedByChannelID, err := c.App.GetGroupsAssociatedToChannelsByTeam(c.Params.TeamId, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(struct {
|
|
|
|
|
GroupsAssociatedToChannels map[string][]*model.GroupWithSchemeAdmin `json:"groups"`
|
|
|
|
|
}{
|
|
|
|
|
GroupsAssociatedToChannels: groupsAssociatedByChannelID,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroupsAssociatedToChannelsByTeam", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
func getGroups(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroups", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-11-14 09:21:23 -05:00
|
|
|
var teamID, channelID string
|
|
|
|
|
|
|
|
|
|
if id := c.Params.NotAssociatedToTeam; model.IsValidId(id) {
|
|
|
|
|
teamID = id
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if id := c.Params.NotAssociatedToChannel; model.IsValidId(id) {
|
|
|
|
|
channelID = id
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
opts := model.GroupSearchOpts{
|
2020-05-29 10:46:52 -04:00
|
|
|
Q: c.Params.Q,
|
|
|
|
|
IncludeMemberCount: c.Params.IncludeMemberCount,
|
|
|
|
|
FilterAllowReference: c.Params.FilterAllowReference,
|
|
|
|
|
FilterParentTeamPermitted: c.Params.FilterParentTeamPermitted,
|
2019-05-10 11:47:21 -04:00
|
|
|
}
|
|
|
|
|
|
2019-11-14 09:21:23 -05:00
|
|
|
if teamID != "" {
|
|
|
|
|
_, err := c.App.GetTeam(teamID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-04-13 11:37:29 -07:00
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
opts.NotAssociatedToTeam = teamID
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-14 09:21:23 -05:00
|
|
|
if channelID != "" {
|
2019-05-15 12:03:47 -04:00
|
|
|
channel, err := c.App.GetChannel(channelID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var permission *model.Permission
|
|
|
|
|
if channel.Type == model.CHANNEL_PRIVATE {
|
|
|
|
|
permission = model.PERMISSION_MANAGE_PRIVATE_CHANNEL_MEMBERS
|
|
|
|
|
} else {
|
|
|
|
|
permission = model.PERMISSION_MANAGE_PUBLIC_CHANNEL_MEMBERS
|
|
|
|
|
}
|
2020-02-13 13:26:58 +01:00
|
|
|
if !c.App.SessionHasPermissionToChannel(*c.App.Session(), channelID, permission) {
|
2019-05-15 12:03:47 -04:00
|
|
|
c.SetPermissionError(permission)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
opts.NotAssociatedToChannel = channelID
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-07 14:35:09 -06:00
|
|
|
sinceString := r.URL.Query().Get("since")
|
|
|
|
|
if len(sinceString) > 0 {
|
|
|
|
|
since, parseError := strconv.ParseInt(sinceString, 10, 64)
|
|
|
|
|
if parseError != nil {
|
|
|
|
|
c.SetInvalidParam("since")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
opts.Since = since
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-10 11:47:21 -04:00
|
|
|
groups, err := c.App.GetGroups(c.Params.Page, c.Params.PerPage, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
c.Err = err
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b, marshalErr := json.Marshal(groups)
|
|
|
|
|
if marshalErr != nil {
|
|
|
|
|
c.Err = model.NewAppError("Api4.getGroups", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Write(b)
|
|
|
|
|
}
|