func TestTeamStoreGetForEmail(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN Must(store.Team().Save(&o1)) u1 := model.User{} u1.TeamId = o1.Id u1.Email = model.NewId() Must(store.User().Save(&u1)) if r1 := <-store.Team().GetTeamsForEmail(u1.Email); r1.Err != nil { t.Fatal(r1.Err) } else { teams := r1.Data.([]*model.Team) if teams[0].Id != o1.Id { t.Fatal("failed to lookup by email") } } if r1 := <-store.Team().GetTeamsForEmail("missing"); r1.Err != nil { t.Fatal(r1.Err) } }
func (s SqlTeamStore) GetByInviteId(inviteId string) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} team := model.Team{} if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Id = :InviteId OR InviteId = :InviteId", map[string]interface{}{"InviteId": inviteId}); err != nil { result.Err = model.NewLocAppError("SqlTeamStore.GetByInviteId", "store.sql_team.get_by_invite_id.finding.app_error", nil, "inviteId="+inviteId+", "+err.Error()) } if len(team.InviteId) == 0 { team.InviteId = team.Id } if len(inviteId) == 0 || team.InviteId != inviteId { result.Err = model.NewLocAppError("SqlTeamStore.GetByInviteId", "store.sql_team.get_by_invite_id.find.app_error", nil, "inviteId="+inviteId) } result.Data = &team storeChannel <- result close(storeChannel) }() return storeChannel }
func updateValetFeature(c *Context, w http.ResponseWriter, r *http.Request) { props := model.MapFromJson(r.Body) allowValetStr := props["allow_valet"] if len(allowValetStr) == 0 { c.SetInvalidParam("updateValetFeature", "allow_valet") return } allowValet := allowValetStr == "true" teamId := props["team_id"] if len(teamId) > 0 && len(teamId) != 26 { c.SetInvalidParam("updateValetFeature", "team_id") return } else if len(teamId) == 0 { teamId = c.Session.TeamId } tchan := Srv.Store.Team().Get(teamId) if !c.HasPermissionsToTeam(teamId, "updateValetFeature") { return } if !model.IsInRole(c.Session.Roles, model.ROLE_TEAM_ADMIN) { c.Err = model.NewAppError("updateValetFeature", "You do not have the appropriate permissions", "userId="+c.Session.UserId) c.Err.StatusCode = http.StatusForbidden return } var team *model.Team if tResult := <-tchan; tResult.Err != nil { c.Err = tResult.Err return } else { team = tResult.Data.(*model.Team) } team.AllowValet = allowValet if result := <-Srv.Store.Team().Update(team); result.Err != nil { c.Err = result.Err return } w.Write([]byte(model.MapToJson(props))) }
func PermanentDeleteTeam(c *Context, team *model.Team) *model.AppError { l4g.Warn(utils.T("api.team.permanent_delete_team.attempting.warn"), team.Name, team.Id) c.Path = "/teams/permanent_delete" c.LogAuditWithUserId("", fmt.Sprintf("attempt teamId=%v", team.Id)) team.DeleteAt = model.GetMillis() if result := <-Srv.Store.Team().Update(team); result.Err != nil { return result.Err } if result := <-Srv.Store.User().GetForExport(team.Id); result.Err != nil { return result.Err } else { users := result.Data.([]*model.User) for _, user := range users { PermanentDeleteUser(c, user) } } if result := <-Srv.Store.Channel().PermanentDeleteByTeam(team.Id); result.Err != nil { return result.Err } if result := <-Srv.Store.Team().PermanentDelete(team.Id); result.Err != nil { return result.Err } l4g.Warn(utils.T("api.team.permanent_delete_team.deleted.warn"), team.Name, team.Id) c.LogAuditWithUserId("", fmt.Sprintf("success teamId=%v", team.Id)) return nil }
func PermanentDeleteTeam(c *Context, team *model.Team) *model.AppError { l4g.Warn(utils.T("api.team.permanent_delete_team.attempting.warn"), team.Name, team.Id) c.Path = "/teams/permanent_delete" c.LogAuditWithUserId("", fmt.Sprintf("attempt teamId=%v", team.Id)) team.DeleteAt = model.GetMillis() if result := <-Srv.Store.Team().Update(team); result.Err != nil { return result.Err } if result := <-Srv.Store.Channel().PermanentDeleteByTeam(team.Id); result.Err != nil { return result.Err } if result := <-Srv.Store.Team().RemoveAllMembersByTeam(team.Id); result.Err != nil { return result.Err } if result := <-Srv.Store.Team().PermanentDelete(team.Id); result.Err != nil { return result.Err } l4g.Warn(utils.T("api.team.permanent_delete_team.deleted.warn"), team.Name, team.Id) c.LogAuditWithUserId("", fmt.Sprintf("success teamId=%v", team.Id)) return nil }
func TestTeamStoreGetByName(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN if err := (<-store.Team().Save(&o1)).Err; err != nil { t.Fatal(err) } if r1 := <-store.Team().GetByName(o1.Name); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(*model.Team).ToJson() != o1.ToJson() { t.Fatal("invalid returned team") } } if err := (<-store.Team().GetByName("")).Err; err == nil { t.Fatal("Missing id should have failed") } }
func TestTeamStoreUpdate(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN if err := (<-store.Team().Save(&o1)).Err; err != nil { t.Fatal(err) } time.Sleep(100 * time.Millisecond) if err := (<-store.Team().Update(&o1)).Err; err != nil { t.Fatal(err) } o1.Id = "missing" if err := (<-store.Team().Update(&o1)).Err; err == nil { t.Fatal("Update should have failed because of missing key") } o1.Id = model.NewId() if err := (<-store.Team().Update(&o1)).Err; err == nil { t.Fatal("Update should have faile because id change") } }
func TestChannelStoreGetMembersForUser(t *testing.T) { Setup() t1 := model.Team{} t1.DisplayName = "Name" t1.Name = model.NewId() t1.Email = model.NewId() + "@nowhere.com" t1.Type = model.TEAM_OPEN Must(store.Team().Save(&t1)) o1 := model.Channel{} o1.TeamId = t1.Id o1.DisplayName = "Channel1" o1.Name = "a" + model.NewId() + "b" o1.Type = model.CHANNEL_OPEN Must(store.Channel().Save(&o1)) o2 := model.Channel{} o2.TeamId = o1.TeamId o2.DisplayName = "Channel2" o2.Name = "a" + model.NewId() + "b" o2.Type = model.CHANNEL_OPEN Must(store.Channel().Save(&o2)) m1 := model.ChannelMember{} m1.ChannelId = o1.Id m1.UserId = model.NewId() m1.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m1)) m2 := model.ChannelMember{} m2.ChannelId = o2.Id m2.UserId = m1.UserId m2.NotifyProps = model.GetDefaultChannelNotifyProps() Must(store.Channel().SaveMember(&m2)) cresult := <-store.Channel().GetMembersForUser(o1.TeamId, m1.UserId) members := cresult.Data.(*model.ChannelMembers) // no unread messages if len(*members) != 2 { t.Fatal("wrong number of members") } }
func updateTeam(c *Context, w http.ResponseWriter, r *http.Request) { team := model.TeamFromJson(r.Body) if team == nil { c.SetInvalidParam("updateTeam", "team") return } team.Id = c.TeamId if !HasPermissionToTeamContext(c, team.Id, model.PERMISSION_MANAGE_TEAM) { c.Err = model.NewLocAppError("updateTeam", "api.team.update_team.permissions.app_error", nil, "userId="+c.Session.UserId) c.Err.StatusCode = http.StatusForbidden return } var oldTeam *model.Team if result := <-Srv.Store.Team().Get(team.Id); result.Err != nil { c.Err = result.Err return } else { oldTeam = result.Data.(*model.Team) } oldTeam.DisplayName = team.DisplayName oldTeam.InviteId = team.InviteId oldTeam.AllowOpenInvite = team.AllowOpenInvite oldTeam.CompanyName = team.CompanyName oldTeam.AllowedDomains = team.AllowedDomains //oldTeam.Type = team.Type if result := <-Srv.Store.Team().Update(oldTeam); result.Err != nil { c.Err = result.Err return } oldTeam.Sanitize() w.Write([]byte(oldTeam.ToJson())) }
func (s SqlTeamStore) Save(team *model.Team) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} if len(team.Id) > 0 { result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.existing.app_error", nil, "id="+team.Id) storeChannel <- result close(storeChannel) return } team.PreSave() if result.Err = team.IsValid(*utils.Cfg.TeamSettings.RestrictTeamNames); result.Err != nil { storeChannel <- result close(storeChannel) return } if err := s.GetMaster().Insert(team); err != nil { if IsUniqueConstraintError(err.Error(), []string{"Name", "teams_name_key"}) { result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.domain_exists.app_error", nil, "id="+team.Id+", "+err.Error()) } else { result.Err = model.NewLocAppError("SqlTeamStore.Save", "store.sql_team.save.app_error", nil, "id="+team.Id+", "+err.Error()) } } else { result.Data = team } storeChannel <- result close(storeChannel) }() return storeChannel }
func (s SqlTeamStore) Save(team *model.Team) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} if len(team.Id) > 0 { result.Err = model.NewAppError("SqlTeamStore.Save", "Must call update for exisiting team", "id="+team.Id) storeChannel <- result close(storeChannel) return } team.PreSave() if result.Err = team.IsValid(); result.Err != nil { storeChannel <- result close(storeChannel) return } if err := s.GetMaster().Insert(team); err != nil { if IsUniqueConstraintError(err.Error(), "Name", "teams_name_key") { result.Err = model.NewAppError("SqlTeamStore.Save", "A team with that domain already exists", "id="+team.Id+", "+err.Error()) } else { result.Err = model.NewAppError("SqlTeamStore.Save", "We couldn't save the team", "id="+team.Id+", "+err.Error()) } } else { result.Data = team } storeChannel <- result close(storeChannel) }() return storeChannel }
func (s SqlTeamStore) GetByName(name string) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} team := model.Team{} if err := s.GetReplica().SelectOne(&team, "SELECT * FROM Teams WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil { result.Err = model.NewLocAppError("SqlTeamStore.GetByName", "store.sql_team.get_by_name.app_error", nil, "name="+name+", "+err.Error()) } if len(team.InviteId) == 0 { team.InviteId = team.Id } result.Data = &team storeChannel <- result close(storeChannel) }() return storeChannel }
func PermanentDeleteTeam(team *model.Team) *model.AppError { team.DeleteAt = model.GetMillis() if result := <-Srv.Store.Team().Update(team); result.Err != nil { return result.Err } if result := <-Srv.Store.Channel().PermanentDeleteByTeam(team.Id); result.Err != nil { return result.Err } if result := <-Srv.Store.Team().RemoveAllMembersByTeam(team.Id); result.Err != nil { return result.Err } if result := <-Srv.Store.Team().PermanentDelete(team.Id); result.Err != nil { return result.Err } return nil }
func (s SqlTeamStore) Update(team *model.Team) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} team.PreUpdate() if result.Err = team.IsValid(*utils.Cfg.TeamSettings.RestrictTeamNames); result.Err != nil { storeChannel <- result close(storeChannel) return } if oldResult, err := s.GetMaster().Get(model.Team{}, team.Id); err != nil { result.Err = model.NewLocAppError("SqlTeamStore.Update", "store.sql_team.update.finding.app_error", nil, "id="+team.Id+", "+err.Error()) } else if oldResult == nil { result.Err = model.NewLocAppError("SqlTeamStore.Update", "store.sql_team.update.find.app_error", nil, "id="+team.Id) } else { oldTeam := oldResult.(*model.Team) team.CreateAt = oldTeam.CreateAt team.UpdateAt = model.GetMillis() team.Name = oldTeam.Name if count, err := s.GetMaster().Update(team); err != nil { result.Err = model.NewLocAppError("SqlTeamStore.Update", "store.sql_team.update.updating.app_error", nil, "id="+team.Id+", "+err.Error()) } else if count != 1 { result.Err = model.NewLocAppError("SqlTeamStore.Update", "store.sql_team.update.app_error", nil, "id="+team.Id) } else { result.Data = team } } storeChannel <- result close(storeChannel) }() return storeChannel }
func (s SqlTeamStore) Update(team *model.Team) StoreChannel { storeChannel := make(StoreChannel) go func() { result := StoreResult{} team.PreUpdate() if result.Err = team.IsValid(*utils.Cfg.TeamSettings.RestrictTeamNames); result.Err != nil { storeChannel <- result close(storeChannel) return } if oldResult, err := s.GetMaster().Get(model.Team{}, team.Id); err != nil { result.Err = model.NewAppError("SqlTeamStore.Update", "We encountered an error finding the team", "id="+team.Id+", "+err.Error()) } else if oldResult == nil { result.Err = model.NewAppError("SqlTeamStore.Update", "We couldn't find the existing team to update", "id="+team.Id) } else { oldTeam := oldResult.(*model.Team) team.CreateAt = oldTeam.CreateAt team.UpdateAt = model.GetMillis() team.Name = oldTeam.Name if count, err := s.GetMaster().Update(team); err != nil { result.Err = model.NewAppError("SqlTeamStore.Update", "We encountered an error updating the team", "id="+team.Id+", "+err.Error()) } else if count != 1 { result.Err = model.NewAppError("SqlTeamStore.Update", "We couldn't update the team", "id="+team.Id) } else { result.Data = team } } storeChannel <- result close(storeChannel) }() return storeChannel }
func TestTeamCount(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "z-z-z" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN o1.AllowOpenInvite = true Must(store.Team().Save(&o1)) if r1 := <-store.Team().AnalyticsTeamCount(); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(int64) == 0 { t.Fatal("should be at least 1 team") } } }
func TestTeamStoreSave(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN if err := (<-store.Team().Save(&o1)).Err; err != nil { t.Fatal("couldn't save item", err) } if err := (<-store.Team().Save(&o1)).Err; err == nil { t.Fatal("shouldn't be able to update from save") } o1.Id = "" if err := (<-store.Team().Save(&o1)).Err; err == nil { t.Fatal("should be unique domain") } }
func TestAllTeamListing(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "a" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN o1.AllowTeamListing = true Must(store.Team().Save(&o1)) o2 := model.Team{} o2.DisplayName = "DisplayName" o2.Name = "a" + model.NewId() + "b" o2.Email = model.NewId() + "@nowhere.com" o2.Type = model.TEAM_OPEN Must(store.Team().Save(&o2)) if r1 := <-store.Team().GetAllTeamListing(); r1.Err != nil { t.Fatal(r1.Err) } else { teams := r1.Data.([]*model.Team) if len(teams) == 0 { t.Fatal("failed team listing") } } }
func updateTeam(c *Context, w http.ResponseWriter, r *http.Request) { team := model.TeamFromJson(r.Body) if team == nil { c.SetInvalidParam("updateTeam", "team") return } team.Id = c.Session.TeamId if !c.IsTeamAdmin() { c.Err = model.NewAppError("updateTeam", "You do not have the appropriate permissions", "userId="+c.Session.UserId) c.Err.StatusCode = http.StatusForbidden return } var oldTeam *model.Team if result := <-Srv.Store.Team().Get(team.Id); result.Err != nil { c.Err = result.Err return } else { oldTeam = result.Data.(*model.Team) } oldTeam.DisplayName = team.DisplayName oldTeam.InviteId = team.InviteId oldTeam.AllowOpenInvite = team.AllowOpenInvite oldTeam.AllowTeamListing = team.AllowTeamListing oldTeam.CompanyName = team.CompanyName oldTeam.AllowedDomains = team.AllowedDomains //oldTeam.Type = team.Type if result := <-Srv.Store.Team().Update(oldTeam); result.Err != nil { c.Err = result.Err return } oldTeam.Sanitize() w.Write([]byte(oldTeam.ToJson())) }
func TestTeamStoreGetByIniviteId(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "z-z-z" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN o1.InviteId = model.NewId() if err := (<-store.Team().Save(&o1)).Err; err != nil { t.Fatal(err) } o2 := model.Team{} o2.DisplayName = "DisplayName" o2.Name = "a" + model.NewId() + "b" o2.Email = model.NewId() + "@nowhere.com" o2.Type = model.TEAM_OPEN if err := (<-store.Team().Save(&o2)).Err; err != nil { t.Fatal(err) } if r1 := <-store.Team().GetByInviteId(o1.InviteId); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(*model.Team).ToJson() != o1.ToJson() { t.Fatal("invalid returned team") } } o2.InviteId = "" <-store.Team().Update(&o2) if r1 := <-store.Team().GetByInviteId(o2.Id); r1.Err != nil { t.Fatal(r1.Err) } else { if r1.Data.(*model.Team).Id != o2.Id { t.Fatal("invalid returned team") } } if err := (<-store.Team().GetByInviteId("")).Err; err == nil { t.Fatal("Missing id should have failed") } }
func TestDelete(t *testing.T) { Setup() o1 := model.Team{} o1.DisplayName = "DisplayName" o1.Name = "z-z-z" + model.NewId() + "b" o1.Email = model.NewId() + "@nowhere.com" o1.Type = model.TEAM_OPEN o1.AllowOpenInvite = true Must(store.Team().Save(&o1)) o2 := model.Team{} o2.DisplayName = "DisplayName" o2.Name = "a" + model.NewId() + "b" o2.Email = model.NewId() + "@nowhere.com" o2.Type = model.TEAM_OPEN Must(store.Team().Save(&o2)) if r1 := <-store.Team().PermanentDelete(o1.Id); r1.Err != nil { t.Fatal(r1.Err) } }
func addUserToTeamFromInvite(c *Context, w http.ResponseWriter, r *http.Request) { params := model.MapFromJson(r.Body) hash := params["hash"] data := params["data"] inviteId := params["invite_id"] teamId := "" var team *model.Team if len(hash) > 0 { props := model.MapFromJson(strings.NewReader(data)) if !model.ComparePassword(hash, fmt.Sprintf("%v:%v", data, utils.Cfg.EmailSettings.InviteSalt)) { c.Err = model.NewLocAppError("addUserToTeamFromInvite", "api.user.create_user.signup_link_invalid.app_error", nil, "") return } t, err := strconv.ParseInt(props["time"], 10, 64) if err != nil || model.GetMillis()-t > 1000*60*60*48 { // 48 hours c.Err = model.NewLocAppError("addUserToTeamFromInvite", "api.user.create_user.signup_link_expired.app_error", nil, "") return } teamId = props["id"] // try to load the team to make sure it exists if result := <-Srv.Store.Team().Get(teamId); result.Err != nil { c.Err = result.Err return } else { team = result.Data.(*model.Team) } } if len(inviteId) > 0 { if result := <-Srv.Store.Team().GetByInviteId(inviteId); result.Err != nil { c.Err = result.Err return } else { team = result.Data.(*model.Team) teamId = team.Id } } if len(teamId) == 0 { c.Err = model.NewLocAppError("addUserToTeamFromInvite", "api.user.create_user.signup_link_invalid.app_error", nil, "") return } uchan := Srv.Store.User().Get(c.Session.UserId) var user *model.User if result := <-uchan; result.Err != nil { c.Err = result.Err return } else { user = result.Data.(*model.User) } tm := c.Session.GetTeamByTeamId(teamId) if tm == nil { err := JoinUserToTeam(team, user) if err != nil { c.Err = err return } } team.Sanitize() w.Write([]byte(team.ToJson())) }