Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add channel name to create-user admin form #2984

Merged
merged 6 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class UserCreateComponent extends UserEdit implements OnInit {

this.buildForm({
username: this.userValidatorsService.USER_USERNAME,
channelName: this.userValidatorsService.USER_CHANNEL_NAME,
email: this.userValidatorsService.USER_EMAIL,
password: this.isPasswordOptional() ? this.userValidatorsService.USER_PASSWORD_OPTIONAL : this.userValidatorsService.USER_PASSWORD,
role: this.userValidatorsService.USER_ROLE,
Expand Down
11 changes: 11 additions & 0 deletions client/src/app/+admin/users/user-edit/user-edit.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
</div>
</div>

<div class="form-group" *ngIf="isCreation()">
<label i18n for="channelName">Channel name</label>
<input
type="text" id="channelName" i18n-placeholder placeholder="john_channel" class="form-control"
formControlName="channelName" [ngClass]="{ 'input-error': formErrors['channelName'] }"
>
<div *ngIf="formErrors.channelName" class="form-error">
{{ formErrors.channelName }}
</div>
</div>

<div class="form-group">
<label i18n for="email">Email</label>
<input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Injectable } from '@angular/core'
@Injectable()
export class UserValidatorsService {
readonly USER_USERNAME: BuildFormValidator
readonly USER_CHANNEL_NAME: BuildFormValidator
readonly USER_EMAIL: BuildFormValidator
readonly USER_PASSWORD: BuildFormValidator
readonly USER_PASSWORD_OPTIONAL: BuildFormValidator
Expand Down Expand Up @@ -36,6 +37,21 @@ export class UserValidatorsService {
}
}

this.USER_CHANNEL_NAME = {
VALIDATORS: [
Validators.required,
Validators.minLength(1),
Validators.maxLength(50),
Validators.pattern(/^[a-z0-9][a-z0-9._]*$/)
],
MESSAGES: {
'required': this.i18n('Channel name is required.'),
'minlength': this.i18n('Channel name must be at least 1 character long.'),
'maxlength': this.i18n('Channel name cannot be more than 50 characters long.'),
'pattern': this.i18n('Channel name should be lowercase alphanumeric; dots and underscores are allowed.')
}
}

this.USER_EMAIL = {
VALIDATORS: [ Validators.required, Validators.email ],
MESSAGES: {
Expand Down
6 changes: 5 additions & 1 deletion server/controllers/api/users/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export {

async function createUser (req: express.Request, res: express.Response) {
const body: UserCreate = req.body

const userToCreate = new UserModel({
username: body.username,
password: body.password,
Expand All @@ -194,7 +195,10 @@ async function createUser (req: express.Request, res: express.Response) {
userToCreate.password = await generateRandomString(20)
}

const { user, account, videoChannel } = await createUserAccountAndChannelAndPlaylist({ userToCreate: userToCreate })
const { user, account, videoChannel } = await createUserAccountAndChannelAndPlaylist({
userToCreate,
channelNames: body.channelName && { name: body.channelName, displayName: body.channelName }
})

auditLogger.create(getAuditIdFromRes(res), new UserAuditView(user.toFormattedJSON()))
logger.info('User %s with its channel and account created.', body.username)
Expand Down
14 changes: 14 additions & 0 deletions server/middlewares/validators/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const usersAddValidator = [
body('username').custom(isUserUsernameValid).withMessage('Should have a valid username (lowercase alphanumeric characters)'),
body('password').custom(isUserPasswordValidOrEmpty).withMessage('Should have a valid password'),
body('email').isEmail().withMessage('Should have a valid email'),
body('channelName').optional().custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
body('videoQuota').custom(isUserVideoQuotaValid).withMessage('Should have a valid user quota'),
body('videoQuotaDaily').custom(isUserVideoQuotaDailyValid).withMessage('Should have a valid daily user quota'),
body('role')
Expand All @@ -75,6 +76,19 @@ const usersAddValidator = [
.json({ error: 'You can only create users (and not administrators or moderators)' })
}

if (req.body.channelName) {
if (req.body.channelName === req.body.username) {
return res.status(400)
.json({ error: 'Channel name cannot be the same as user username.' })
}

const existing = await ActorModel.loadLocalByName(req.body.channelName)
if (existing) {
return res.status(409)
.json({ error: `Channel with name ${req.body.channelName} already exists.` })
}
}

return next()
}
]
Expand Down
1 change: 1 addition & 0 deletions shared/models/users/user-create.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface UserCreate {
videoQuotaDaily: number
role: UserRole
adminFlags?: UserAdminFlag
channelName?: string
}