github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/app/license.go (about)

     1  // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package app
     5  
     6  import (
     7  	"net/http"
     8  	"strings"
     9  
    10  	l4g "github.com/alecthomas/log4go"
    11  	"github.com/mattermost/mattermost-server/model"
    12  	"github.com/mattermost/mattermost-server/utils"
    13  )
    14  
    15  func (a *App) LoadLicense() {
    16  	utils.RemoveLicense()
    17  
    18  	licenseId := ""
    19  	if result := <-a.Srv.Store.System().Get(); result.Err == nil {
    20  		props := result.Data.(model.StringMap)
    21  		licenseId = props[model.SYSTEM_ACTIVE_LICENSE_ID]
    22  	}
    23  
    24  	if len(licenseId) != 26 {
    25  		// Lets attempt to load the file from disk since it was missing from the DB
    26  		license, licenseBytes := utils.GetAndValidateLicenseFileFromDisk(*a.Config().ServiceSettings.LicenseFileLocation)
    27  
    28  		if license != nil {
    29  			if _, err := a.SaveLicense(licenseBytes); err != nil {
    30  				l4g.Info("Failed to save license key loaded from disk err=%v", err.Error())
    31  			} else {
    32  				licenseId = license.Id
    33  			}
    34  		}
    35  	}
    36  
    37  	if result := <-a.Srv.Store.License().Get(licenseId); result.Err == nil {
    38  		record := result.Data.(*model.LicenseRecord)
    39  		utils.LoadLicense([]byte(record.Bytes))
    40  		l4g.Info("License key valid unlocking enterprise features.")
    41  	} else {
    42  		l4g.Info(utils.T("mattermost.load_license.find.warn"))
    43  	}
    44  }
    45  
    46  func (a *App) SaveLicense(licenseBytes []byte) (*model.License, *model.AppError) {
    47  	var license *model.License
    48  
    49  	if success, licenseStr := utils.ValidateLicense(licenseBytes); success {
    50  		license = model.LicenseFromJson(strings.NewReader(licenseStr))
    51  
    52  		if result := <-a.Srv.Store.User().AnalyticsUniqueUserCount(""); result.Err != nil {
    53  			return nil, model.NewAppError("addLicense", "api.license.add_license.invalid_count.app_error", nil, result.Err.Error(), http.StatusBadRequest)
    54  		} else {
    55  			uniqueUserCount := result.Data.(int64)
    56  
    57  			if uniqueUserCount > int64(*license.Features.Users) {
    58  				return nil, model.NewAppError("addLicense", "api.license.add_license.unique_users.app_error", map[string]interface{}{"Users": *license.Features.Users, "Count": uniqueUserCount}, "", http.StatusBadRequest)
    59  			}
    60  		}
    61  
    62  		if ok := utils.SetLicense(license); !ok {
    63  			return nil, model.NewAppError("addLicense", model.EXPIRED_LICENSE_ERROR, nil, "", http.StatusBadRequest)
    64  		}
    65  
    66  		record := &model.LicenseRecord{}
    67  		record.Id = license.Id
    68  		record.Bytes = string(licenseBytes)
    69  		rchan := a.Srv.Store.License().Save(record)
    70  
    71  		if result := <-rchan; result.Err != nil {
    72  			a.RemoveLicense()
    73  			return nil, model.NewAppError("addLicense", "api.license.add_license.save.app_error", nil, "err="+result.Err.Error(), http.StatusInternalServerError)
    74  		}
    75  
    76  		sysVar := &model.System{}
    77  		sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
    78  		sysVar.Value = license.Id
    79  		schan := a.Srv.Store.System().SaveOrUpdate(sysVar)
    80  
    81  		if result := <-schan; result.Err != nil {
    82  			a.RemoveLicense()
    83  			return nil, model.NewAppError("addLicense", "api.license.add_license.save_active.app_error", nil, "", http.StatusInternalServerError)
    84  		}
    85  	} else {
    86  		return nil, model.NewAppError("addLicense", model.INVALID_LICENSE_ERROR, nil, "", http.StatusBadRequest)
    87  	}
    88  
    89  	a.ReloadConfig()
    90  	a.InvalidateAllCaches()
    91  
    92  	// start job server if necessary - this handles the edge case where a license file is uploaded, but the job server
    93  	// doesn't start until the server is restarted, which prevents the 'run job now' buttons in system console from
    94  	// functioning as expected
    95  	if *a.Config().JobSettings.RunJobs {
    96  		a.Jobs.StartWorkers()
    97  	}
    98  	if *a.Config().JobSettings.RunScheduler {
    99  		a.Jobs.StartSchedulers()
   100  	}
   101  
   102  	return license, nil
   103  }
   104  
   105  func (a *App) RemoveLicense() *model.AppError {
   106  	utils.RemoveLicense()
   107  
   108  	sysVar := &model.System{}
   109  	sysVar.Name = model.SYSTEM_ACTIVE_LICENSE_ID
   110  	sysVar.Value = ""
   111  
   112  	if result := <-a.Srv.Store.System().SaveOrUpdate(sysVar); result.Err != nil {
   113  		utils.RemoveLicense()
   114  		return result.Err
   115  	}
   116  
   117  	a.ReloadConfig()
   118  
   119  	a.InvalidateAllCaches()
   120  
   121  	return nil
   122  }