memos/api/v2/workspace_service.go

91 lines
3.3 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" {
_, err := s.Store.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
2023-09-06 21:54:12 +08:00
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" {
if s.Profile.Mode == "demo" {
return nil, status.Errorf(codes.PermissionDenied, "disabling password login is not allowed in demo mode")
}
_, err := s.Store.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
2023-09-06 21:54:12 +08:00
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" {
if s.Profile.Mode == "demo" {
return nil, status.Errorf(codes.PermissionDenied, "additional script is not allowed in demo mode")
}
_, err := s.Store.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
2023-09-06 21:54:12 +08:00
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" {
if s.Profile.Mode == "demo" {
return nil, status.Errorf(codes.PermissionDenied, "additional style is not allowed in demo mode")
}
_, err := s.Store.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
2023-09-06 21:54:12 +08:00
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
}