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