2021-03-14 15:50:36 +08:00
|
|
|
// Copyright 2021 The casbin Authors. All Rights Reserved.
|
2021-02-13 12:15:19 +08:00
|
|
|
//
|
|
|
|
// Licensed 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 {message} from "antd";
|
|
|
|
import React from "react";
|
|
|
|
import {isMobile as isMobileDevice} from "react-device-detect";
|
2021-02-19 23:23:59 +08:00
|
|
|
import "./i18n";
|
|
|
|
import i18next from "i18next";
|
2021-03-14 23:08:08 +08:00
|
|
|
import copy from "copy-to-clipboard";
|
2021-04-28 22:40:21 +08:00
|
|
|
import {authConfig} from "./auth/Auth";
|
2021-04-29 21:28:24 +08:00
|
|
|
import {Helmet} from "react-helmet";
|
2021-02-13 12:15:19 +08:00
|
|
|
|
2021-02-13 21:04:23 +08:00
|
|
|
export let ServerUrl = "";
|
2021-02-13 12:15:19 +08:00
|
|
|
|
2021-06-12 12:07:24 +08:00
|
|
|
// export const StaticBaseUrl = "https://cdn.jsdelivr.net/gh/casbin/static";
|
|
|
|
export const StaticBaseUrl = "https://cdn.casbin.org";
|
2021-06-12 11:52:59 +08:00
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
export function initServerUrl() {
|
|
|
|
const hostname = window.location.hostname;
|
2021-02-13 21:04:23 +08:00
|
|
|
if (hostname === "localhost") {
|
2021-02-13 12:15:19 +08:00
|
|
|
ServerUrl = `http://${hostname}:8000`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-14 15:55:50 +08:00
|
|
|
function isLocalhost() {
|
|
|
|
const hostname = window.location.hostname;
|
|
|
|
return hostname === "localhost";
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
export function isProviderVisible(providerItem) {
|
|
|
|
if (providerItem.provider === undefined || providerItem.provider === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-17 01:49:05 +08:00
|
|
|
if (providerItem.provider.category !== "OAuth") {
|
|
|
|
return false;
|
2021-05-14 15:55:50 +08:00
|
|
|
}
|
|
|
|
|
2021-06-17 01:49:05 +08:00
|
|
|
if (providerItem.provider.type === "GitHub") {
|
|
|
|
if (isLocalhost()) {
|
|
|
|
return providerItem.provider.name.includes("localhost");
|
|
|
|
} else {
|
|
|
|
return !providerItem.provider.name.includes("localhost");
|
|
|
|
}
|
2021-05-14 15:55:50 +08:00
|
|
|
} else {
|
2021-06-17 01:49:05 +08:00
|
|
|
return true;
|
2021-06-14 21:35:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isProviderVisibleForSignUp(providerItem) {
|
|
|
|
if (providerItem.canSignUp === false) {
|
|
|
|
return false;
|
2021-05-14 15:55:50 +08:00
|
|
|
}
|
2021-06-14 21:35:19 +08:00
|
|
|
|
|
|
|
return isProviderVisible(providerItem);
|
2021-05-14 15:55:50 +08:00
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
export function isProviderVisibleForSignIn(providerItem) {
|
|
|
|
if (providerItem.canSignIn === false) {
|
2021-06-09 20:39:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-06-14 21:35:19 +08:00
|
|
|
return isProviderVisible(providerItem);
|
2021-06-09 20:39:43 +08:00
|
|
|
}
|
|
|
|
|
2021-06-18 23:43:36 +08:00
|
|
|
export function isProviderPrompted(providerItem) {
|
|
|
|
return isProviderVisible(providerItem) && providerItem.prompted;
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
export function parseJson(s) {
|
|
|
|
if (s === "") {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return JSON.parse(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function myParseInt(i) {
|
|
|
|
const res = parseInt(i);
|
|
|
|
return isNaN(res) ? 0 : res;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function openLink(link) {
|
|
|
|
// this.props.history.push(link);
|
|
|
|
const w = window.open('about:blank');
|
|
|
|
w.location.href = link;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function goToLink(link) {
|
|
|
|
window.location.href = link;
|
|
|
|
}
|
|
|
|
|
2021-03-26 21:58:19 +08:00
|
|
|
export function goToLinkSoft(ths, link) {
|
|
|
|
ths.props.history.push(link);
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
export function showMessage(type, text) {
|
|
|
|
if (type === "") {
|
|
|
|
return;
|
|
|
|
} else if (type === "success") {
|
|
|
|
message.success(text);
|
|
|
|
} else if (type === "error") {
|
|
|
|
message.error(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 17:34:32 +08:00
|
|
|
export function isAdminUser(account) {
|
2021-03-28 16:35:59 +08:00
|
|
|
if (account === undefined || account === null) {
|
2021-03-21 00:44:19 +08:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-15 22:14:19 +08:00
|
|
|
return account.owner === "built-in" || account.isGlobalAdmin === true;
|
2021-02-13 17:34:32 +08:00
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
export function deepCopy(obj) {
|
|
|
|
return Object.assign({}, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function addRow(array, row) {
|
|
|
|
return [...array, row];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function prependRow(array, row) {
|
|
|
|
return [row, ...array];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteRow(array, i) {
|
|
|
|
// return array = array.slice(0, i).concat(array.slice(i + 1));
|
|
|
|
return [...array.slice(0, i), ...array.slice(i + 1)];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function swapRow(array, i, j) {
|
|
|
|
return [...array.slice(0, i), array[j], ...array.slice(i + 1, j), array[i], ...array.slice(j + 1)];
|
|
|
|
}
|
|
|
|
|
2021-05-24 20:48:04 +08:00
|
|
|
export function trim(str, ch) {
|
|
|
|
if (str === undefined) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start = 0;
|
|
|
|
let end = str.length;
|
|
|
|
|
|
|
|
while(start < end && str[start] === ch)
|
|
|
|
++start;
|
|
|
|
|
|
|
|
while(end > start && str[end - 1] === ch)
|
|
|
|
--end;
|
|
|
|
|
|
|
|
return (start > 0 || end < str.length) ? str.substring(start, end) : str;
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
export function isMobile() {
|
|
|
|
// return getIsMobileView();
|
|
|
|
return isMobileDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFormattedDate(date) {
|
|
|
|
if (date === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
date = date.replace('T', ' ');
|
|
|
|
date = date.replace('+08:00', ' ');
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getFormattedDateShort(date) {
|
|
|
|
return date.slice(0, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getShortName(s) {
|
|
|
|
return s.split('/').slice(-1)[0];
|
|
|
|
}
|
|
|
|
|
2021-02-13 23:00:43 +08:00
|
|
|
export function getShortText(s, maxLength=35) {
|
|
|
|
if (s.length > maxLength) {
|
|
|
|
return `${s.slice(0, maxLength)}...`;
|
|
|
|
} else {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 12:15:19 +08:00
|
|
|
function getRandomInt(s) {
|
|
|
|
let hash = 0;
|
|
|
|
if (s.length !== 0) {
|
|
|
|
for (let i = 0; i < s.length; i ++) {
|
|
|
|
let char = s.charCodeAt(i);
|
|
|
|
hash = ((hash << 5) - hash) + char;
|
|
|
|
hash = hash & hash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAvatarColor(s) {
|
|
|
|
const colorList = ['#f56a00', '#7265e6', '#ffbf00', '#00a2ae'];
|
|
|
|
let random = getRandomInt(s);
|
|
|
|
if (random < 0) {
|
|
|
|
random = -random;
|
|
|
|
}
|
|
|
|
return colorList[random % 4];
|
|
|
|
}
|
2021-02-19 23:23:59 +08:00
|
|
|
|
|
|
|
export function setLanguage() {
|
|
|
|
let language = localStorage.getItem('language');
|
|
|
|
if (language === undefined) {
|
|
|
|
language = "en"
|
|
|
|
}
|
|
|
|
i18next.changeLanguage(language)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function changeLanguage(language) {
|
|
|
|
localStorage.setItem("language", language)
|
|
|
|
i18next.changeLanguage(language)
|
|
|
|
window.location.reload(true);
|
|
|
|
}
|
2021-03-14 23:08:08 +08:00
|
|
|
|
|
|
|
export function getClickable(text) {
|
|
|
|
return (
|
2021-03-27 11:38:15 +08:00
|
|
|
// eslint-disable-next-line jsx-a11y/anchor-is-valid
|
2021-03-14 23:08:08 +08:00
|
|
|
<a onClick={() => {
|
|
|
|
copy(text);
|
|
|
|
showMessage("success", `Copied to clipboard`);
|
|
|
|
}}>
|
|
|
|
{text}
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
2021-04-18 23:14:46 +08:00
|
|
|
|
2021-04-19 01:14:41 +08:00
|
|
|
export function getProviderLogo(provider) {
|
|
|
|
const idp = provider.type.toLowerCase();
|
2021-06-12 11:52:59 +08:00
|
|
|
const url = `${StaticBaseUrl}/img/social_${idp}.png`;
|
2021-04-18 23:14:46 +08:00
|
|
|
return (
|
|
|
|
<img width={30} height={30} src={url} alt={idp} />
|
|
|
|
)
|
|
|
|
}
|
2021-04-28 15:54:50 +08:00
|
|
|
|
|
|
|
export function renderLogo(application) {
|
|
|
|
if (application === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (application.homepageUrl !== "") {
|
|
|
|
return (
|
|
|
|
<a target="_blank" rel="noreferrer" href={application.homepageUrl}>
|
|
|
|
<img width={250} src={application.logo} alt={application.displayName} style={{marginBottom: '30px'}}/>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
<img width={250} src={application.logo} alt={application.displayName} style={{marginBottom: '30px'}}/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-04-28 22:40:21 +08:00
|
|
|
|
|
|
|
export function goToLogin(ths, application) {
|
|
|
|
if (application === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-14 23:23:59 +08:00
|
|
|
if (!application.enablePassword && window.location.pathname.includes("/signup/oauth/authorize")) {
|
|
|
|
const link = window.location.href.replace("/signup/oauth/authorize", "/login/oauth/authorize");
|
|
|
|
goToLink(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-28 22:40:21 +08:00
|
|
|
if (authConfig.appName === application.name) {
|
|
|
|
goToLinkSoft(ths, "/login");
|
|
|
|
} else {
|
2021-06-14 22:55:08 +08:00
|
|
|
if (application.signinUrl === "") {
|
|
|
|
goToLink(`${application.homepageUrl}/login`);
|
|
|
|
} else {
|
|
|
|
goToLink(application.signinUrl);
|
|
|
|
}
|
2021-04-28 22:40:21 +08:00
|
|
|
}
|
|
|
|
}
|
2021-04-29 21:28:24 +08:00
|
|
|
|
2021-05-03 00:48:02 +08:00
|
|
|
export function goToSignup(ths, application) {
|
|
|
|
if (application === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-14 23:23:59 +08:00
|
|
|
if (!application.enablePassword && window.location.pathname.includes("/login/oauth/authorize")) {
|
|
|
|
const link = window.location.href.replace("/login/oauth/authorize", "/signup/oauth/authorize");
|
|
|
|
goToLink(link);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-03 00:48:02 +08:00
|
|
|
if (authConfig.appName === application.name) {
|
|
|
|
goToLinkSoft(ths, "/signup");
|
|
|
|
} else {
|
|
|
|
if (application.signupUrl === "") {
|
|
|
|
goToLinkSoft(ths, `/signup/${application.name}`);
|
|
|
|
} else {
|
|
|
|
goToLink(application.signupUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function goToForget(ths, application) {
|
|
|
|
if (application === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authConfig.appName === application.name) {
|
|
|
|
goToLinkSoft(ths, "/forget");
|
|
|
|
} else {
|
2021-06-04 01:21:53 +08:00
|
|
|
if (application.forgetUrl === "") {
|
2021-05-03 00:48:02 +08:00
|
|
|
goToLinkSoft(ths, `/forget/${application.name}`);
|
|
|
|
} else {
|
|
|
|
goToLink(application.forgetUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-29 21:28:24 +08:00
|
|
|
export function renderHelmet(application) {
|
|
|
|
if (application === undefined || application === null || application.organizationObj === undefined || application.organizationObj === null ||application.organizationObj === "") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Helmet>
|
|
|
|
<title>{application.organizationObj.displayName}</title>
|
|
|
|
<link rel="icon" href={application.organizationObj.favicon} />
|
|
|
|
</Helmet>
|
|
|
|
)
|
|
|
|
}
|