mirror of
https://github.com/aykhans/slash-e.git
synced 2025-04-20 22:07:15 +00:00
chore: update get workspace setting
This commit is contained in:
parent
0af4903657
commit
e82e61d54d
@ -2,7 +2,9 @@ package v2
|
||||
|
||||
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.
|
||||
func isUnauthorizeAllowedMethod(methodName string) bool {
|
||||
|
@ -24,6 +24,17 @@ func NewWorkspaceSettingService(store *store.Store) *WorkspaceSettingService {
|
||||
}
|
||||
|
||||
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{})
|
||||
if err != nil {
|
||||
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 {
|
||||
if v.Key == storepb.WorkspaceSettingKey_WORKSAPCE_SETTING_ENABLE_SIGNUP {
|
||||
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 {
|
||||
workspaceSetting.CustomStyle = v.GetCustomStyle()
|
||||
} else if v.Key == storepb.WorkspaceSettingKey_WORKSPACE_SETTING_CUSTOM_SCRIPT {
|
||||
workspaceSetting.CustomScript = v.GetCustomScript()
|
||||
} else {
|
||||
return nil, status.Errorf(codes.Internal, "invalid workspace setting key: %s", v.Key.String())
|
||||
} else if isAdmin {
|
||||
// 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{
|
||||
|
@ -1,11 +1,14 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Outlet } from "react-router-dom";
|
||||
import DemoBanner from "./components/DemoBanner";
|
||||
import { workspaceSettingServiceClient } from "./grpcweb";
|
||||
import { globalService } from "./services";
|
||||
import useUserStore from "./stores/v1/user";
|
||||
import { WorkspaceSetting } from "./types/proto/api/v2/workspace_setting_service";
|
||||
|
||||
function App() {
|
||||
const userStore = useUserStore();
|
||||
const [workspaceSetting, setWorkspaceSetting] = useState<WorkspaceSetting>(WorkspaceSetting.fromPartial({}));
|
||||
const [loading, setLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
@ -16,6 +19,15 @@ function App() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
try {
|
||||
const { setting } = await workspaceSettingServiceClient.getWorkspaceSetting({});
|
||||
if (setting) {
|
||||
setWorkspaceSetting(setting);
|
||||
}
|
||||
} catch (error) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
try {
|
||||
await userStore.fetchCurrentUser();
|
||||
} catch (error) {
|
||||
@ -28,6 +40,13 @@ function App() {
|
||||
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 ? (
|
||||
<>
|
||||
<DemoBanner />
|
||||
|
Loading…
x
Reference in New Issue
Block a user