Unverified Commit 8b1f7c52 authored by boojack's avatar boojack Committed by GitHub

choer: add system setting api (#2194)

parent 9987337e
......@@ -3,6 +3,7 @@ package v2
import (
"context"
"os"
"strconv"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/server/profile"
......@@ -53,3 +54,64 @@ func (s *SystemService) GetSystemInfo(ctx context.Context, _ *apiv2pb.GetSystemI
}
return response, nil
}
func (s *SystemService) UpdateSystemInfo(ctx context.Context, request *apiv2pb.UpdateSystemInfoRequest) (*apiv2pb.UpdateSystemInfoResponse, error) {
userID := ctx.Value(UserIDContextKey).(int32)
user, err := s.Store.GetUser(ctx, &store.FindUser{
ID: &userID,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get user: %v", err)
}
if user.Role != store.RoleHost {
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}
if request.UpdateMask == nil || len(request.UpdateMask) == 0 {
return nil, status.Errorf(codes.InvalidArgument, "update mask is required")
}
// Update system settings.
for _, path := range request.UpdateMask {
if path == "allow_registration" {
_, 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)
}
} else if path == "disable_password_login" {
_, 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)
}
} else if path == "additional_script" {
_, 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)
}
} else if path == "additional_style" {
_, 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
}
......@@ -10,6 +10,9 @@ service SystemService {
rpc GetSystemInfo(GetSystemInfoRequest) returns (GetSystemInfoResponse) {
option (google.api.http) = {get: "/api/v2/system/info"};
}
rpc UpdateSystemInfo(UpdateSystemInfoRequest) returns (UpdateSystemInfoResponse) {
option (google.api.http) = {post: "/api/v2/system/info"};
}
}
message SystemInfo {
......@@ -27,3 +30,14 @@ message GetSystemInfoRequest {}
message GetSystemInfoResponse {
SystemInfo system_info = 1;
}
message UpdateSystemInfoRequest {
// System info is the updated data.
SystemInfo system_info = 1;
// Update mask is the array of paths.
repeated string update_mask = 2;
}
message UpdateSystemInfoResponse {
SystemInfo system_info = 1;
}
......@@ -21,6 +21,8 @@
- [GetSystemInfoRequest](#memos-api-v2-GetSystemInfoRequest)
- [GetSystemInfoResponse](#memos-api-v2-GetSystemInfoResponse)
- [SystemInfo](#memos-api-v2-SystemInfo)
- [UpdateSystemInfoRequest](#memos-api-v2-UpdateSystemInfoRequest)
- [UpdateSystemInfoResponse](#memos-api-v2-UpdateSystemInfoResponse)
- [SystemService](#memos-api-v2-SystemService)
......@@ -254,6 +256,37 @@
<a name="memos-api-v2-UpdateSystemInfoRequest"></a>
### UpdateSystemInfoRequest
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| system_info | [SystemInfo](#memos-api-v2-SystemInfo) | | System info is the updated data. |
| update_mask | [string](#string) | repeated | Update mask is the array of paths. |
<a name="memos-api-v2-UpdateSystemInfoResponse"></a>
### UpdateSystemInfoResponse
| Field | Type | Label | Description |
| ----- | ---- | ----- | ----------- |
| system_info | [SystemInfo](#memos-api-v2-SystemInfo) | | |
......@@ -269,6 +302,7 @@
| Method Name | Request Type | Response Type | Description |
| ----------- | ------------ | ------------- | ------------|
| GetSystemInfo | [GetSystemInfoRequest](#memos-api-v2-GetSystemInfoRequest) | [GetSystemInfoResponse](#memos-api-v2-GetSystemInfoResponse) | |
| UpdateSystemInfo | [UpdateSystemInfoRequest](#memos-api-v2-UpdateSystemInfoRequest) | [UpdateSystemInfoResponse](#memos-api-v2-UpdateSystemInfoResponse) | |
......
This diff is collapsed.
......@@ -49,6 +49,42 @@ func local_request_SystemService_GetSystemInfo_0(ctx context.Context, marshaler
}
var (
filter_SystemService_UpdateSystemInfo_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)}
)
func request_SystemService_UpdateSystemInfo_0(ctx context.Context, marshaler runtime.Marshaler, client SystemServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq UpdateSystemInfoRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_SystemService_UpdateSystemInfo_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := client.UpdateSystemInfo(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD))
return msg, metadata, err
}
func local_request_SystemService_UpdateSystemInfo_0(ctx context.Context, marshaler runtime.Marshaler, server SystemServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) {
var protoReq UpdateSystemInfoRequest
var metadata runtime.ServerMetadata
if err := req.ParseForm(); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_SystemService_UpdateSystemInfo_0); err != nil {
return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err)
}
msg, err := server.UpdateSystemInfo(ctx, &protoReq)
return msg, metadata, err
}
// RegisterSystemServiceHandlerServer registers the http handlers for service SystemService to "mux".
// UnaryRPC :call SystemServiceServer directly.
// StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906.
......@@ -80,6 +116,31 @@ func RegisterSystemServiceHandlerServer(ctx context.Context, mux *runtime.ServeM
})
mux.Handle("POST", pattern_SystemService_UpdateSystemInfo_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
var stream runtime.ServerTransportStream
ctx = grpc.NewContextWithServerTransportStream(ctx, &stream)
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/memos.api.v2.SystemService/UpdateSystemInfo", runtime.WithHTTPPathPattern("/api/v2/system/info"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := local_request_SystemService_UpdateSystemInfo_0(annotatedContext, inboundMarshaler, server, req, pathParams)
md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer())
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_SystemService_UpdateSystemInfo_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
......@@ -143,13 +204,39 @@ func RegisterSystemServiceHandlerClient(ctx context.Context, mux *runtime.ServeM
})
mux.Handle("POST", pattern_SystemService_UpdateSystemInfo_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) {
ctx, cancel := context.WithCancel(req.Context())
defer cancel()
inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req)
var err error
var annotatedContext context.Context
annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/memos.api.v2.SystemService/UpdateSystemInfo", runtime.WithHTTPPathPattern("/api/v2/system/info"))
if err != nil {
runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err)
return
}
resp, md, err := request_SystemService_UpdateSystemInfo_0(annotatedContext, inboundMarshaler, client, req, pathParams)
annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md)
if err != nil {
runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err)
return
}
forward_SystemService_UpdateSystemInfo_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...)
})
return nil
}
var (
pattern_SystemService_GetSystemInfo_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"api", "v2", "system", "info"}, ""))
pattern_SystemService_UpdateSystemInfo_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2, 2, 3}, []string{"api", "v2", "system", "info"}, ""))
)
var (
forward_SystemService_GetSystemInfo_0 = runtime.ForwardResponseMessage
forward_SystemService_UpdateSystemInfo_0 = runtime.ForwardResponseMessage
)
......@@ -19,7 +19,8 @@ import (
const _ = grpc.SupportPackageIsVersion7
const (
SystemService_GetSystemInfo_FullMethodName = "/memos.api.v2.SystemService/GetSystemInfo"
SystemService_GetSystemInfo_FullMethodName = "/memos.api.v2.SystemService/GetSystemInfo"
SystemService_UpdateSystemInfo_FullMethodName = "/memos.api.v2.SystemService/UpdateSystemInfo"
)
// SystemServiceClient is the client API for SystemService service.
......@@ -27,6 +28,7 @@ const (
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
type SystemServiceClient interface {
GetSystemInfo(ctx context.Context, in *GetSystemInfoRequest, opts ...grpc.CallOption) (*GetSystemInfoResponse, error)
UpdateSystemInfo(ctx context.Context, in *UpdateSystemInfoRequest, opts ...grpc.CallOption) (*UpdateSystemInfoResponse, error)
}
type systemServiceClient struct {
......@@ -46,11 +48,21 @@ func (c *systemServiceClient) GetSystemInfo(ctx context.Context, in *GetSystemIn
return out, nil
}
func (c *systemServiceClient) UpdateSystemInfo(ctx context.Context, in *UpdateSystemInfoRequest, opts ...grpc.CallOption) (*UpdateSystemInfoResponse, error) {
out := new(UpdateSystemInfoResponse)
err := c.cc.Invoke(ctx, SystemService_UpdateSystemInfo_FullMethodName, in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// SystemServiceServer is the server API for SystemService service.
// All implementations must embed UnimplementedSystemServiceServer
// for forward compatibility
type SystemServiceServer interface {
GetSystemInfo(context.Context, *GetSystemInfoRequest) (*GetSystemInfoResponse, error)
UpdateSystemInfo(context.Context, *UpdateSystemInfoRequest) (*UpdateSystemInfoResponse, error)
mustEmbedUnimplementedSystemServiceServer()
}
......@@ -61,6 +73,9 @@ type UnimplementedSystemServiceServer struct {
func (UnimplementedSystemServiceServer) GetSystemInfo(context.Context, *GetSystemInfoRequest) (*GetSystemInfoResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetSystemInfo not implemented")
}
func (UnimplementedSystemServiceServer) UpdateSystemInfo(context.Context, *UpdateSystemInfoRequest) (*UpdateSystemInfoResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UpdateSystemInfo not implemented")
}
func (UnimplementedSystemServiceServer) mustEmbedUnimplementedSystemServiceServer() {}
// UnsafeSystemServiceServer may be embedded to opt out of forward compatibility for this service.
......@@ -92,6 +107,24 @@ func _SystemService_GetSystemInfo_Handler(srv interface{}, ctx context.Context,
return interceptor(ctx, in, info, handler)
}
func _SystemService_UpdateSystemInfo_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UpdateSystemInfoRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(SystemServiceServer).UpdateSystemInfo(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: SystemService_UpdateSystemInfo_FullMethodName,
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(SystemServiceServer).UpdateSystemInfo(ctx, req.(*UpdateSystemInfoRequest))
}
return interceptor(ctx, in, info, handler)
}
// SystemService_ServiceDesc is the grpc.ServiceDesc for SystemService service.
// It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy)
......@@ -103,6 +136,10 @@ var SystemService_ServiceDesc = grpc.ServiceDesc{
MethodName: "GetSystemInfo",
Handler: _SystemService_GetSystemInfo_Handler,
},
{
MethodName: "UpdateSystemInfo",
Handler: _SystemService_UpdateSystemInfo_Handler,
},
},
Streams: []grpc.StreamDesc{},
Metadata: "api/v2/system_service.proto",
......
......@@ -103,3 +103,60 @@ export declare class GetSystemInfoResponse extends Message<GetSystemInfoResponse
static equals(a: GetSystemInfoResponse | PlainMessage<GetSystemInfoResponse> | undefined, b: GetSystemInfoResponse | PlainMessage<GetSystemInfoResponse> | undefined): boolean;
}
/**
* @generated from message memos.api.v2.UpdateSystemInfoRequest
*/
export declare class UpdateSystemInfoRequest extends Message<UpdateSystemInfoRequest> {
/**
* System info is the updated data.
*
* @generated from field: memos.api.v2.SystemInfo system_info = 1;
*/
systemInfo?: SystemInfo;
/**
* Update mask is the array of paths.
*
* @generated from field: repeated string update_mask = 2;
*/
updateMask: string[];
constructor(data?: PartialMessage<UpdateSystemInfoRequest>);
static readonly runtime: typeof proto3;
static readonly typeName = "memos.api.v2.UpdateSystemInfoRequest";
static readonly fields: FieldList;
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): UpdateSystemInfoRequest;
static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): UpdateSystemInfoRequest;
static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): UpdateSystemInfoRequest;
static equals(a: UpdateSystemInfoRequest | PlainMessage<UpdateSystemInfoRequest> | undefined, b: UpdateSystemInfoRequest | PlainMessage<UpdateSystemInfoRequest> | undefined): boolean;
}
/**
* @generated from message memos.api.v2.UpdateSystemInfoResponse
*/
export declare class UpdateSystemInfoResponse extends Message<UpdateSystemInfoResponse> {
/**
* @generated from field: memos.api.v2.SystemInfo system_info = 1;
*/
systemInfo?: SystemInfo;
constructor(data?: PartialMessage<UpdateSystemInfoResponse>);
static readonly runtime: typeof proto3;
static readonly typeName = "memos.api.v2.UpdateSystemInfoResponse";
static readonly fields: FieldList;
static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): UpdateSystemInfoResponse;
static fromJson(jsonValue: JsonValue, options?: Partial<JsonReadOptions>): UpdateSystemInfoResponse;
static fromJsonString(jsonString: string, options?: Partial<JsonReadOptions>): UpdateSystemInfoResponse;
static equals(a: UpdateSystemInfoResponse | PlainMessage<UpdateSystemInfoResponse> | undefined, b: UpdateSystemInfoResponse | PlainMessage<UpdateSystemInfoResponse> | undefined): boolean;
}
......@@ -39,3 +39,24 @@ export const GetSystemInfoResponse = proto3.makeMessageType(
],
);
/**
* @generated from message memos.api.v2.UpdateSystemInfoRequest
*/
export const UpdateSystemInfoRequest = proto3.makeMessageType(
"memos.api.v2.UpdateSystemInfoRequest",
() => [
{ no: 1, name: "system_info", kind: "message", T: SystemInfo },
{ no: 2, name: "update_mask", kind: "scalar", T: 9 /* ScalarType.STRING */, repeated: true },
],
);
/**
* @generated from message memos.api.v2.UpdateSystemInfoResponse
*/
export const UpdateSystemInfoResponse = proto3.makeMessageType(
"memos.api.v2.UpdateSystemInfoResponse",
() => [
{ no: 1, name: "system_info", kind: "message", T: SystemInfo },
],
);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment