package handler
import (
apimodel "github.com/goodrain/rainbond/api/model"
"github.com/goodrain/rainbond/db"
dbmodel "github.com/goodrain/rainbond/db/model"
"github.com/jinzhu/gorm"
"github.com/pquerna/ffjson/ffjson"
"github.com/sirupsen/logrus"
)
type LicenseAction struct{}
func (l *LicenseAction) PackLicense(encrypted string) ([]byte, error) {
return decrypt(key, encrypted)
}
func (l *LicenseAction) StoreLicense(license, token string) error {
ls := &dbmodel.LicenseInfo{
Token: token,
License: license,
}
if err := db.GetManager().LicenseDao().AddModel(ls); err != nil {
return err
}
return nil
}
type LicensesInfos struct {
Infos map[string]*apimodel.LicenseInfo
}
func (l *LicensesInfos) ShowInfos() (map[string]*apimodel.LicenseInfo, error) {
return l.Infos, nil
}
func ListLicense() (map[string]*apimodel.LicenseInfo, error) {
licenses, err := db.GetManager().LicenseDao().ListLicenses()
if err != nil {
if err == gorm.ErrRecordNotFound {
return nil, err
}
}
LMlicense := make(map[string]*apimodel.LicenseInfo)
for _, license := range licenses {
mLicense := &apimodel.LicenseInfo{}
lc, err := GetLicenseHandler().PackLicense(license.License)
if err != nil {
logrus.Errorf("init license error.")
continue
}
if err := ffjson.Unmarshal(lc, mLicense); err != nil {
logrus.Errorf("unmashal license error, %v", err)
continue
}
LMlicense[license.Token] = mLicense
}
return LMlicense, nil
}