diff --git a/channel/email.go b/channel/email.go index 13f2909..63729fb 100644 --- a/channel/email.go +++ b/channel/email.go @@ -7,6 +7,12 @@ import ( ) func SendEmailMessage(message *model.Message, user *model.User) error { + if message.To != "" { + if user.SendEmailToOthers != common.SendEmailToOthersAllowed && user.Role < common.RoleAdminUser { + return errors.New("没有权限发送邮件给其他人,请联系管理员为你添加该权限") + } + user.Email = message.To + } if user.Email == "" { return errors.New("未配置邮箱地址") } @@ -21,6 +27,5 @@ func SendEmailMessage(message *model.Message, user *model.User) error { common.SysLog(err.Error()) } } - // TODO: support message.To return common.SendEmail(subject, user.Email, message.HTMLContent) } diff --git a/common/constants.go b/common/constants.go index 54dcf77..0d683a3 100644 --- a/common/constants.go +++ b/common/constants.go @@ -87,3 +87,8 @@ const ( UserStatusEnabled = 1 // don't use 0, 0 is the default value! UserStatusDisabled = 2 // also don't use 0 ) + +const ( + SendEmailToOthersAllowed = 1 + SendEmailToOthersDisallowed = 2 +) diff --git a/controller/user.go b/controller/user.go index fb00c7c..1e06955 100644 --- a/controller/user.go +++ b/controller/user.go @@ -631,6 +631,10 @@ func ManageUser(c *gin.Context) { user.Role = common.RoleAdminUser case "demote": user.Role = common.RoleCommonUser + case "allow_send_email_to_others": + user.SendEmailToOthers = common.SendEmailToOthersAllowed + case "disallow_send_email_to_others": + user.SendEmailToOthers = common.SendEmailToOthersDisallowed } if err := user.Update(false); err != nil { @@ -641,8 +645,9 @@ func ManageUser(c *gin.Context) { return } clearUser := model.User{ - Role: user.Role, - Status: user.Status, + Role: user.Role, + Status: user.Status, + SendEmailToOthers: user.SendEmailToOthers, } c.JSON(http.StatusOK, gin.H{ "success": true, diff --git a/model/user.go b/model/user.go index f02dc25..a6062b5 100644 --- a/model/user.go +++ b/model/user.go @@ -42,6 +42,7 @@ type User struct { TelegramBotToken string `json:"telegram_bot_token"` TelegramChatId string `json:"telegram_chat_id"` DiscordWebhookURL string `json:"discord_webhook_url"` + SendEmailToOthers int `json:"send_email_to_others" gorm:"type:int;default:0"` } func GetMaxUserId() int { @@ -51,7 +52,7 @@ func GetMaxUserId() int { } func GetAllUsers(startIdx int, num int) (users []*User, err error) { - err = DB.Order("id desc").Limit(num).Offset(startIdx).Select([]string{"id", "username", "display_name", "role", "status", "email"}).Find(&users).Error + err = DB.Order("id desc").Limit(num).Offset(startIdx).Select([]string{"id", "username", "display_name", "role", "status", "email", "send_email_to_others"}).Find(&users).Error return users, err } diff --git a/web/src/components/UsersTable.js b/web/src/components/UsersTable.js index ac6891a..1398fb9 100644 --- a/web/src/components/UsersTable.js +++ b/web/src/components/UsersTable.js @@ -1,5 +1,12 @@ import React, { useEffect, useState } from 'react'; -import { Button, Form, Label, Pagination, Table } from 'semantic-ui-react'; +import { + Button, + Dropdown, + Form, + Label, + Pagination, + Table, +} from 'semantic-ui-react'; import { Link } from 'react-router-dom'; import { API, showError, showSuccess } from '../helpers'; @@ -77,6 +84,7 @@ const UsersTable = () => { } else { newUsers[realIdx].status = user.status; newUsers[realIdx].role = user.role; + newUsers[realIdx].send_email_to_others = user.send_email_to_others; } setUsers(newUsers); } else { @@ -265,6 +273,30 @@ const UsersTable = () => { > 编辑 + + + { + manageUser( + user.username, + user.send_email_to_others === 1 + ? 'disallow_send_email_to_others' + : 'allow_send_email_to_others', + idx + ); + }} + > + {user.send_email_to_others === 1 + ? '撤回发送任意邮件的权限' + : '授予发送任意邮件的权限'} + + +