diff options
author | Chocobozzz <me@florianbigard.com> | 2022-03-04 10:57:36 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2022-03-04 10:57:36 +0100 |
commit | d3d3deaa7aae49d6d949aebcba015af75d292ccc (patch) | |
tree | 42e487810f5b7362c28f744371d160a4effc7566 /server/lib | |
parent | 90370f7cc85ee4b45eb0d1bafed2d9d68610df9f (diff) | |
download | PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.gz PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.tar.zst PeerTube-d3d3deaa7aae49d6d949aebcba015af75d292ccc.zip |
Refactor user build and express file middlewares
Diffstat (limited to 'server/lib')
-rw-r--r-- | server/lib/auth/oauth-model.ts | 20 | ||||
-rw-r--r-- | server/lib/user.ts | 57 |
2 files changed, 60 insertions, 17 deletions
diff --git a/server/lib/auth/oauth-model.ts b/server/lib/auth/oauth-model.ts index 5d68f44e9..910fdeec1 100644 --- a/server/lib/auth/oauth-model.ts +++ b/server/lib/auth/oauth-model.ts | |||
@@ -5,14 +5,14 @@ import { ActorModel } from '@server/models/actor/actor' | |||
5 | import { MOAuthClient } from '@server/types/models' | 5 | import { MOAuthClient } from '@server/types/models' |
6 | import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' | 6 | import { MOAuthTokenUser } from '@server/types/models/oauth/oauth-token' |
7 | import { MUser } from '@server/types/models/user/user' | 7 | import { MUser } from '@server/types/models/user/user' |
8 | import { UserAdminFlag } from '@shared/models/users/user-flag.model' | 8 | import { pick } from '@shared/core-utils' |
9 | import { UserRole } from '@shared/models/users/user-role' | 9 | import { UserRole } from '@shared/models/users/user-role' |
10 | import { logger } from '../../helpers/logger' | 10 | import { logger } from '../../helpers/logger' |
11 | import { CONFIG } from '../../initializers/config' | 11 | import { CONFIG } from '../../initializers/config' |
12 | import { OAuthClientModel } from '../../models/oauth/oauth-client' | 12 | import { OAuthClientModel } from '../../models/oauth/oauth-client' |
13 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' | 13 | import { OAuthTokenModel } from '../../models/oauth/oauth-token' |
14 | import { UserModel } from '../../models/user/user' | 14 | import { UserModel } from '../../models/user/user' |
15 | import { createUserAccountAndChannelAndPlaylist } from '../user' | 15 | import { buildUser, createUserAccountAndChannelAndPlaylist } from '../user' |
16 | import { TokensCache } from './tokens-cache' | 16 | import { TokensCache } from './tokens-cache' |
17 | 17 | ||
18 | type TokenInfo = { | 18 | type TokenInfo = { |
@@ -229,19 +229,13 @@ async function createUserFromExternal (pluginAuth: string, options: { | |||
229 | const actor = await ActorModel.loadLocalByName(options.username) | 229 | const actor = await ActorModel.loadLocalByName(options.username) |
230 | if (actor) return null | 230 | if (actor) return null |
231 | 231 | ||
232 | const userToCreate = new UserModel({ | 232 | const userToCreate = buildUser({ |
233 | username: options.username, | 233 | ...pick(options, [ 'username', 'email', 'role' ]), |
234 | |||
235 | emailVerified: null, | ||
234 | password: null, | 236 | password: null, |
235 | email: options.email, | ||
236 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
237 | p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, | ||
238 | autoPlayVideo: true, | ||
239 | role: options.role, | ||
240 | videoQuota: CONFIG.USER.VIDEO_QUOTA, | ||
241 | videoQuotaDaily: CONFIG.USER.VIDEO_QUOTA_DAILY, | ||
242 | adminFlags: UserAdminFlag.NONE, | ||
243 | pluginAuth | 237 | pluginAuth |
244 | }) as MUser | 238 | }) |
245 | 239 | ||
246 | const { user } = await createUserAccountAndChannelAndPlaylist({ | 240 | const { user } = await createUserAccountAndChannelAndPlaylist({ |
247 | userToCreate, | 241 | userToCreate, |
diff --git a/server/lib/user.ts b/server/lib/user.ts index 3f7499296..ea755f4be 100644 --- a/server/lib/user.ts +++ b/server/lib/user.ts | |||
@@ -1,9 +1,11 @@ | |||
1 | import { Transaction } from 'sequelize/types' | 1 | import { Transaction } from 'sequelize/types' |
2 | import { logger } from '@server/helpers/logger' | ||
3 | import { CONFIG } from '@server/initializers/config' | ||
2 | import { UserModel } from '@server/models/user/user' | 4 | import { UserModel } from '@server/models/user/user' |
3 | import { MActorDefault } from '@server/types/models/actor' | 5 | import { MActorDefault } from '@server/types/models/actor' |
4 | import { buildUUID } from '@shared/extra-utils' | 6 | import { buildUUID } from '@shared/extra-utils' |
5 | import { ActivityPubActorType } from '../../shared/models/activitypub' | 7 | import { ActivityPubActorType } from '../../shared/models/activitypub' |
6 | import { UserNotificationSetting, UserNotificationSettingValue } from '../../shared/models/users' | 8 | import { UserAdminFlag, UserNotificationSetting, UserNotificationSettingValue, UserRole } from '../../shared/models/users' |
7 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' | 9 | import { SERVER_ACTOR_NAME, WEBSERVER } from '../initializers/constants' |
8 | import { sequelizeTypescript } from '../initializers/database' | 10 | import { sequelizeTypescript } from '../initializers/database' |
9 | import { AccountModel } from '../models/account/account' | 11 | import { AccountModel } from '../models/account/account' |
@@ -19,10 +21,56 @@ import { buildActorInstance } from './local-actor' | |||
19 | import { Redis } from './redis' | 21 | import { Redis } from './redis' |
20 | import { createLocalVideoChannel } from './video-channel' | 22 | import { createLocalVideoChannel } from './video-channel' |
21 | import { createWatchLaterPlaylist } from './video-playlist' | 23 | import { createWatchLaterPlaylist } from './video-playlist' |
22 | import { logger } from '@server/helpers/logger' | ||
23 | 24 | ||
24 | type ChannelNames = { name: string, displayName: string } | 25 | type ChannelNames = { name: string, displayName: string } |
25 | 26 | ||
27 | function buildUser (options: { | ||
28 | username: string | ||
29 | password: string | ||
30 | email: string | ||
31 | |||
32 | role?: UserRole // Default to UserRole.User | ||
33 | adminFlags?: UserAdminFlag // Default to UserAdminFlag.NONE | ||
34 | |||
35 | emailVerified: boolean | null | ||
36 | |||
37 | videoQuota?: number // Default to CONFIG.USER.VIDEO_QUOTA | ||
38 | videoQuotaDaily?: number // Default to CONFIG.USER.VIDEO_QUOTA_DAILY | ||
39 | |||
40 | pluginAuth?: string | ||
41 | }): MUser { | ||
42 | const { | ||
43 | username, | ||
44 | password, | ||
45 | email, | ||
46 | role = UserRole.USER, | ||
47 | emailVerified, | ||
48 | videoQuota = CONFIG.USER.VIDEO_QUOTA, | ||
49 | videoQuotaDaily = CONFIG.USER.VIDEO_QUOTA_DAILY, | ||
50 | adminFlags = UserAdminFlag.NONE, | ||
51 | pluginAuth | ||
52 | } = options | ||
53 | |||
54 | return new UserModel({ | ||
55 | username, | ||
56 | password, | ||
57 | email, | ||
58 | |||
59 | nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, | ||
60 | p2pEnabled: CONFIG.DEFAULTS.P2P.WEBAPP.ENABLED, | ||
61 | autoPlayVideo: true, | ||
62 | |||
63 | role, | ||
64 | emailVerified, | ||
65 | adminFlags, | ||
66 | |||
67 | videoQuota: videoQuota, | ||
68 | videoQuotaDaily: videoQuotaDaily, | ||
69 | |||
70 | pluginAuth | ||
71 | }) | ||
72 | } | ||
73 | |||
26 | async function createUserAccountAndChannelAndPlaylist (parameters: { | 74 | async function createUserAccountAndChannelAndPlaylist (parameters: { |
27 | userToCreate: MUser | 75 | userToCreate: MUser |
28 | userDisplayName?: string | 76 | userDisplayName?: string |
@@ -118,7 +166,7 @@ async function sendVerifyUserEmail (user: MUser, isPendingEmail = false) { | |||
118 | const email = isPendingEmail ? user.pendingEmail : user.email | 166 | const email = isPendingEmail ? user.pendingEmail : user.email |
119 | const username = user.username | 167 | const username = user.username |
120 | 168 | ||
121 | await Emailer.Instance.addVerifyEmailJob(username, email, url) | 169 | Emailer.Instance.addVerifyEmailJob(username, email, url) |
122 | } | 170 | } |
123 | 171 | ||
124 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { | 172 | async function getOriginalVideoFileTotalFromUser (user: MUserId) { |
@@ -180,7 +228,8 @@ export { | |||
180 | createUserAccountAndChannelAndPlaylist, | 228 | createUserAccountAndChannelAndPlaylist, |
181 | createLocalAccountWithoutKeys, | 229 | createLocalAccountWithoutKeys, |
182 | sendVerifyUserEmail, | 230 | sendVerifyUserEmail, |
183 | isAbleToUploadVideo | 231 | isAbleToUploadVideo, |
232 | buildUser | ||
184 | } | 233 | } |
185 | 234 | ||
186 | // --------------------------------------------------------------------------- | 235 | // --------------------------------------------------------------------------- |