memos/api/v2/workspace_service.go

82 lines
2.9 KiB
Go
Raw Normal View History

package v2
import (
"context"
2023-09-06 21:54:12 +08:00
"strconv"
2023-09-17 22:55:13 +08:00
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/store"
)
2024-01-28 07:38:01 +08:00
func (s *APIV2Service) GetWorkspaceProfile(_ context.Context, _ *apiv2pb.GetWorkspaceProfileRequest) (*apiv2pb.GetWorkspaceProfileResponse, error) {
2024-01-28 07:35:42 +08:00
workspaceProfile := &apiv2pb.WorkspaceProfile{
Version: s.Profile.Version,
Mode: s.Profile.Mode,
2023-09-14 20:16:17 +08:00
}
2024-01-28 07:35:42 +08:00
response := &apiv2pb.GetWorkspaceProfileResponse{
WorkspaceProfile: workspaceProfile,
}
return response, nil
}
2023-09-06 21:54:12 +08:00
2024-01-28 07:35:42 +08:00
func (s *APIV2Service) UpdateWorkspaceProfile(ctx context.Context, request *apiv2pb.UpdateWorkspaceProfileRequest) (*apiv2pb.UpdateWorkspaceProfileResponse, error) {
2023-09-14 20:16:17 +08:00
user, err := getCurrentUser(ctx, s.Store)
2023-09-06 21:54:12 +08:00
if err != nil {
2023-09-14 20:16:17 +08:00
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
2023-09-06 21:54:12 +08:00
}
if user.Role != store.RoleHost {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
2023-10-21 12:41:55 +08:00
if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
2023-09-06 21:54:12 +08:00
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
}
// Update system settings.
2023-11-30 23:08:54 +08:00
for _, field := range request.UpdateMask.Paths {
if field == "allow_registration" {
2023-09-06 21:54:12 +08:00
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
Name: "allow-signup",
2024-01-28 07:35:42 +08:00
Value: strconv.FormatBool(request.WorkspaceProfile.AllowRegistration),
2023-09-06 21:54:12 +08:00
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update allow_registration system setting: %v", err)
}
2023-11-30 23:08:54 +08:00
} else if field == "disable_password_login" {
2023-09-06 21:54:12 +08:00
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
Name: "disable-password-login",
2024-01-28 07:35:42 +08:00
Value: strconv.FormatBool(request.WorkspaceProfile.DisablePasswordLogin),
2023-09-06 21:54:12 +08:00
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update disable_password_login system setting: %v", err)
}
2023-11-30 23:08:54 +08:00
} else if field == "additional_script" {
2023-09-06 21:54:12 +08:00
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
Name: "additional-script",
2024-01-28 07:35:42 +08:00
Value: request.WorkspaceProfile.AdditionalScript,
2023-09-06 21:54:12 +08:00
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update additional_script system setting: %v", err)
}
2023-11-30 23:08:54 +08:00
} else if field == "additional_style" {
2023-09-06 21:54:12 +08:00
_, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
Name: "additional-style",
2024-01-28 07:35:42 +08:00
Value: request.WorkspaceProfile.AdditionalStyle,
2023-09-06 21:54:12 +08:00
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update additional_style system setting: %v", err)
}
}
}
2024-01-28 07:35:42 +08:00
workspaceProfileMessage, err := s.GetWorkspaceProfile(ctx, &apiv2pb.GetWorkspaceProfileRequest{})
2023-09-06 21:54:12 +08:00
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get system info: %v", err)
}
2024-01-28 07:35:42 +08:00
return &apiv2pb.UpdateWorkspaceProfileResponse{
WorkspaceProfile: workspaceProfileMessage.WorkspaceProfile,
2023-09-06 21:54:12 +08:00
}, nil
}