workspace_service.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package v2
  2. import (
  3. "context"
  4. "strconv"
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
  8. "github.com/usememos/memos/store"
  9. )
  10. func (s *APIV2Service) GetWorkspaceProfile(_ context.Context, _ *apiv2pb.GetWorkspaceProfileRequest) (*apiv2pb.GetWorkspaceProfileResponse, error) {
  11. workspaceProfile := &apiv2pb.WorkspaceProfile{
  12. Version: s.Profile.Version,
  13. Mode: s.Profile.Mode,
  14. }
  15. response := &apiv2pb.GetWorkspaceProfileResponse{
  16. WorkspaceProfile: workspaceProfile,
  17. }
  18. return response, nil
  19. }
  20. func (s *APIV2Service) UpdateWorkspaceProfile(ctx context.Context, request *apiv2pb.UpdateWorkspaceProfileRequest) (*apiv2pb.UpdateWorkspaceProfileResponse, error) {
  21. user, err := getCurrentUser(ctx, s.Store)
  22. if err != nil {
  23. return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
  24. }
  25. if user.Role != store.RoleHost {
  26. return nil, status.Errorf(codes.PermissionDenied, "permission denied")
  27. }
  28. if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
  29. return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
  30. }
  31. // Update system settings.
  32. for _, field := range request.UpdateMask.Paths {
  33. if field == "allow_registration" {
  34. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  35. Name: "allow-signup",
  36. Value: strconv.FormatBool(request.WorkspaceProfile.AllowRegistration),
  37. })
  38. if err != nil {
  39. return nil, status.Errorf(codes.Internal, "failed to update allow_registration system setting: %v", err)
  40. }
  41. } else if field == "disable_password_login" {
  42. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  43. Name: "disable-password-login",
  44. Value: strconv.FormatBool(request.WorkspaceProfile.DisablePasswordLogin),
  45. })
  46. if err != nil {
  47. return nil, status.Errorf(codes.Internal, "failed to update disable_password_login system setting: %v", err)
  48. }
  49. } else if field == "additional_script" {
  50. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  51. Name: "additional-script",
  52. Value: request.WorkspaceProfile.AdditionalScript,
  53. })
  54. if err != nil {
  55. return nil, status.Errorf(codes.Internal, "failed to update additional_script system setting: %v", err)
  56. }
  57. } else if field == "additional_style" {
  58. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  59. Name: "additional-style",
  60. Value: request.WorkspaceProfile.AdditionalStyle,
  61. })
  62. if err != nil {
  63. return nil, status.Errorf(codes.Internal, "failed to update additional_style system setting: %v", err)
  64. }
  65. }
  66. }
  67. workspaceProfileMessage, err := s.GetWorkspaceProfile(ctx, &apiv2pb.GetWorkspaceProfileRequest{})
  68. if err != nil {
  69. return nil, status.Errorf(codes.Internal, "failed to get system info: %v", err)
  70. }
  71. return &apiv2pb.UpdateWorkspaceProfileResponse{
  72. WorkspaceProfile: workspaceProfileMessage.WorkspaceProfile,
  73. }, nil
  74. }