Skip to content

Commit b8e6d41

Browse files
authored
Merge pull request #593 from CodeForPhilly/587_tweak-new-user-modal
587: Tweak new user modal
2 parents 0f3de25 + 57d54d3 commit b8e6d41

File tree

7 files changed

+70
-40
lines changed

7 files changed

+70
-40
lines changed

src/client/src/pages/UserManagement/Components/Dialog/NewUserDialog.jsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
buildUsernameValidation
2020
} from '../../Validations';
2121
import useAlert from "../../../../hooks/useAlert";
22+
import RolesRadioGroup from "../RolesRadioGroup";
2223

2324

2425
export default function NewUserDialog(props) {
@@ -49,7 +50,8 @@ export default function NewUserDialog(props) {
4950
username: data.username,
5051
full_name: data.name,
5152
role: data.role,
52-
password: data.password
53+
password: data.password,
54+
active: "Y",
5355
};
5456

5557
createUser(newUser, token)
@@ -99,18 +101,6 @@ export default function NewUserDialog(props) {
99101
{(responseError || errors.username) && // This is a little janky... Could be improved upon.
100102
<Typography color="error">{responseError || errors.username.message}</Typography>
101103
}
102-
<TextField
103-
{...register("role")}
104-
margin="dense"
105-
id="role-input"
106-
label="Role - user/editor/admin"
107-
onBlur={() => trigger("role")}
108-
variant="standard"
109-
fullWidth
110-
/>
111-
{errors.role &&
112-
<Typography color="error">{errors.role.message}</Typography>
113-
}
114104
<TextField
115105
{...register("password")}
116106
margin="dense"
@@ -135,6 +125,10 @@ export default function NewUserDialog(props) {
135125
{errors.confirmPassword &&
136126
<Typography color="error">{errors.confirmPassword.message}</Typography>
137127
}
128+
<RolesRadioGroup id="roles-radio-group" register={register} />
129+
{errors.role &&
130+
<Typography color="error">{errors.role.message}</Typography>
131+
}
138132
<DialogActions>
139133
<Button
140134
onClick={onClose}

src/client/src/pages/UserManagement/Components/Dialog/UpdateUserDialog.jsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import * as Yup from 'yup';
1616
import { updateUser } from "../../../../utils/api";
1717
import { buildNameValidation, buildRoleValidation } from '../../Validations';
1818
import useAlert from "../../../../hooks/useAlert";
19+
import RolesRadioGroup from "../RolesRadioGroup";
1920

2021

2122
export default function UpdateUserDialog(props) {
@@ -28,7 +29,6 @@ export default function UpdateUserDialog(props) {
2829
const {
2930
username,
3031
full_name: name,
31-
role,
3232
active
3333
} = user;
3434

@@ -104,16 +104,7 @@ export default function UpdateUserDialog(props) {
104104
{(errors.username) &&
105105
<Typography color="error">{errors.username.message}</Typography>
106106
}
107-
<TextField
108-
{...register("role")}
109-
defaultValue={role}
110-
margin="dense"
111-
id="role-input"
112-
label="Role - user/editor/admin"
113-
onBlur={() => trigger("role")}
114-
variant="standard"
115-
fullWidth
116-
/>
107+
<RolesRadioGroup id="roles-radio-group" user={user} register={register} />
117108
{errors.role &&
118109
<Typography color="error">{errors.role.message}</Typography>
119110
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import {
2+
FormControlLabel,
3+
Radio,
4+
RadioGroup,
5+
} from "@material-ui/core";
6+
import React from "react";
7+
8+
const UserRoles = {
9+
Admin: "admin",
10+
User: "user",
11+
};
12+
13+
const options = [
14+
{
15+
label: "Admin",
16+
value: UserRoles.Admin,
17+
},
18+
{
19+
label: "User",
20+
value: UserRoles.User,
21+
},
22+
];
23+
24+
export default function RolesRadioGroup(props) {
25+
const { register, user } = props;
26+
const [selectedRole, setSelectedRole] = React.useState(user ? user.role : undefined);
27+
28+
React.useEffect(() => {
29+
setSelectedRole(user ? user.role : null);
30+
}, [user]);
31+
32+
const generateRadioOptions = () => {
33+
return options.map((option) => (
34+
<FormControlLabel
35+
key={option.value}
36+
value={option.value}
37+
label={option.label}
38+
control={<Radio />}
39+
checked={selectedRole === option.value}
40+
onClick={(() => setSelectedRole(option.value))}
41+
{...register("role")}
42+
/>
43+
));
44+
};
45+
46+
return (
47+
<RadioGroup
48+
row
49+
name="role"
50+
{...register("role")}
51+
>
52+
{generateRadioOptions()}
53+
</RadioGroup>
54+
);
55+
};

src/client/src/pages/UserManagement/Validations.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const buildUsernameValidation = () => {
3030
export const buildRoleValidation = () => {
3131
return Yup.string()
3232
.trim()
33-
.oneOf(["user", "editor", "admin"], "Role must be one of the following: user/editor/admin")
33+
.oneOf(["user", "admin"], "Role must be one of the following: user/admin")
3434
.required("Role is required")
3535
}
3636

@@ -50,6 +50,6 @@ export const buildPasswordValidation = (username) => {
5050
return [...DISALLOWED_WORDS, lowercaseUsername].every((word) => !lowercasePassword.includes(word))
5151
})
5252
.matches(/^[a-zA-Z0-9!@#$%^*]+$/, "Password can only contain numbers, letters, and the following symbols: !@#$%^*")
53-
.min(12, "Password must contain at least 12 letters")
53+
.min(12, "Password must contain at least 12 characters")
5454
.max(36, "Password must be 36 characters or less")
5555
}

src/server/api/user_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ def user_create():
266266
username : str
267267
full_name : str
268268
password : str
269-
role : str, one of `user`, `editor`, `admin`
269+
role : str, one of `user`, `admin`
270270
271271
Returns
272272
----------

src/server/db_setup/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Form POST Parameters
1111
username : str
1212
full_name : str
1313
password : str
14-
role : str, one of `user`, `editor`, `admin`
14+
role : str, one of `user`, `admin`
1515

1616
Returns
1717
----------
@@ -28,6 +28,5 @@ Returns
2828
One header row of field names, one row per user
2929
"['username', 'full_name', 'active', 'role'],
3030
['admin', None, 'Y', 'admin'],
31-
['editor', None, 'Y', 'editor'],
3231
['steve11', 'Steve the User', 'Y', 'admin'],
3332
['user', None, 'Y', 'user'],"

src/server/db_setup/base_users.py

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@
88

99

1010
try:
11-
from secrets_dict import BASEUSER_PW, BASEEDITOR_PW, BASEADMIN_PW
11+
from secrets_dict import BASEUSER_PW, BASEADMIN_PW
1212
except ImportError:
1313
# Not running locally
1414
logger.debug("Couldn't get BASE user PWs from file, trying environment **********")
1515
from os import environ
1616

1717
try:
1818
BASEUSER_PW = environ['BASEUSER_PW']
19-
BASEEDITOR_PW = environ['BASEEDITOR_PW']
2019
BASEADMIN_PW = environ['BASEADMIN_PW']
2120

2221
except KeyError:
@@ -39,15 +38,14 @@ def create_base_roles():
3938
role_count = len(result.fetchall())
4039
if role_count == 0:
4140
connection.execute("INSERT into pdp_user_roles values (1, 'user') ")
42-
connection.execute("INSERT into pdp_user_roles values (2, 'editor') ")
4341
connection.execute("INSERT into pdp_user_roles values (9, 'admin') ")
4442

4543
else:
4644
logger.debug("%d roles already present in DB, not creating", role_count)
4745

4846

4947
def create_base_users(): # TODO: Just call create_user for each
50-
""" Creates three users (user, editor, admin) for testing
48+
""" Creates two users (user, admin) for testing
5149
Password for each is user name with 'pw' appended """
5250
with engine.connect() as connection:
5351

@@ -73,13 +71,6 @@ def create_base_users(): # TODO: Just call create_user for each
7371
)
7472
connection.execute(ins_stmt)
7573

76-
# editor
77-
pw_hash = user_api.hash_password(BASEEDITOR_PW)
78-
ins_stmt = pu.insert().values(
79-
username="base_editor", full_name="Base Editor", password=pw_hash, active="Y", role=2,
80-
)
81-
connection.execute(ins_stmt)
82-
8374
# admin
8475
pw_hash = user_api.hash_password(BASEADMIN_PW)
8576
ins_stmt = pu.insert().values(

0 commit comments

Comments
 (0)