Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: Profile forms support read-only logic

haitaoo7445dbc3369e
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts
index 9da8d699..f10575a2 100644
--- a/ui/src/common/interface.ts
+++ b/ui/src/common/interface.ts
@@ -326,6 +326,16 @@ export interface AdminSettingsSmtp {
test_email_recipient?: string;
}
+export interface AdminSettingsUsers {
+ allow_update_avatar: boolean;
+ allow_update_bio: boolean;
+ allow_update_display_name: boolean;
+ allow_update_location: boolean;
+ allow_update_username: boolean;
+ allow_update_website: boolean;
+ default_avatar: string;
+}
+
export interface SiteSettings {
branding: AdminSettingBranding;
general: AdminSettingsGeneral;
@@ -334,6 +344,7 @@ export interface SiteSettings {
custom_css_html: AdminSettingsCustom;
theme: AdminSettingsTheme;
site_seo: AdminSettingsSeo;
+ site_users: AdminSettingsUsers;
version: string;
revision: string;
}
diff --git a/ui/src/components/UploadImg/index.tsx b/ui/src/components/UploadImg/index.tsx
index f28a9ecc..07dde645 100644
--- a/ui/src/components/UploadImg/index.tsx
+++ b/ui/src/components/UploadImg/index.tsx
@@ -9,6 +9,7 @@ interface IProps {
className?: string;
children?: React.ReactNode;
acceptType?: string;
+ disabled?: boolean;
uploadCallback: (img: string) => void;
}
@@ -18,6 +19,7 @@ const Index: React.FC<IProps> = ({
children,
acceptType = '',
className,
+ disabled = false,
}) => {
const { t } = useTranslation();
const [status, setStatus] = useState(false);
@@ -52,6 +54,7 @@ const Index: React.FC<IProps> = ({
<input
type="file"
className="d-none"
+ disabled={disabled}
accept={`image/jpeg,image/jpg,image/png,image/webp${acceptType}`}
onChange={onChange}
/>
diff --git a/ui/src/pages/Admin/SettingsUsers/index.tsx b/ui/src/pages/Admin/SettingsUsers/index.tsx
index 0ab65c75..b8da8529 100644
--- a/ui/src/pages/Admin/SettingsUsers/index.tsx
+++ b/ui/src/pages/Admin/SettingsUsers/index.tsx
@@ -12,13 +12,14 @@ import {
} from '@/services';
import { handleFormError } from '@/utils';
import * as Type from '@/common/interface';
+import { siteInfoStore } from '@/stores';
const Index: FC = () => {
const { t } = useTranslation('translation', {
keyPrefix: 'admin.settings_users',
});
const Toast = useToast();
-
+ const { updateUsers: updateUsersStore } = siteInfoStore();
const schema: JSONSchema = {
title: t('title'),
properties: {
@@ -129,6 +130,7 @@ const Index: FC = () => {
};
putUsersSetting(reqParams)
.then(() => {
+ updateUsersStore(reqParams);
Toast.onShow({
msg: t('update', { keyPrefix: 'toast' }),
variant: 'success',
diff --git a/ui/src/pages/Users/Settings/Profile/index.tsx b/ui/src/pages/Users/Settings/Profile/index.tsx
index 8728ba01..ae1ae01e 100644
--- a/ui/src/pages/Users/Settings/Profile/index.tsx
+++ b/ui/src/pages/Users/Settings/Profile/index.tsx
@@ -6,9 +6,14 @@ import MD5 from 'md5';
import type { FormDataType } from '@/common/interface';
import { UploadImg, Avatar, Icon } from '@/components';
-import { loggedUserInfoStore, userCenterStore } from '@/stores';
+import { loggedUserInfoStore, userCenterStore, siteInfoStore } from '@/stores';
import { useToast } from '@/hooks';
-import { modifyUserInfo, getLoggedUserInfo, getUcSettings } from '@/services';
+import {
+ modifyUserInfo,
+ getLoggedUserInfo,
+ getUcSettings,
+ UcSettingAgent,
+} from '@/services';
import { handleFormError } from '@/utils';
const Index: React.FC = () => {
@@ -18,9 +23,10 @@ const Index: React.FC = () => {
const toast = useToast();
const { user, update } = loggedUserInfoStore();
const { agent: ucAgent } = userCenterStore();
+ const { users: usersSetting } = siteInfoStore();
const [mailHash, setMailHash] = useState('');
const [count] = useState(0);
- const [profileAgent, setProfileAgent] = useState('');
+ const [profileAgent, setProfileAgent] = useState<UcSettingAgent>();
const [formData, setFormData] = useState<FormDataType>({
display_name: {
@@ -248,11 +254,9 @@ const Index: React.FC = () => {
const initData = () => {
if (ucAgent?.enabled) {
getUcSettings().then((resp) => {
- if (
- resp.profile_setting_agent?.enabled &&
- resp.profile_setting_agent?.redirect_url
- ) {
- setProfileAgent(resp.profile_setting_agent.redirect_url);
+ setProfileAgent(resp.profile_setting_agent);
+ if (resp.profile_setting_agent?.enabled === false) {
+ getProfile();
}
});
} else {
@@ -266,16 +270,19 @@ const Index: React.FC = () => {
return (
<>
<h3 className="mb-4">{t('heading')}</h3>
- {profileAgent ? (
- <a href={profileAgent}>{t('goto_modify', { keyPrefix: 'settings' })}</a>
+ {profileAgent?.enabled && profileAgent?.redirect_url ? (
+ <a href={profileAgent.redirect_url}>
+ {t('goto_modify', { keyPrefix: 'settings' })}
+ </a>
) : null}
- {!ucAgent?.enabled ? (
+ {!ucAgent?.enabled || profileAgent?.enabled === false ? (
<Form noValidate onSubmit={handleSubmit}>
<Form.Group controlId="displayName" className="mb-3">
<Form.Label>{t('display_name.label')}</Form.Label>
<Form.Control
required
type="text"
+ readOnly={!usersSetting.allow_update_display_name}
value={formData.display_name.value}
isInvalid={formData.display_name.isInvalid}
onChange={(e) =>
@@ -298,6 +305,7 @@ const Index: React.FC = () => {
<Form.Control
required
type="text"
+ readOnly={!usersSetting.allow_update_username}
value={formData.username.value}
isInvalid={formData.username.isInvalid}
onChange={(e) =>
@@ -321,6 +329,7 @@ const Index: React.FC = () => {
<div className="mb-3">
<Form.Select
name="avatar.type"
+ disabled={!usersSetting.allow_update_avatar}
value={formData.avatar.type}
onChange={handleAvatarChange}>
<option value="gravatar" key="gravatar">
@@ -369,11 +378,15 @@ const Index: React.FC = () => {
className="me-2 bg-gray-300 "
/>
<ButtonGroup vertical className="fit-content">
- <UploadImg type="avatar" uploadCallback={avatarUpload}>
+ <UploadImg
+ type="avatar"
+ disabled={!usersSetting.allow_update_avatar}
+ uploadCallback={avatarUpload}>
<Icon name="cloud-upload" />
</UploadImg>
<Button
variant="outline-secondary"
+ disabled={!usersSetting.allow_update_avatar}
onClick={removeCustomAvatar}>
<Icon name="trash" />
</Button>
@@ -410,6 +423,7 @@ const Index: React.FC = () => {
required
as="textarea"
rows={5}
+ readOnly={!usersSetting.allow_update_bio}
value={formData.bio.value}
isInvalid={formData.bio.isInvalid}
onChange={(e) =>
@@ -435,6 +449,7 @@ const Index: React.FC = () => {
required
type="url"
placeholder={t('website.placeholder')}
+ readOnly={!usersSetting.allow_update_website}
value={formData.website.value}
isInvalid={formData.website.isInvalid}
onChange={(e) =>
@@ -460,6 +475,7 @@ const Index: React.FC = () => {
required
type="text"
placeholder={t('location.placeholder')}
+ readOnly={!usersSetting.allow_update_location}
value={formData.location.value}
isInvalid={formData.location.isInvalid}
onChange={(e) =>
diff --git a/ui/src/stores/siteInfo.ts b/ui/src/stores/siteInfo.ts
index 1072501a..0b263bad 100644
--- a/ui/src/stores/siteInfo.ts
+++ b/ui/src/stores/siteInfo.ts
@@ -1,6 +1,6 @@
import create from 'zustand';
-import { AdminSettingsGeneral } from '@/common/interface';
+import { AdminSettingsGeneral, AdminSettingsUsers } from '@/common/interface';
import { DEFAULT_SITE_NAME } from '@/common/constants';
interface SiteInfoType {
@@ -9,6 +9,8 @@ interface SiteInfoType {
revision: string;
update: (params: AdminSettingsGeneral) => void;
updateVersion: (ver: string, revision: string) => void;
+ users: AdminSettingsUsers;
+ updateUsers: (users: SiteInfoType['users']) => void;
}
const siteInfo = create<SiteInfoType>((set) => ({
@@ -20,6 +22,15 @@ const siteInfo = create<SiteInfoType>((set) => ({
contact_email: '',
permalink: 1,
},
+ users: {
+ allow_update_avatar: false,
+ allow_update_bio: false,
+ allow_update_display_name: false,
+ allow_update_location: false,
+ allow_update_username: false,
+ allow_update_website: false,
+ default_avatar: 'system',
+ },
version: '',
revision: '',
update: (params) =>
@@ -37,6 +48,11 @@ const siteInfo = create<SiteInfoType>((set) => ({
return { version: ver, revision };
});
},
+ updateUsers: (users) => {
+ set(() => {
+ return { users };
+ });
+ },
}));
export default siteInfo;
diff --git a/ui/src/utils/guard.ts b/ui/src/utils/guard.ts
index 84511e62..a1ec5603 100644
--- a/ui/src/utils/guard.ts
+++ b/ui/src/utils/guard.ts
@@ -371,6 +371,7 @@ export const initAppSettingsStore = async () => {
siteInfoStore
.getState()
.updateVersion(appSettings.version, appSettings.revision);
+ siteInfoStore.getState().updateUsers(appSettings.site_users);
interfaceStore.getState().update(appSettings.interface);
brandingStore.getState().update(appSettings.branding);
loginSettingStore.getState().update(appSettings.login);