github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/model/license.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  	"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  	GuestAccounts             *bool `json:"guest_accounts"`
    63  	GuestAccountsPermissions  *bool `json:"guest_accounts_permissions"`
    64  	IDLoadedPushNotifications *bool `json:"id_loaded"`
    65  	LockBranchmateNameDisplay *bool `json:"lock_branchmate_name_display"`
    66  
    67  	// after we enabled more features we'll need to control them with this
    68  	FutureFeatures *bool `json:"future_features"`
    69  }
    70  
    71  func (f *Features) ToMap() map[string]interface{} {
    72  	return map[string]interface{}{
    73  		"ldap":                         *f.LDAP,
    74  		"ldap_groups":                  *f.LDAPGroups,
    75  		"mfa":                          *f.MFA,
    76  		"google":                       *f.GoogleOAuth,
    77  		"office365":                    *f.Office365OAuth,
    78  		"compliance":                   *f.Compliance,
    79  		"cluster":                      *f.Cluster,
    80  		"metrics":                      *f.Metrics,
    81  		"mhpns":                        *f.MHPNS,
    82  		"saml":                         *f.SAML,
    83  		"elastic_search":               *f.Elasticsearch,
    84  		"email_notification_contents":  *f.EmailNotificationContents,
    85  		"data_retention":               *f.DataRetention,
    86  		"message_export":               *f.MessageExport,
    87  		"custom_permissions_schemes":   *f.CustomPermissionsSchemes,
    88  		"guest_accounts":               *f.GuestAccounts,
    89  		"guest_accounts_permissions":   *f.GuestAccountsPermissions,
    90  		"id_loaded":                    *f.IDLoadedPushNotifications,
    91  		"lock_branchmate_name_display": *f.LockBranchmateNameDisplay,
    92  		"future":                       *f.FutureFeatures,
    93  	}
    94  }
    95  
    96  func (f *Features) SetDefaults() {
    97  	if f.FutureFeatures == nil {
    98  		f.FutureFeatures = NewBool(true)
    99  	}
   100  
   101  	if f.Users == nil {
   102  		f.Users = NewInt(0)
   103  	}
   104  
   105  	if f.LDAP == nil {
   106  		f.LDAP = NewBool(*f.FutureFeatures)
   107  	}
   108  
   109  	if f.LDAPGroups == nil {
   110  		f.LDAPGroups = NewBool(*f.FutureFeatures)
   111  	}
   112  
   113  	if f.MFA == nil {
   114  		f.MFA = NewBool(*f.FutureFeatures)
   115  	}
   116  
   117  	if f.GoogleOAuth == nil {
   118  		f.GoogleOAuth = NewBool(*f.FutureFeatures)
   119  	}
   120  
   121  	if f.Office365OAuth == nil {
   122  		f.Office365OAuth = NewBool(*f.FutureFeatures)
   123  	}
   124  
   125  	if f.Compliance == nil {
   126  		f.Compliance = NewBool(*f.FutureFeatures)
   127  	}
   128  
   129  	if f.Cluster == nil {
   130  		f.Cluster = NewBool(*f.FutureFeatures)
   131  	}
   132  
   133  	if f.Metrics == nil {
   134  		f.Metrics = NewBool(*f.FutureFeatures)
   135  	}
   136  
   137  	if f.MHPNS == nil {
   138  		f.MHPNS = NewBool(*f.FutureFeatures)
   139  	}
   140  
   141  	if f.SAML == nil {
   142  		f.SAML = NewBool(*f.FutureFeatures)
   143  	}
   144  
   145  	if f.Elasticsearch == nil {
   146  		f.Elasticsearch = NewBool(*f.FutureFeatures)
   147  	}
   148  
   149  	if f.Announcement == nil {
   150  		f.Announcement = NewBool(true)
   151  	}
   152  
   153  	if f.ThemeManagement == nil {
   154  		f.ThemeManagement = NewBool(true)
   155  	}
   156  
   157  	if f.EmailNotificationContents == nil {
   158  		f.EmailNotificationContents = NewBool(*f.FutureFeatures)
   159  	}
   160  
   161  	if f.DataRetention == nil {
   162  		f.DataRetention = NewBool(*f.FutureFeatures)
   163  	}
   164  
   165  	if f.MessageExport == nil {
   166  		f.MessageExport = NewBool(*f.FutureFeatures)
   167  	}
   168  
   169  	if f.CustomPermissionsSchemes == nil {
   170  		f.CustomPermissionsSchemes = NewBool(*f.FutureFeatures)
   171  	}
   172  
   173  	if f.GuestAccounts == nil {
   174  		f.GuestAccounts = NewBool(*f.FutureFeatures)
   175  	}
   176  
   177  	if f.GuestAccountsPermissions == nil {
   178  		f.GuestAccountsPermissions = NewBool(*f.FutureFeatures)
   179  	}
   180  
   181  	if f.CustomTermsOfService == nil {
   182  		f.CustomTermsOfService = NewBool(*f.FutureFeatures)
   183  	}
   184  
   185  	if f.IDLoadedPushNotifications == nil {
   186  		f.IDLoadedPushNotifications = NewBool(*f.FutureFeatures)
   187  	}
   188  
   189  	if f.LockBranchmateNameDisplay == nil {
   190  		f.LockBranchmateNameDisplay = NewBool(*f.FutureFeatures)
   191  	}
   192  }
   193  
   194  func (l *License) IsExpired() bool {
   195  	return l.ExpiresAt < GetMillis()
   196  }
   197  
   198  func (l *License) IsStarted() bool {
   199  	return l.StartsAt < GetMillis()
   200  }
   201  
   202  func (l *License) ToJson() string {
   203  	b, _ := json.Marshal(l)
   204  	return string(b)
   205  }
   206  
   207  // NewTestLicense returns a license that expires in the future and has the given features.
   208  func NewTestLicense(features ...string) *License {
   209  	ret := &License{
   210  		ExpiresAt: GetMillis() + 90*24*60*60*1000,
   211  		Customer:  &Customer{},
   212  		Features:  &Features{},
   213  	}
   214  	ret.Features.SetDefaults()
   215  
   216  	featureMap := map[string]bool{}
   217  	for _, feature := range features {
   218  		featureMap[feature] = true
   219  	}
   220  	featureJson, _ := json.Marshal(featureMap)
   221  	json.Unmarshal(featureJson, &ret.Features)
   222  
   223  	return ret
   224  }
   225  
   226  func LicenseFromJson(data io.Reader) *License {
   227  	var o *License
   228  	json.NewDecoder(data).Decode(&o)
   229  	return o
   230  }
   231  
   232  func (lr *LicenseRecord) IsValid() *AppError {
   233  	if len(lr.Id) != 26 {
   234  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest)
   235  	}
   236  
   237  	if lr.CreateAt == 0 {
   238  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   239  	}
   240  
   241  	if len(lr.Bytes) == 0 || len(lr.Bytes) > 10000 {
   242  		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
   243  	}
   244  
   245  	return nil
   246  }
   247  
   248  func (lr *LicenseRecord) PreSave() {
   249  	lr.CreateAt = GetMillis()
   250  }