-
Notifications
You must be signed in to change notification settings - Fork 6
/
user.js
133 lines (128 loc) · 4.1 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Api user resource routes
*/
const Joi = require('@hapi/joi');
const AuthCtrl = require('modules/authorization/controllers/authorization');
const UserCtrl = require('modules/authorization/controllers/user');
const User = require('models/authorization/user');
const Resources = require('enums/resources');
// GET /user
exports.list = {
description: 'Lists available users',
pre: [AuthCtrl.authorize(Resources.USER)],
handler: UserCtrl.list,
validate: {
query: Joi.object({
limit: Joi.number()
.integer()
.min(1)
.max(100)
.description('The limit of users to get'),
page: Joi.number()
.integer()
.positive()
.description('The page number to get the users from'),
search: Joi.string().description('The search criteria'),
sort: Joi.string().description('The sorting order')
})
}
};
// GET /user/{id}
exports.get = {
description: 'Get user by ID',
pre: [AuthCtrl.authorize(Resources.USER)],
handler: UserCtrl.get,
validate: {
params: Joi.object({
id: Joi.number()
.integer()
.positive()
.required()
.description('The Id of the user')
})
}
};
// POST /user
exports.create = {
description: 'Add a new user',
pre: [AuthCtrl.authorize(Resources.USER)],
handler: UserCtrl.create,
validate: {
payload: Joi.object({
username: Joi.string()
.min(User.USERNAME_MIN_LENGTH)
.max(User.USERNAME_MAX_LENGTH)
.required()
.description('The username of the user'),
name: Joi.string()
.min(User.NAME_MIN_LENGTH)
.max(User.NAME_MAX_LENGTH)
.required()
.description('The real name of the user'),
email: Joi.string()
.email()
.required()
.description('The email of the user'),
avatar: Joi.string()
.uri({
scheme: ['http', 'https'],
allowRelative: true
})
.description('The URI to the user avatar'),
password: Joi.string()
.min(User.PASSWORD_MIN_LENGTH)
.max(User.PASSWORD_MAX_LENGTH)
.required()
.description('The password of the user')
})
}
};
// DELETE /user/{id}
exports.delete = {
description: 'Delete an existing user',
pre: [AuthCtrl.authorize(Resources.USER)],
handler: UserCtrl.delete,
validate: {
params: Joi.object({
id: Joi.number()
.integer()
.positive()
.required()
.description('The ID of the user')
})
}
};
// PUT /user/{id}
exports.update = {
description: 'Update an existing user',
pre: [AuthCtrl.authorize(Resources.USER)],
handler: UserCtrl.update,
validate: {
payload: Joi.object({
id: Joi.forbidden(),
username: Joi.string()
.min(User.USERNAME_MIN_LENGTH)
.max(User.USERNAME_MAX_LENGTH)
.description('The username of the user'),
name: Joi.string()
.min(User.NAME_MIN_LENGTH)
.max(User.NAME_MAX_LENGTH)
.description('The real name of the user'),
email: Joi.string()
.email()
.description('the email of the user'),
password: Joi.string()
.min(User.PASSWORD_MIN_LENGTH)
.max(User.PASSWORD_MAX_LENGTH)
.description('The password of the user'),
avatar: Joi.string()
.uri({
scheme: ['http', 'https'],
allowRelative: true
})
.description('The URI to the user avatar'),
active: Joi.boolean().description('If the user is active'),
roles: Joi.forbidden()
})
}
};