Prechádzať zdrojové kódy

feat: support send Email to others (close #44)

JustSong 2 rokov pred
rodič
commit
c16530cf6e

+ 6 - 1
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)
 }

+ 5 - 0
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
+)

+ 7 - 2
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,

+ 2 - 1
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
 }
 

+ 33 - 1
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 = () => {
                       >
                         编辑
                       </Button>
+                      <Dropdown
+                        size={'small'}
+                        text='更多'
+                        button
+                        className={'small'}
+                      >
+                        <Dropdown.Menu>
+                          <Dropdown.Item
+                            onClick={() => {
+                              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
+                              ? '撤回发送任意邮件的权限'
+                              : '授予发送任意邮件的权限'}
+                          </Dropdown.Item>
+                        </Dropdown.Menu>
+                      </Dropdown>
                     </div>
                   </Table.Cell>
                 </Table.Row>