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