Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
products_lesson
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavel Mishakov
products_lesson
Commits
d7b12d88
Commit
d7b12d88
authored
Apr 07, 2023
by
Pavel Mishakov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
94 done BACK
parent
eb23ea9e
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
51 additions
and
13 deletions
+51
-13
settings.json
.vscode/settings.json
+22
-0
products.ts
src/controllers/products.ts
+4
-3
user.ts
src/controllers/user.ts
+12
-4
ERoles.ts
src/enums/ERoles.ts
+5
-0
EStatuses.ts
src/enums/EStatuses.ts
+3
-3
permission.ts
src/middlewares/permission.ts
+2
-2
User.ts
src/models/mongo/User.ts
+3
-1
No files found.
.vscode/settings.json
0 → 100644
View file @
d7b12d88
{
"workbench.colorCustomizations"
:
{
"activityBar.activeBackground"
:
"#65c89b"
,
"activityBar.background"
:
"#65c89b"
,
"activityBar.foreground"
:
"#15202b"
,
"activityBar.inactiveForeground"
:
"#15202b99"
,
"activityBarBadge.background"
:
"#945bc4"
,
"activityBarBadge.foreground"
:
"#e7e7e7"
,
"commandCenter.border"
:
"#15202b99"
,
"sash.hoverBorder"
:
"#65c89b"
,
"statusBar.background"
:
"#42b883"
,
"statusBar.foreground"
:
"#15202b"
,
"statusBarItem.hoverBackground"
:
"#359268"
,
"statusBarItem.remoteBackground"
:
"#42b883"
,
"statusBarItem.remoteForeground"
:
"#15202b"
,
"titleBar.activeBackground"
:
"#42b883"
,
"titleBar.activeForeground"
:
"#15202b"
,
"titleBar.inactiveBackground"
:
"#42b88399"
,
"titleBar.inactiveForeground"
:
"#15202b99"
},
"peacock.color"
:
"#42b883"
}
\ No newline at end of file
src/controllers/products.ts
View file @
d7b12d88
...
...
@@ -4,6 +4,7 @@ import { config } from '../index.config'
import
{
auth
}
from
'../middlewares/auth'
import
{
productService
,
ProductService
}
from
'../services/products'
import
{
permission
}
from
'../middlewares/permission'
import
{
ERoles
}
from
'../enums/ERoles'
const
storage
=
multer
.
diskStorage
({
destination
(
req
,
file
,
callback
)
{
...
...
@@ -24,10 +25,10 @@ export class ProductsController {
constructor
()
{
this
.
router
=
express
.
Router
()
this
.
router
.
get
(
'/'
,
permission
(
'USER'
)
,
this
.
getProducts
)
this
.
router
.
get
(
'/'
,
auth
,
this
.
getProducts
)
this
.
router
.
get
(
'/:id'
,
auth
,
this
.
getProductById
)
this
.
router
.
post
(
'/'
,
[
auth
,
upload
.
single
(
'image'
)],
this
.
addProduct
)
this
.
router
.
delete
(
'/:id'
,
auth
,
this
.
deleteProductById
)
this
.
router
.
post
(
'/'
,
[
permission
(
ERoles
.
SUPER_ADMIN
)
,
upload
.
single
(
'image'
)],
this
.
addProduct
)
this
.
router
.
delete
(
'/:id'
,
permission
(
ERoles
.
SUPER_ADMIN
)
,
this
.
deleteProductById
)
this
.
service
=
productService
}
...
...
src/controllers/user.ts
View file @
d7b12d88
...
...
@@ -7,6 +7,8 @@ import IUserGetDto from "../interfaces/IUserGetDto"
import
{
auth
}
from
"../middlewares/auth"
import
IRequestWithTokenData
from
"../interfaces/IRequestWithTokenData"
import
jwt
from
'jsonwebtoken'
import
{
permission
}
from
"../middlewares/permission"
import
{
ERoles
}
from
"../enums/ERoles"
...
...
@@ -16,11 +18,11 @@ export class UserController {
constructor
()
{
this
.
service
=
userService
this
.
router
=
express
.
Router
()
this
.
router
.
get
(
'/'
,
auth
,
this
.
getUsers
)
this
.
router
.
get
(
'/'
,
permission
(
ERoles
.
ADMIN
,
ERoles
.
SUPER_ADMIN
)
,
this
.
getUsers
)
this
.
router
.
post
(
'/'
,
this
.
createUser
)
this
.
router
.
post
(
'/login'
,
this
.
login
)
this
.
router
.
get
(
'/token'
,
auth
,
this
.
checkToken
)
this
.
router
.
put
(
'/'
,
auth
,
this
.
editUser
)
this
.
router
.
put
(
'/'
,
permission
(
ERoles
.
ADMIN
)
,
this
.
editUser
)
this
.
router
.
post
(
'/forgot-password'
,
this
.
sendEmailPassword
)
this
.
router
.
put
(
'/reset-password'
,
this
.
resetPassword
)
}
...
...
@@ -28,14 +30,20 @@ export class UserController {
return
this
.
router
}
private
createUser
=
async
(
req
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
await
this
.
service
.
createUser
(
req
.
body
)
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
await
this
.
service
.
createUser
({
username
:
req
.
body
.
username
,
password
:
req
.
body
.
password
})
res
.
status
(
200
).
send
(
response
)
}
private
editUser
=
async
(
expressReq
:
Request
,
res
:
Response
):
Promise
<
void
>
=>
{
const
req
=
expressReq
as
IRequestWithTokenData
const
user
=
req
.
dataFromToken
as
IUserGetDto
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
await
this
.
service
.
editUser
(
req
.
body
,
user
.
_id
)
const
response
:
IResponse
<
IUserGetDto
|
undefined
>
=
await
this
.
service
.
editUser
({
username
:
req
.
body
.
username
,
password
:
req
.
body
.
password
},
user
.
_id
)
res
.
status
(
200
).
send
(
response
)
}
...
...
src/enums/ERoles.ts
0 → 100644
View file @
d7b12d88
export
enum
ERoles
{
ADMIN
=
'ADMIN'
,
USER
=
'USER'
,
SUPER_ADMIN
=
'SUPER_ADMIN'
}
\ No newline at end of file
src/enums/EStatuses.ts
View file @
d7b12d88
export
const
enum
EStatuses
{
OK
=
1
,
NOT_OK
=
0
export
enum
EStatuses
{
NOT_OK
=
0
,
OK
=
1
}
\ No newline at end of file
src/middlewares/permission.ts
View file @
d7b12d88
...
...
@@ -3,7 +3,7 @@ import { EStatuses } from "../enums/EStatuses";
import
IResponse
from
"../interfaces/IResponse"
;
import
jwt
from
'jsonwebtoken'
import
IRequestWithTokenData
from
"../interfaces/IRequestWithTokenData"
;
export
const
permission
=
(
role
:
string
)
=>
{
export
const
permission
=
(
...
roles
:
string
[]
)
=>
{
return
(
expressReq
:
Request
,
res
:
Response
,
next
:
NextFunction
)
=>
{
const
req
=
expressReq
as
IRequestWithTokenData
if
(
req
.
method
===
'OPTIONS'
)
{
...
...
@@ -11,7 +11,7 @@ export const permission = (role: string) => {
}
try
{
const
data
=
jwt
.
verify
(
req
.
get
(
'Authorization'
)
||
''
,
process
.
env
.
SECRET_KEY
||
''
)
if
(
data
&&
typeof
data
==
'object'
&&
data
.
role
===
role
)
{
if
(
data
&&
typeof
data
==
'object'
&&
roles
.
includes
(
data
.
role
)
)
{
req
.
dataFromToken
=
data
next
()
}
else
{
...
...
src/models/mongo/User.ts
View file @
d7b12d88
import
mongoose
,
{
Schema
}
from
'mongoose'
import
IUser
from
'../../interfaces/IUser'
import
bcrypt
from
'bcrypt'
import
{
ERoles
}
from
'../../enums/ERoles'
const
UserSchema
:
Schema
=
new
Schema
<
IUser
>
({
username
:
{
...
...
@@ -15,7 +16,8 @@ const UserSchema: Schema = new Schema<IUser>({
role
:
{
type
:
String
,
required
:
true
,
default
:
'USER'
default
:
'USER'
,
enum
:
Object
.
values
(
ERoles
)
}
})
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment