github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/api4/team.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package api4
     5  
     6  import (
     7  	"bytes"
     8  	"encoding/base64"
     9  	"fmt"
    10  	"net/http"
    11  	"strconv"
    12  
    13  	"github.com/mattermost/mattermost-server/model"
    14  )
    15  
    16  const (
    17  	MAX_ADD_MEMBERS_BATCH = 20
    18  )
    19  
    20  func (api *API) InitTeam() {
    21  	api.BaseRoutes.Teams.Handle("", api.ApiSessionRequired(createTeam)).Methods("POST")
    22  	api.BaseRoutes.Teams.Handle("", api.ApiSessionRequired(getAllTeams)).Methods("GET")
    23  	api.BaseRoutes.Teams.Handle("/{team_id:[A-Za-z0-9]+}/scheme", api.ApiSessionRequired(updateTeamScheme)).Methods("PUT")
    24  	api.BaseRoutes.Teams.Handle("/search", api.ApiSessionRequired(searchTeams)).Methods("POST")
    25  	api.BaseRoutes.TeamsForUser.Handle("", api.ApiSessionRequired(getTeamsForUser)).Methods("GET")
    26  	api.BaseRoutes.TeamsForUser.Handle("/unread", api.ApiSessionRequired(getTeamsUnreadForUser)).Methods("GET")
    27  
    28  	api.BaseRoutes.Team.Handle("", api.ApiSessionRequired(getTeam)).Methods("GET")
    29  	api.BaseRoutes.Team.Handle("", api.ApiSessionRequired(updateTeam)).Methods("PUT")
    30  	api.BaseRoutes.Team.Handle("", api.ApiSessionRequired(deleteTeam)).Methods("DELETE")
    31  	api.BaseRoutes.Team.Handle("/patch", api.ApiSessionRequired(patchTeam)).Methods("PUT")
    32  	api.BaseRoutes.Team.Handle("/stats", api.ApiSessionRequired(getTeamStats)).Methods("GET")
    33  
    34  	api.BaseRoutes.Team.Handle("/image", api.ApiSessionRequiredTrustRequester(getTeamIcon)).Methods("GET")
    35  	api.BaseRoutes.Team.Handle("/image", api.ApiSessionRequired(setTeamIcon)).Methods("POST")
    36  	api.BaseRoutes.Team.Handle("/image", api.ApiSessionRequired(removeTeamIcon)).Methods("DELETE")
    37  
    38  	api.BaseRoutes.TeamMembers.Handle("", api.ApiSessionRequired(getTeamMembers)).Methods("GET")
    39  	api.BaseRoutes.TeamMembers.Handle("/ids", api.ApiSessionRequired(getTeamMembersByIds)).Methods("POST")
    40  	api.BaseRoutes.TeamMembersForUser.Handle("", api.ApiSessionRequired(getTeamMembersForUser)).Methods("GET")
    41  	api.BaseRoutes.TeamMembers.Handle("", api.ApiSessionRequired(addTeamMember)).Methods("POST")
    42  	api.BaseRoutes.Teams.Handle("/members/invite", api.ApiSessionRequired(addUserToTeamFromInvite)).Methods("POST")
    43  	api.BaseRoutes.TeamMembers.Handle("/batch", api.ApiSessionRequired(addTeamMembers)).Methods("POST")
    44  	api.BaseRoutes.TeamMember.Handle("", api.ApiSessionRequired(removeTeamMember)).Methods("DELETE")
    45  
    46  	api.BaseRoutes.TeamForUser.Handle("/unread", api.ApiSessionRequired(getTeamUnread)).Methods("GET")
    47  
    48  	api.BaseRoutes.TeamByName.Handle("", api.ApiSessionRequired(getTeamByName)).Methods("GET")
    49  	api.BaseRoutes.TeamMember.Handle("", api.ApiSessionRequired(getTeamMember)).Methods("GET")
    50  	api.BaseRoutes.TeamByName.Handle("/exists", api.ApiSessionRequired(teamExists)).Methods("GET")
    51  	api.BaseRoutes.TeamMember.Handle("/roles", api.ApiSessionRequired(updateTeamMemberRoles)).Methods("PUT")
    52  
    53  	api.BaseRoutes.Team.Handle("/import", api.ApiSessionRequired(importTeam)).Methods("POST")
    54  	api.BaseRoutes.Team.Handle("/invite/email", api.ApiSessionRequired(inviteUsersToTeam)).Methods("POST")
    55  	api.BaseRoutes.Teams.Handle("/invite/{invite_id:[A-Za-z0-9]+}", api.ApiHandler(getInviteInfo)).Methods("GET")
    56  }
    57  
    58  func createTeam(c *Context, w http.ResponseWriter, r *http.Request) {
    59  	team := model.TeamFromJson(r.Body)
    60  	if team == nil {
    61  		c.SetInvalidParam("team")
    62  		return
    63  	}
    64  
    65  	if !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_CREATE_TEAM) {
    66  		c.Err = model.NewAppError("createTeam", "api.team.is_team_creation_allowed.disabled.app_error", nil, "", http.StatusForbidden)
    67  		return
    68  	}
    69  
    70  	rteam, err := c.App.CreateTeamWithUser(team, c.Session.UserId)
    71  	if err != nil {
    72  		c.Err = err
    73  		return
    74  	}
    75  
    76  	// Don't sanitize the team here since the user will be a team admin and their session won't reflect that yet
    77  
    78  	w.WriteHeader(http.StatusCreated)
    79  	w.Write([]byte(rteam.ToJson()))
    80  }
    81  
    82  func getTeam(c *Context, w http.ResponseWriter, r *http.Request) {
    83  	c.RequireTeamId()
    84  	if c.Err != nil {
    85  		return
    86  	}
    87  
    88  	if team, err := c.App.GetTeam(c.Params.TeamId); err != nil {
    89  		c.Err = err
    90  		return
    91  	} else {
    92  		if (!team.AllowOpenInvite || team.Type != model.TEAM_OPEN) && !c.App.SessionHasPermissionToTeam(c.Session, team.Id, model.PERMISSION_VIEW_TEAM) {
    93  			c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
    94  			return
    95  		}
    96  
    97  		c.App.SanitizeTeam(c.Session, team)
    98  
    99  		w.Write([]byte(team.ToJson()))
   100  		return
   101  	}
   102  }
   103  
   104  func getTeamByName(c *Context, w http.ResponseWriter, r *http.Request) {
   105  	c.RequireTeamName()
   106  	if c.Err != nil {
   107  		return
   108  	}
   109  
   110  	if team, err := c.App.GetTeamByName(c.Params.TeamName); err != nil {
   111  		c.Err = err
   112  		return
   113  	} else {
   114  		if (!team.AllowOpenInvite || team.Type != model.TEAM_OPEN) && !c.App.SessionHasPermissionToTeam(c.Session, team.Id, model.PERMISSION_VIEW_TEAM) {
   115  			c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   116  			return
   117  		}
   118  
   119  		c.App.SanitizeTeam(c.Session, team)
   120  
   121  		w.Write([]byte(team.ToJson()))
   122  		return
   123  	}
   124  }
   125  
   126  func updateTeam(c *Context, w http.ResponseWriter, r *http.Request) {
   127  	c.RequireTeamId()
   128  	if c.Err != nil {
   129  		return
   130  	}
   131  
   132  	team := model.TeamFromJson(r.Body)
   133  
   134  	if team == nil {
   135  		c.SetInvalidParam("team")
   136  		return
   137  	}
   138  
   139  	team.Id = c.Params.TeamId
   140  
   141  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM) {
   142  		c.SetPermissionError(model.PERMISSION_MANAGE_TEAM)
   143  		return
   144  	}
   145  
   146  	updatedTeam, err := c.App.UpdateTeam(team)
   147  
   148  	if err != nil {
   149  		c.Err = err
   150  		return
   151  	}
   152  
   153  	c.App.SanitizeTeam(c.Session, updatedTeam)
   154  
   155  	w.Write([]byte(updatedTeam.ToJson()))
   156  }
   157  
   158  func patchTeam(c *Context, w http.ResponseWriter, r *http.Request) {
   159  	c.RequireTeamId()
   160  	if c.Err != nil {
   161  		return
   162  	}
   163  
   164  	team := model.TeamPatchFromJson(r.Body)
   165  
   166  	if team == nil {
   167  		c.SetInvalidParam("team")
   168  		return
   169  	}
   170  
   171  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM) {
   172  		c.SetPermissionError(model.PERMISSION_MANAGE_TEAM)
   173  		return
   174  	}
   175  
   176  	patchedTeam, err := c.App.PatchTeam(c.Params.TeamId, team)
   177  
   178  	if err != nil {
   179  		c.Err = err
   180  		return
   181  	}
   182  
   183  	c.App.SanitizeTeam(c.Session, patchedTeam)
   184  
   185  	c.LogAudit("")
   186  	w.Write([]byte(patchedTeam.ToJson()))
   187  }
   188  
   189  func deleteTeam(c *Context, w http.ResponseWriter, r *http.Request) {
   190  	c.RequireTeamId()
   191  	if c.Err != nil {
   192  		return
   193  	}
   194  
   195  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM) {
   196  		c.SetPermissionError(model.PERMISSION_MANAGE_TEAM)
   197  		return
   198  	}
   199  
   200  	var err *model.AppError
   201  	if c.Params.Permanent && *c.App.Config().ServiceSettings.EnableAPITeamDeletion {
   202  		err = c.App.PermanentDeleteTeamId(c.Params.TeamId)
   203  	} else {
   204  		err = c.App.SoftDeleteTeam(c.Params.TeamId)
   205  	}
   206  
   207  	if err != nil {
   208  		c.Err = err
   209  		return
   210  	}
   211  
   212  	ReturnStatusOK(w)
   213  }
   214  
   215  func getTeamsForUser(c *Context, w http.ResponseWriter, r *http.Request) {
   216  	c.RequireUserId()
   217  	if c.Err != nil {
   218  		return
   219  	}
   220  
   221  	if c.Session.UserId != c.Params.UserId && !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   222  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   223  		return
   224  	}
   225  
   226  	if teams, err := c.App.GetTeamsForUser(c.Params.UserId); err != nil {
   227  		c.Err = err
   228  		return
   229  	} else {
   230  		c.App.SanitizeTeams(c.Session, teams)
   231  
   232  		w.Write([]byte(model.TeamListToJson(teams)))
   233  	}
   234  }
   235  
   236  func getTeamsUnreadForUser(c *Context, w http.ResponseWriter, r *http.Request) {
   237  	c.RequireUserId()
   238  	if c.Err != nil {
   239  		return
   240  	}
   241  
   242  	if c.Session.UserId != c.Params.UserId && !c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   243  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   244  		return
   245  	}
   246  
   247  	// optional team id to be excluded from the result
   248  	teamId := r.URL.Query().Get("exclude_team")
   249  
   250  	unreadTeamsList, err := c.App.GetTeamsUnreadForUser(teamId, c.Params.UserId)
   251  	if err != nil {
   252  		c.Err = err
   253  		return
   254  	}
   255  
   256  	w.Write([]byte(model.TeamsUnreadToJson(unreadTeamsList)))
   257  }
   258  
   259  func getTeamMember(c *Context, w http.ResponseWriter, r *http.Request) {
   260  	c.RequireTeamId().RequireUserId()
   261  	if c.Err != nil {
   262  		return
   263  	}
   264  
   265  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) {
   266  		c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   267  		return
   268  	}
   269  
   270  	if team, err := c.App.GetTeamMember(c.Params.TeamId, c.Params.UserId); err != nil {
   271  		c.Err = err
   272  		return
   273  	} else {
   274  		w.Write([]byte(team.ToJson()))
   275  		return
   276  	}
   277  }
   278  
   279  func getTeamMembers(c *Context, w http.ResponseWriter, r *http.Request) {
   280  	c.RequireTeamId()
   281  	if c.Err != nil {
   282  		return
   283  	}
   284  
   285  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) {
   286  		c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   287  		return
   288  	}
   289  
   290  	if members, err := c.App.GetTeamMembers(c.Params.TeamId, c.Params.Page*c.Params.PerPage, c.Params.PerPage); err != nil {
   291  		c.Err = err
   292  		return
   293  	} else {
   294  		w.Write([]byte(model.TeamMembersToJson(members)))
   295  		return
   296  	}
   297  }
   298  
   299  func getTeamMembersForUser(c *Context, w http.ResponseWriter, r *http.Request) {
   300  	c.RequireUserId()
   301  	if c.Err != nil {
   302  		return
   303  	}
   304  
   305  	if !c.App.SessionHasPermissionToUser(c.Session, c.Params.UserId) {
   306  		c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS)
   307  		return
   308  	}
   309  
   310  	members, err := c.App.GetTeamMembersForUser(c.Params.UserId)
   311  	if err != nil {
   312  		c.Err = err
   313  		return
   314  	}
   315  
   316  	w.Write([]byte(model.TeamMembersToJson(members)))
   317  }
   318  
   319  func getTeamMembersByIds(c *Context, w http.ResponseWriter, r *http.Request) {
   320  	c.RequireTeamId()
   321  	if c.Err != nil {
   322  		return
   323  	}
   324  
   325  	userIds := model.ArrayFromJson(r.Body)
   326  
   327  	if len(userIds) == 0 {
   328  		c.SetInvalidParam("user_ids")
   329  		return
   330  	}
   331  
   332  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) {
   333  		c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   334  		return
   335  	}
   336  
   337  	members, err := c.App.GetTeamMembersByIds(c.Params.TeamId, userIds)
   338  	if err != nil {
   339  		c.Err = err
   340  		return
   341  	}
   342  
   343  	w.Write([]byte(model.TeamMembersToJson(members)))
   344  }
   345  
   346  func addTeamMember(c *Context, w http.ResponseWriter, r *http.Request) {
   347  	c.RequireTeamId()
   348  	if c.Err != nil {
   349  		return
   350  	}
   351  
   352  	var err *model.AppError
   353  	member := model.TeamMemberFromJson(r.Body)
   354  	if member.TeamId != c.Params.TeamId {
   355  		c.SetInvalidParam("team_id")
   356  		return
   357  	}
   358  
   359  	if len(member.UserId) != 26 {
   360  		c.SetInvalidParam("user_id")
   361  		return
   362  	}
   363  
   364  	if !c.App.SessionHasPermissionToTeam(c.Session, member.TeamId, model.PERMISSION_ADD_USER_TO_TEAM) {
   365  		c.SetPermissionError(model.PERMISSION_ADD_USER_TO_TEAM)
   366  		return
   367  	}
   368  
   369  	member, err = c.App.AddTeamMember(member.TeamId, member.UserId)
   370  
   371  	if err != nil {
   372  		c.Err = err
   373  		return
   374  	}
   375  
   376  	w.WriteHeader(http.StatusCreated)
   377  	w.Write([]byte(member.ToJson()))
   378  }
   379  
   380  func addUserToTeamFromInvite(c *Context, w http.ResponseWriter, r *http.Request) {
   381  	tokenId := r.URL.Query().Get("token")
   382  	inviteId := r.URL.Query().Get("invite_id")
   383  
   384  	var member *model.TeamMember
   385  	var err *model.AppError
   386  
   387  	if len(tokenId) > 0 {
   388  		member, err = c.App.AddTeamMemberByToken(c.Session.UserId, tokenId)
   389  	} else if len(inviteId) > 0 {
   390  		member, err = c.App.AddTeamMemberByInviteId(inviteId, c.Session.UserId)
   391  	} else {
   392  		err = model.NewAppError("addTeamMember", "api.team.add_user_to_team.missing_parameter.app_error", nil, "", http.StatusBadRequest)
   393  	}
   394  
   395  	if err != nil {
   396  		c.Err = err
   397  		return
   398  	}
   399  
   400  	w.WriteHeader(http.StatusCreated)
   401  	w.Write([]byte(member.ToJson()))
   402  }
   403  
   404  func addTeamMembers(c *Context, w http.ResponseWriter, r *http.Request) {
   405  	c.RequireTeamId()
   406  	if c.Err != nil {
   407  		return
   408  	}
   409  
   410  	var err *model.AppError
   411  	members := model.TeamMembersFromJson(r.Body)
   412  
   413  	if len(members) > MAX_ADD_MEMBERS_BATCH || len(members) == 0 {
   414  		c.SetInvalidParam("too many members in batch")
   415  		return
   416  	}
   417  
   418  	var userIds []string
   419  	for _, member := range members {
   420  		if member.TeamId != c.Params.TeamId {
   421  			c.SetInvalidParam("team_id for member with user_id=" + member.UserId)
   422  			return
   423  		}
   424  
   425  		if len(member.UserId) != 26 {
   426  			c.SetInvalidParam("user_id")
   427  			return
   428  		}
   429  
   430  		userIds = append(userIds, member.UserId)
   431  	}
   432  
   433  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_ADD_USER_TO_TEAM) {
   434  		c.SetPermissionError(model.PERMISSION_ADD_USER_TO_TEAM)
   435  		return
   436  	}
   437  
   438  	members, err = c.App.AddTeamMembers(c.Params.TeamId, userIds, c.Session.UserId)
   439  
   440  	if err != nil {
   441  		c.Err = err
   442  		return
   443  	}
   444  
   445  	w.WriteHeader(http.StatusCreated)
   446  	w.Write([]byte(model.TeamMembersToJson(members)))
   447  }
   448  
   449  func removeTeamMember(c *Context, w http.ResponseWriter, r *http.Request) {
   450  	c.RequireTeamId().RequireUserId()
   451  	if c.Err != nil {
   452  		return
   453  	}
   454  
   455  	if c.Session.UserId != c.Params.UserId {
   456  		if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_REMOVE_USER_FROM_TEAM) {
   457  			c.SetPermissionError(model.PERMISSION_REMOVE_USER_FROM_TEAM)
   458  			return
   459  		}
   460  	}
   461  
   462  	if err := c.App.RemoveUserFromTeam(c.Params.TeamId, c.Params.UserId, c.Session.UserId); err != nil {
   463  		c.Err = err
   464  		return
   465  	}
   466  
   467  	ReturnStatusOK(w)
   468  }
   469  
   470  func getTeamUnread(c *Context, w http.ResponseWriter, r *http.Request) {
   471  	c.RequireTeamId().RequireUserId()
   472  	if c.Err != nil {
   473  		return
   474  	}
   475  
   476  	if !c.App.SessionHasPermissionToUser(c.Session, c.Params.UserId) {
   477  		c.SetPermissionError(model.PERMISSION_EDIT_OTHER_USERS)
   478  		return
   479  	}
   480  
   481  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) {
   482  		c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   483  		return
   484  	}
   485  
   486  	unreadTeam, err := c.App.GetTeamUnread(c.Params.TeamId, c.Params.UserId)
   487  	if err != nil {
   488  		c.Err = err
   489  		return
   490  	}
   491  
   492  	w.Write([]byte(unreadTeam.ToJson()))
   493  }
   494  
   495  func getTeamStats(c *Context, w http.ResponseWriter, r *http.Request) {
   496  	c.RequireTeamId()
   497  	if c.Err != nil {
   498  		return
   499  	}
   500  
   501  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) {
   502  		c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   503  		return
   504  	}
   505  
   506  	if stats, err := c.App.GetTeamStats(c.Params.TeamId); err != nil {
   507  		c.Err = err
   508  		return
   509  	} else {
   510  		w.Write([]byte(stats.ToJson()))
   511  		return
   512  	}
   513  }
   514  
   515  func updateTeamMemberRoles(c *Context, w http.ResponseWriter, r *http.Request) {
   516  	c.RequireTeamId().RequireUserId()
   517  	if c.Err != nil {
   518  		return
   519  	}
   520  
   521  	props := model.MapFromJson(r.Body)
   522  
   523  	newRoles := props["roles"]
   524  	if !model.IsValidUserRoles(newRoles) {
   525  		c.SetInvalidParam("team_member_roles")
   526  		return
   527  	}
   528  
   529  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM_ROLES) {
   530  		c.SetPermissionError(model.PERMISSION_MANAGE_TEAM_ROLES)
   531  		return
   532  	}
   533  
   534  	if _, err := c.App.UpdateTeamMemberRoles(c.Params.TeamId, c.Params.UserId, newRoles); err != nil {
   535  		c.Err = err
   536  		return
   537  	}
   538  
   539  	ReturnStatusOK(w)
   540  }
   541  
   542  func getAllTeams(c *Context, w http.ResponseWriter, r *http.Request) {
   543  	var teams []*model.Team
   544  	var err *model.AppError
   545  
   546  	if c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   547  		teams, err = c.App.GetAllTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage)
   548  	} else {
   549  		teams, err = c.App.GetAllOpenTeamsPage(c.Params.Page*c.Params.PerPage, c.Params.PerPage)
   550  	}
   551  
   552  	if err != nil {
   553  		c.Err = err
   554  		return
   555  	}
   556  
   557  	c.App.SanitizeTeams(c.Session, teams)
   558  
   559  	w.Write([]byte(model.TeamListToJson(teams)))
   560  }
   561  
   562  func searchTeams(c *Context, w http.ResponseWriter, r *http.Request) {
   563  	props := model.TeamSearchFromJson(r.Body)
   564  	if props == nil {
   565  		c.SetInvalidParam("team_search")
   566  		return
   567  	}
   568  
   569  	if len(props.Term) == 0 {
   570  		c.SetInvalidParam("term")
   571  		return
   572  	}
   573  
   574  	var teams []*model.Team
   575  	var err *model.AppError
   576  
   577  	if c.App.SessionHasPermissionTo(c.Session, model.PERMISSION_MANAGE_SYSTEM) {
   578  		teams, err = c.App.SearchAllTeams(props.Term)
   579  	} else {
   580  		teams, err = c.App.SearchOpenTeams(props.Term)
   581  	}
   582  
   583  	if err != nil {
   584  		c.Err = err
   585  		return
   586  	}
   587  
   588  	c.App.SanitizeTeams(c.Session, teams)
   589  
   590  	w.Write([]byte(model.TeamListToJson(teams)))
   591  }
   592  
   593  func teamExists(c *Context, w http.ResponseWriter, r *http.Request) {
   594  	c.RequireTeamName()
   595  	if c.Err != nil {
   596  		return
   597  	}
   598  
   599  	resp := make(map[string]bool)
   600  
   601  	if _, err := c.App.GetTeamByName(c.Params.TeamName); err != nil {
   602  		resp["exists"] = false
   603  	} else {
   604  		resp["exists"] = true
   605  	}
   606  
   607  	w.Write([]byte(model.MapBoolToJson(resp)))
   608  }
   609  
   610  func importTeam(c *Context, w http.ResponseWriter, r *http.Request) {
   611  	c.RequireTeamId()
   612  	if c.Err != nil {
   613  		return
   614  	}
   615  
   616  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_IMPORT_TEAM) {
   617  		c.SetPermissionError(model.PERMISSION_IMPORT_TEAM)
   618  		return
   619  	}
   620  
   621  	if err := r.ParseMultipartForm(10000000); err != nil {
   622  		c.Err = model.NewAppError("importTeam", "api.team.import_team.parse.app_error", nil, err.Error(), http.StatusInternalServerError)
   623  		return
   624  	}
   625  
   626  	importFromArray, ok := r.MultipartForm.Value["importFrom"]
   627  	if !ok || len(importFromArray) < 1 {
   628  		c.Err = model.NewAppError("importTeam", "api.team.import_team.no_import_from.app_error", nil, "", http.StatusBadRequest)
   629  		return
   630  	}
   631  	importFrom := importFromArray[0]
   632  
   633  	fileSizeStr, ok := r.MultipartForm.Value["filesize"]
   634  	if !ok || len(fileSizeStr) < 1 {
   635  		c.Err = model.NewAppError("importTeam", "api.team.import_team.unavailable.app_error", nil, "", http.StatusBadRequest)
   636  		return
   637  	}
   638  
   639  	fileSize, err := strconv.ParseInt(fileSizeStr[0], 10, 64)
   640  	if err != nil {
   641  		c.Err = model.NewAppError("importTeam", "api.team.import_team.integer.app_error", nil, "", http.StatusBadRequest)
   642  		return
   643  	}
   644  
   645  	fileInfoArray, ok := r.MultipartForm.File["file"]
   646  	if !ok {
   647  		c.Err = model.NewAppError("importTeam", "api.team.import_team.no_file.app_error", nil, "", http.StatusBadRequest)
   648  		return
   649  	}
   650  
   651  	if len(fileInfoArray) <= 0 {
   652  		c.Err = model.NewAppError("importTeam", "api.team.import_team.array.app_error", nil, "", http.StatusBadRequest)
   653  		return
   654  	}
   655  
   656  	fileInfo := fileInfoArray[0]
   657  
   658  	fileData, err := fileInfo.Open()
   659  	if err != nil {
   660  		c.Err = model.NewAppError("importTeam", "api.team.import_team.open.app_error", nil, err.Error(), http.StatusBadRequest)
   661  		return
   662  	}
   663  	defer fileData.Close()
   664  
   665  	var log *bytes.Buffer
   666  	switch importFrom {
   667  	case "slack":
   668  		var err *model.AppError
   669  		if err, log = c.App.SlackImport(fileData, fileSize, c.Params.TeamId); err != nil {
   670  			c.Err = err
   671  			c.Err.StatusCode = http.StatusBadRequest
   672  		}
   673  	}
   674  
   675  	data := map[string]string{}
   676  	data["results"] = base64.StdEncoding.EncodeToString([]byte(log.Bytes()))
   677  	if c.Err != nil {
   678  		w.WriteHeader(c.Err.StatusCode)
   679  	}
   680  	w.Write([]byte(model.MapToJson(data)))
   681  }
   682  
   683  func inviteUsersToTeam(c *Context, w http.ResponseWriter, r *http.Request) {
   684  	c.RequireTeamId()
   685  	if c.Err != nil {
   686  		return
   687  	}
   688  
   689  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_INVITE_USER) {
   690  		c.SetPermissionError(model.PERMISSION_INVITE_USER)
   691  		return
   692  	}
   693  
   694  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_ADD_USER_TO_TEAM) {
   695  		c.SetPermissionError(model.PERMISSION_INVITE_USER)
   696  		return
   697  	}
   698  
   699  	emailList := model.ArrayFromJson(r.Body)
   700  
   701  	if len(emailList) == 0 {
   702  		c.SetInvalidParam("user_email")
   703  		return
   704  	}
   705  
   706  	err := c.App.InviteNewUsersToTeam(emailList, c.Params.TeamId, c.Session.UserId)
   707  	if err != nil {
   708  		c.Err = err
   709  		return
   710  	}
   711  
   712  	ReturnStatusOK(w)
   713  }
   714  
   715  func getInviteInfo(c *Context, w http.ResponseWriter, r *http.Request) {
   716  	c.RequireInviteId()
   717  	if c.Err != nil {
   718  		return
   719  	}
   720  
   721  	if team, err := c.App.GetTeamByInviteId(c.Params.InviteId); err != nil {
   722  		c.Err = err
   723  		return
   724  	} else {
   725  		if !(team.Type == model.TEAM_OPEN) {
   726  			c.Err = model.NewAppError("getInviteInfo", "api.team.get_invite_info.not_open_team", nil, "id="+c.Params.InviteId, http.StatusForbidden)
   727  			return
   728  		}
   729  
   730  		result := map[string]string{}
   731  		result["display_name"] = team.DisplayName
   732  		result["description"] = team.Description
   733  		result["name"] = team.Name
   734  		result["id"] = team.Id
   735  		w.Write([]byte(model.MapToJson(result)))
   736  	}
   737  }
   738  
   739  func getTeamIcon(c *Context, w http.ResponseWriter, r *http.Request) {
   740  	c.RequireTeamId()
   741  	if c.Err != nil {
   742  		return
   743  	}
   744  
   745  	if team, err := c.App.GetTeam(c.Params.TeamId); err != nil {
   746  		c.Err = err
   747  		return
   748  	} else {
   749  		if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_VIEW_TEAM) &&
   750  			(team.Type != model.TEAM_OPEN || team.AllowOpenInvite) {
   751  			c.SetPermissionError(model.PERMISSION_VIEW_TEAM)
   752  			return
   753  		}
   754  
   755  		etag := strconv.FormatInt(team.LastTeamIconUpdate, 10)
   756  
   757  		if c.HandleEtag(etag, "Get Team Icon", w, r) {
   758  			return
   759  		}
   760  
   761  		if img, err := c.App.GetTeamIcon(team); err != nil {
   762  			c.Err = err
   763  			return
   764  		} else {
   765  			w.Header().Set("Content-Type", "image/png")
   766  			w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%v, public", 24*60*60)) // 24 hrs
   767  			w.Header().Set(model.HEADER_ETAG_SERVER, etag)
   768  			w.Write(img)
   769  		}
   770  	}
   771  }
   772  
   773  func setTeamIcon(c *Context, w http.ResponseWriter, r *http.Request) {
   774  	c.RequireTeamId()
   775  	if c.Err != nil {
   776  		return
   777  	}
   778  
   779  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM) {
   780  		c.SetPermissionError(model.PERMISSION_MANAGE_TEAM)
   781  		return
   782  	}
   783  
   784  	if r.ContentLength > *c.App.Config().FileSettings.MaxFileSize {
   785  		c.Err = model.NewAppError("setTeamIcon", "api.team.set_team_icon.too_large.app_error", nil, "", http.StatusBadRequest)
   786  		return
   787  	}
   788  
   789  	if err := r.ParseMultipartForm(*c.App.Config().FileSettings.MaxFileSize); err != nil {
   790  		c.Err = model.NewAppError("setTeamIcon", "api.team.set_team_icon.parse.app_error", nil, err.Error(), http.StatusBadRequest)
   791  		return
   792  	}
   793  
   794  	m := r.MultipartForm
   795  
   796  	imageArray, ok := m.File["image"]
   797  	if !ok {
   798  		c.Err = model.NewAppError("setTeamIcon", "api.team.set_team_icon.no_file.app_error", nil, "", http.StatusBadRequest)
   799  		return
   800  	}
   801  
   802  	if len(imageArray) <= 0 {
   803  		c.Err = model.NewAppError("setTeamIcon", "api.team.set_team_icon.array.app_error", nil, "", http.StatusBadRequest)
   804  		return
   805  	}
   806  
   807  	imageData := imageArray[0]
   808  
   809  	if err := c.App.SetTeamIcon(c.Params.TeamId, imageData); err != nil {
   810  		c.Err = err
   811  		return
   812  	}
   813  
   814  	c.LogAudit("")
   815  	ReturnStatusOK(w)
   816  }
   817  
   818  func removeTeamIcon(c *Context, w http.ResponseWriter, r *http.Request) {
   819  	c.RequireTeamId()
   820  	if c.Err != nil {
   821  		return
   822  	}
   823  
   824  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_TEAM) {
   825  		c.SetPermissionError(model.PERMISSION_MANAGE_TEAM)
   826  		return
   827  	}
   828  
   829  	if err := c.App.RemoveTeamIcon(c.Params.TeamId); err != nil {
   830  		c.Err = err
   831  		return
   832  	}
   833  
   834  	c.LogAudit("")
   835  	ReturnStatusOK(w)
   836  }
   837  
   838  func updateTeamScheme(c *Context, w http.ResponseWriter, r *http.Request) {
   839  	c.RequireTeamId()
   840  	if c.Err != nil {
   841  		return
   842  	}
   843  
   844  	schemeID := model.SchemeIDFromJson(r.Body)
   845  	if schemeID == nil || (len(*schemeID) != 26 && *schemeID != "") {
   846  		c.SetInvalidParam("scheme_id")
   847  		return
   848  	}
   849  
   850  	if c.App.License() == nil {
   851  		c.Err = model.NewAppError("Api4.UpdateTeamScheme", "api.team.update_team_scheme.license.error", nil, "", http.StatusNotImplemented)
   852  		return
   853  	}
   854  
   855  	if !c.App.SessionHasPermissionToTeam(c.Session, c.Params.TeamId, model.PERMISSION_MANAGE_SYSTEM) {
   856  		c.SetPermissionError(model.PERMISSION_MANAGE_SYSTEM)
   857  		return
   858  	}
   859  
   860  	if *schemeID != "" {
   861  		scheme, err := c.App.GetScheme(*schemeID)
   862  		if err != nil {
   863  			c.Err = err
   864  			return
   865  		}
   866  
   867  		if scheme.Scope != model.SCHEME_SCOPE_TEAM {
   868  			c.Err = model.NewAppError("Api4.UpdateTeamScheme", "api.team.update_team_scheme.scheme_scope.error", nil, "", http.StatusBadRequest)
   869  			return
   870  		}
   871  	}
   872  
   873  	team, err := c.App.GetTeam(c.Params.TeamId)
   874  	if err != nil {
   875  		c.Err = err
   876  		return
   877  	}
   878  
   879  	team.SchemeId = schemeID
   880  
   881  	_, err = c.App.UpdateTeamScheme(team)
   882  	if err != nil {
   883  		c.Err = err
   884  		return
   885  	}
   886  
   887  	ReturnStatusOK(w)
   888  }