1 /***************************************************************************
2  begin       : Tue May 03 2005
3  copyright   : (C) 2018 by Martin Preuss
4  email       : martin@libchipcard.de
5 
6  ***************************************************************************
7  *          Please see toplevel file COPYING for license details           *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 
15 #include "globals_l.h"
16 
17 #include <gwenhywfar/text.h>
18 
19 #include "aqhbci/banking/user.h"
20 
21 
22 
AH_Control_SetMaxTransfers(AB_PROVIDER * pro,GWEN_DB_NODE * dbArgs,int argc,char ** argv)23 int AH_Control_SetMaxTransfers(AB_PROVIDER *pro,
24                                GWEN_DB_NODE *dbArgs,
25                                int argc,
26                                char **argv)
27 {
28   GWEN_DB_NODE *db;
29   uint32_t uid;
30   AB_USER *u=NULL;
31   int rv;
32   int maxTransfers;
33   int maxDebitNotes;
34   const GWEN_ARGS args[]= {
35     {
36       GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
37       GWEN_ArgsType_Int,            /* type */
38       "userId",                     /* name */
39       0,                            /* minnum */
40       1,                            /* maxnum */
41       "u",                          /* short option */
42       "user",                       /* long option */
43       "Specify the unique user id",    /* short description */
44       "Specify the unique user id"     /* long description */
45     },
46     {
47       GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
48       GWEN_ArgsType_Int,            /* type */
49       "maxTransfers",               /* name */
50       0,                            /* minnum */
51       1,                            /* maxnum */
52       "t",                          /* short option */
53       "transfers",                  /* long option */
54       "Specify the maximum number of transfers per job",    /* short description */
55       "Specify the maximum number of transfers per job"     /* long description */
56     },
57     {
58       GWEN_ARGS_FLAGS_HAS_ARGUMENT, /* flags */
59       GWEN_ArgsType_Int,            /* type */
60       "maxDebitNotes",              /* name */
61       0,                            /* minnum */
62       1,                            /* maxnum */
63       "d",                          /* short option */
64       "debitnotes",                 /* long option */
65       "Specify the maximum number of debit notes per job",    /* short description */
66       "Specify the maximum number of debit notes per job"     /* long description */
67     },
68     {
69       GWEN_ARGS_FLAGS_HELP | GWEN_ARGS_FLAGS_LAST, /* flags */
70       GWEN_ArgsType_Int,            /* type */
71       "help",                       /* name */
72       0,                            /* minnum */
73       0,                            /* maxnum */
74       "h",                          /* short option */
75       "help",                       /* long option */
76       "Show this help screen",      /* short description */
77       "Show this help screen"       /* long description */
78     }
79   };
80 
81   db=GWEN_DB_GetGroup(dbArgs, GWEN_DB_FLAGS_DEFAULT, "local");
82   rv=GWEN_Args_Check(argc, argv, 1,
83                      0 /*GWEN_ARGS_MODE_ALLOW_FREEPARAM*/,
84                      args,
85                      db);
86   if (rv==GWEN_ARGS_RESULT_ERROR) {
87     fprintf(stderr, "ERROR: Could not parse arguments\n");
88     return 1;
89   }
90   else if (rv==GWEN_ARGS_RESULT_HELP) {
91     GWEN_BUFFER *ubuf;
92 
93     ubuf=GWEN_Buffer_new(0, 1024, 0, 1);
94     if (GWEN_Args_Usage(args, ubuf, GWEN_ArgsOutType_Txt)) {
95       fprintf(stderr, "ERROR: Could not create help string\n");
96       return 1;
97     }
98     fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(ubuf));
99     GWEN_Buffer_free(ubuf);
100     return 0;
101   }
102 
103   /* get and check params */
104   maxTransfers=GWEN_DB_GetIntValue(db, "maxTransfers", 0, -1);
105   maxDebitNotes=GWEN_DB_GetIntValue(db, "maxDebitNotes", 0, -1);
106 
107 
108   /* doit */
109   uid=(uint32_t) GWEN_DB_GetIntValue(db, "userId", 0, 0);
110   if (uid==0) {
111     fprintf(stderr, "ERROR: Invalid or missing unique user id\n");
112     return 1;
113   }
114 
115   rv=AB_Provider_HasUser(pro, uid);
116   if (rv<0) {
117     fprintf(stderr, "ERROR: User with id %lu not found\n", (unsigned long int) uid);
118     return 2;
119   }
120   rv=AB_Provider_GetUser(pro, uid, 1, 0, &u); /* don't lock to allow for AH_Provider_EndExclUseUser */
121   if (rv<0) {
122     fprintf(stderr, "ERROR: User with id %lu not found\n", (unsigned long int) uid);
123     return 2;
124   }
125   else {
126     /* modify */
127     if (maxTransfers>0) {
128       fprintf(stderr, "Setting maximum number of transfers per job to %d\n", maxTransfers);
129       AH_User_SetMaxTransfersPerJob(u, maxTransfers);
130     }
131 
132     if (maxDebitNotes>0) {
133       fprintf(stderr, "Setting maximum number of debit notes per job to %d\n", maxDebitNotes);
134       AH_User_SetMaxDebitNotesPerJob(u, maxDebitNotes);
135     }
136 
137     /* unlock user */
138     rv=AB_Provider_EndExclUseUser(pro, u, 0);
139     if (rv<0) {
140       fprintf(stderr, "ERROR: Could not unlock user (%d)\n", rv);
141       AB_Provider_EndExclUseUser(pro, u, 1); /* abort */
142       AB_User_free(u);
143       return 4;
144     }
145   }
146   AB_User_free(u);
147 
148   return 0;
149 }
150 
151 
152 
153 
154