github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/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 OpenId *bool `json:"openid"` 67 Compliance *bool `json:"compliance"` 68 Cluster *bool `json:"cluster"` 69 Metrics *bool `json:"metrics"` 70 MHPNS *bool `json:"mhpns"` 71 SAML *bool `json:"saml"` 72 Elasticsearch *bool `json:"elastic_search"` 73 Announcement *bool `json:"announcement"` 74 ThemeManagement *bool `json:"theme_management"` 75 EmailNotificationContents *bool `json:"email_notification_contents"` 76 DataRetention *bool `json:"data_retention"` 77 MessageExport *bool `json:"message_export"` 78 CustomPermissionsSchemes *bool `json:"custom_permissions_schemes"` 79 CustomTermsOfService *bool `json:"custom_terms_of_service"` 80 GuestAccounts *bool `json:"guest_accounts"` 81 GuestAccountsPermissions *bool `json:"guest_accounts_permissions"` 82 IDLoadedPushNotifications *bool `json:"id_loaded"` 83 LockTeammateNameDisplay *bool `json:"lock_teammate_name_display"` 84 EnterprisePlugins *bool `json:"enterprise_plugins"` 85 AdvancedLogging *bool `json:"advanced_logging"` 86 Cloud *bool `json:"cloud"` 87 SharedChannels *bool `json:"shared_channels"` 88 RemoteClusterService *bool `json:"remote_cluster_service"` 89 90 // after we enabled more features we'll need to control them with this 91 FutureFeatures *bool `json:"future_features"` 92 } 93 94 func (f *Features) ToMap() map[string]interface{} { 95 return map[string]interface{}{ 96 "ldap": *f.LDAP, 97 "ldap_groups": *f.LDAPGroups, 98 "mfa": *f.MFA, 99 "google": *f.GoogleOAuth, 100 "office365": *f.Office365OAuth, 101 "openid": *f.OpenId, 102 "compliance": *f.Compliance, 103 "cluster": *f.Cluster, 104 "metrics": *f.Metrics, 105 "mhpns": *f.MHPNS, 106 "saml": *f.SAML, 107 "elastic_search": *f.Elasticsearch, 108 "email_notification_contents": *f.EmailNotificationContents, 109 "data_retention": *f.DataRetention, 110 "message_export": *f.MessageExport, 111 "custom_permissions_schemes": *f.CustomPermissionsSchemes, 112 "guest_accounts": *f.GuestAccounts, 113 "guest_accounts_permissions": *f.GuestAccountsPermissions, 114 "id_loaded": *f.IDLoadedPushNotifications, 115 "lock_teammate_name_display": *f.LockTeammateNameDisplay, 116 "enterprise_plugins": *f.EnterprisePlugins, 117 "advanced_logging": *f.AdvancedLogging, 118 "cloud": *f.Cloud, 119 "shared_channels": *f.SharedChannels, 120 "remote_cluster_service": *f.RemoteClusterService, 121 "future": *f.FutureFeatures, 122 } 123 } 124 125 func (f *Features) SetDefaults() { 126 if f.FutureFeatures == nil { 127 f.FutureFeatures = NewBool(true) 128 } 129 130 if f.Users == nil { 131 f.Users = NewInt(0) 132 } 133 134 if f.LDAP == nil { 135 f.LDAP = NewBool(*f.FutureFeatures) 136 } 137 138 if f.LDAPGroups == nil { 139 f.LDAPGroups = NewBool(*f.FutureFeatures) 140 } 141 142 if f.MFA == nil { 143 f.MFA = NewBool(*f.FutureFeatures) 144 } 145 146 if f.GoogleOAuth == nil { 147 f.GoogleOAuth = NewBool(*f.FutureFeatures) 148 } 149 150 if f.Office365OAuth == nil { 151 f.Office365OAuth = NewBool(*f.FutureFeatures) 152 } 153 154 if f.OpenId == nil { 155 f.OpenId = NewBool(*f.FutureFeatures) 156 } 157 158 if f.Compliance == nil { 159 f.Compliance = NewBool(*f.FutureFeatures) 160 } 161 162 if f.Cluster == nil { 163 f.Cluster = NewBool(*f.FutureFeatures) 164 } 165 166 if f.Metrics == nil { 167 f.Metrics = NewBool(*f.FutureFeatures) 168 } 169 170 if f.MHPNS == nil { 171 f.MHPNS = NewBool(*f.FutureFeatures) 172 } 173 174 if f.SAML == nil { 175 f.SAML = NewBool(*f.FutureFeatures) 176 } 177 178 if f.Elasticsearch == nil { 179 f.Elasticsearch = NewBool(*f.FutureFeatures) 180 } 181 182 if f.Announcement == nil { 183 f.Announcement = NewBool(true) 184 } 185 186 if f.ThemeManagement == nil { 187 f.ThemeManagement = NewBool(true) 188 } 189 190 if f.EmailNotificationContents == nil { 191 f.EmailNotificationContents = NewBool(*f.FutureFeatures) 192 } 193 194 if f.DataRetention == nil { 195 f.DataRetention = NewBool(*f.FutureFeatures) 196 } 197 198 if f.MessageExport == nil { 199 f.MessageExport = NewBool(*f.FutureFeatures) 200 } 201 202 if f.CustomPermissionsSchemes == nil { 203 f.CustomPermissionsSchemes = NewBool(*f.FutureFeatures) 204 } 205 206 if f.GuestAccounts == nil { 207 f.GuestAccounts = NewBool(*f.FutureFeatures) 208 } 209 210 if f.GuestAccountsPermissions == nil { 211 f.GuestAccountsPermissions = NewBool(*f.FutureFeatures) 212 } 213 214 if f.CustomTermsOfService == nil { 215 f.CustomTermsOfService = NewBool(*f.FutureFeatures) 216 } 217 218 if f.IDLoadedPushNotifications == nil { 219 f.IDLoadedPushNotifications = NewBool(*f.FutureFeatures) 220 } 221 222 if f.LockTeammateNameDisplay == nil { 223 f.LockTeammateNameDisplay = NewBool(*f.FutureFeatures) 224 } 225 226 if f.EnterprisePlugins == nil { 227 f.EnterprisePlugins = NewBool(*f.FutureFeatures) 228 } 229 230 if f.AdvancedLogging == nil { 231 f.AdvancedLogging = NewBool(*f.FutureFeatures) 232 } 233 234 if f.Cloud == nil { 235 f.Cloud = NewBool(false) 236 } 237 238 if f.SharedChannels == nil { 239 f.SharedChannels = NewBool(*f.FutureFeatures) 240 } 241 242 if f.RemoteClusterService == nil { 243 f.RemoteClusterService = f.SharedChannels 244 } 245 } 246 247 func (l *License) IsExpired() bool { 248 return l.ExpiresAt < GetMillis() 249 } 250 251 func (l *License) IsPastGracePeriod() bool { 252 timeDiff := GetMillis() - l.ExpiresAt 253 return timeDiff > LICENSE_GRACE_PERIOD 254 } 255 256 func (l *License) IsStarted() bool { 257 return l.StartsAt < GetMillis() 258 } 259 260 func (l *License) ToJson() string { 261 b, _ := json.Marshal(l) 262 return string(b) 263 } 264 265 // NewTestLicense returns a license that expires in the future and has the given features. 266 func NewTestLicense(features ...string) *License { 267 ret := &License{ 268 ExpiresAt: GetMillis() + 90*24*60*60*1000, 269 Customer: &Customer{}, 270 Features: &Features{}, 271 } 272 ret.Features.SetDefaults() 273 274 featureMap := map[string]bool{} 275 for _, feature := range features { 276 featureMap[feature] = true 277 } 278 featureJson, _ := json.Marshal(featureMap) 279 json.Unmarshal(featureJson, &ret.Features) 280 281 return ret 282 } 283 284 func LicenseFromJson(data io.Reader) *License { 285 var o *License 286 json.NewDecoder(data).Decode(&o) 287 return o 288 } 289 290 func (lr *LicenseRecord) IsValid() *AppError { 291 if !IsValidId(lr.Id) { 292 return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest) 293 } 294 295 if lr.CreateAt == 0 { 296 return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest) 297 } 298 299 if lr.Bytes == "" || len(lr.Bytes) > 10000 { 300 return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest) 301 } 302 303 return nil 304 } 305 306 func (lr *LicenseRecord) PreSave() { 307 lr.CreateAt = GetMillis() 308 }