github.com/wgh-/mattermost-server@v4.8.0-rc2+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  }
    31  
    32  type Customer struct {
    33  	Id          string `json:"id"`
    34  	Name        string `json:"name"`
    35  	Email       string `json:"email"`
    36  	Company     string `json:"company"`
    37  	PhoneNumber string `json:"phone_number"`
    38  }
    39  
    40  type Features struct {
    41  	Users                     *int  `json:"users"`
    42  	LDAP                      *bool `json:"ldap"`
    43  	MFA                       *bool `json:"mfa"`
    44  	GoogleOAuth               *bool `json:"google_oauth"`
    45  	Office365OAuth            *bool `json:"office365_oauth"`
    46  	Compliance                *bool `json:"compliance"`
    47  	Cluster                   *bool `json:"cluster"`
    48  	Metrics                   *bool `json:"metrics"`
    49  	CustomBrand               *bool `json:"custom_brand"`
    50  	MHPNS                     *bool `json:"mhpns"`
    51  	SAML                      *bool `json:"saml"`
    52  	PasswordRequirements      *bool `json:"password_requirements"`
    53  	Elasticsearch             *bool `json:"elastic_search"`
    54  	Announcement              *bool `json:"announcement"`
    55  	ThemeManagement           *bool `json:"theme_management"`
    56  	EmailNotificationContents *bool `json:"email_notification_contents"`
    57  	DataRetention             *bool `json:"data_retention"`
    58  	MessageExport             *bool `json:"message_export"`
    59  
    60  	// after we enabled more features for webrtc we'll need to control them with this
    61  	FutureFeatures *bool `json:"future_features"`
    62  }
    63  
    64  func (f *Features) ToMap() map[string]interface{} {
    65  	return map[string]interface{}{
    66  		"ldap":                        *f.LDAP,
    67  		"mfa":                         *f.MFA,
    68  		"google":                      *f.GoogleOAuth,
    69  		"office365":                   *f.Office365OAuth,
    70  		"compliance":                  *f.Compliance,
    71  		"cluster":                     *f.Cluster,
    72  		"metrics":                     *f.Metrics,
    73  		"custom_brand":                *f.CustomBrand,
    74  		"mhpns":                       *f.MHPNS,
    75  		"saml":                        *f.SAML,
    76  		"password":                    *f.PasswordRequirements,
    77  		"elastic_search":              *f.Elasticsearch,
    78  		"email_notification_contents": *f.EmailNotificationContents,
    79  		"data_retention":              *f.DataRetention,
    80  		"message_export":              *f.MessageExport,
    81  		"future":                      *f.FutureFeatures,
    82  	}
    83  }
    84  
    85  func (f *Features) SetDefaults() {
    86  	if f.FutureFeatures == nil {
    87  		f.FutureFeatures = NewBool(true)
    88  	}
    89  
    90  	if f.Users == nil {
    91  		f.Users = NewInt(0)
    92  	}
    93  
    94  	if f.LDAP == nil {
    95  		f.LDAP = NewBool(*f.FutureFeatures)
    96  	}
    97  
    98  	if f.MFA == nil {
    99  		f.MFA = NewBool(*f.FutureFeatures)
   100  	}
   101  
   102  	if f.GoogleOAuth == nil {
   103  		f.GoogleOAuth = NewBool(*f.FutureFeatures)
   104  	}
   105  
   106  	if f.Office365OAuth == nil {
   107  		f.Office365OAuth = NewBool(*f.FutureFeatures)
   108  	}
   109  
   110  	if f.Compliance == nil {
   111  		f.Compliance = NewBool(*f.FutureFeatures)
   112  	}
   113  
   114  	if f.Cluster == nil {
   115  		f.Cluster = NewBool(*f.FutureFeatures)
   116  	}
   117  
   118  	if f.Metrics == nil {
   119  		f.Metrics = NewBool(*f.FutureFeatures)
   120  	}
   121  
   122  	if f.CustomBrand == nil {
   123  		f.CustomBrand = NewBool(*f.FutureFeatures)
   124  	}
   125  
   126  	if f.MHPNS == nil {
   127  		f.MHPNS = NewBool(*f.FutureFeatures)
   128  	}
   129  
   130  	if f.SAML == nil {
   131  		f.SAML = NewBool(*f.FutureFeatures)
   132  	}
   133  
   134  	if f.PasswordRequirements == nil {
   135  		f.PasswordRequirements = NewBool(*f.FutureFeatures)
   136  	}
   137  
   138  	if f.Elasticsearch == nil {
   139  		f.Elasticsearch = NewBool(*f.FutureFeatures)
   140  	}
   141  
   142  	if f.Announcement == nil {
   143  		f.Announcement = NewBool(true)
   144  	}
   145  
   146  	if f.ThemeManagement == nil {
   147  		f.ThemeManagement = NewBool(true)
   148  	}
   149  
   150  	if f.EmailNotificationContents == nil {
   151  		f.EmailNotificationContents = NewBool(*f.FutureFeatures)
   152  	}
   153  
   154  	if f.DataRetention == nil {
   155  		f.DataRetention = NewBool(*f.FutureFeatures)
   156  	}
   157  
   158  	if f.MessageExport == nil {
   159  		f.MessageExport = NewBool(*f.FutureFeatures)
   160  	}
   161  }
   162  
   163  func (l *License) IsExpired() bool {
   164  	return l.ExpiresAt < GetMillis()
   165  }
   166  
   167  func (l *License) IsStarted() bool {
   168  	return l.StartsAt < GetMillis()
   169  }
   170  
   171  func (l *License) ToJson() string {
   172  	b, _ := json.Marshal(l)
   173  	return string(b)
   174  }
   175  
   176  // NewTestLicense returns a license that expires in the future and has the given features.
   177  func NewTestLicense(features ...string) *License {
   178  	ret := &License{
   179  		ExpiresAt: GetMillis() + 90*24*60*60*1000,
   180  		Customer:  &Customer{},
   181  		Features:  &Features{},
   182  	}
   183  	ret.Features.SetDefaults()
   184  
   185  	featureMap := map[string]bool{}
   186  	for _, feature := range features {
   187  		featureMap[feature] = true
   188  	}
   189  	featureJson, _ := json.Marshal(featureMap)
   190  	json.Unmarshal(featureJson, &ret.Features)
   191  
   192  	return ret
   193  }
   194  
   195  func LicenseFromJson(data io.Reader) *License {
   196  	var o *License
   197  	json.NewDecoder(data).Decode(&o)
   198  	return o
   199  }
   200  
   201  func (lr *LicenseRecord) IsValid() *AppError {
   202  	if len(lr.Id) != 26 {
   203  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest)
   204  	}
   205  
   206  	if lr.CreateAt == 0 {
   207  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   208  	}
   209  
   210  	if len(lr.Bytes) == 0 || len(lr.Bytes) > 10000 {
   211  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   212  	}
   213  
   214  	return nil
   215  }
   216  
   217  func (lr *LicenseRecord) PreSave() {
   218  	lr.CreateAt = GetMillis()
   219  }