增加邮件服务功能
This commit is contained in:
@ -1,12 +1,19 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"github.com/jordan-wright/email"
|
||||
"github.com/sirupsen/logrus"
|
||||
"net/smtp"
|
||||
"next-terminal/pkg/global"
|
||||
"next-terminal/pkg/guacd"
|
||||
)
|
||||
|
||||
const (
|
||||
SshMode = "ssh-mode"
|
||||
SshMode = "ssh-mode"
|
||||
MailHost = "mail-host"
|
||||
MailPort = "mail-port"
|
||||
MailUsername = "mail-username"
|
||||
MailPassword = "mail-password"
|
||||
)
|
||||
|
||||
type Property struct {
|
||||
@ -64,3 +71,26 @@ func GetRecordingPath() (string, error) {
|
||||
}
|
||||
return property.Value, nil
|
||||
}
|
||||
|
||||
func SendMail(to, subject, text string) {
|
||||
propertiesMap := FindAllPropertiesMap()
|
||||
host := propertiesMap[MailHost]
|
||||
port := propertiesMap[MailPort]
|
||||
username := propertiesMap[MailUsername]
|
||||
password := propertiesMap[MailPassword]
|
||||
|
||||
if host == "" || port == "" || username == "" || password == "" {
|
||||
logrus.Debugf("邮箱信息不完整,跳过发送邮件。")
|
||||
return
|
||||
}
|
||||
|
||||
e := email.NewEmail()
|
||||
e.From = "Next Terminal <" + username + ">"
|
||||
e.To = []string{to}
|
||||
e.Subject = subject
|
||||
e.Text = []byte(text)
|
||||
err := e.Send(host+":"+port, smtp.PlainAuth("", username, password, host))
|
||||
if err != nil {
|
||||
logrus.Errorf("邮件发送失败: %v", err.Error())
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,8 @@ type UserVo struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Nickname string `json:"nickname"`
|
||||
TOTPSecret string `json:"totpSecret"`
|
||||
Mail string `json:"mail"`
|
||||
Online bool `json:"online"`
|
||||
Enabled bool `json:"enabled"`
|
||||
Created utils.JsonTime `json:"created"`
|
||||
@ -50,8 +52,8 @@ func FindAllUser() (o []User) {
|
||||
return
|
||||
}
|
||||
|
||||
func FindPageUser(pageIndex, pageSize int, username, nickname, order, field string) (o []UserVo, total int64, err error) {
|
||||
db := global.DB.Table("users").Select("users.id,users.username,users.nickname,users.online,users.enabled,users.created,users.type, count(resource_sharers.user_id) as sharer_asset_count").Joins("left join resource_sharers on users.id = resource_sharers.user_id and resource_sharers.resource_type = 'asset'").Group("users.id")
|
||||
func FindPageUser(pageIndex, pageSize int, username, nickname, mail, order, field string) (o []UserVo, total int64, err error) {
|
||||
db := global.DB.Table("users").Select("users.id,users.username,users.nickname,users.mail,users.online,users.enabled,users.created,users.type, count(resource_sharers.user_id) as sharer_asset_count, users.totp_secret").Joins("left join resource_sharers on users.id = resource_sharers.user_id and resource_sharers.resource_type = 'asset'").Group("users.id")
|
||||
dbCounter := global.DB.Table("users")
|
||||
if len(username) > 0 {
|
||||
db = db.Where("users.username like ?", "%"+username+"%")
|
||||
@ -63,6 +65,11 @@ func FindPageUser(pageIndex, pageSize int, username, nickname, order, field stri
|
||||
dbCounter = dbCounter.Where("nickname like ?", "%"+nickname+"%")
|
||||
}
|
||||
|
||||
if len(mail) > 0 {
|
||||
db = db.Where("users.mail like ?", "%"+mail+"%")
|
||||
dbCounter = dbCounter.Where("mail like ?", "%"+mail+"%")
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
@ -86,6 +93,14 @@ func FindPageUser(pageIndex, pageSize int, username, nickname, order, field stri
|
||||
if o == nil {
|
||||
o = make([]UserVo, 0)
|
||||
}
|
||||
|
||||
for i := 0; i < len(o); i++ {
|
||||
if o[i].TOTPSecret == "" || o[i].TOTPSecret == "-" {
|
||||
o[i].TOTPSecret = "0"
|
||||
} else {
|
||||
o[i].TOTPSecret = "1"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user