memos/api/v2/system_service.go

93 lines
3.1 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"
)
2023-10-27 09:07:35 +08:00
func (s *APIV2Service) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemInfoRequest) (*apiv2pb.GetSystemInfoResponse, error) {
defaultSystemInfo := &apiv2pb.SystemInfo{}
// Get the database size if the user is a host.
2023-09-14 20:16:17 +08:00
currentUser, err := getCurrentUser(ctx, s.Store)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get current user: %v", err)
}
if currentUser != nil && currentUser.Role == store.RoleHost {
size, err := s.Store.GetCurrentDBSize(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get db size: %v", err)
}
defaultSystemInfo.DbSize = size
}
response := &apiv2pb.GetSystemInfoResponse{
SystemInfo: defaultSystemInfo,
}
return response, nil
}
2023-09-06 21:54:12 +08:00
2023-10-27 09:07:35 +08:00
func (s *APIV2Service) UpdateSystemInfo(ctx context.Context, request *apiv2pb.UpdateSystemInfoRequest) (*apiv2pb.UpdateSystemInfoResponse, 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",
Value: strconv.FormatBool(request.SystemInfo.AllowRegistration),
})
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",
Value: strconv.FormatBool(request.SystemInfo.DisablePasswordLogin),
})
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",
Value: request.SystemInfo.AdditionalScript,
})
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",
Value: request.SystemInfo.AdditionalStyle,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update additional_style system setting: %v", err)
}
}
}
systemInfo, err := s.GetSystemInfo(ctx, &apiv2pb.GetSystemInfoRequest{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get system info: %v", err)
}
return &apiv2pb.UpdateSystemInfoResponse{
SystemInfo: systemInfo.SystemInfo,
}, nil
}