mirror of
https://github.com/apache/superset.git
synced 2026-09-08 08:14:56 +00:00
113 lines
3.6 KiB
TypeScript
113 lines
3.6 KiB
TypeScript
/**
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
* or more contributor license agreements. See the NOTICE file
|
|
* distributed with this work for additional information
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
* to you under the Apache License, Version 2.0 (the
|
|
* "License"); you may not use this file except in compliance
|
|
* with the License. You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
import { t } from '@apache-superset/core/translation';
|
|
import { getClientErrorObject, SupersetClient } from '@superset-ui/core';
|
|
import { SelectOption } from 'src/components/ListView';
|
|
import { Actions } from 'src/constants';
|
|
import { FormValues } from './types';
|
|
|
|
type AddDangerToast = (message: string) => void;
|
|
|
|
export const handleUserError = async (
|
|
err: Response,
|
|
action: Actions.CREATE | Actions.UPDATE,
|
|
addDangerToast: AddDangerToast,
|
|
): Promise<never> => {
|
|
let errorMessage =
|
|
action === Actions.CREATE
|
|
? t('There was an error creating the user. Please, try again.')
|
|
: t('There was an error updating the user. Please, try again.');
|
|
|
|
if (err.status === 400 || err.status === 422) {
|
|
const errorData = await getClientErrorObject(err);
|
|
const message: unknown = errorData.message;
|
|
|
|
if (err.status === 400 && message && errorData.error) {
|
|
errorMessage = errorData.error;
|
|
} else if (
|
|
err.status === 422 &&
|
|
errorData.error?.includes('duplicate key value')
|
|
) {
|
|
if (errorData.error.includes('ab_user_username_key')) {
|
|
errorMessage = t(
|
|
'This username is already taken. Please choose another one.',
|
|
);
|
|
} else if (errorData.error.includes('ab_user_email_key')) {
|
|
errorMessage = t(
|
|
'This email is already associated with an account. Please choose another one.',
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
addDangerToast(errorMessage);
|
|
throw err;
|
|
};
|
|
|
|
export const createUser = async (values: FormValues) => {
|
|
const { confirmPassword: _confirmPassword, ...payload } = values;
|
|
if (payload.active == null) {
|
|
payload.active = false;
|
|
}
|
|
await SupersetClient.post({
|
|
endpoint: '/api/v1/security/users/',
|
|
jsonPayload: { ...payload },
|
|
});
|
|
};
|
|
|
|
export const updateUser = async (user_Id: number, values: FormValues) => {
|
|
await SupersetClient.put({
|
|
endpoint: `/api/v1/security/users/${user_Id}`,
|
|
jsonPayload: { ...values },
|
|
});
|
|
};
|
|
|
|
export const deleteUser = async (userId: number) =>
|
|
SupersetClient.delete({
|
|
endpoint: `/api/v1/security/users/${userId}`,
|
|
});
|
|
|
|
export const getUserDisplayLabel = (user: {
|
|
first_name?: string;
|
|
last_name?: string;
|
|
username?: string;
|
|
}): string =>
|
|
[user.first_name, user.last_name].filter(Boolean).join(' ') ||
|
|
user.username ||
|
|
t('N/A');
|
|
|
|
export const atLeastOneRoleOrGroup =
|
|
(fieldToCheck: 'roles' | 'groups') =>
|
|
({
|
|
getFieldValue,
|
|
}: {
|
|
getFieldValue: (field: string) => Array<SelectOption>;
|
|
}) => ({
|
|
validator(_: object, value: Array<SelectOption>) {
|
|
const current = value || [];
|
|
const other = getFieldValue(fieldToCheck) || [];
|
|
if (current.length === 0 && other.length === 0) {
|
|
return Promise.reject(
|
|
new Error(t('Please select at least one role or group')),
|
|
);
|
|
}
|
|
return Promise.resolve();
|
|
},
|
|
});
|