Rewrite admin. (#1713)

* Rewrite admin.

* Clean up password forms.

* Fix naming issues.

* CSS Naming.
This commit is contained in:
Brian Cao 2022-12-26 16:57:59 -08:00 committed by GitHub
parent f4db04c3c6
commit e1f99a7d01
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
113 changed files with 2054 additions and 1872 deletions

View file

@ -26,7 +26,7 @@ export default async (
const { id } = req.query;
if (req.method === 'GET') {
if (await canViewUser(userId, id)) {
if (!isAdmin && !(await canViewUser(userId, id))) {
return unauthorized(res);
}
@ -36,7 +36,7 @@ export default async (
}
if (req.method === 'POST') {
if (await canUpdateUser(userId, id)) {
if (!isAdmin && !(await canUpdateUser(userId, id))) {
return unauthorized(res);
}
@ -46,7 +46,8 @@ export default async (
const data: any = {};
if (password) {
// Only admin can change these fields
if (password && isAdmin) {
data.password = hashPassword(password);
}
@ -70,7 +71,7 @@ export default async (
}
if (req.method === 'DELETE') {
if (isAdmin) {
if (!isAdmin) {
return unauthorized(res);
}

View file

@ -30,15 +30,15 @@ export default async (
const { current_password, new_password } = req.body;
const { id } = req.query;
const {
user: { id: userId },
user: { id: userId, isAdmin },
} = req.auth;
if (req.method === 'POST') {
if (canUpdateUser(userId, id)) {
if (!isAdmin && !(await canUpdateUser(userId, id))) {
return unauthorized(res);
}
const user = await getUser({ id });
const user = await getUser({ id }, { includePassword: true });
if (!checkPassword(current_password, user.password)) {
return badRequest(res, 'Current password is incorrect');

View file

@ -23,7 +23,7 @@ export default async (
} = req.auth;
if (req.method === 'GET') {
if (isAdmin) {
if (!isAdmin) {
return unauthorized(res);
}
@ -33,7 +33,7 @@ export default async (
}
if (req.method === 'POST') {
if (isAdmin) {
if (!isAdmin) {
return unauthorized(res);
}

18
pages/profile/index.js Normal file
View file

@ -0,0 +1,18 @@
import Settings from 'components/pages/Settings';
import ProfileSettings from 'components/pages/ProfileSettings';
import useRequireLogin from 'hooks/useRequireLogin';
import React from 'react';
export default function TeamsPage() {
const { loading } = useRequireLogin();
if (loading) {
return null;
}
return (
<Settings>
<ProfileSettings />
</Settings>
);
}

View file

@ -1,3 +0,0 @@
import Index from './index';
export default Index;

View file

@ -1,3 +0,0 @@
import Index from './index';
export default Index;

21
pages/teams/[...id].js Normal file
View file

@ -0,0 +1,21 @@
import Settings from 'components/pages/Settings';
import TeamDetails from 'components/pages/TeamDetails';
import useRequireLogin from 'hooks/useRequireLogin';
import { useRouter } from 'next/router';
import React from 'react';
export default function TeamDetailPage() {
const { loading } = useRequireLogin();
const router = useRouter();
const { id } = router.query;
if (loading) {
return null;
}
return (
<Settings>
<TeamDetails teamId={id} />
</Settings>
);
}

18
pages/teams/index.js Normal file
View file

@ -0,0 +1,18 @@
import Settings from 'components/pages/Settings';
import TeamsList from 'components/pages/TeamsList';
import useRequireLogin from 'hooks/useRequireLogin';
import React from 'react';
export default function TeamsPage() {
const { loading } = useRequireLogin();
if (loading) {
return null;
}
return (
<Settings>
<TeamsList />
</Settings>
);
}

21
pages/users/[...id].js Normal file
View file

@ -0,0 +1,21 @@
import Settings from 'components/pages/Settings';
import UserSettings from 'components/pages/UserSettings';
import useRequireLogin from 'hooks/useRequireLogin';
import { useRouter } from 'next/router';
import React from 'react';
export default function TeamDetailPage() {
const { loading } = useRequireLogin();
const router = useRouter();
const { id } = router.query;
if (loading) {
return null;
}
return (
<Settings>
<UserSettings userId={id} />
</Settings>
);
}

View file

@ -1,10 +1,10 @@
import React from 'react';
import Layout from 'components/layout/Layout';
import Settings from 'components/pages/Settings';
import useRequireLogin from 'hooks/useRequireLogin';
import useConfig from 'hooks/useConfig';
import useRequireLogin from 'hooks/useRequireLogin';
import React from 'react';
import UsersList from 'components/pages/UsersList';
export default function SettingsPage() {
export default function UsersPage() {
const { loading } = useRequireLogin();
const { adminDisabled } = useConfig();
@ -13,8 +13,8 @@ export default function SettingsPage() {
}
return (
<Layout>
<Settings />
</Layout>
<Settings>
<UsersList />
</Settings>
);
}

View file

@ -13,11 +13,9 @@ export default function DetailsPage() {
return null;
}
const [websiteId] = id;
return (
<Layout>
<WebsiteDetails websiteId={websiteId} />
<WebsiteDetails websiteId={id} />
</Layout>
);
}

View file

@ -0,0 +1,21 @@
import React from 'react';
import { useRouter } from 'next/router';
import WebsiteSettings from 'components/pages/WebsiteSettings';
import useRequireLogin from 'hooks/useRequireLogin';
import Settings from 'components/pages/Settings';
export default function WebsiteSettingsPage() {
const { loading } = useRequireLogin();
const router = useRouter();
const { id } = router.query;
if (!id || loading) {
return null;
}
return (
<Settings>
<WebsiteSettings websiteId={id} />
</Settings>
);
}

20
pages/websites/index.js Normal file
View file

@ -0,0 +1,20 @@
import Settings from 'components/pages/Settings';
import useConfig from 'hooks/useConfig';
import useRequireLogin from 'hooks/useRequireLogin';
import React from 'react';
import WebsitesList from 'components/pages/WebsitesList';
export default function WebsitesPage() {
const { loading } = useRequireLogin();
const { adminDisabled } = useConfig();
if (adminDisabled || loading) {
return null;
}
return (
<Settings>
<WebsitesList />
</Settings>
);
}