system_service.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemInfoRequest) (*apiv2pb.GetSystemInfoResponse, error) {
  11. defaultSystemInfo := &apiv2pb.SystemInfo{}
  12. // Get the database size if the user is a host.
  13. currentUser, err := getCurrentUser(ctx, s.Store)
  14. if err != nil {
  15. return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
  16. }
  17. if currentUser != nil && currentUser.Role == store.RoleHost {
  18. size, err := s.Store.GetCurrentDBSize(ctx)
  19. if err != nil {
  20. return nil, status.Errorf(codes.Internal, "failed to get db size: %v", err)
  21. }
  22. defaultSystemInfo.DbSize = size
  23. }
  24. response := &apiv2pb.GetSystemInfoResponse{
  25. SystemInfo: defaultSystemInfo,
  26. }
  27. return response, nil
  28. }
  29. func (s *APIV2Service) UpdateSystemInfo(ctx context.Context, request *apiv2pb.UpdateSystemInfoRequest) (*apiv2pb.UpdateSystemInfoResponse, error) {
  30. user, err := getCurrentUser(ctx, s.Store)
  31. if err != nil {
  32. return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
  33. }
  34. if user.Role != store.RoleHost {
  35. return nil, status.Errorf(codes.PermissionDenied, "permission denied")
  36. }
  37. if request.UpdateMask == nil || len(request.UpdateMask.Paths) == 0 {
  38. return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
  39. }
  40. // Update system settings.
  41. for _, path := range request.UpdateMask.Paths {
  42. if path == "allow_registration" {
  43. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  44. Name: "allow-signup",
  45. Value: strconv.FormatBool(request.SystemInfo.AllowRegistration),
  46. })
  47. if err != nil {
  48. return nil, status.Errorf(codes.Internal, "failed to update allow_registration system setting: %v", err)
  49. }
  50. } else if path == "disable_password_login" {
  51. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  52. Name: "disable-password-login",
  53. Value: strconv.FormatBool(request.SystemInfo.DisablePasswordLogin),
  54. })
  55. if err != nil {
  56. return nil, status.Errorf(codes.Internal, "failed to update disable_password_login system setting: %v", err)
  57. }
  58. } else if path == "additional_script" {
  59. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  60. Name: "additional-script",
  61. Value: request.SystemInfo.AdditionalScript,
  62. })
  63. if err != nil {
  64. return nil, status.Errorf(codes.Internal, "failed to update additional_script system setting: %v", err)
  65. }
  66. } else if path == "additional_style" {
  67. _, err := s.Store.UpsertSystemSetting(ctx, &store.SystemSetting{
  68. Name: "additional-style",
  69. Value: request.SystemInfo.AdditionalStyle,
  70. })
  71. if err != nil {
  72. return nil, status.Errorf(codes.Internal, "failed to update additional_style system setting: %v", err)
  73. }
  74. }
  75. }
  76. systemInfo, err := s.GetSystemInfo(ctx, &apiv2pb.GetSystemInfoRequest{})
  77. if err != nil {
  78. return nil, status.Errorf(codes.Internal, "failed to get system info: %v", err)
  79. }
  80. return &apiv2pb.UpdateSystemInfoResponse{
  81. SystemInfo: systemInfo.SystemInfo,
  82. }, nil
  83. }