Skip to content

Commit 067ed9a

Browse files
committed
Hide create user if password auth is disabled
1 parent 50d0dcb commit 067ed9a

File tree

3 files changed

+75
-10
lines changed

3 files changed

+75
-10
lines changed

coderd/users.go

+9
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,15 @@ func (api *API) postUser(rw http.ResponseWriter, r *http.Request) {
293293
return
294294
}
295295

296+
// If password auth is disabled, don't allow new users to be
297+
// created with a password!
298+
if api.DeploymentConfig.DisablePasswordAuth.Value {
299+
httpapi.Write(ctx, rw, http.StatusForbidden, codersdk.Response{
300+
Message: "You cannot manually provision new users with password authentication disabled!",
301+
})
302+
return
303+
}
304+
296305
// TODO: @emyrk Authorize the organization create if the createUser will do that.
297306

298307
_, err := api.Database.GetUserByEmailOrUsername(ctx, database.GetUserByEmailOrUsernameParams{

site/src/components/UsersLayout/UsersLayout.tsx

+14-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Link from "@material-ui/core/Link"
33
import { makeStyles } from "@material-ui/core/styles"
44
import GroupAdd from "@material-ui/icons/GroupAddOutlined"
55
import PersonAdd from "@material-ui/icons/PersonAddOutlined"
6+
import { useMachine } from "@xstate/react"
67
import { USERS_LINK } from "components/NavbarView/NavbarView"
78
import { PageHeader, PageHeaderTitle } from "components/PageHeader/PageHeader"
89
import { useFeatureVisibility } from "hooks/useFeatureVisibility"
@@ -15,13 +16,15 @@ import {
1516
useNavigate,
1617
} from "react-router-dom"
1718
import { combineClasses } from "util/combineClasses"
19+
import { authMethodsXService } from "xServices/auth/authMethodsXService"
1820
import { Margins } from "../../components/Margins/Margins"
1921
import { Stack } from "../../components/Stack/Stack"
2022

2123
export const UsersLayout: FC = () => {
2224
const styles = useStyles()
2325
const { createUser: canCreateUser, createGroup: canCreateGroup } =
2426
usePermissions()
27+
const [authMethods] = useMachine(authMethodsXService)
2528
const navigate = useNavigate()
2629
const { template_rbac: isTemplateRBACEnabled } = useFeatureVisibility()
2730

@@ -31,16 +34,17 @@ export const UsersLayout: FC = () => {
3134
<PageHeader
3235
actions={
3336
<>
34-
{canCreateUser && (
35-
<Button
36-
onClick={() => {
37-
navigate("/users/create")
38-
}}
39-
startIcon={<PersonAdd />}
40-
>
41-
Create user
42-
</Button>
43-
)}
37+
{canCreateUser &&
38+
authMethods.context.authMethods?.password.enabled && (
39+
<Button
40+
onClick={() => {
41+
navigate("/users/create")
42+
}}
43+
startIcon={<PersonAdd />}
44+
>
45+
Create user
46+
</Button>
47+
)}
4448
{canCreateGroup && isTemplateRBACEnabled && (
4549
<Link
4650
underline="none"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { assign, createMachine } from "xstate";
2+
import * as TypeGen from "api/typesGenerated"
3+
import * as API from "api/api"
4+
5+
export interface AuthMethodsContext {
6+
authMethods?: TypeGen.AuthMethods
7+
error?: Error | unknown
8+
}
9+
10+
export const authMethodsXService = createMachine({
11+
id: "authMethods",
12+
predictableActionArguments: true,
13+
tsTypes: {} as import("./authMethodsXService.typegen").Typegen0,
14+
schema: {
15+
context: {} as AuthMethodsContext,
16+
services: {} as {
17+
getAuthMethods: {
18+
data: TypeGen.AuthMethods
19+
}
20+
}
21+
},
22+
context: {},
23+
initial: "gettingAuthMethods",
24+
states: {
25+
gettingAuthMethods: {
26+
invoke: {
27+
src: "getAuthMethods",
28+
onDone: {
29+
target: "idle",
30+
actions: ["assignAuthMethods"]
31+
},
32+
onError: {
33+
target: "idle",
34+
actions: ["setError"]
35+
},
36+
},
37+
},
38+
idle: {},
39+
},
40+
}, {
41+
actions: {
42+
assignAuthMethods: assign({
43+
authMethods: (_, event) => event.data,
44+
}),
45+
setError: assign({
46+
error: (_, event) => event.data,
47+
}),
48+
},
49+
services: {
50+
getAuthMethods: () => API.getAuthMethods(),
51+
},
52+
})

0 commit comments

Comments
 (0)