feat: impl delete user apiv2

This commit is contained in:
Steven
2023-08-22 09:15:27 +08:00
parent 1c58702716
commit 34f8a97309
11 changed files with 609 additions and 165 deletions

View File

@ -14,6 +14,7 @@ func isUnauthorizeAllowedMethod(methodName string) bool {
var allowedMethodsOnlyForAdmin = map[string]bool{
"/slash.api.v2.UserService/CreateUser": true,
"/slash.api.v2.UserService/DeleteUser": true,
}
// isOnlyForAdminAllowedMethod returns true if the method is allowed to be called only by admin.

View File

@ -86,6 +86,22 @@ func (s *UserService) CreateUser(ctx context.Context, request *apiv2pb.CreateUse
return response, nil
}
func (s *UserService) DeleteUser(ctx context.Context, request *apiv2pb.DeleteUserRequest) (*apiv2pb.DeleteUserResponse, error) {
userID := ctx.Value(UserIDContextKey).(int32)
if userID == request.Id {
return nil, status.Errorf(codes.InvalidArgument, "cannot delete self")
}
err := s.Store.DeleteUser(ctx, &store.DeleteUser{
ID: request.Id,
})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to delete user: %v", err)
}
response := &apiv2pb.DeleteUserResponse{}
return response, nil
}
func (s *UserService) ListUserAccessTokens(ctx context.Context, request *apiv2pb.ListUserAccessTokensRequest) (*apiv2pb.ListUserAccessTokensResponse, error) {
userID := ctx.Value(UserIDContextKey).(int32)
if userID != request.Id {