-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25bde10
commit a0d0890
Showing
20 changed files
with
10,182 additions
and
2,421 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
web/api-modules/users/controllers/edit-user-status-by-id.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { query } from '../../../database/connect.js'; | ||
|
||
const editUserStatusById = async (req, res, next) => { | ||
const { id: userId } = req.params; | ||
|
||
if (!userId) { | ||
return res.status(400).json({ | ||
message: 'User id not provided', | ||
error: 'No user id provided' | ||
}); | ||
} | ||
|
||
try { | ||
await query('UPDATE users SET status = NOT status WHERE user_id = $1', [ | ||
userId | ||
]); | ||
|
||
return res.status(200).json({ | ||
message: 'User status updated successfully', | ||
success: true | ||
}); | ||
} catch (err) { | ||
return next(err); | ||
} | ||
}; | ||
|
||
export default editUserStatusById; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { query } from '../../../database/connect.js'; | ||
|
||
const getUsers = async (req, res, next) => { | ||
try { | ||
const users = await query('SELECT * FROM users'); | ||
return res.json({ | ||
users: users.rows | ||
}); | ||
} catch (err) { | ||
return next(err); | ||
} | ||
}; | ||
|
||
export default getUsers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Router } from 'express'; | ||
import getUsers from './get-users.js'; | ||
import editUserStatusById from './edit-user-status-by-id.js'; | ||
|
||
const users = Router(); | ||
|
||
users.get('/', getUsers); | ||
users.patch('/:id', editUserStatusById); | ||
|
||
export default users; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Controllers from './controllers/index.js'; | ||
|
||
export default { Controllers }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
BEGIN; | ||
DROP TABLE IF EXISTS users; | ||
CREATE TABLE IF NOT EXISTS users ( | ||
"id" SERIAL PRIMARY KEY, | ||
user_id TEXT NOT NULL UNIQUE, | ||
shop TEXT, | ||
name TEXT, | ||
status BOOLEAN NOT NULL DEFAULT FALSE | ||
); | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,17 @@ | ||
import { Router } from 'express'; | ||
import getProducts from './get-products.js'; | ||
|
||
import checkUserAccessPermissions from '../../../middleware/checkUserAccessPermissions.js'; | ||
|
||
import checkOfflineSession from '../../../middleware/checkOfflineSession.js'; | ||
|
||
const products = Router(); | ||
|
||
products.get('/', checkOfflineSession, getProducts); | ||
products.post( | ||
'/', | ||
checkOfflineSession, | ||
checkUserAccessPermissions, | ||
getProducts | ||
); | ||
|
||
export default products; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Checkbox } from '@shopify/polaris'; | ||
import { useQueryClient } from 'react-query'; | ||
import { useAppQuery, useAppMutation } from '../hooks'; | ||
|
||
const HubUsers = () => { | ||
const queryClient = useQueryClient(); | ||
const { data, isLoading } = useAppQuery({ | ||
url: '/api/hub-users' | ||
}); | ||
|
||
const { mutateAsync, isLoading: updateUserStatusLoading } = useAppMutation({ | ||
reactQueryOptions: { | ||
onSuccess: () => { | ||
queryClient.invalidateQueries('/api/hub-users'); | ||
} | ||
} | ||
}); | ||
|
||
if (isLoading) { | ||
return 'loading...'; | ||
} | ||
|
||
const users = data?.users; | ||
|
||
return users.map((user) => ( | ||
<div | ||
key={user.id} | ||
style={{ | ||
display: 'flex', | ||
alignItems: 'center' | ||
}} | ||
> | ||
<Checkbox | ||
style={{ | ||
width: '50px' | ||
}} | ||
disabled={updateUserStatusLoading} | ||
checked={user.status} | ||
onChange={async () => { | ||
await mutateAsync({ | ||
url: `/api/hub-users/${user.userId}`, | ||
fetchInit: { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
} | ||
} | ||
}); | ||
}} | ||
/> | ||
<p | ||
style={{ | ||
margin: '0 10px' | ||
}} | ||
> | ||
{user.name} | ||
</p> | ||
<p>{user.userId}</p> | ||
</div> | ||
)); | ||
}; | ||
|
||
export default HubUsers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Issuer } from 'openid-client'; | ||
import { query } from '../database/connect.js'; | ||
|
||
const clientId = process.env.OIDC_CLIENT_ID; | ||
const clientSecret = process.env.OIDC_CLIENT_SECRET; | ||
const issuerURL = process.env.OIDC_ISSUER; | ||
|
||
const checkUserAccessPermissions = async (req, res, next) => { | ||
const { userId, accessToken } = req.body; | ||
|
||
if (!userId) { | ||
return res.status(403).json({ | ||
message: 'User access denied', | ||
error: 'No user id provided' | ||
}); | ||
} | ||
|
||
const issuer = await Issuer.discover(issuerURL); | ||
|
||
const client = new issuer.Client({ | ||
client_id: clientId, | ||
client_secret: clientSecret | ||
}); | ||
|
||
const tokenSet = await client.introspect(accessToken); | ||
|
||
if (!tokenSet.active) { | ||
return res.status(403).json({ | ||
message: 'User access denied', | ||
error: 'User not authorized' | ||
}); | ||
} | ||
|
||
const userName = tokenSet.name; | ||
|
||
try { | ||
const user = await query('SELECT * FROM users WHERE user_id = $1', [ | ||
userId | ||
]); | ||
|
||
if (!user || user.rows.length === 0) { | ||
// insert this user into the database with status false | ||
await query( | ||
'INSERT INTO users (user_id, status,name) VALUES ($1, $2,$3)', | ||
[userId, false, userName] | ||
); | ||
return res.status(403).json({ | ||
message: 'User access denied', | ||
error: 'User not found in database' | ||
}); | ||
} | ||
|
||
const { status } = user.rows[0]; | ||
|
||
if (status === true) { | ||
return next(); | ||
} | ||
|
||
return res.status(403).json({ | ||
message: 'User access denied', | ||
error: 'User not authorized' | ||
}); | ||
} catch (err) { | ||
return res.status(500).json({ | ||
message: 'User access denied', | ||
error: err.message | ||
}); | ||
} | ||
}; | ||
|
||
export default checkUserAccessPermissions; |
Oops, something went wrong.