summaryrefslogtreecommitdiffstats
path: root/stepped-solutions/35
diff options
context:
space:
mode:
authorRandy Ridge <randyridge@gmail.com>2018-09-14 20:01:25 -0400
committerGitHub <noreply@github.com>2018-09-14 20:01:25 -0400
commit908180c77cd38011eeedcae949613da2a3391e5e (patch)
treeb59bf1aae819a04d453cde72f6e601f5561a740f /stepped-solutions/35
parent1f6667d233a4a2e9df977204d83a8f749c050c75 (diff)
parentb3bebda57ba187b7fa10398054b54c05d3fd3555 (diff)
Merge branch 'master' into randyridge/our
Diffstat (limited to 'stepped-solutions/35')
-rwxr-xr-xstepped-solutions/35/backend/src/index.js46
-rwxr-xr-xstepped-solutions/35/backend/src/resolvers/Query.js34
-rwxr-xr-xstepped-solutions/35/backend/src/schema.graphql31
-rwxr-xr-xstepped-solutions/35/frontend/components/Permissions.js73
-rwxr-xr-xstepped-solutions/35/frontend/pages/permissions.js12
5 files changed, 196 insertions, 0 deletions
diff --git a/stepped-solutions/35/backend/src/index.js b/stepped-solutions/35/backend/src/index.js
new file mode 100755
index 0000000..804d6d6
--- /dev/null
+++ b/stepped-solutions/35/backend/src/index.js
@@ -0,0 +1,46 @@
+const cookieParser = require('cookie-parser');
+const jwt = require('jsonwebtoken');
+
+require('dotenv').config({ path: 'variables.env' });
+const createServer = require('./createServer');
+const db = require('./db');
+
+const server = createServer();
+
+server.express.use(cookieParser());
+
+// decode the JWT so we can get the user Id on each request
+server.express.use((req, res, next) => {
+ const { token } = req.cookies;
+ if (token) {
+ const { userId } = jwt.verify(token, process.env.APP_SECRET);
+ // put the userId onto the req for future requests to access
+ req.userId = userId;
+ }
+ next();
+});
+
+// 2. Create a middleware that populates the user on each request
+
+server.express.use(async (req, res, next) => {
+ // if they aren't logged in, skip this
+ if (!req.userId) return next();
+ const user = await db.query.user(
+ { where: { id: req.userId } },
+ '{ id, permissions, email, name }'
+ );
+ req.user = user;
+ next();
+});
+
+server.start(
+ {
+ cors: {
+ credentials: true,
+ origin: process.env.FRONTEND_URL,
+ },
+ },
+ deets => {
+ console.log(`Server is now running on port http://localhost:${deets.port}`);
+ }
+);
diff --git a/stepped-solutions/35/backend/src/resolvers/Query.js b/stepped-solutions/35/backend/src/resolvers/Query.js
new file mode 100755
index 0000000..8af7b6c
--- /dev/null
+++ b/stepped-solutions/35/backend/src/resolvers/Query.js
@@ -0,0 +1,34 @@
+const { forwardTo } = require('prisma-binding');
+const { hasPermission } = require('../utils');
+
+const Query = {
+ items: forwardTo('db'),
+ item: forwardTo('db'),
+ itemsConnection: forwardTo('db'),
+ me(parent, args, ctx, info) {
+ // check if there is a current user ID
+ if (!ctx.request.userId) {
+ return null;
+ }
+ return ctx.db.query.user(
+ {
+ where: { id: ctx.request.userId },
+ },
+ info
+ );
+ },
+ async users(parent, args, ctx, info) {
+ // 1. Check if they are logged in
+ if (!ctx.request.userId) {
+ throw new Error('You must be logged in!');
+ }
+ console.log(ctx.request.userId);
+ // 2. Check if the user has the permissions to query all the users
+ hasPermission(ctx.request.user, ['ADMIN', 'PERMISSIONUPDATE']);
+
+ // 2. if they do, query all the users!
+ return ctx.db.query.users({}, info);
+ },
+};
+
+module.exports = Query;
diff --git a/stepped-solutions/35/backend/src/schema.graphql b/stepped-solutions/35/backend/src/schema.graphql
new file mode 100755
index 0000000..323b1dc
--- /dev/null
+++ b/stepped-solutions/35/backend/src/schema.graphql
@@ -0,0 +1,31 @@
+# import * from './generated/prisma.graphql'
+
+type SuccessMessage {
+ message: String
+}
+
+type Mutation {
+ createItem(title: String, description: String, price: Int, image: String, largeImage: String): Item!
+ updateItem(id: ID!, title: String, description: String, price: Int): Item!
+ deleteItem(id: ID!): Item
+ signup(email: String!, password: String!, name: String!): User!
+ signin(email: String!, password: String!): User!
+ signout: SuccessMessage
+ requestReset(email: String!): SuccessMessage
+ resetPassword(resetToken: String!, password: String!, confirmPassword: String!): User!
+}
+
+type Query {
+ items(where: ItemWhereInput, orderBy: ItemOrderByInput, skip: Int, first: Int): [Item]!
+ item(where: ItemWhereUniqueInput!): Item
+ itemsConnection(where: ItemWhereInput): ItemConnection!
+ me: User
+ users: [User]!
+}
+
+type User{
+ id: ID!
+ name: String!
+ email: String!
+ permissions: [Permission!]!
+}
diff --git a/stepped-solutions/35/frontend/components/Permissions.js b/stepped-solutions/35/frontend/components/Permissions.js
new file mode 100755
index 0000000..ca42ef6
--- /dev/null
+++ b/stepped-solutions/35/frontend/components/Permissions.js
@@ -0,0 +1,73 @@
+import { Query } from 'react-apollo';
+import Error from './ErrorMessage';
+import gql from 'graphql-tag';
+import Table from './styles/Table';
+import SickButton from './styles/SickButton';
+
+const possiblePermissions = [
+ 'ADMIN',
+ 'USER',
+ 'ITEMCREATE',
+ 'ITEMUPDATE',
+ 'ITEMDELETE',
+ 'PERMISSIONUPDATE',
+];
+
+const ALL_USERS_QUERY = gql`
+ query {
+ users {
+ id
+ name
+ email
+ permissions
+ }
+ }
+`;
+
+const Permissions = props => (
+ <Query query={ALL_USERS_QUERY}>
+ {({ data, loading, error }) => (
+ <div>
+ <Error error={error} />
+ <div>
+ <h2>Manage Permissions</h2>
+ <Table>
+ <thead>
+ <tr>
+ <th>Name</th>
+ <th>Email</th>
+ {possiblePermissions.map(permission => <th>{permission}</th>)}
+ <th>👇🏻</th>
+ </tr>
+ </thead>
+ <tbody>{data.users.map(user => <User user={user} />)}</tbody>
+ </Table>
+ </div>
+ </div>
+ )}
+ </Query>
+);
+
+class User extends React.Component {
+ render() {
+ const user = this.props.user;
+ return (
+ <tr>
+ <td>{user.name}</td>
+ <td>{user.email}</td>
+ {possiblePermissions.map(permission => (
+ <td>
+ <label htmlFor={`${user.id}-permission-${permission}`}>
+ <input type="checkbox" />
+ </label>
+ </td>
+ ))}
+ <td>
+ <SickButton>Update</SickButton>
+ </td>
+ </tr>
+ );
+ }
+}
+
+export default Permissions;
diff --git a/stepped-solutions/35/frontend/pages/permissions.js b/stepped-solutions/35/frontend/pages/permissions.js
new file mode 100755
index 0000000..de58c51
--- /dev/null
+++ b/stepped-solutions/35/frontend/pages/permissions.js
@@ -0,0 +1,12 @@
+import PleaseSignIn from '../components/PleaseSignIn';
+import Permissions from '../components/Permissions';
+
+const PermissionsPage = props => (
+ <div>
+ <PleaseSignIn>
+ <Permissions />
+ </PleaseSignIn>
+ </div>
+);
+
+export default PermissionsPage;