license.go 5.18 KB
Newer Older
1
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
2 3 4 5 6 7 8
// See License.txt for license information.

package model

import (
	"encoding/json"
	"io"
9
	"net/http"
10 11
)

12 13 14 15 16
const (
	EXPIRED_LICENSE_ERROR = "api.license.add_license.expired.app_error"
	INVALID_LICENSE_ERROR = "api.license.add_license.invalid.app_error"
)

17 18 19 20 21 22
type LicenseRecord struct {
	Id       string `json:"id"`
	CreateAt int64  `json:"create_at"`
	Bytes    string `json:"-"`
}

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
type License struct {
	Id        string    `json:"id"`
	IssuedAt  int64     `json:"issued_at"`
	StartsAt  int64     `json:"starts_at"`
	ExpiresAt int64     `json:"expires_at"`
	Customer  *Customer `json:"customer"`
	Features  *Features `json:"features"`
}

type Customer struct {
	Id          string `json:"id"`
	Name        string `json:"name"`
	Email       string `json:"email"`
	Company     string `json:"company"`
	PhoneNumber string `json:"phone_number"`
}

type Features struct {
41 42 43 44 45 46 47 48 49 50 51 52 53 54
	Users                     *int  `json:"users"`
	LDAP                      *bool `json:"ldap"`
	MFA                       *bool `json:"mfa"`
	GoogleOAuth               *bool `json:"google_oauth"`
	Office365OAuth            *bool `json:"office365_oauth"`
	Compliance                *bool `json:"compliance"`
	Cluster                   *bool `json:"cluster"`
	Metrics                   *bool `json:"metrics"`
	CustomBrand               *bool `json:"custom_brand"`
	MHPNS                     *bool `json:"mhpns"`
	SAML                      *bool `json:"saml"`
	PasswordRequirements      *bool `json:"password_requirements"`
	Elasticsearch             *bool `json:"elastic_search"`
	Announcement              *bool `json:"announcement"`
55
	ThemeManagement           *bool `json:"theme_management"`
56
	EmailNotificationContents *bool `json:"email_notification_contents"`
57
	DataRetention             *bool `json:"data_retention"`
58

enahum's avatar
enahum committed
59
	// after we enabled more features for webrtc we'll need to control them with this
60
	FutureFeatures *bool `json:"future_features"`
JoramWilander's avatar
JoramWilander committed
61 62
}

63 64
func (f *Features) ToMap() map[string]interface{} {
	return map[string]interface{}{
65 66 67 68 69 70 71 72 73 74 75 76 77
		"ldap":                        *f.LDAP,
		"mfa":                         *f.MFA,
		"google":                      *f.GoogleOAuth,
		"office365":                   *f.Office365OAuth,
		"compliance":                  *f.Compliance,
		"cluster":                     *f.Cluster,
		"metrics":                     *f.Metrics,
		"custom_brand":                *f.CustomBrand,
		"mhpns":                       *f.MHPNS,
		"saml":                        *f.SAML,
		"password":                    *f.PasswordRequirements,
		"elastic_search":              *f.Elasticsearch,
		"email_notification_contents": *f.EmailNotificationContents,
78
		"data_retention":              *f.DataRetention,
79
		"future":                      *f.FutureFeatures,
80 81 82
	}
}

JoramWilander's avatar
JoramWilander committed
83
func (f *Features) SetDefaults() {
84
	if f.FutureFeatures == nil {
85
		f.FutureFeatures = NewBool(true)
86 87
	}

JoramWilander's avatar
JoramWilander committed
88
	if f.Users == nil {
89
		f.Users = NewInt(0)
JoramWilander's avatar
JoramWilander committed
90 91 92
	}

	if f.LDAP == nil {
93
		f.LDAP = NewBool(*f.FutureFeatures)
JoramWilander's avatar
JoramWilander committed
94 95
	}

JoramWilander's avatar
JoramWilander committed
96
	if f.MFA == nil {
97
		f.MFA = NewBool(*f.FutureFeatures)
JoramWilander's avatar
JoramWilander committed
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)
JoramWilander's avatar
JoramWilander committed
106
	}
=Corey Hulen's avatar
=Corey Hulen committed
107 108

	if f.Compliance == nil {
109
		f.Compliance = NewBool(*f.FutureFeatures)
=Corey Hulen's avatar
=Corey Hulen committed
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.CustomBrand == nil {
121
		f.CustomBrand = NewBool(*f.FutureFeatures)
122 123 124
	}

	if f.MHPNS == nil {
125
		f.MHPNS = NewBool(*f.FutureFeatures)
126
	}
enahum's avatar
enahum committed
127 128

	if f.SAML == nil {
129
		f.SAML = NewBool(*f.FutureFeatures)
enahum's avatar
enahum committed
130
	}
131 132

	if f.PasswordRequirements == nil {
133
		f.PasswordRequirements = NewBool(*f.FutureFeatures)
134
	}
135

136
	if f.Elasticsearch == nil {
137
		f.Elasticsearch = NewBool(*f.FutureFeatures)
138
	}
139 140

	if f.Announcement == nil {
141
		f.Announcement = NewBool(true)
142
	}
143

144
	if f.ThemeManagement == nil {
145
		f.ThemeManagement = NewBool(true)
146 147
	}

148
	if f.EmailNotificationContents == nil {
149
		f.EmailNotificationContents = NewBool(*f.FutureFeatures)
150
	}
151 152

	if f.DataRetention == nil {
153
		f.DataRetention = NewBool(*f.FutureFeatures)
154
	}
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
}

func (l *License) IsExpired() bool {
	now := GetMillis()
	if l.ExpiresAt < now {
		return true
	}
	return false
}

func (l *License) IsStarted() bool {
	now := GetMillis()
	if l.StartsAt < now {
		return true
	}
	return false
}

func (l *License) ToJson() string {
	b, err := json.Marshal(l)
	if err != nil {
		return ""
	} else {
		return string(b)
	}
}

func LicenseFromJson(data io.Reader) *License {
	decoder := json.NewDecoder(data)
	var o License
	err := decoder.Decode(&o)
	if err == nil {
		return &o
	} else {
		return nil
	}
}
192 193 194

func (lr *LicenseRecord) IsValid() *AppError {
	if len(lr.Id) != 26 {
195
		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.id.app_error", nil, "", http.StatusBadRequest)
196 197 198
	}

	if lr.CreateAt == 0 {
199
		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
200 201 202
	}

	if len(lr.Bytes) == 0 || len(lr.Bytes) > 10000 {
203
		return NewAppError("LicenseRecord.IsValid", "model.license_record.is_valid.create_at.app_error", nil, "", http.StatusBadRequest)
204 205 206 207 208 209 210 211
	}

	return nil
}

func (lr *LicenseRecord) PreSave() {
	lr.CreateAt = GetMillis()
}