diff options
author | Chocobozzz <me@florianbigard.com> | 2021-05-05 15:26:28 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-05-05 15:26:28 +0200 |
commit | b31d72625dd32143a45277528b90bb67a881f249 (patch) | |
tree | 7721accc994121de668933672a3d51b892e3cb77 | |
parent | 7a22a0a56aa75fbb1ba986a5d2c606e1343f30c2 (diff) | |
download | PeerTube-b31d72625dd32143a45277528b90bb67a881f249.tar.gz PeerTube-b31d72625dd32143a45277528b90bb67a881f249.tar.zst PeerTube-b31d72625dd32143a45277528b90bb67a881f249.zip |
Plugin user.getAuthUser is now async
So we can load the full user
-rw-r--r-- | server/lib/plugins/plugin-helpers-builder.ts | 8 | ||||
-rw-r--r-- | server/models/account/user.ts | 4 | ||||
-rw-r--r-- | server/tests/fixtures/peertube-plugin-test-four/main.js | 5 | ||||
-rw-r--r-- | server/tests/plugins/plugin-helpers.ts | 1 | ||||
-rw-r--r-- | server/types/plugins/register-server-option.model.ts | 4 | ||||
-rw-r--r-- | support/doc/plugins/guide.md | 4 |
6 files changed, 19 insertions, 7 deletions
diff --git a/server/lib/plugins/plugin-helpers-builder.ts b/server/lib/plugins/plugin-helpers-builder.ts index d57c69ef0..f1bc24d8b 100644 --- a/server/lib/plugins/plugin-helpers-builder.ts +++ b/server/lib/plugins/plugin-helpers-builder.ts | |||
@@ -17,6 +17,7 @@ import { VideoBlacklistCreate } from '@shared/models' | |||
17 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' | 17 | import { addAccountInBlocklist, addServerInBlocklist, removeAccountFromBlocklist, removeServerFromBlocklist } from '../blocklist' |
18 | import { getServerConfig } from '../config' | 18 | import { getServerConfig } from '../config' |
19 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' | 19 | import { blacklistVideo, unblacklistVideo } from '../video-blacklist' |
20 | import { UserModel } from '@server/models/account/user' | ||
20 | 21 | ||
21 | function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { | 22 | function buildPluginHelpers (pluginModel: MPlugin, npmName: string): PeerTubeHelpers { |
22 | const logger = buildPluginLogger(npmName) | 23 | const logger = buildPluginLogger(npmName) |
@@ -163,6 +164,11 @@ function buildPluginRelatedHelpers (plugin: MPlugin, npmName: string) { | |||
163 | 164 | ||
164 | function buildUserHelpers () { | 165 | function buildUserHelpers () { |
165 | return { | 166 | return { |
166 | getAuthUser: (res: express.Response) => res.locals.oauth?.token?.User | 167 | getAuthUser: (res: express.Response) => { |
168 | const user = res.locals.oauth?.token?.User | ||
169 | if (!user) return undefined | ||
170 | |||
171 | return UserModel.loadByIdFull(user.id) | ||
172 | } | ||
167 | } | 173 | } |
168 | } | 174 | } |
diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 00c6d73aa..513455773 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts | |||
@@ -565,6 +565,10 @@ export class UserModel extends Model { | |||
565 | return UserModel.unscoped().findByPk(id) | 565 | return UserModel.unscoped().findByPk(id) |
566 | } | 566 | } |
567 | 567 | ||
568 | static loadByIdFull (id: number): Promise<MUserDefault> { | ||
569 | return UserModel.findByPk(id) | ||
570 | } | ||
571 | |||
568 | static loadByIdWithChannels (id: number, withStats = false): Promise<MUserDefault> { | 572 | static loadByIdWithChannels (id: number, withStats = false): Promise<MUserDefault> { |
569 | const scopes = [ | 573 | const scopes = [ |
570 | ScopeNames.WITH_VIDEOCHANNELS | 574 | ScopeNames.WITH_VIDEOCHANNELS |
diff --git a/server/tests/fixtures/peertube-plugin-test-four/main.js b/server/tests/fixtures/peertube-plugin-test-four/main.js index 6ed0c20d2..b9b207b81 100644 --- a/server/tests/fixtures/peertube-plugin-test-four/main.js +++ b/server/tests/fixtures/peertube-plugin-test-four/main.js | |||
@@ -88,8 +88,8 @@ async function register ({ | |||
88 | return res.json({ routerRoute }) | 88 | return res.json({ routerRoute }) |
89 | }) | 89 | }) |
90 | 90 | ||
91 | router.get('/user', (req, res) => { | 91 | router.get('/user', async (req, res) => { |
92 | const user = peertubeHelpers.user.getAuthUser(res) | 92 | const user = await peertubeHelpers.user.getAuthUser(res) |
93 | if (!user) return res.sendStatus(404) | 93 | if (!user) return res.sendStatus(404) |
94 | 94 | ||
95 | const isAdmin = user.role === 0 | 95 | const isAdmin = user.role === 0 |
@@ -98,6 +98,7 @@ async function register ({ | |||
98 | 98 | ||
99 | return res.json({ | 99 | return res.json({ |
100 | username: user.username, | 100 | username: user.username, |
101 | displayName: user.Account.name, | ||
101 | isAdmin, | 102 | isAdmin, |
102 | isModerator, | 103 | isModerator, |
103 | isUser | 104 | isUser |
diff --git a/server/tests/plugins/plugin-helpers.ts b/server/tests/plugins/plugin-helpers.ts index 20020ec41..f72de8229 100644 --- a/server/tests/plugins/plugin-helpers.ts +++ b/server/tests/plugins/plugin-helpers.ts | |||
@@ -133,6 +133,7 @@ describe('Test plugin helpers', function () { | |||
133 | }) | 133 | }) |
134 | 134 | ||
135 | expect(res.body.username).to.equal('root') | 135 | expect(res.body.username).to.equal('root') |
136 | expect(res.body.displayName).to.equal('root') | ||
136 | expect(res.body.isAdmin).to.be.true | 137 | expect(res.body.isAdmin).to.be.true |
137 | expect(res.body.isModerator).to.be.false | 138 | expect(res.body.isModerator).to.be.false |
138 | expect(res.body.isUser).to.be.false | 139 | expect(res.body.isUser).to.be.false |
diff --git a/server/types/plugins/register-server-option.model.ts b/server/types/plugins/register-server-option.model.ts index 4af476ed2..2432b7ac4 100644 --- a/server/types/plugins/register-server-option.model.ts +++ b/server/types/plugins/register-server-option.model.ts | |||
@@ -70,13 +70,13 @@ export type PeerTubeHelpers = { | |||
70 | 70 | ||
71 | user: { | 71 | user: { |
72 | // PeerTube >= 3.2 | 72 | // PeerTube >= 3.2 |
73 | getAuthUser: (response: Response) => { | 73 | getAuthUser: (response: Response) => Promise<{ |
74 | id?: string | 74 | id?: string |
75 | username: string | 75 | username: string |
76 | email: string | 76 | email: string |
77 | blocked: boolean | 77 | blocked: boolean |
78 | role: UserRole | 78 | role: UserRole |
79 | } | undefined | 79 | } | undefined> |
80 | } | 80 | } |
81 | } | 81 | } |
82 | 82 | ||
diff --git a/support/doc/plugins/guide.md b/support/doc/plugins/guide.md index 53d53c26d..5b7d1cb31 100644 --- a/support/doc/plugins/guide.md +++ b/support/doc/plugins/guide.md | |||
@@ -261,8 +261,8 @@ function register ({ | |||
261 | router.get('/ping', (req, res) => res.json({ message: 'pong' })) | 261 | router.get('/ping', (req, res) => res.json({ message: 'pong' })) |
262 | 262 | ||
263 | // Users are automatically authenticated | 263 | // Users are automatically authenticated |
264 | router.get('/auth', (res, res) => { | 264 | router.get('/auth', async (res, res) => { |
265 | const user = peertubeHelpers.user.getAuthUser(res) | 265 | const user = await peertubeHelpers.user.getAuthUser(res) |
266 | 266 | ||
267 | const isAdmin = user.role === 0 | 267 | const isAdmin = user.role === 0 |
268 | const isModerator = user.role === 1 | 268 | const isModerator = user.role === 1 |