github.com/mad-app/mattermost-server@v5.11.1+incompatible/api4/group.go (about)

     1  // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"database/sql"
     8  	"encoding/json"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"net/http"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  )
    15  
    16  const (
    17  	groupMemberActionCreate = iota
    18  	groupMemberActionDelete
    19  )
    20  
    21  func (api *API) InitGroup() {
    22  	// GET /api/v4/groups/:group_id
    23  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}",
    24  		api.ApiSessionRequired(getGroup)).Methods("GET")
    25  
    26  	// PUT /api/v4/groups/:group_id/patch
    27  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/patch",
    28  		api.ApiSessionRequired(patchGroup)).Methods("PUT")
    29  
    30  	// POST /api/v4/groups/:group_id/teams/:team_id/link
    31  	// POST /api/v4/groups/:group_id/channels/:channel_id/link
    32  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/link",
    33  		api.ApiSessionRequired(linkGroupSyncable)).Methods("POST")
    34  
    35  	// DELETE /api/v4/groups/:group_id/teams/:team_id/link
    36  	// DELETE /api/v4/groups/:group_id/channels/:channel_id/link
    37  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/link",
    38  		api.ApiSessionRequired(unlinkGroupSyncable)).Methods("DELETE")
    39  
    40  	// GET /api/v4/groups/:group_id/teams/:team_id
    41  	// GET /api/v4/groups/:group_id/channels/:channel_id
    42  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}",
    43  		api.ApiSessionRequired(getGroupSyncable)).Methods("GET")
    44  
    45  	// GET /api/v4/groups/:group_id/teams
    46  	// GET /api/v4/groups/:group_id/channels
    47  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}",
    48  		api.ApiSessionRequired(getGroupSyncables)).Methods("GET")
    49  
    50  	// PUT /api/v4/groups/:group_id/teams/:team_id/patch
    51  	// PUT /api/v4/groups/:group_id/channels/:channel_id/patch
    52  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/{syncable_type:teams|channels}/{syncable_id:[A-Za-z0-9]+}/patch",
    53  		api.ApiSessionRequired(patchGroupSyncable)).Methods("PUT")
    54  
    55  	// GET /api/v4/groups/:group_id/members?page=0&per_page=100
    56  	api.BaseRoutes.Groups.Handle("/{group_id:[A-Za-z0-9]+}/members",
    57  		api.ApiSessionRequired(getGroupMembers)).Methods("GET")
    58  }
    59  
    60  func getGroup(c *Context, w http.ResponseWriter, r *http.Request) {
    61  	c.RequireGroupId()
    62  	if c.Err != nil {
    63  		return
    64  	}
    65  
    66  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
    67  		c.Err = model.NewAppError("Api4.getGroup", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
    68  		return
    69  	}
    70  
    71  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
    72  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
    73  		return
    74  	}
    75  
    76  	group, err := c.App.GetGroup(c.Params.GroupId)
    77  	if err != nil {
    78  		c.Err = err
    79  		return
    80  	}
    81  
    82  	b, marshalErr := json.Marshal(group)
    83  	if marshalErr != nil {
    84  		c.Err = model.NewAppError("Api4.getGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
    85  		return
    86  	}
    87  
    88  	w.Write(b)
    89  }
    90  
    91  func patchGroup(c *Context, w http.ResponseWriter, r *http.Request) {
    92  	c.RequireGroupId()
    93  	if c.Err != nil {
    94  		return
    95  	}
    96  
    97  	groupPatch := model.GroupPatchFromJson(r.Body)
    98  	if groupPatch == nil {
    99  		c.SetInvalidParam("group")
   100  		return
   101  	}
   102  
   103  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   104  		c.Err = model.NewAppError("Api4.patchGroup", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
   105  		return
   106  	}
   107  
   108  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   109  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   110  		return
   111  	}
   112  
   113  	group, err := c.App.GetGroup(c.Params.GroupId)
   114  	if err != nil {
   115  		c.Err = err
   116  		return
   117  	}
   118  
   119  	group.Patch(groupPatch)
   120  
   121  	group, err = c.App.UpdateGroup(group)
   122  	if err != nil {
   123  		c.Err = err
   124  		return
   125  	}
   126  
   127  	b, marshalErr := json.Marshal(group)
   128  	if marshalErr != nil {
   129  		c.Err = model.NewAppError("Api4.patchGroup", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
   130  		return
   131  	}
   132  
   133  	w.Write(b)
   134  }
   135  
   136  func linkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
   137  	c.RequireGroupId()
   138  	if c.Err != nil {
   139  		return
   140  	}
   141  
   142  	c.RequireSyncableId()
   143  	if c.Err != nil {
   144  		return
   145  	}
   146  	syncableID := c.Params.SyncableId
   147  
   148  	c.RequireSyncableType()
   149  	if c.Err != nil {
   150  		return
   151  	}
   152  	syncableType := c.Params.SyncableType
   153  
   154  	body, err := ioutil.ReadAll(r.Body)
   155  	if err != nil {
   156  		c.Err = model.NewAppError("Api4.createGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
   157  		return
   158  	}
   159  
   160  	var patch *model.GroupSyncablePatch
   161  	err = json.Unmarshal(body, &patch)
   162  	if err != nil || patch == nil {
   163  		c.SetInvalidParam(fmt.Sprintf("Group%s", syncableType.String()))
   164  		return
   165  	}
   166  
   167  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   168  		c.Err = model.NewAppError("Api4.createGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
   169  		return
   170  	}
   171  
   172  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   173  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   174  		return
   175  	}
   176  
   177  	groupSyncable, appErr := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
   178  	if appErr != nil && appErr.DetailedError != sql.ErrNoRows.Error() {
   179  		c.Err = appErr
   180  		return
   181  	}
   182  
   183  	if groupSyncable == nil {
   184  		groupSyncable = &model.GroupSyncable{
   185  			GroupId:    c.Params.GroupId,
   186  			SyncableId: syncableID,
   187  			Type:       syncableType,
   188  		}
   189  		groupSyncable.Patch(patch)
   190  		groupSyncable, appErr = c.App.CreateGroupSyncable(groupSyncable)
   191  		if appErr != nil {
   192  			c.Err = appErr
   193  			return
   194  		}
   195  	} else {
   196  		groupSyncable.DeleteAt = 0
   197  		groupSyncable.Patch(patch)
   198  		groupSyncable, appErr = c.App.UpdateGroupSyncable(groupSyncable)
   199  		if appErr != nil {
   200  			c.Err = appErr
   201  			return
   202  		}
   203  	}
   204  
   205  	w.WriteHeader(http.StatusCreated)
   206  
   207  	b, marshalErr := json.Marshal(groupSyncable)
   208  	if marshalErr != nil {
   209  		c.Err = model.NewAppError("Api4.createGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
   210  		return
   211  	}
   212  
   213  	w.Write(b)
   214  }
   215  
   216  func getGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
   217  	c.RequireGroupId()
   218  	if c.Err != nil {
   219  		return
   220  	}
   221  
   222  	c.RequireSyncableId()
   223  	if c.Err != nil {
   224  		return
   225  	}
   226  	syncableID := c.Params.SyncableId
   227  
   228  	c.RequireSyncableType()
   229  	if c.Err != nil {
   230  		return
   231  	}
   232  	syncableType := c.Params.SyncableType
   233  
   234  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   235  		c.Err = model.NewAppError("Api4.getGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
   236  		return
   237  	}
   238  
   239  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   240  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   241  		return
   242  	}
   243  
   244  	groupSyncable, err := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
   245  	if err != nil {
   246  		c.Err = err
   247  		return
   248  	}
   249  
   250  	b, marshalErr := json.Marshal(groupSyncable)
   251  	if marshalErr != nil {
   252  		c.Err = model.NewAppError("Api4.getGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
   253  		return
   254  	}
   255  
   256  	w.Write(b)
   257  }
   258  
   259  func getGroupSyncables(c *Context, w http.ResponseWriter, r *http.Request) {
   260  	c.RequireGroupId()
   261  	if c.Err != nil {
   262  		return
   263  	}
   264  
   265  	c.RequireSyncableType()
   266  	if c.Err != nil {
   267  		return
   268  	}
   269  	syncableType := c.Params.SyncableType
   270  
   271  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   272  		c.Err = model.NewAppError("Api4.getGroupSyncables", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
   273  		return
   274  	}
   275  
   276  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   277  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   278  		return
   279  	}
   280  
   281  	groupSyncables, err := c.App.GetGroupSyncables(c.Params.GroupId, syncableType)
   282  	if err != nil {
   283  		c.Err = err
   284  		return
   285  	}
   286  
   287  	b, marshalErr := json.Marshal(groupSyncables)
   288  	if marshalErr != nil {
   289  		c.Err = model.NewAppError("Api4.getGroupSyncables", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
   290  		return
   291  	}
   292  
   293  	w.Write(b)
   294  }
   295  
   296  func patchGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
   297  	c.RequireGroupId()
   298  	if c.Err != nil {
   299  		return
   300  	}
   301  
   302  	c.RequireSyncableId()
   303  	if c.Err != nil {
   304  		return
   305  	}
   306  	syncableID := c.Params.SyncableId
   307  
   308  	c.RequireSyncableType()
   309  	if c.Err != nil {
   310  		return
   311  	}
   312  	syncableType := c.Params.SyncableType
   313  
   314  	body, err := ioutil.ReadAll(r.Body)
   315  	if err != nil {
   316  		c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.io_error", nil, err.Error(), http.StatusBadRequest)
   317  		return
   318  	}
   319  
   320  	var patch *model.GroupSyncablePatch
   321  	err = json.Unmarshal(body, &patch)
   322  	if err != nil || patch == nil {
   323  		c.SetInvalidParam(fmt.Sprintf("Group[%s]Patch", syncableType.String()))
   324  		return
   325  	}
   326  
   327  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   328  		c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.ldap_groups.license_error", nil, "",
   329  			http.StatusNotImplemented)
   330  		return
   331  	}
   332  
   333  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   334  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   335  		return
   336  	}
   337  
   338  	groupSyncable, appErr := c.App.GetGroupSyncable(c.Params.GroupId, syncableID, syncableType)
   339  	if appErr != nil {
   340  		c.Err = appErr
   341  		return
   342  	}
   343  
   344  	groupSyncable.Patch(patch)
   345  
   346  	groupSyncable, appErr = c.App.UpdateGroupSyncable(groupSyncable)
   347  	if appErr != nil {
   348  		c.Err = appErr
   349  		return
   350  	}
   351  
   352  	b, marshalErr := json.Marshal(groupSyncable)
   353  	if marshalErr != nil {
   354  		c.Err = model.NewAppError("Api4.patchGroupSyncable", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
   355  		return
   356  	}
   357  
   358  	w.Write(b)
   359  }
   360  
   361  func unlinkGroupSyncable(c *Context, w http.ResponseWriter, r *http.Request) {
   362  	c.RequireGroupId()
   363  	if c.Err != nil {
   364  		return
   365  	}
   366  
   367  	c.RequireSyncableId()
   368  	if c.Err != nil {
   369  		return
   370  	}
   371  	syncableID := c.Params.SyncableId
   372  
   373  	c.RequireSyncableType()
   374  	if c.Err != nil {
   375  		return
   376  	}
   377  	syncableType := c.Params.SyncableType
   378  
   379  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   380  		c.Err = model.NewAppError("Api4.unlinkGroupSyncable", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
   381  		return
   382  	}
   383  
   384  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   385  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   386  		return
   387  	}
   388  
   389  	_, err := c.App.DeleteGroupSyncable(c.Params.GroupId, syncableID, syncableType)
   390  	if err != nil {
   391  		c.Err = err
   392  		return
   393  	}
   394  
   395  	ReturnStatusOK(w)
   396  }
   397  
   398  func getGroupMembers(c *Context, w http.ResponseWriter, r *http.Request) {
   399  	c.RequireGroupId()
   400  	if c.Err != nil {
   401  		return
   402  	}
   403  
   404  	if c.App.License() == nil || !*c.App.License().Features.LDAPGroups {
   405  		c.Err = model.NewAppError("Api4.getGroupMembers", "api.ldap_groups.license_error", nil, "", http.StatusNotImplemented)
   406  		return
   407  	}
   408  
   409  	if !c.App.SessionHasPermissionTo(c.App.Session, model.PERMISSION_MANAGE_SYSTEM) {
   410  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   411  		return
   412  	}
   413  
   414  	members, count, err := c.App.GetGroupMemberUsersPage(c.Params.GroupId, c.Params.Page, c.Params.PerPage)
   415  	if err != nil {
   416  		c.Err = err
   417  		return
   418  	}
   419  
   420  	b, marshalErr := json.Marshal(struct {
   421  		Members []*model.User `json:"members"`
   422  		Count   int           `json:"total_member_count"`
   423  	}{
   424  		Members: members,
   425  		Count:   count,
   426  	})
   427  	if marshalErr != nil {
   428  		c.Err = model.NewAppError("Api4.getGroupMembers", "api.marshal_error", nil, marshalErr.Error(), http.StatusInternalServerError)
   429  		return
   430  	}
   431  
   432  	w.Write(b)
   433  }