feat: support admin api bind address to user (#630)

This commit is contained in:
Dream Hunter
2025-04-12 19:49:59 +08:00
committed by GitHub
parent 47e2cb56b4
commit 0894ac0dc9
4 changed files with 22 additions and 16 deletions

View File

@@ -5,6 +5,7 @@ import { getJsonSetting, saveSetting, checkUserPassword, getDomains, getUserRole
import { UserSettings, GeoData, UserInfo } from "../models";
import { handleListQuery } from '../common'
import { HonoCustomType } from '../types';
import UserBindAddressModule from '../user_api/bind_address';
export default {
getSetting: async (c: Context<HonoCustomType>) => {
@@ -144,22 +145,12 @@ export default {
}
return c.json({ success: true })
},
bindAddress: async (c: Context<HonoCustomType>) => {
const { user_id, address_id } = await c.req.json();
return await UserBindAddressModule.bindByID(c, user_id, address_id);
},
getBindedAddresses: async (c: Context<HonoCustomType>) => {
const { user_id } = c.req.param();
if (!user_id) return c.text("Invalid user_id", 400);
// select binded address
const { results } = await c.env.DB.prepare(
`SELECT a.*,`
+ ` (SELECT COUNT(*) FROM raw_mails WHERE address = a.name) AS mail_count,`
+ ` (SELECT COUNT(*) FROM sendbox WHERE address = a.name) AS send_count`
+ ` FROM address a `
+ ` JOIN users_address ua `
+ ` ON ua.address_id = a.id `
+ ` WHERE ua.user_id = ?`
+ ` ORDER BY a.id DESC`
).bind(user_id).all();
return c.json({
results: results,
})
return await UserBindAddressModule.getBindedAddressesById(c, user_id);
},
}

View File

@@ -328,6 +328,7 @@ api.post('/admin/users/:user_id/reset_password', admin_user_api.resetPassword)
api.get('/admin/user_roles', async (c) => c.json(getUserRoles(c)))
api.post('/admin/user_roles', admin_user_api.updateUserRoles)
api.get('/admin/users/bind_address/:user_id', admin_user_api.getBindedAddresses)
api.post('/admin/users/bind_address', admin_user_api.bindAddress)
// user oauth2 settings
api.get('/admin/user_oauth2_settings', oauth2_settings.getUserOauth2Settings)

View File

@@ -7,10 +7,16 @@ import { getJsonSetting } from "../utils"
import { CONSTANTS } from "../constants";
import { unbindTelegramByAddress } from '../telegram_api/common';
export default {
const UserBindAddressModule = {
bind: async (c: Context<HonoCustomType>) => {
const { user_id } = c.get("userPayload");
const { address_id } = c.get("jwtPayload");
return await UserBindAddressModule.bindByID(c, user_id, address_id)
},
bindByID: async (
c: Context<HonoCustomType>,
user_id: number | string, address_id: number | string
) => {
if (!address_id || !user_id) {
return c.text("No address or user token", 400)
}
@@ -96,6 +102,11 @@ export default {
},
getBindedAddresses: async (c: Context<HonoCustomType>) => {
const { user_id } = c.get("userPayload");
return await UserBindAddressModule.getBindedAddressesById(c, user_id);
},
getBindedAddressesById: async (
c: Context<HonoCustomType>, user_id: number | string
) => {
if (!user_id) {
return c.text("No user token", 400)
}
@@ -229,3 +240,5 @@ export default {
return c.json({ success: true })
}
}
export default UserBindAddressModule;