123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import { css } from '@emotion/css';
- import { withI18n, withI18nProps } from '@lingui/react';
- import React, { PureComponent } from 'react';
- import { ConfirmButton, ConfirmModal, Button } from '@grafana/ui';
- import { contextSrv } from 'app/core/core';
- import { AccessControlAction, UserSession } from 'app/types';
- interface Props extends withI18nProps {
- sessions: UserSession[];
- onSessionRevoke: (id: number) => void;
- onAllSessionsRevoke: () => void;
- }
- interface State {
- showLogoutModal: boolean;
- }
- class BaseUserSessions extends PureComponent<Props, State> {
- forceAllLogoutButton = React.createRef<HTMLButtonElement>();
- state: State = {
- showLogoutModal: false,
- };
- showLogoutConfirmationModal = () => {
- this.setState({ showLogoutModal: true });
- };
- dismissLogoutConfirmationModal = () => {
- this.setState({ showLogoutModal: false }, () => {
- this.forceAllLogoutButton.current?.focus();
- });
- };
- onSessionRevoke = (id: number) => {
- return () => {
- this.props.onSessionRevoke(id);
- };
- };
- onAllSessionsRevoke = () => {
- this.setState({ showLogoutModal: false });
- this.props.onAllSessionsRevoke();
- };
- render() {
- const { sessions, i18n } = this.props;
- const { showLogoutModal } = this.state;
- const logoutFromAllDevicesClass = css`
- margin-top: 0.8rem;
- `;
- const canLogout = contextSrv.hasPermission(AccessControlAction.UsersLogout);
- return (
- <>
- <h3 className="page-heading">Sessions</h3>
- <div className="gf-form-group">
- <div className="gf-form">
- <table className="filter-table form-inline">
- <thead>
- <tr>
- <th>Last seen</th>
- <th>Logged on</th>
- <th>IP address</th>
- <th colSpan={2}>Browser and OS</th>
- </tr>
- </thead>
- <tbody>
- {sessions &&
- sessions.map((session, index) => (
- <tr key={`${session.id}-${index}`}>
- <td>{session.isActive ? 'Now' : session.seenAt}</td>
- <td>{i18n.date(session.createdAt, { dateStyle: 'long' })}</td>
- <td>{session.clientIp}</td>
- <td>{`${session.browser} on ${session.os} ${session.osVersion}`}</td>
- <td>
- <div className="pull-right">
- {canLogout && (
- <ConfirmButton
- confirmText="Confirm logout"
- confirmVariant="destructive"
- onConfirm={this.onSessionRevoke(session.id)}
- autoFocus
- >
- Force logout
- </ConfirmButton>
- )}
- </div>
- </td>
- </tr>
- ))}
- </tbody>
- </table>
- </div>
- <div className={logoutFromAllDevicesClass}>
- {canLogout && sessions.length > 0 && (
- <Button variant="secondary" onClick={this.showLogoutConfirmationModal} ref={this.forceAllLogoutButton}>
- Force logout from all devices
- </Button>
- )}
- <ConfirmModal
- isOpen={showLogoutModal}
- title="Force logout from all devices"
- body="Are you sure you want to force logout from all devices?"
- confirmText="Force logout"
- onConfirm={this.onAllSessionsRevoke}
- onDismiss={this.dismissLogoutConfirmationModal}
- />
- </div>
- </div>
- </>
- );
- }
- }
- export const UserSessions = withI18n()(BaseUserSessions);
|