mirror of
https://gitlab.com/comunic/comunicconsole
synced 2024-11-23 13:59:23 +00:00
Can generate reset access token for an account
This commit is contained in:
parent
585a66ef0a
commit
9b0f3c1fec
@ -39,6 +39,11 @@ export interface AdminAccountKey {
|
||||
time_add: number;
|
||||
}
|
||||
|
||||
export interface AdminResetToken {
|
||||
token: string;
|
||||
expire: number;
|
||||
}
|
||||
|
||||
const SESSION_STORAGE_TOKEN = "auth_token";
|
||||
|
||||
let currentAccount: AdminAccount | null = null;
|
||||
@ -150,6 +155,17 @@ export class AccountHelper {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a access reset token for an admin
|
||||
*
|
||||
* @param adminID The id of the target administrator
|
||||
*/
|
||||
static async GenerateResetToken(adminID: number): Promise<AdminResetToken> {
|
||||
return await serverRequest("accounts/generate_reset_token", {
|
||||
id: adminID,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* First step of access key enrollment
|
||||
*/
|
||||
|
@ -26,6 +26,7 @@ import {
|
||||
AdminAccount,
|
||||
AdminAccountKey,
|
||||
} from "../../helpers/AccountHelper";
|
||||
import { CopyToClipboard } from "../../utils/ClipboardUtils";
|
||||
import { AsyncWidget } from "../widgets/AsyncWidget";
|
||||
import {
|
||||
input,
|
||||
@ -192,6 +193,7 @@ export class KeySettingsSection extends React.Component<
|
||||
|
||||
this.load = this.load.bind(this);
|
||||
this.build = this.build.bind(this);
|
||||
this.generateResetToken = this.generateResetToken.bind(this);
|
||||
this.registerNewKey = this.registerNewKey.bind(this);
|
||||
this.deleteKey = this.deleteKey.bind(this);
|
||||
}
|
||||
@ -202,6 +204,27 @@ export class KeySettingsSection extends React.Component<
|
||||
this.setState({ keys: keys });
|
||||
}
|
||||
|
||||
async generateResetToken() {
|
||||
try {
|
||||
if (
|
||||
!(await matConfirm(
|
||||
"Do you really want to generate a reset token for this account?"
|
||||
))
|
||||
)
|
||||
return;
|
||||
|
||||
const token = await AccountHelper.GenerateResetToken(
|
||||
this.props.admin.id
|
||||
);
|
||||
|
||||
CopyToClipboard(token.token);
|
||||
snackbar("Reset token was successfully copied to the clipboard!");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
matAlert("Failed to generate a token!");
|
||||
}
|
||||
}
|
||||
|
||||
async registerNewKey() {
|
||||
try {
|
||||
const challenge = await AccountHelper.GetKeyRegistrationChallenge();
|
||||
@ -297,6 +320,9 @@ export class KeySettingsSection extends React.Component<
|
||||
margin: "5px 10px",
|
||||
}}
|
||||
>
|
||||
<Button onClick={this.generateResetToken}>
|
||||
New reset token
|
||||
</Button>
|
||||
<Button
|
||||
disabled={
|
||||
this.props.admin.id !==
|
||||
@ -316,7 +342,7 @@ function SettingsSection(p: { title: string; children?: React.ReactNode }) {
|
||||
return (
|
||||
<Grid item sm={6}>
|
||||
<Paper>
|
||||
<Typography variant="h5" style={{ padding: "10px 10px " }}>
|
||||
<Typography variant="h6" style={{ padding: "10px 15px " }}>
|
||||
{p.title}
|
||||
</Typography>
|
||||
<Divider />
|
||||
|
23
src/utils/ClipboardUtils.ts
Normal file
23
src/utils/ClipboardUtils.ts
Normal file
@ -0,0 +1,23 @@
|
||||
/**
|
||||
* Clipboard utilities
|
||||
*
|
||||
* @author Pierre Hubert
|
||||
*/
|
||||
|
||||
/**
|
||||
* Copy some piece of text to the clipboard
|
||||
*
|
||||
* @param str The content to copy
|
||||
*/
|
||||
export function CopyToClipboard(str: string) {
|
||||
const input = document.createElement("input");
|
||||
input.value = str;
|
||||
|
||||
document.body.appendChild(input);
|
||||
|
||||
input.select();
|
||||
input.setSelectionRange(0, str.length);
|
||||
document.execCommand("copy");
|
||||
|
||||
input.remove();
|
||||
}
|
Loading…
Reference in New Issue
Block a user