github.com/rajatvaryani/mattermost-server@v5.11.1+incompatible/model/scheme.go (about)

     1  // Copyright (c) 2018-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  	"regexp"
    11  )
    12  
    13  const (
    14  	SCHEME_DISPLAY_NAME_MAX_LENGTH = 128
    15  	SCHEME_NAME_MAX_LENGTH         = 64
    16  	SCHEME_DESCRIPTION_MAX_LENGTH  = 1024
    17  	SCHEME_SCOPE_TEAM              = "team"
    18  	SCHEME_SCOPE_CHANNEL           = "channel"
    19  )
    20  
    21  type Scheme struct {
    22  	Id                      string `json:"id"`
    23  	Name                    string `json:"name"`
    24  	DisplayName             string `json:"display_name"`
    25  	Description             string `json:"description"`
    26  	CreateAt                int64  `json:"create_at"`
    27  	UpdateAt                int64  `json:"update_at"`
    28  	DeleteAt                int64  `json:"delete_at"`
    29  	Scope                   string `json:"scope"`
    30  	DefaultTeamAdminRole    string `json:"default_team_admin_role"`
    31  	DefaultTeamUserRole     string `json:"default_team_user_role"`
    32  	DefaultChannelAdminRole string `json:"default_channel_admin_role"`
    33  	DefaultChannelUserRole  string `json:"default_channel_user_role"`
    34  }
    35  
    36  type SchemePatch struct {
    37  	Name        *string `json:"name"`
    38  	DisplayName *string `json:"display_name"`
    39  	Description *string `json:"description"`
    40  }
    41  
    42  type SchemeIDPatch struct {
    43  	SchemeID *string `json:"scheme_id"`
    44  }
    45  
    46  // SchemeConveyor is used for importing and exporting a Scheme and its associated Roles.
    47  type SchemeConveyor struct {
    48  	Name         string  `json:"name"`
    49  	DisplayName  string  `json:"display_name"`
    50  	Description  string  `json:"description"`
    51  	Scope        string  `json:"scope"`
    52  	TeamAdmin    string  `json:"default_team_admin_role"`
    53  	TeamUser     string  `json:"default_team_user_role"`
    54  	ChannelAdmin string  `json:"default_channel_admin_role"`
    55  	ChannelUser  string  `json:"default_channel_user_role"`
    56  	Roles        []*Role `json:"roles"`
    57  }
    58  
    59  func (sc *SchemeConveyor) Scheme() *Scheme {
    60  	return &Scheme{
    61  		DisplayName:             sc.DisplayName,
    62  		Name:                    sc.Name,
    63  		Description:             sc.Description,
    64  		Scope:                   sc.Scope,
    65  		DefaultTeamAdminRole:    sc.TeamAdmin,
    66  		DefaultTeamUserRole:     sc.TeamUser,
    67  		DefaultChannelAdminRole: sc.ChannelAdmin,
    68  		DefaultChannelUserRole:  sc.ChannelUser,
    69  	}
    70  }
    71  
    72  type SchemeRoles struct {
    73  	SchemeAdmin bool `json:"scheme_admin"`
    74  	SchemeUser  bool `json:"scheme_user"`
    75  }
    76  
    77  func (scheme *Scheme) ToJson() string {
    78  	b, _ := json.Marshal(scheme)
    79  	return string(b)
    80  }
    81  
    82  func SchemeFromJson(data io.Reader) *Scheme {
    83  	var scheme *Scheme
    84  	json.NewDecoder(data).Decode(&scheme)
    85  	return scheme
    86  }
    87  
    88  func SchemesToJson(schemes []*Scheme) string {
    89  	b, _ := json.Marshal(schemes)
    90  	return string(b)
    91  }
    92  
    93  func SchemesFromJson(data io.Reader) []*Scheme {
    94  	var schemes []*Scheme
    95  	if err := json.NewDecoder(data).Decode(&schemes); err == nil {
    96  		return schemes
    97  	} else {
    98  		return nil
    99  	}
   100  }
   101  
   102  func (scheme *Scheme) IsValid() bool {
   103  	if len(scheme.Id) != 26 {
   104  		return false
   105  	}
   106  
   107  	return scheme.IsValidForCreate()
   108  }
   109  
   110  func (scheme *Scheme) IsValidForCreate() bool {
   111  	if len(scheme.DisplayName) == 0 || len(scheme.DisplayName) > SCHEME_DISPLAY_NAME_MAX_LENGTH {
   112  		return false
   113  	}
   114  
   115  	if !IsValidSchemeName(scheme.Name) {
   116  		return false
   117  	}
   118  
   119  	if len(scheme.Description) > SCHEME_DESCRIPTION_MAX_LENGTH {
   120  		return false
   121  	}
   122  
   123  	switch scheme.Scope {
   124  	case SCHEME_SCOPE_TEAM, SCHEME_SCOPE_CHANNEL:
   125  	default:
   126  		return false
   127  	}
   128  
   129  	if !IsValidRoleName(scheme.DefaultChannelAdminRole) {
   130  		return false
   131  	}
   132  
   133  	if !IsValidRoleName(scheme.DefaultChannelUserRole) {
   134  		return false
   135  	}
   136  
   137  	if scheme.Scope == SCHEME_SCOPE_TEAM {
   138  		if !IsValidRoleName(scheme.DefaultTeamAdminRole) {
   139  			return false
   140  		}
   141  
   142  		if !IsValidRoleName(scheme.DefaultTeamUserRole) {
   143  			return false
   144  		}
   145  	}
   146  
   147  	if scheme.Scope == SCHEME_SCOPE_CHANNEL {
   148  		if len(scheme.DefaultTeamAdminRole) != 0 {
   149  			return false
   150  		}
   151  
   152  		if len(scheme.DefaultTeamUserRole) != 0 {
   153  			return false
   154  		}
   155  	}
   156  
   157  	return true
   158  }
   159  
   160  func (scheme *Scheme) Patch(patch *SchemePatch) {
   161  	if patch.DisplayName != nil {
   162  		scheme.DisplayName = *patch.DisplayName
   163  	}
   164  	if patch.Name != nil {
   165  		scheme.Name = *patch.Name
   166  	}
   167  	if patch.Description != nil {
   168  		scheme.Description = *patch.Description
   169  	}
   170  }
   171  
   172  func (patch *SchemePatch) ToJson() string {
   173  	b, _ := json.Marshal(patch)
   174  	return string(b)
   175  }
   176  
   177  func SchemePatchFromJson(data io.Reader) *SchemePatch {
   178  	var patch *SchemePatch
   179  	json.NewDecoder(data).Decode(&patch)
   180  	return patch
   181  }
   182  
   183  func SchemeIDFromJson(data io.Reader) *string {
   184  	var p *SchemeIDPatch
   185  	json.NewDecoder(data).Decode(&p)
   186  	return p.SchemeID
   187  }
   188  
   189  func (p *SchemeIDPatch) ToJson() string {
   190  	b, _ := json.Marshal(p)
   191  	return string(b)
   192  }
   193  
   194  func IsValidSchemeName(name string) bool {
   195  	re := regexp.MustCompile(fmt.Sprintf("^[a-z0-9_]{2,%d}$", SCHEME_NAME_MAX_LENGTH))
   196  	return re.MatchString(name)
   197  }
   198  
   199  func (schemeRoles *SchemeRoles) ToJson() string {
   200  	b, _ := json.Marshal(schemeRoles)
   201  	return string(b)
   202  }
   203  
   204  func SchemeRolesFromJson(data io.Reader) *SchemeRoles {
   205  	var schemeRoles *SchemeRoles
   206  	json.NewDecoder(data).Decode(&schemeRoles)
   207  	return schemeRoles
   208  }