github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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 LICENSE_GRACE_PERIOD = 1000 * 60 * 60 * 24 * 10 //10 days 16 LICENSE_RENEWAL_LINK = "https://mattermost.com/renew/" 17 ) 18 19 type LicenseRecord struct { 20 Id string `json:"id"` 21 CreateAt int64 `json:"create_at"` 22 Bytes string `json:"-"` 23 } 24 25 type License struct { 26 Id string `json:"id"` 27 IssuedAt int64 `json:"issued_at"` 28 StartsAt int64 `json:"starts_at"` 29 ExpiresAt int64 `json:"expires_at"` 30 Customer *Customer `json:"customer"` 31 Features *Features `json:"features"` 32 SkuName string `json:"sku_name"` 33 SkuShortName string `json:"sku_short_name"` 34 } 35 36 type Customer struct { 37 Id string `json:"id"` 38 Name string `json:"name"` 39 Email string `json:"email"` 40 Company string `json:"company"` 41 } 42 43 type TrialLicenseRequest struct { 44 ServerID string `json:"server_id"` 45 Email string `json:"email"` 46 Name string `json:"name"` 47 SiteURL string `json:"site_url"` 48 SiteName string `json:"site_name"` 49 Users int `json:"users"` 50 TermsAccepted bool `json:"terms_accepted"` 51 ReceiveEmailsAccepted bool `json:"receive_emails_accepted"` 52 } 53 54 func (tlr *TrialLicenseRequest) ToJson() string { 55 b, _ := json.Marshal(tlr) 56 return string(b) 57 } 58 59 type Features struct { 60 Users *int `json:"users"` 61 LDAP *bool `json:"ldap"` 62 LDAPGroups *bool `json:"ldap_groups"` 63 MFA *bool `json:"mfa"` 64 GoogleOAuth *bool `json:"google_oauth"` 65 Office365OAuth *bool `json:"office365_oauth"` 66 Compliance *bool `json:"compliance"` 67 Cluster *bool `json:"cluster"` 68 Metrics *bool `json:"metrics"` 69 MHPNS *bool `json:"mhpns"` 70 SAML *bool `json:"saml"` 71 Elasticsearch *bool `json:"elastic_search"` 72 Announcement *bool `json:"announcement"` 73 ThemeManagement *bool `json:"theme_management"` 74 EmailNotificationContents *bool `json:"email_notification_contents"` 75 DataRetention *bool `json:"data_retention"` 76 MessageExport *bool `json:"message_export"` 77 CustomPermissionsSchemes *bool `json:"custom_permissions_schemes"` 78 CustomTermsOfService *bool `json:"custom_terms_of_service"` 79 GuestAccounts *bool `json:"guest_accounts"` 80 GuestAccountsPermissions *bool `json:"guest_accounts_permissions"` 81 IDLoadedPushNotifications *bool `json:"id_loaded"` 82 LockTeammateNameDisplay *bool `json:"lock_teammate_name_display"` 83 EnterprisePlugins *bool `json:"enterprise_plugins"` 84 AdvancedLogging *bool `json:"advanced_logging"` 85 86 // after we enabled more features we'll need to control them with this 87 FutureFeatures *bool `json:"future_features"` 88 } 89 90 func (f *Features) ToMap() map[string]interface{} { 91 return map[string]interface{}{ 92 "ldap": *f.LDAP, 93 "ldap_groups": *f.LDAPGroups, 94 "mfa": *f.MFA, 95 "google": *f.GoogleOAuth, 96 "office365": *f.Office365OAuth, 97 "compliance": *f.Compliance, 98 "cluster": *f.Cluster, 99 "metrics": *f.Metrics, 100 "mhpns": *f.MHPNS, 101 "saml": *f.SAML, 102 "elastic_search": *f.Elasticsearch, 103 "email_notification_contents": *f.EmailNotificationContents, 104 "data_retention": *f.DataRetention, 105 "message_export": *f.MessageExport, 106 "custom_permissions_schemes": *f.CustomPermissionsSchemes, 107 "guest_accounts": *f.GuestAccounts, 108 "guest_accounts_permissions": *f.GuestAccountsPermissions, 109 "id_loaded": *f.IDLoadedPushNotifications, 110 "lock_teammate_name_display": *f.LockTeammateNameDisplay, 111 "enterprise_plugins": *f.EnterprisePlugins, 112 "advanced_logging": *f.AdvancedLogging, 113 "future": *f.FutureFeatures, 114 } 115 } 116 117 func (f *Features) SetDefaults() { 118 if f.FutureFeatures == nil { 119 f.FutureFeatures = NewBool(true) 120 } 121 122 if f.Users == nil { 123 f.Users = NewInt(0) 124 } 125 126 if f.LDAP == nil { 127 f.LDAP = NewBool(*f.FutureFeatures) 128 } 129 130 if f.LDAPGroups == nil { 131 f.LDAPGroups = NewBool(*f.FutureFeatures) 132 } 133 134 if f.MFA == nil { 135 f.MFA = NewBool(*f.FutureFeatures) 136 } 137 138 if f.GoogleOAuth == nil { 139 f.GoogleOAuth = NewBool(*f.FutureFeatures) 140 } 141 142 if f.Office365OAuth == nil { 143 f.Office365OAuth = NewBool(*f.FutureFeatures) 144 } 145 146 if f.Compliance == nil { 147 f.Compliance = NewBool(*f.FutureFeatures) 148 } 149 150 if f.Cluster == nil { 151 f.Cluster = NewBool(*f.FutureFeatures) 152 } 153 154 if f.Metrics == nil { 155 f.Metrics = NewBool(*f.FutureFeatures) 156 } 157 158 if f.MHPNS == nil { 159 f.MHPNS = NewBool(*f.FutureFeatures) 160 } 161 162 if f.SAML == nil { 163 f.SAML = NewBool(*f.FutureFeatures) 164 } 165 166 if f.Elasticsearch == nil { 167 f.Elasticsearch = NewBool(*f.FutureFeatures) 168 } 169 170 if f.Announcement == nil { 171 f.Announcement = NewBool(true) 172 } 173 174 if f.ThemeManagement == nil { 175 f.ThemeManagement = NewBool(true) 176 } 177 178 if f.EmailNotificationContents == nil { 179 f.EmailNotificationContents = NewBool(*f.FutureFeatures) 180 } 181 182 if f.DataRetention == nil { 183 f.DataRetention = NewBool(*f.FutureFeatures) 184 } 185 186 if f.MessageExport == nil { 187 f.MessageExport = NewBool(*f.FutureFeatures) 188 } 189 190 if f.CustomPermissionsSchemes == nil { 191 f.CustomPermissionsSchemes = NewBool(*f.FutureFeatures) 192 } 193 194 if f.GuestAccounts == nil { 195 f.GuestAccounts = NewBool(*f.FutureFeatures) 196 } 197 198 if f.GuestAccountsPermissions == nil { 199 f.GuestAccountsPermissions = NewBool(*f.FutureFeatures) 200 } 201 202 if f.CustomTermsOfService == nil { 203 f.CustomTermsOfService = NewBool(*f.FutureFeatures) 204 } 205 206 if f.IDLoadedPushNotifications == nil { 207 f.IDLoadedPushNotifications = NewBool(*f.FutureFeatures) 208 } 209 210 if f.LockTeammateNameDisplay == nil { 211 f.LockTeammateNameDisplay = NewBool(*f.FutureFeatures) 212 } 213 214 if f.EnterprisePlugins == nil { 215 f.EnterprisePlugins = NewBool(*f.FutureFeatures) 216 } 217 218 if f.AdvancedLogging == nil { 219 f.AdvancedLogging = NewBool(*f.FutureFeatures) 220 } 221 } 222 223 func (l *License) IsExpired() bool { 224 return l.ExpiresAt < GetMillis() 225 } 226 227 func (l *License) IsPastGracePeriod() bool { 228 timeDiff := GetMillis() - l.ExpiresAt 229 return timeDiff > LICENSE_GRACE_PERIOD 230 } 231 232 func (l *License) IsStarted() bool { 233 return l.StartsAt < GetMillis() 234 } 235 236 func (l *License) ToJson() string { 237 b, _ := json.Marshal(l) 238 return string(b) 239 } 240 241 // NewTestLicense returns a license that expires in the future and has the given features. 242 func NewTestLicense(features ...string) *License { 243 ret := &License{ 244 ExpiresAt: GetMillis() + 90*24*60*60*1000, 245 Customer: &Customer{}, 246 Features: &Features{}, 247 } 248 ret.Features.SetDefaults() 249 250 featureMap := map[string]bool{} 251 for _, feature := range features { 252 featureMap[feature] = true 253 } 254 featureJson, _ := json.Marshal(featureMap) 255 json.Unmarshal(featureJson, &ret.Features) 256 257 return ret 258 } 259 260 func LicenseFromJson(data io.Reader) *License { 261 var o *License 262 json.NewDecoder(data).Decode(&o) 263 return o 264 } 265 266 func (lr *LicenseRecord) IsValid() *AppError { 267 if !IsValidId(lr.Id) { 268 return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest) 269 } 270 271 if lr.CreateAt == 0 { 272 return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest) 273 } 274 275 if len(lr.Bytes) == 0 || len(lr.Bytes) > 10000 { 276 return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest) 277 } 278 279 return nil 280 } 281 282 func (lr *LicenseRecord) PreSave() { 283 lr.CreateAt = GetMillis() 284 }