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