chore: update get workspace setting

This commit is contained in:
Steven 2023-09-20 23:33:04 +08:00
parent 0af4903657
commit e82e61d54d
3 changed files with 38 additions and 5 deletions

View File

@ -2,7 +2,9 @@ package v2
import "strings" import "strings"
var allowedMethodsWhenUnauthorized = map[string]bool{} var allowedMethodsWhenUnauthorized = map[string]bool{
"/slash.api.v2.WorkspaceSettingService/GetWorkspaceSetting": true,
}
// isUnauthorizeAllowedMethod returns true if the method is allowed to be called when the user is not authorized. // isUnauthorizeAllowedMethod returns true if the method is allowed to be called when the user is not authorized.
func isUnauthorizeAllowedMethod(methodName string) bool { func isUnauthorizeAllowedMethod(methodName string) bool {

View File

@ -24,6 +24,17 @@ func NewWorkspaceSettingService(store *store.Store) *WorkspaceSettingService {
} }
func (s *WorkspaceSettingService) GetWorkspaceSetting(ctx context.Context, _ *apiv2pb.GetWorkspaceSettingRequest) (*apiv2pb.GetWorkspaceSettingResponse, error) { func (s *WorkspaceSettingService) GetWorkspaceSetting(ctx context.Context, _ *apiv2pb.GetWorkspaceSettingRequest) (*apiv2pb.GetWorkspaceSettingResponse, error) {
isAdmin := false
userID, ok := ctx.Value(userIDContextKey).(int32)
if ok {
user, err := s.Store.GetUser(ctx, &store.FindUser{ID: &userID})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
}
if user.Role == store.RoleAdmin {
isAdmin = true
}
}
workspaceSettings, err := s.Store.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{}) workspaceSettings, err := s.Store.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{})
if err != nil { if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list workspace settings: %v", err) return nil, status.Errorf(codes.Internal, "failed to list workspace settings: %v", err)
@ -32,14 +43,15 @@ func (s *WorkspaceSettingService) GetWorkspaceSetting(ctx context.Context, _ *ap
for _, v := range workspaceSettings { for _, v := range workspaceSettings {
if v.Key == storepb.WorkspaceSettingKey_WORKSAPCE_SETTING_ENABLE_SIGNUP { if v.Key == storepb.WorkspaceSettingKey_WORKSAPCE_SETTING_ENABLE_SIGNUP {
workspaceSetting.EnableSignup = v.GetEnableSignup() workspaceSetting.EnableSignup = v.GetEnableSignup()
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_RESOURCE_RELATIVE_PATH {
workspaceSetting.ResourceRelativePath = v.GetResourceRelativePath()
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_CUSTOM_STYLE { } else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_CUSTOM_STYLE {
workspaceSetting.CustomStyle = v.GetCustomStyle() workspaceSetting.CustomStyle = v.GetCustomStyle()
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_CUSTOM_SCRIPT { } else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_CUSTOM_SCRIPT {
workspaceSetting.CustomScript = v.GetCustomScript() workspaceSetting.CustomScript = v.GetCustomScript()
} else { } else if isAdmin {
return nil, status.Errorf(codes.Internal, "invalid workspace setting key: %s", v.Key.String()) // For some settings, only admin can get the value.
if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_RESOURCE_RELATIVE_PATH {
workspaceSetting.ResourceRelativePath = v.GetResourceRelativePath()
}
} }
} }
return &apiv2pb.GetWorkspaceSettingResponse{ return &apiv2pb.GetWorkspaceSettingResponse{

View File

@ -1,11 +1,14 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Outlet } from "react-router-dom"; import { Outlet } from "react-router-dom";
import DemoBanner from "./components/DemoBanner"; import DemoBanner from "./components/DemoBanner";
import { workspaceSettingServiceClient } from "./grpcweb";
import { globalService } from "./services"; import { globalService } from "./services";
import useUserStore from "./stores/v1/user"; import useUserStore from "./stores/v1/user";
import { WorkspaceSetting } from "./types/proto/api/v2/workspace_setting_service";
function App() { function App() {
const userStore = useUserStore(); const userStore = useUserStore();
const [workspaceSetting, setWorkspaceSetting] = useState<WorkspaceSetting>(WorkspaceSetting.fromPartial({}));
const [loading, setLoading] = useState(true); const [loading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
@ -16,6 +19,15 @@ function App() {
// do nothing // do nothing
} }
try {
const { setting } = await workspaceSettingServiceClient.getWorkspaceSetting({});
if (setting) {
setWorkspaceSetting(setting);
}
} catch (error) {
// do nothing
}
try { try {
await userStore.fetchCurrentUser(); await userStore.fetchCurrentUser();
} catch (error) { } catch (error) {
@ -28,6 +40,13 @@ function App() {
initialState(); initialState();
}, []); }, []);
useEffect(() => {
const styleEl = document.createElement("style");
styleEl.innerHTML = workspaceSetting.customStyle;
styleEl.setAttribute("type", "text/css");
document.body.insertAdjacentElement("beforeend", styleEl);
}, [workspaceSetting.customStyle]);
return !loading ? ( return !loading ? (
<> <>
<DemoBanner /> <DemoBanner />