Skip to content

Commit

Permalink
Admin partial search. Corretti alcuni errori.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard1984 committed Oct 23, 2018
1 parent b6aad43 commit 325275d
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 61 deletions.
21 changes: 20 additions & 1 deletion server/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const passport = require('passport')

// Config
const {
bearer
bearer,
login
} = require('../config/passport')

passport.use('bearer', bearer)
passport.use('login', login)

const authenticate = (req, res, next) => passport.authenticate('bearer', {
session: false,
Expand All @@ -25,6 +27,22 @@ const authenticate = (req, res, next) => passport.authenticate('bearer', {
}
})(req, res, next)

const signin = (req, res, next) => passport.authenticate('login', {
session: false,
failureFlash: true
}, (err, user, info) => {
if (err) {
next(new Error('Si è verificato un errore durante il login.'))
} else if (!user) {
return res.status(401).json({
message: info
})
} else {
req.user = user
return next()
}
})(req, res, next)

const authenticateAdmin = (req, res, next) => {
if (req.user.privileges._id !== 'admin') {
return res.status(401).json({
Expand Down Expand Up @@ -69,6 +87,7 @@ var authenticateAccesses = (req, res, next) => {

module.exports = {
authenticate,
signin,
authenticateUser,
authenticateAdmin,
authenticateAccesses
Expand Down
86 changes: 43 additions & 43 deletions server/routes/api/v1/routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,53 +93,53 @@ router.get('/users/:id', authenticate, authenticateAdmin, asyncMiddleware(async
* Utente admin
*/
router.get('/users/search/:key', authenticate, authenticateAdmin, asyncMiddleware(async (req, res) => {
// let regex = req.params.key.split(' ')
// regex = regex.join('|')
//
// let users = await User.find({
// $and: [{
// $or: [{
// firstname: {
// $regex: regex,
// $options: 'i'
// }
// }, {
// lastname: {
// $regex: regex,
// $options: 'i'
// }
// }]
// }, {
// _id: {
// $ne: req.user._id
// }
// }, {
// state: {
// $ne: 'pending'
// }
// }]
// })
let regex = req.params.key.split(' ')
regex = regex.join('|')

let users = await User.find({
$text: {
$search: req.params.key
},
_id: {
$ne: req.user._id
},
state: {
$ne: 'pending'
}
}, {
score: {
$meta: 'textScore'
}
}).sort({
score: {
$meta: 'textScore'
}
$and: [{
$or: [{
firstname: {
$regex: regex,
$options: 'i'
}
}, {
lastname: {
$regex: regex,
$options: 'i'
}
}]
}, {
_id: {
$ne: req.user._id
}
}, {
state: {
$ne: 'pending'
}
}]
})

// let users = await User.find({
// $text: {
// $search: req.params.key
// },
// _id: {
// $ne: req.user._id
// },
// state: {
// $ne: 'pending'
// }
// }, {
// score: {
// $meta: 'textScore'
// }
// }).sort({
// score: {
// $meta: 'textScore'
// }
// })

if (users.length && req.params.key.length !== 0) {
res.status(200).json(users)
} else {
Expand Down
2 changes: 1 addition & 1 deletion server/routes/api/v1/routes/documents.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const {

const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, path.join(__dirname, '..', 'public', 'public', 'documents'))
cb(null, path.join(__dirname, '..', '..', '..', '..', 'public', 'public', 'documents'))
},
filename: function (req, file, cb) {
cb(null, new Date().toISOString() + path.extname(file.originalname))
Expand Down
2 changes: 1 addition & 1 deletion server/routes/api/v1/routes/subjects.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ router.get('/', asyncMiddleware(async (req, res) => {
res.status(200).send(subjects)
}))

router.post('/subjects/search/partial/', asyncMiddleware(async (req, res) => {
router.post('/search/partial/', asyncMiddleware(async (req, res) => {
let query = req.body.query
let regex = query.split(' ').join('|')

Expand Down
6 changes: 3 additions & 3 deletions server/routes/api/v1/routes/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ const {
router.get('/me/', authenticate, asyncMiddleware(async (req, res) => {
let user = _.pick(req.user, ['_id', 'firstname', 'lastname', 'email', 'accesses', 'privileges'])

let documents = await Document.count({
let documents = await Document.countDocuments({
author: user._id
})

Expand Down Expand Up @@ -104,7 +104,7 @@ router.get('/me/documents/:visibility', authenticate, asyncMiddleware(async (req
* Utente loggato
*/
router.get('/me/documents/count/:visibility', authenticate, asyncMiddleware(async (req, res) => {
let documents = await Document.count({
let documents = await Document.countDocuments({
author: req.user._id,
visibility: req.params.visibility
})
Expand Down Expand Up @@ -239,7 +239,7 @@ router.post('/search/partial/', authenticate, asyncMiddleware(async (req, res) =
/*
Utente loggato
*/
router.post('/me/logged', authenticate, (req, res) => {
router.post('/me/logged/', authenticate, (req, res) => {
res.status(200).send()
})

Expand Down
11 changes: 4 additions & 7 deletions server/routes/login.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
const express = require('express')
const passport = require('passport')
const router = express.Router()

// Config
const {
login
} = require('../config/passport')
signin
} = require('../middleware/authenticate')

// Middleware
const {
asyncMiddleware
} = require('../middleware/async')

passport.use('login', login)

/*
* Utente non loggato
*/
router.post('/', passport.authenticate('login', { session: false }), asyncMiddleware(async (req, res) => {
router.post('/', signin, asyncMiddleware(async (req, res) => {
const {user} = req
let token
try {
token = await user.generateAuthToken()
} catch (e) {
throw new Error('Si è verificato un errore durante la generazione del token.')
throw e
}
res.status(200).json({
token
Expand Down
5 changes: 0 additions & 5 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ app.use((req, res, next) => {
next()
})

// const {
// asyncMiddleware
// } = require('./middleware/async')
// app.use(asyncMiddleware)

// Routes
app.use('/signup', signup)
app.use('/login', login)
Expand Down

0 comments on commit 325275d

Please sign in to comment.