github.com/lologarithm/mattermost-server@v5.3.2-0.20181002060438-c82a84ed765b+incompatible/model/team.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"regexp"
    12  	"strings"
    13  	"unicode/utf8"
    14  )
    15  
    16  const (
    17  	TEAM_OPEN                       = "O"
    18  	TEAM_INVITE                     = "I"
    19  	TEAM_ALLOWED_DOMAINS_MAX_LENGTH = 500
    20  	TEAM_COMPANY_NAME_MAX_LENGTH    = 64
    21  	TEAM_DESCRIPTION_MAX_LENGTH     = 255
    22  	TEAM_DISPLAY_NAME_MAX_RUNES     = 64
    23  	TEAM_EMAIL_MAX_LENGTH           = 128
    24  	TEAM_NAME_MAX_LENGTH            = 64
    25  	TEAM_NAME_MIN_LENGTH            = 2
    26  )
    27  
    28  type Team struct {
    29  	Id                 string  `json:"id"`
    30  	CreateAt           int64   `json:"create_at"`
    31  	UpdateAt           int64   `json:"update_at"`
    32  	DeleteAt           int64   `json:"delete_at"`
    33  	DisplayName        string  `json:"display_name"`
    34  	Name               string  `json:"name"`
    35  	Description        string  `json:"description"`
    36  	Email              string  `json:"email"`
    37  	Type               string  `json:"type"`
    38  	CompanyName        string  `json:"company_name"`
    39  	AllowedDomains     string  `json:"allowed_domains"`
    40  	InviteId           string  `json:"invite_id"`
    41  	AllowOpenInvite    bool    `json:"allow_open_invite"`
    42  	LastTeamIconUpdate int64   `json:"last_team_icon_update,omitempty"`
    43  	SchemeId           *string `json:"scheme_id"`
    44  }
    45  
    46  type TeamPatch struct {
    47  	DisplayName     *string `json:"display_name"`
    48  	Description     *string `json:"description"`
    49  	CompanyName     *string `json:"company_name"`
    50  	AllowedDomains  *string `json:"allowed_domains"`
    51  	InviteId        *string `json:"invite_id"`
    52  	AllowOpenInvite *bool   `json:"allow_open_invite"`
    53  }
    54  
    55  type TeamForExport struct {
    56  	Team
    57  	SchemeName *string
    58  }
    59  
    60  type Invites struct {
    61  	Invites []map[string]string `json:"invites"`
    62  }
    63  
    64  func InvitesFromJson(data io.Reader) *Invites {
    65  	var o *Invites
    66  	json.NewDecoder(data).Decode(&o)
    67  	return o
    68  }
    69  
    70  func (o *Invites) ToEmailList() []string {
    71  	emailList := make([]string, len(o.Invites))
    72  	for _, invite := range o.Invites {
    73  		emailList = append(emailList, invite["email"])
    74  	}
    75  	return emailList
    76  }
    77  
    78  func (o *Invites) ToJson() string {
    79  	b, _ := json.Marshal(o)
    80  	return string(b)
    81  }
    82  
    83  func (o *Team) ToJson() string {
    84  	b, _ := json.Marshal(o)
    85  	return string(b)
    86  }
    87  
    88  func TeamFromJson(data io.Reader) *Team {
    89  	var o *Team
    90  	json.NewDecoder(data).Decode(&o)
    91  	return o
    92  }
    93  
    94  func TeamMapToJson(u map[string]*Team) string {
    95  	b, _ := json.Marshal(u)
    96  	return string(b)
    97  }
    98  
    99  func TeamMapFromJson(data io.Reader) map[string]*Team {
   100  	var teams map[string]*Team
   101  	json.NewDecoder(data).Decode(&teams)
   102  	return teams
   103  }
   104  
   105  func TeamListToJson(t []*Team) string {
   106  	b, _ := json.Marshal(t)
   107  	return string(b)
   108  }
   109  
   110  func TeamListFromJson(data io.Reader) []*Team {
   111  	var teams []*Team
   112  	json.NewDecoder(data).Decode(&teams)
   113  	return teams
   114  }
   115  
   116  func (o *Team) Etag() string {
   117  	return Etag(o.Id, o.UpdateAt)
   118  }
   119  
   120  func (o *Team) IsValid() *AppError {
   121  
   122  	if len(o.Id) != 26 {
   123  		return NewAppError("Team.IsValid", "model.team.is_valid.id.app_error", nil, "", http.StatusBadRequest)
   124  	}
   125  
   126  	if o.CreateAt == 0 {
   127  		return NewAppError("Team.IsValid", "model.team.is_valid.create_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   128  	}
   129  
   130  	if o.UpdateAt == 0 {
   131  		return NewAppError("Team.IsValid", "model.team.is_valid.update_at.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   132  	}
   133  
   134  	if len(o.Email) > TEAM_EMAIL_MAX_LENGTH {
   135  		return NewAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   136  	}
   137  
   138  	if len(o.Email) > 0 && !IsValidEmail(o.Email) {
   139  		return NewAppError("Team.IsValid", "model.team.is_valid.email.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   140  	}
   141  
   142  	if utf8.RuneCountInString(o.DisplayName) == 0 || utf8.RuneCountInString(o.DisplayName) > TEAM_DISPLAY_NAME_MAX_RUNES {
   143  		return NewAppError("Team.IsValid", "model.team.is_valid.name.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   144  	}
   145  
   146  	if len(o.Name) > TEAM_NAME_MAX_LENGTH {
   147  		return NewAppError("Team.IsValid", "model.team.is_valid.url.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   148  	}
   149  
   150  	if len(o.Description) > TEAM_DESCRIPTION_MAX_LENGTH {
   151  		return NewAppError("Team.IsValid", "model.team.is_valid.description.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   152  	}
   153  
   154  	if IsReservedTeamName(o.Name) {
   155  		return NewAppError("Team.IsValid", "model.team.is_valid.reserved.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   156  	}
   157  
   158  	if !IsValidTeamName(o.Name) {
   159  		return NewAppError("Team.IsValid", "model.team.is_valid.characters.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   160  	}
   161  
   162  	if !(o.Type == TEAM_OPEN || o.Type == TEAM_INVITE) {
   163  		return NewAppError("Team.IsValid", "model.team.is_valid.type.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   164  	}
   165  
   166  	if len(o.CompanyName) > TEAM_COMPANY_NAME_MAX_LENGTH {
   167  		return NewAppError("Team.IsValid", "model.team.is_valid.company.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   168  	}
   169  
   170  	if len(o.AllowedDomains) > TEAM_ALLOWED_DOMAINS_MAX_LENGTH {
   171  		return NewAppError("Team.IsValid", "model.team.is_valid.domains.app_error", nil, "id="+o.Id, http.StatusBadRequest)
   172  	}
   173  
   174  	return nil
   175  }
   176  
   177  func (o *Team) PreSave() {
   178  	if o.Id == "" {
   179  		o.Id = NewId()
   180  	}
   181  
   182  	o.CreateAt = GetMillis()
   183  	o.UpdateAt = o.CreateAt
   184  
   185  	if len(o.InviteId) == 0 {
   186  		o.InviteId = NewId()
   187  	}
   188  }
   189  
   190  func (o *Team) PreUpdate() {
   191  	o.UpdateAt = GetMillis()
   192  }
   193  
   194  func IsReservedTeamName(s string) bool {
   195  	s = strings.ToLower(s)
   196  
   197  	for _, value := range reservedName {
   198  		if strings.Index(s, value) == 0 {
   199  			return true
   200  		}
   201  	}
   202  
   203  	return false
   204  }
   205  
   206  func IsValidTeamName(s string) bool {
   207  
   208  	if !IsValidAlphaNum(s) {
   209  		return false
   210  	}
   211  
   212  	if len(s) < TEAM_NAME_MIN_LENGTH {
   213  		return false
   214  	}
   215  
   216  	return true
   217  }
   218  
   219  var validTeamNameCharacter = regexp.MustCompile(`^[a-z0-9-]$`)
   220  
   221  func CleanTeamName(s string) string {
   222  	s = strings.ToLower(strings.Replace(s, " ", "-", -1))
   223  
   224  	for _, value := range reservedName {
   225  		if strings.Index(s, value) == 0 {
   226  			s = strings.Replace(s, value, "", -1)
   227  		}
   228  	}
   229  
   230  	s = strings.TrimSpace(s)
   231  
   232  	for _, c := range s {
   233  		char := fmt.Sprintf("%c", c)
   234  		if !validTeamNameCharacter.MatchString(char) {
   235  			s = strings.Replace(s, char, "", -1)
   236  		}
   237  	}
   238  
   239  	s = strings.Trim(s, "-")
   240  
   241  	if !IsValidTeamName(s) {
   242  		s = NewId()
   243  	}
   244  
   245  	return s
   246  }
   247  
   248  func (o *Team) Sanitize() {
   249  	o.Email = ""
   250  }
   251  
   252  func (t *Team) Patch(patch *TeamPatch) {
   253  	if patch.DisplayName != nil {
   254  		t.DisplayName = *patch.DisplayName
   255  	}
   256  
   257  	if patch.Description != nil {
   258  		t.Description = *patch.Description
   259  	}
   260  
   261  	if patch.CompanyName != nil {
   262  		t.CompanyName = *patch.CompanyName
   263  	}
   264  
   265  	if patch.AllowedDomains != nil {
   266  		t.AllowedDomains = *patch.AllowedDomains
   267  	}
   268  
   269  	if patch.InviteId != nil {
   270  		t.InviteId = *patch.InviteId
   271  	}
   272  
   273  	if patch.AllowOpenInvite != nil {
   274  		t.AllowOpenInvite = *patch.AllowOpenInvite
   275  	}
   276  }
   277  
   278  func (t *TeamPatch) ToJson() string {
   279  	b, err := json.Marshal(t)
   280  	if err != nil {
   281  		return ""
   282  	}
   283  
   284  	return string(b)
   285  }
   286  
   287  func TeamPatchFromJson(data io.Reader) *TeamPatch {
   288  	decoder := json.NewDecoder(data)
   289  	var team TeamPatch
   290  	err := decoder.Decode(&team)
   291  	if err != nil {
   292  		return nil
   293  	}
   294  
   295  	return &team
   296  }