1
0
Fork 0

Plugin user.getAuthUser is now async

So we can load the full user
This commit is contained in:
Chocobozzz 2021-05-05 15:26:28 +02:00
parent 7a22a0a56a
commit b31d72625d
No known key found for this signature in database
GPG key ID: 583A612D890159BE
6 changed files with 19 additions and 7 deletions

View file

@ -17,6 +17,7 @@ import { VideoBlacklistCreate } from '@shared/models'
import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist'
import { getServerConfig } from '../config'
import { blacklistVideo, unblacklistVideo } from '../video-blacklist'
import { UserModel } from '@server/models/account/user'
function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers {
const logger = buildPluginLogger(npmName)
@ -163,6 +164,11 @@ function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) {
function buildUserHelpers () {
return {
getAuthUser: (res: express.Response) => res.locals.oauth?.token?.User
getAuthUser: (res: express.Response) => {
const user = res.locals.oauth?.token?.User
if (!user) return undefined
return UserModel.loadByIdFull(user.id)
}
}
}

View file

@ -565,6 +565,10 @@ export class UserModel extends Model {
return UserModel.unscoped().findByPk(id)
}
static loadByIdFull (id: number): Promise<MUserDefault> {
return UserModel.findByPk(id)
}
static loadByIdWithChannels (id: number, withStats = false): Promise<MUserDefault> {
const scopes = [
ScopeNames.WITH_VIDEOCHANNELS

View file

@ -88,8 +88,8 @@ async function register ({
return res.json({ routerRoute })
})
router.get('/user', (req, res) => {
const user = peertubeHelpers.user.getAuthUser(res)
router.get('/user', async (req, res) => {
const user = await peertubeHelpers.user.getAuthUser(res)
if (!user) return res.sendStatus(404)
const isAdmin = user.role === 0
@ -98,6 +98,7 @@ async function register ({
return res.json({
username: user.username,
displayName: user.Account.name,
isAdmin,
isModerator,
isUser

View file

@ -133,6 +133,7 @@ describe('Test plugin helpers', function () {
})
expect(res.body.username).to.equal('root')
expect(res.body.displayName).to.equal('root')
expect(res.body.isAdmin).to.be.true
expect(res.body.isModerator).to.be.false
expect(res.body.isUser).to.be.false

View file

@ -70,13 +70,13 @@ export type PeerTubeHelpers = {
user: {
// PeerTube >= 3.2
getAuthUser: (response: Response) => {
getAuthUser: (response: Response) => Promise<{
id?: string
username: string
email: string
blocked: boolean
role: UserRole
} | undefined
} | undefined>
}
}

View file

@ -261,8 +261,8 @@ function register ({
router.get('/ping', (req, res) => res.json({ message: 'pong' }))
// Users are automatically authenticated
router.get('/auth', (res, res) => {
const user = peertubeHelpers.user.getAuthUser(res)
router.get('/auth', async (res, res) => {
const user = await peertubeHelpers.user.getAuthUser(res)
const isAdmin = user.role === 0
const isModerator = user.role === 1