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

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package model
     5  
     6  import (
     7  	"encoding/json"
     8  	"io"
     9  	"net/http"
    10  )
    11  
    12  const (
    13  	EXPIRED_LICENSE_ERROR = "api.license.add_license.expired.app_error"
    14  	INVALID_LICENSE_ERROR = "api.license.add_license.invalid.app_error"
    15  )
    16  
    17  type LicenseRecord struct {
    18  	Id       string `json:"id"`
    19  	CreateAt int64  `json:"create_at"`
    20  	Bytes    string `json:"-"`
    21  }
    22  
    23  type License struct {
    24  	Id           string    `json:"id"`
    25  	IssuedAt     int64     `json:"issued_at"`
    26  	StartsAt     int64     `json:"starts_at"`
    27  	ExpiresAt    int64     `json:"expires_at"`
    28  	Customer     *Customer `json:"customer"`
    29  	Features     *Features `json:"features"`
    30  	SkuName      string    `json:"sku_name"`
    31  	SkuShortName string    `json:"sku_short_name"`
    32  }
    33  
    34  type Customer struct {
    35  	Id          string `json:"id"`
    36  	Name        string `json:"name"`
    37  	Email       string `json:"email"`
    38  	Company     string `json:"company"`
    39  	PhoneNumber string `json:"phone_number"`
    40  }
    41  
    42  type Features struct {
    43  	Users                     *int  `json:"users"`
    44  	LDAP                      *bool `json:"ldap"`
    45  	LDAPGroups                *bool `json:"ldap_groups"`
    46  	MFA                       *bool `json:"mfa"`
    47  	GoogleOAuth               *bool `json:"google_oauth"`
    48  	Office365OAuth            *bool `json:"office365_oauth"`
    49  	Compliance                *bool `json:"compliance"`
    50  	Cluster                   *bool `json:"cluster"`
    51  	Metrics                   *bool `json:"metrics"`
    52  	MHPNS                     *bool `json:"mhpns"`
    53  	SAML                      *bool `json:"saml"`
    54  	Elasticsearch             *bool `json:"elastic_search"`
    55  	Announcement              *bool `json:"announcement"`
    56  	ThemeManagement           *bool `json:"theme_management"`
    57  	EmailNotificationContents *bool `json:"email_notification_contents"`
    58  	DataRetention             *bool `json:"data_retention"`
    59  	MessageExport             *bool `json:"message_export"`
    60  	CustomPermissionsSchemes  *bool `json:"custom_permissions_schemes"`
    61  	CustomTermsOfService      *bool `json:"custom_terms_of_service"`
    62  
    63  	// after we enabled more features we'll need to control them with this
    64  	FutureFeatures *bool `json:"future_features"`
    65  }
    66  
    67  func (f *Features) ToMap() map[string]interface{} {
    68  	return map[string]interface{}{
    69  		"ldap":                        *f.LDAP,
    70  		"ldap_groups":                 *f.LDAPGroups,
    71  		"mfa":                         *f.MFA,
    72  		"google":                      *f.GoogleOAuth,
    73  		"office365":                   *f.Office365OAuth,
    74  		"compliance":                  *f.Compliance,
    75  		"cluster":                     *f.Cluster,
    76  		"metrics":                     *f.Metrics,
    77  		"mhpns":                       *f.MHPNS,
    78  		"saml":                        *f.SAML,
    79  		"elastic_search":              *f.Elasticsearch,
    80  		"email_notification_contents": *f.EmailNotificationContents,
    81  		"data_retention":              *f.DataRetention,
    82  		"message_export":              *f.MessageExport,
    83  		"custom_permissions_schemes":  *f.CustomPermissionsSchemes,
    84  		"future":                      *f.FutureFeatures,
    85  	}
    86  }
    87  
    88  func (f *Features) SetDefaults() {
    89  	if f.FutureFeatures == nil {
    90  		f.FutureFeatures = NewBool(true)
    91  	}
    92  
    93  	if f.Users == nil {
    94  		f.Users = NewInt(0)
    95  	}
    96  
    97  	if f.LDAP == nil {
    98  		f.LDAP = NewBool(*f.FutureFeatures)
    99  	}
   100  
   101  	if f.LDAPGroups == nil {
   102  		f.LDAPGroups = NewBool(*f.FutureFeatures)
   103  	}
   104  
   105  	if f.MFA == nil {
   106  		f.MFA = NewBool(*f.FutureFeatures)
   107  	}
   108  
   109  	if f.GoogleOAuth == nil {
   110  		f.GoogleOAuth = NewBool(*f.FutureFeatures)
   111  	}
   112  
   113  	if f.Office365OAuth == nil {
   114  		f.Office365OAuth = NewBool(*f.FutureFeatures)
   115  	}
   116  
   117  	if f.Compliance == nil {
   118  		f.Compliance = NewBool(*f.FutureFeatures)
   119  	}
   120  
   121  	if f.Cluster == nil {
   122  		f.Cluster = NewBool(*f.FutureFeatures)
   123  	}
   124  
   125  	if f.Metrics == nil {
   126  		f.Metrics = NewBool(*f.FutureFeatures)
   127  	}
   128  
   129  	if f.MHPNS == nil {
   130  		f.MHPNS = NewBool(*f.FutureFeatures)
   131  	}
   132  
   133  	if f.SAML == nil {
   134  		f.SAML = NewBool(*f.FutureFeatures)
   135  	}
   136  
   137  	if f.Elasticsearch == nil {
   138  		f.Elasticsearch = NewBool(*f.FutureFeatures)
   139  	}
   140  
   141  	if f.Announcement == nil {
   142  		f.Announcement = NewBool(true)
   143  	}
   144  
   145  	if f.ThemeManagement == nil {
   146  		f.ThemeManagement = NewBool(true)
   147  	}
   148  
   149  	if f.EmailNotificationContents == nil {
   150  		f.EmailNotificationContents = NewBool(*f.FutureFeatures)
   151  	}
   152  
   153  	if f.DataRetention == nil {
   154  		f.DataRetention = NewBool(*f.FutureFeatures)
   155  	}
   156  
   157  	if f.MessageExport == nil {
   158  		f.MessageExport = NewBool(*f.FutureFeatures)
   159  	}
   160  
   161  	if f.CustomPermissionsSchemes == nil {
   162  		f.CustomPermissionsSchemes = NewBool(*f.FutureFeatures)
   163  	}
   164  
   165  	if f.CustomTermsOfService == nil {
   166  		f.CustomTermsOfService = NewBool(*f.FutureFeatures)
   167  	}
   168  }
   169  
   170  func (l *License) IsExpired() bool {
   171  	return l.ExpiresAt < GetMillis()
   172  }
   173  
   174  func (l *License) IsStarted() bool {
   175  	return l.StartsAt < GetMillis()
   176  }
   177  
   178  func (l *License) ToJson() string {
   179  	b, _ := json.Marshal(l)
   180  	return string(b)
   181  }
   182  
   183  // NewTestLicense returns a license that expires in the future and has the given features.
   184  func NewTestLicense(features ...string) *License {
   185  	ret := &License{
   186  		ExpiresAt: GetMillis() + 90*24*60*60*1000,
   187  		Customer:  &Customer{},
   188  		Features:  &Features{},
   189  	}
   190  	ret.Features.SetDefaults()
   191  
   192  	featureMap := map[string]bool{}
   193  	for _, feature := range features {
   194  		featureMap[feature] = true
   195  	}
   196  	featureJson, _ := json.Marshal(featureMap)
   197  	json.Unmarshal(featureJson, &ret.Features)
   198  
   199  	return ret
   200  }
   201  
   202  func LicenseFromJson(data io.Reader) *License {
   203  	var o *License
   204  	json.NewDecoder(data).Decode(&o)
   205  	return o
   206  }
   207  
   208  func (lr *LicenseRecord) IsValid() *AppError {
   209  	if len(lr.Id) != 26 {
   210  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest)
   211  	}
   212  
   213  	if lr.CreateAt == 0 {
   214  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   215  	}
   216  
   217  	if len(lr.Bytes) == 0 || len(lr.Bytes) > 10000 {
   218  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   219  	}
   220  
   221  	return nil
   222  }
   223  
   224  func (lr *LicenseRecord) PreSave() {
   225  	lr.CreateAt = GetMillis()
   226  }