From 1ab7034574de2229d3cfc49391e6579db37a3064 Mon Sep 17 00:00:00 2001 From: Antti Ahti Date: Wed, 14 Oct 2015 18:27:03 +0300 Subject: [PATCH 1/6] Use team display name in team switcher menu - /teams/find_teams returns team objects instead of just team names - use display_name in UI instead of name in the team switch menu --- api/team.go | 8 +++----- api/team_test.go | 9 ++++++--- model/client.go | 2 +- web/react/components/navbar_dropdown.jsx | 13 ++++++++----- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/api/team.go b/api/team.go index 6aa5ec1bb2..7148caac3e 100644 --- a/api/team.go +++ b/api/team.go @@ -409,14 +409,12 @@ func findTeams(c *Context, w http.ResponseWriter, r *http.Request) { return } else { teams := result.Data.([]*model.Team) - - s := make([]string, 0, len(teams)) - + m := make(map[string]*model.Team) for _, v := range teams { - s = append(s, v.Name) + m[v.Id] = v } - w.Write([]byte(model.ArrayToJson(s))) + w.Write([]byte(model.TeamMapToJson(m))) } } diff --git a/api/team_test.go b/api/team_test.go index 9b701911b9..507f4252a3 100644 --- a/api/team_test.go +++ b/api/team_test.go @@ -121,9 +121,12 @@ func TestFindTeamByEmail(t *testing.T) { if r1, err := Client.FindTeams(user.Email); err != nil { t.Fatal(err) } else { - domains := r1.Data.([]string) - if domains[0] != team.Name { - t.Fatal(domains) + teams := r1.Data.(map[string]*model.Team) + if teams[team.Id].Name != team.Name { + t.Fatal() + } + if teams[team.Id].DisplayName != team.DisplayName { + t.Fatal() } } diff --git a/model/client.go b/model/client.go index 77b0aaad28..19c99df729 100644 --- a/model/client.go +++ b/model/client.go @@ -185,7 +185,7 @@ func (c *Client) FindTeams(email string) (*Result, *AppError) { } else { return &Result{r.Header.Get(HEADER_REQUEST_ID), - r.Header.Get(HEADER_ETAG_SERVER), ArrayFromJson(r.Body)}, nil + r.Header.Get(HEADER_ETAG_SERVER), TeamMapFromJson(r.Body)}, nil } } diff --git a/web/react/components/navbar_dropdown.jsx b/web/react/components/navbar_dropdown.jsx index 49d517419e..b3f8e44188 100644 --- a/web/react/components/navbar_dropdown.jsx +++ b/web/react/components/navbar_dropdown.jsx @@ -145,7 +145,7 @@ export default class NavbarDropdown extends React.Component { var teams = []; - if (this.state.teams.length > 1) { + if (Object.keys(this.state.teams).length > 1) { teams.push(
  • ); - this.state.teams.forEach((teamName) => { - if (teamName !== this.props.teamName) { - teams.push(
  • {'Switch to ' + teamName}
  • ); + for (let teamId in this.state.teams) { + if (this.state.teams.hasOwnProperty(teamId)) { + let team = this.state.teams[teamId]; + if (team.name !== this.props.teamName) { + teams.push(
  • {'Switch to ' + team.display_name}
  • ); + } } - }); + } } if (global.window.config.EnableTeamCreation === 'true') { From 731595c1a93e51bcd4e6034b088b53766883a9eb Mon Sep 17 00:00:00 2001 From: Antti Ahti Date: Wed, 14 Oct 2015 18:31:58 +0300 Subject: [PATCH 2/6] Fix typo (isTreamCreationAllowed -> isTeamCreationAllowed) --- api/team.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/team.go b/api/team.go index 7148caac3e..18acf33ce6 100644 --- a/api/team.go +++ b/api/team.go @@ -52,7 +52,7 @@ func signupTeam(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !isTreamCreationAllowed(c, email) { + if !isTeamCreationAllowed(c, email) { return } @@ -100,7 +100,7 @@ func createTeamFromSSO(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !isTreamCreationAllowed(c, team.Email) { + if !isTeamCreationAllowed(c, team.Email) { return } @@ -169,7 +169,7 @@ func createTeamFromSignup(c *Context, w http.ResponseWriter, r *http.Request) { return } - if !isTreamCreationAllowed(c, teamSignup.Team.Email) { + if !isTeamCreationAllowed(c, teamSignup.Team.Email) { return } @@ -257,7 +257,7 @@ func CreateTeam(c *Context, team *model.Team) *model.Team { return nil } - if !isTreamCreationAllowed(c, team.Email) { + if !isTeamCreationAllowed(c, team.Email) { return nil } @@ -276,12 +276,12 @@ func CreateTeam(c *Context, team *model.Team) *model.Team { } } -func isTreamCreationAllowed(c *Context, email string) bool { +func isTeamCreationAllowed(c *Context, email string) bool { email = strings.ToLower(email) if !utils.Cfg.TeamSettings.EnableTeamCreation { - c.Err = model.NewAppError("isTreamCreationAllowed", "Team creation has been disabled. Please ask your systems administrator for details.", "") + c.Err = model.NewAppError("isTeamCreationAllowed", "Team creation has been disabled. Please ask your systems administrator for details.", "") return false } @@ -298,7 +298,7 @@ func isTreamCreationAllowed(c *Context, email string) bool { } if len(utils.Cfg.TeamSettings.RestrictCreationToDomains) > 0 && !matched { - c.Err = model.NewAppError("isTreamCreationAllowed", "Email must be from a specific domain (e.g. @example.com). Please ask your systems administrator for details.", "") + c.Err = model.NewAppError("isTeamCreationAllowed", "Email must be from a specific domain (e.g. @example.com). Please ask your systems administrator for details.", "") return false } From 8f96db4d0b52d0701398703364948e659f3ce34f Mon Sep 17 00:00:00 2001 From: Antti Ahti Date: Wed, 14 Oct 2015 19:27:19 +0300 Subject: [PATCH 3/6] Sanitize team data We don't want to expose sensitive data that might pose a security risk. --- api/team.go | 1 + model/team.go | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/api/team.go b/api/team.go index 18acf33ce6..f6038566a8 100644 --- a/api/team.go +++ b/api/team.go @@ -411,6 +411,7 @@ func findTeams(c *Context, w http.ResponseWriter, r *http.Request) { teams := result.Data.([]*model.Team) m := make(map[string]*model.Team) for _, v := range teams { + v.Sanitize() m[v.Id] = v } diff --git a/model/team.go b/model/team.go index c0f6524cd5..584c78f8d8 100644 --- a/model/team.go +++ b/model/team.go @@ -219,3 +219,9 @@ func CleanTeamName(s string) string { func (o *Team) PreExport() { } + +func (o *Team) Sanitize() { + o.Email = "" + o.Type = "" + o.AllowedDomains = "" +} From 059df3de0126f2a506b525f92911035eba02bbd3 Mon Sep 17 00:00:00 2001 From: Antti Ahti Date: Thu, 15 Oct 2015 08:41:52 +0300 Subject: [PATCH 4/6] Sort teams by display name First we need to convert the object to array, because objects cannot be sorted. --- web/react/components/navbar_dropdown.jsx | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/web/react/components/navbar_dropdown.jsx b/web/react/components/navbar_dropdown.jsx index b3f8e44188..d4308ad727 100644 --- a/web/react/components/navbar_dropdown.jsx +++ b/web/react/components/navbar_dropdown.jsx @@ -11,7 +11,25 @@ var AboutBuildModal = require('./about_build_modal.jsx'); var Constants = require('../utils/constants.jsx'); function getStateFromStores() { - return {teams: UserStore.getTeams()}; + let teams = []; + let teamsObject = UserStore.getTeams(); + for (let teamId in teamsObject) { + if (teamsObject.hasOwnProperty(teamId)) { + teams.push(teamsObject[teamId]) + } + } + teams.sort(function (teamA, teamB) { + let teamADisplayName = teamA.display_name.toLowerCase(); + let teamBDisplayName = teamB.display_name.toLowerCase(); + if (teamADisplayName < teamBDisplayName) { + return -1 + } else if (teamADisplayName > teamBDisplayName) { + return 1; + } else { + return 0; + } + }); + return {teams}; } export default class NavbarDropdown extends React.Component { From c0809bbc3fc7a9630659872850c313eb6df701d9 Mon Sep 17 00:00:00 2001 From: Antti Ahti Date: Thu, 15 Oct 2015 08:54:20 +0300 Subject: [PATCH 5/6] Treat teams as an array instead of object --- web/react/components/navbar_dropdown.jsx | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/web/react/components/navbar_dropdown.jsx b/web/react/components/navbar_dropdown.jsx index d4308ad727..564a64f48c 100644 --- a/web/react/components/navbar_dropdown.jsx +++ b/web/react/components/navbar_dropdown.jsx @@ -163,7 +163,7 @@ export default class NavbarDropdown extends React.Component { var teams = []; - if (Object.keys(this.state.teams).length > 1) { + if (this.state.teams.length > 1) { teams.push(
  • ); - for (let teamId in this.state.teams) { - if (this.state.teams.hasOwnProperty(teamId)) { - let team = this.state.teams[teamId]; - if (team.name !== this.props.teamName) { - teams.push(
  • {'Switch to ' + team.display_name}
  • ); - } + this.state.teams.forEach((team) => { + if (team.name !== this.props.teamName) { + teams.push(
  • {'Switch to ' + team.display_name}
  • ); } - } + }); } if (global.window.config.EnableTeamCreation === 'true') { From 7abde676a1828714c71b25184fdd91a81c2d77a1 Mon Sep 17 00:00:00 2001 From: Antti Ahti Date: Thu, 15 Oct 2015 19:35:45 +0300 Subject: [PATCH 6/6] Fix lint --- web/react/components/navbar_dropdown.jsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/web/react/components/navbar_dropdown.jsx b/web/react/components/navbar_dropdown.jsx index 564a64f48c..dc41775c7b 100644 --- a/web/react/components/navbar_dropdown.jsx +++ b/web/react/components/navbar_dropdown.jsx @@ -15,19 +15,18 @@ function getStateFromStores() { let teamsObject = UserStore.getTeams(); for (let teamId in teamsObject) { if (teamsObject.hasOwnProperty(teamId)) { - teams.push(teamsObject[teamId]) + teams.push(teamsObject[teamId]); } } - teams.sort(function (teamA, teamB) { + teams.sort(function sortByDisplayName(teamA, teamB) { let teamADisplayName = teamA.display_name.toLowerCase(); let teamBDisplayName = teamB.display_name.toLowerCase(); if (teamADisplayName < teamBDisplayName) { - return -1 + return -1; } else if (teamADisplayName > teamBDisplayName) { return 1; - } else { - return 0; } + return 0; }); return {teams}; }