grafana/public/app/core/components/Select/UserPicker.tsx
Marcus Efraimsson 8fd153edb7
API: Restrict anonymous user information access (#18422)
Existing /api/alert-notifications now requires at least editor access.
Existing /api/alert-notifiers now requires at least editor access.
New /api/alert-notifications/lookup returns less information than
/api/alert-notifications and can be access by any authenticated user.
Existing /api/org/users now requires org admin role.
New /api/org/users/lookup returns less information than
/api/org/users and can be access by users that are org admins,
admin in any folder or admin of any team.
UserPicker component now uses /api/org/users/lookup instead
of /api/org/users.

Fixes #17318
2019-08-12 20:03:48 +02:00

81 lines
1.8 KiB
TypeScript

// Libraries
import React, { Component } from 'react';
import _ from 'lodash';
// Components
import { AsyncSelect } from '@grafana/ui';
// Utils & Services
import { debounce } from 'lodash';
import { getBackendSrv } from 'app/core/services/backend_srv';
// Types
import { User } from 'app/types';
export interface Props {
onSelected: (user: User) => void;
className?: string;
}
export interface State {
isLoading: boolean;
}
export class UserPicker extends Component<Props, State> {
debouncedSearch: any;
constructor(props: Props) {
super(props);
this.state = { isLoading: false };
this.search = this.search.bind(this);
this.debouncedSearch = debounce(this.search, 300, {
leading: true,
trailing: true,
});
}
search(query?: string) {
const backendSrv = getBackendSrv();
this.setState({ isLoading: true });
if (_.isNil(query)) {
query = '';
}
return backendSrv
.get(`/api/org/users/lookup?query=${query}&limit=10`)
.then((result: any) => {
return result.map((user: any) => ({
id: user.userId,
value: user.userId,
label: user.login,
imgUrl: user.avatarUrl,
login: user.login,
}));
})
.finally(() => {
this.setState({ isLoading: false });
});
}
render() {
const { className, onSelected } = this.props;
const { isLoading } = this.state;
return (
<div className="user-picker">
<AsyncSelect
className={className}
isLoading={isLoading}
defaultOptions={true}
loadOptions={this.debouncedSearch}
onChange={onSelected}
placeholder="Select user"
noOptionsMessage={() => 'No users found'}
/>
</div>
);
}
}