From 999d5fa321132782c0b01c647f32ce2bfa72a659 Mon Sep 17 00:00:00 2001 From: viktorstrate Date: Thu, 1 Aug 2019 22:58:33 +0200 Subject: [PATCH] Add change password option for admins --- api/src/resolvers/users.js | 20 +++++++ api/src/schema.graphql | 3 + docker-compose.example.yml | 3 + ui/src/Pages/SettingsPage/UserRow.js | 79 ++++++++++++++++++++++++- ui/src/Pages/SettingsPage/UsersTable.js | 4 +- 5 files changed, 107 insertions(+), 2 deletions(-) diff --git a/api/src/resolvers/users.js b/api/src/resolvers/users.js index cdf1db6c..be3bd271 100644 --- a/api/src/resolvers/users.js +++ b/api/src/resolvers/users.js @@ -104,6 +104,26 @@ const Mutation = { return neo4jgraphql(root, args, ctx, info) }, + async changeUserPassword(root, args, ctx, info) { + const { newPassword, id } = args + + const session = ctx.driver.session() + + await session.run( + `MATCH (u:User { id: {id} }) SET u.password = {newPassword}`, + { + id, + newPassword, + } + ) + + session.close + + return { + success: true, + errorMessage: null, + } + }, } export const registerUser = Mutation.registerUser diff --git a/api/src/schema.graphql b/api/src/schema.graphql index e8966d0c..dcc0e20a 100644 --- a/api/src/schema.graphql +++ b/api/src/schema.graphql @@ -112,6 +112,9 @@ type Mutation { createUser(id: ID, username: String, rootPath: String, admin: Boolean): User @hasRole(roles: [admin]) deleteUser(id: ID!): User @hasRole(roles: [admin]) + + changeUserPassword(id: ID!, newPassword: String!): Result + @hasRole(roles: [admin]) } type Query { diff --git a/docker-compose.example.yml b/docker-compose.example.yml index b4597b59..bdd9bf59 100644 --- a/docker-compose.example.yml +++ b/docker-compose.example.yml @@ -20,6 +20,9 @@ services: - neo4j environment: - NEO4J_URI=bolt://neo4j:7687 + volumes: + # Change This: Link photo paths from the host machine + - ./photos_path:/photos ui: build: diff --git a/ui/src/Pages/SettingsPage/UserRow.js b/ui/src/Pages/SettingsPage/UserRow.js index 34276738..94894a6d 100644 --- a/ui/src/Pages/SettingsPage/UserRow.js +++ b/ui/src/Pages/SettingsPage/UserRow.js @@ -1,6 +1,14 @@ import React, { useState } from 'react' import { Mutation } from 'react-apollo' -import { Table, Icon, Button, Input, Checkbox, Modal } from 'semantic-ui-react' +import { + Table, + Icon, + Button, + Input, + Checkbox, + Modal, + Form, +} from 'semantic-ui-react' import gql from 'graphql-tag' const updateUserMutation = gql` @@ -33,6 +41,65 @@ const deleteUserMutation = gql` } ` +const changeUserPasswordMutation = gql` + mutation changeUserPassword($userId: ID!, $password: String!) { + changeUserPassword(id: $userId, newPassword: $password) { + success + errorMessage + } + } +` + +const ChangePasswordModal = ({ onClose, user, ...props }) => { + const [passwordInput, setPasswordInput] = useState('') + + return ( + { + onClose() + }} + > + {(changePassword, { data }) => ( + + Change password + +

+ Change password for {user.username} +

+
+ + + setPasswordInput(e.target.value)} + type="password" + /> + +
+
+ + + + +
+ )} +
+ ) +} + const UserRow = ({ user, refetchUsers }) => { const [state, setState] = useState({ ...user, @@ -40,6 +107,7 @@ const UserRow = ({ user, refetchUsers }) => { }) const [showComfirmDelete, setConfirmDelete] = useState(false) + const [showChangePassword, setChangePassword] = useState(false) function updateInput(event, key) { setState({ @@ -149,6 +217,15 @@ const UserRow = ({ user, refetchUsers }) => { Edit + + setChangePassword(false)} + />