private restExtractor: RestExtractor
) {}
- addUser(username: string, password: string) {
+ addUser(username: string, password: string, email: string) {
const body = {
username,
+ email,
password
};
</div>
</div>
+ <div class="form-group">
+ <label for="email">Email</label>
+ <input
+ type="text" class="form-control" id="email" placeholder="Email"
+ formControlName="email"
+ >
+ <div *ngIf="formErrors.email" class="alert alert-danger">
+ {{ formErrors.email }}
+ </div>
+ </div>
+
<div class="form-group">
<label for="password">Password</label>
<input
import { NotificationsService } from 'angular2-notifications';
import { UserService } from '../shared';
-import { FormReactive, USER_USERNAME, USER_PASSWORD } from '../../../shared';
+import {
+ FormReactive,
+ USER_USERNAME,
+ USER_EMAIL,
+ USER_PASSWORD
+} from '../../../shared';
@Component({
selector: 'my-user-add',
form: FormGroup;
formErrors = {
'username': '',
+ 'email': '',
'password': ''
};
validationMessages = {
'username': USER_USERNAME.MESSAGES,
+ 'email': USER_EMAIL.MESSAGES,
'password': USER_PASSWORD.MESSAGES,
};
buildForm() {
this.form = this.formBuilder.group({
username: [ '', USER_USERNAME.VALIDATORS ],
+ email: [ '', USER_EMAIL.VALIDATORS ],
password: [ '', USER_PASSWORD.VALIDATORS ],
});
addUser() {
this.error = null;
- const { username, password } = this.form.value;
+ const { username, password, email } = this.form.value;
- this.userService.addUser(username, password).subscribe(
+ this.userService.addUser(username, password, email).subscribe(
() => {
this.notificationsService.success('Success', `User ${username} created.`);
this.router.navigate([ '/admin/users/list' ]);
username: {
title: 'Username'
},
+ email: {
+ title: 'Email'
+ },
role: {
title: 'Role',
sort: false
<my-confirm></my-confirm>
<footer>
- PeerTube, CopyLeft 2015-2016
+ PeerTube, CopyLeft 2015-2017
</footer>
</div>
--- /dev/null
+import { FormControl } from '@angular/forms';
+
+export function validateEmail(c: FormControl) {
+ // Thanks to http://emailregex.com/
+ /* tslint:disable */
+ const EMAIL_REGEXP = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
+
+ return EMAIL_REGEXP.test(c.value) ? null : {
+ email: {
+ valid: false
+ }
+ };
+}
export function validateHost(c: FormControl) {
// Thanks to http://stackoverflow.com/a/106223
- let HOST_REGEXP = new RegExp(
+ const HOST_REGEXP = new RegExp(
'^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$'
);
+export * from './email.validator';
export * from './host.validator';
export * from './user';
export * from './video-abuse';
import { Validators } from '@angular/forms';
+import { validateEmail } from './email.validator';
+
export const USER_USERNAME = {
VALIDATORS: [ Validators.required, Validators.minLength(3), Validators.maxLength(20) ],
MESSAGES: {
'maxlength': 'Username cannot be more than 20 characters long.'
}
};
+export const USER_EMAIL = {
+ VALIDATORS: [ Validators.required, validateEmail ],
+ MESSAGES: {
+ 'required': 'Email is required.',
+ 'email': 'Email must be valid.',
+ }
+};
export const USER_PASSWORD = {
VALIDATORS: [ Validators.required, Validators.minLength(6) ],
MESSAGES: {
const user = db.User.build({
username: req.body.username,
password: req.body.password,
+ email: req.body.email,
role: constants.USER_ROLES.USER
})
const username = 'root'
const role = constants.USER_ROLES.ADMIN
+ const email = constants.CONFIG.ADMIN.EMAIL
const createOptions = {}
let password = ''
const userData = {
username,
+ email,
password,
role
}
function usersAdd (req, res, next) {
req.checkBody('username', 'Should have a valid username').isUserUsernameValid()
req.checkBody('password', 'Should have a valid password').isUserPasswordValid()
+ req.checkBody('email', 'Should have a valid email').isEmail()
logger.debug('Checking usersAdd parameters', { parameters: req.body })
checkErrors(req, res, function () {
- db.User.loadByUsername(req.body.username, function (err, user) {
+ db.User.loadByUsernameOrEmail(req.body.username, req.body.email, function (err, user) {
if (err) {
logger.error('Error in usersAdd request validator.', { error: err })
return res.sendStatus(500)
if (transaction) query.transaction = transaction
this.findOrCreate(query).asCallback(function (err, result) {
+ if (err) return callback(err)
+
// [ instance, wasCreated ]
- return callback(err, result[0])
+ return callback(null, result[0])
})
}
},
email: {
type: DataTypes.STRING(400),
- allowNull: false
+ allowNull: false,
+ validate: {
+ isEmail: true
+ }
}
},
{
}
}
},
+ email: {
+ type: DataTypes.STRING,
+ allowNull: false,
+ validate: {
+ isEmail: true
+ }
+ },
role: {
type: DataTypes.ENUM(values(constants.USER_ROLES)),
allowNull: false
{
fields: [ 'username' ],
unique: true
+ },
+ {
+ fields: [ 'email' ],
+ unique: true
}
],
classMethods: {
list,
listForApi,
loadById,
- loadByUsername
+ loadByUsername,
+ loadByUsernameOrEmail
},
instanceMethods: {
isPasswordMatch,
return {
id: this.id,
username: this.username,
+ email: this.email,
role: this.role,
createdAt: this.createdAt
}
return this.findOne(query).asCallback(callback)
}
+
+function loadByUsernameOrEmail (username, email, callback) {
+ const query = {
+ where: {
+ $or: [ { username }, { email } ]
+ }
+ }
+
+ return this.findOne(query).asCallback(callback)
+}
], done)
})
- describe('When making friends', function () {
+ describe('When managing friends', function () {
let userAccessToken = null
before(function (done) {
it('Should fail without public key', function (done) {
const data = {
+ email: 'testexample.com',
host: 'coucou.com'
}
requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
})
+ it('Should fail without an email', function (done) {
+ const data = {
+ host: 'coucou.com',
+ publicKey: 'mysuperpublickey'
+ }
+ requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
+ })
+
+ it('Should fail without an invalid email', function (done) {
+ const data = {
+ host: 'coucou.com',
+ email: 'testexample.com',
+ publicKey: 'mysuperpublickey'
+ }
+ requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
+ })
+
it('Should fail without an host', function (done) {
const data = {
+ email: 'testexample.com',
publicKey: 'mysuperpublickey'
}
requestsUtils.makePostBodyRequest(server.url, path, null, data, done)
it('Should fail with an incorrect host', function (done) {
const data = {
host: 'http://coucou.com',
+ email: 'testexample.com',
publicKey: 'mysuperpublickey'
}
requestsUtils.makePostBodyRequest(server.url, path, null, data, function () {
it('Should succeed with the correct parameters', function (done) {
const data = {
host: 'coucou.com',
+ email: 'test@example.com',
publicKey: 'mysuperpublickey'
}
requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 200)
it('Should fail with a host that already exists', function (done) {
const data = {
host: 'coucou.com',
+ email: 'test@example.com',
publicKey: 'mysuperpublickey'
}
requestsUtils.makePostBodyRequest(server.url, path, null, data, done, 409)
it('Should fail with a too small username', function (done) {
const data = {
username: 'ji',
+ email: 'test@example.com',
password: 'mysuperpassword'
}
it('Should fail with a too long username', function (done) {
const data = {
username: 'mysuperusernamewhichisverylong',
+ email: 'test@example.com',
password: 'mysuperpassword'
}
it('Should fail with an incorrect username', function (done) {
const data = {
username: 'my username',
+ email: 'test@example.com',
+ password: 'mysuperpassword'
+ }
+
+ requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
+ })
+
+ it('Should fail with a missing email', function (done) {
+ const data = {
+ username: 'ji',
+ password: 'mysuperpassword'
+ }
+
+ requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done)
+ })
+
+ it('Should fail with an invalid email', function (done) {
+ const data = {
+ username: 'mysuperusernamewhichisverylong',
+ email: 'testexample.com',
password: 'mysuperpassword'
}
it('Should fail with a too small password', function (done) {
const data = {
username: 'myusername',
+ email: 'test@example.com',
password: 'bla'
}
it('Should fail with a too long password', function (done) {
const data = {
username: 'myusername',
+ email: 'test@example.com',
password: 'my super long password which is very very very very very very very very very very very very very very' +
'very very very very very very very very very very very very very very very veryv very very very very' +
'very very very very very very very very very very very very very very very very very very very very long'
it('Should fail with an non authenticated user', function (done) {
const data = {
username: 'myusername',
+ email: 'test@example.com',
password: 'my super password'
}
it('Should fail if we add a user with the same username', function (done) {
const data = {
username: 'user1',
+ email: 'test@example.com',
+ password: 'my super password'
+ }
+
+ requestsUtils.makePostBodyRequest(server.url, path, server.accessToken, data, done, 409)
+ })
+
+ it('Should fail if we add a user with the same email', function (done) {
+ const data = {
+ username: 'myusername',
+ email: 'user1@example.com',
password: 'my super password'
}
it('Should succeed with the correct params', function (done) {
const data = {
username: 'user2',
+ email: 'test@example.com',
password: 'my super password'
}
it('Should fail with a non admin user', function (done) {
server.user = {
username: 'user1',
+ email: 'test@example.com',
password: 'my super password'
}
const data = {
username: 'user3',
+ email: 'test@example.com',
password: 'my super password'
}
const user = res.body
expect(user.username).to.equal('user_1')
+ expect(user.email).to.equal('user_1@example.com')
expect(user.id).to.exist
done()
const user = users[0]
expect(user.username).to.equal('user_1')
+ expect(user.email).to.equal('user_1@example.com')
const rootUser = users[1]
expect(rootUser.username).to.equal('root')
+ expect(rootUser.email).to.equal('admin1@example.com')
userId = user.id
done()
const user = users[0]
expect(user.username).to.equal('root')
+ expect(user.email).to.equal('admin1@example.com')
done()
})
const user = users[0]
expect(user.username).to.equal('user_1')
+ expect(user.email).to.equal('user_1@example.com')
done()
})
const user = users[0]
expect(user.username).to.equal('user_1')
+ expect(user.email).to.equal('user_1@example.com')
done()
})
expect(users.length).to.equal(2)
expect(users[0].username).to.equal('root')
+ expect(users[0].email).to.equal('admin1@example.com')
expect(users[1].username).to.equal('user_1')
+ expect(users[1].email).to.equal('user_1@example.com')
done()
})
}
const path = '/api/v1/users'
+ const body = {
+ username,
+ password,
+ email: username + '@example.com'
+ }
request(url)
.post(path)
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + accessToken)
- .send({ username: username, password: password })
+ .send(body)
.expect(specialStatus)
.end(end)
}