merchant

Merchant backend to process payments, run by merchants
Log | Files | Refs | Submodules | README | LICENSE

taler-merchant-httpd_delete-management-instances-INSTANCE.c (5407B)


      1 /*
      2   This file is part of TALER
      3   (C) 2020-2026 Taler Systems SA
      4 
      5   TALER is free software; you can redistribute it and/or modify it under the
      6   terms of the GNU Affero General Public License as published by the Free Software
      7   Foundation; either version 3, or (at your option) any later version.
      8 
      9   TALER is distributed in the hope that it will be useful, but WITHOUT ANY
     10   WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11   A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12 
     13   You should have received a copy of the GNU General Public License along with
     14   TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15 */
     16 /**
     17  * @file src/backend/taler-merchant-httpd_delete-management-instances-INSTANCE.c
     18  * @brief implement DELETE /instances/$ID
     19  * @author Christian Grothoff
     20  */
     21 #include "platform.h"
     22 #include "taler-merchant-httpd_delete-management-instances-INSTANCE.h"
     23 #include <taler/taler_json_lib.h>
     24 #include <taler/taler_dbevents.h>
     25 #include "taler-merchant-httpd_mfa.h"
     26 #include "merchant-database/delete_instance_private_key.h"
     27 #include "merchant-database/event_notify.h"
     28 #include "merchant-database/purge_instance.h"
     29 
     30 /**
     31  * Handle a DELETE "/instances/$ID" request.
     32  *
     33  * @param[in,out] hc http request context
     34  * @param mfa_check true if a MFA check is required
     35  * @param mi instance to delete
     36  * @param connection the MHD connection to handle
     37  * @return MHD result code
     38  */
     39 static enum MHD_Result
     40 delete_instances_ID (struct TMH_HandlerContext *hc,
     41                      bool mfa_check,
     42                      struct TMH_MerchantInstance *mi,
     43                      struct MHD_Connection *connection)
     44 {
     45   bool purge;
     46   enum GNUNET_DB_QueryStatus qs;
     47 
     48   GNUNET_assert (NULL != mi);
     49   if (mfa_check)
     50   {
     51     enum GNUNET_GenericReturnValue ret =
     52       TMH_mfa_check_simple (hc,
     53                             TALER_MERCHANT_MFA_CO_INSTANCE_DELETION,
     54                             mi);
     55 
     56     if (GNUNET_OK != ret)
     57     {
     58       return (GNUNET_NO == ret)
     59         ? MHD_YES
     60         : MHD_NO;
     61     }
     62   }
     63 
     64   {
     65     const char *purge_s;
     66 
     67     purge_s = MHD_lookup_connection_value (connection,
     68                                            MHD_GET_ARGUMENT_KIND,
     69                                            "purge");
     70     if (NULL == purge_s)
     71       purge_s = "no";
     72     purge = (0 == strcasecmp (purge_s,
     73                               "yes"));
     74   }
     75   if (purge)
     76     qs = TALER_MERCHANTDB_purge_instance (TMH_db,
     77                                           mi->settings.id);
     78   else
     79     qs = TALER_MERCHANTDB_delete_instance_private_key (TMH_db,
     80                                                        mi->settings.id);
     81   {
     82     struct GNUNET_DB_EventHeaderP es = {
     83       .size = htons (sizeof (es)),
     84       .type = htons (TALER_DBEVENT_MERCHANT_ACCOUNTS_CHANGED)
     85     };
     86 
     87     TALER_MERCHANTDB_event_notify (TMH_db,
     88                                    &es,
     89                                    NULL,
     90                                    0);
     91   }
     92   switch (qs)
     93   {
     94   case GNUNET_DB_STATUS_HARD_ERROR:
     95     return TALER_MHD_reply_with_error (connection,
     96                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
     97                                        TALER_EC_GENERIC_DB_STORE_FAILED,
     98                                        "delete private key");
     99   case GNUNET_DB_STATUS_SOFT_ERROR:
    100     GNUNET_break (0);
    101     return TALER_MHD_reply_with_error (connection,
    102                                        MHD_HTTP_INTERNAL_SERVER_ERROR,
    103                                        TALER_EC_GENERIC_INTERNAL_INVARIANT_FAILURE,
    104                                        NULL);
    105   case GNUNET_DB_STATUS_SUCCESS_NO_RESULTS:
    106     return TALER_MHD_reply_with_error (connection,
    107                                        MHD_HTTP_NOT_FOUND,
    108                                        TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN,
    109                                        purge
    110                                        ? "Instance unknown"
    111                                        : "Private key unknown");
    112   case GNUNET_DB_STATUS_SUCCESS_ONE_RESULT:
    113     TMH_reload_instances (mi->settings.id);
    114     return TALER_MHD_reply_static (connection,
    115                                    MHD_HTTP_NO_CONTENT,
    116                                    NULL,
    117                                    NULL,
    118                                    0);
    119   }
    120   GNUNET_assert (0);
    121   return MHD_NO;
    122 }
    123 
    124 
    125 enum MHD_Result
    126 TMH_private_delete_instances_ID (
    127   const struct TMH_RequestHandler *rh,
    128   struct MHD_Connection *connection,
    129   struct TMH_HandlerContext *hc)
    130 {
    131   struct TMH_MerchantInstance *mi = hc->instance;
    132 
    133   (void) rh;
    134   return delete_instances_ID (hc,
    135                               true,
    136                               mi,
    137                               connection);
    138 }
    139 
    140 
    141 enum MHD_Result
    142 TMH_private_delete_instances_default_ID (
    143   const struct TMH_RequestHandler *rh,
    144   struct MHD_Connection *connection,
    145   struct TMH_HandlerContext *hc)
    146 {
    147   struct TMH_MerchantInstance *mi;
    148 
    149   (void) rh;
    150   mi = TMH_lookup_instance (hc->infix);
    151   if (NULL == mi)
    152   {
    153     return TALER_MHD_reply_with_error (
    154       connection,
    155       MHD_HTTP_NOT_FOUND,
    156       TALER_EC_MERCHANT_GENERIC_INSTANCE_UNKNOWN,
    157       hc->infix);
    158   }
    159   return delete_instances_ID (hc,
    160                               false,
    161                               mi,
    162                               connection);
    163 }
    164 
    165 
    166 /* end of taler-merchant-httpd_delete-management-instances-INSTANCE.c */