]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/c-apps/oils_utils.c
deprecate libfieldmapper by moving to the idl interface built for cstore
[working/Evergreen.git] / Open-ILS / src / c-apps / oils_utils.c
1 #include "oils_utils.h"
2 #include "oils_idl.h"
3
4 // XXX this is entirely untested!!!
5 osrfHash* oilsInitIDL(char* idl_filename) {
6
7         if (!idl_filename) {
8                 osrfLogError(OSRF_LOG_MARK, "At least give me a file to try!");
9                 return NULL;
10         }
11
12         if (!oilsIDLInit( idl_filename )) {
13                 osrfLogError(OSRF_LOG_MARK, "Problem loading IDL file [%s]!", idl_filename);
14                 return NULL;
15         }
16
17         return oilsIDL();
18 }
19
20 char* oilsFMGetString( jsonObject* object, char* field ) {
21         return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
22 }
23
24
25 jsonObject* oilsFMGetObject( jsonObject* object, char* field ) {
26         if(!(object && field)) return NULL;
27         if( object->type != JSON_ARRAY || !object->classname ) return NULL;
28         int pos = fm_ntop(object->classname, field);
29         if( pos > -1 ) return jsonObjectGetIndex( object, pos );
30         return NULL;
31 }
32
33
34 int oilsFMSetString( jsonObject* object, char* field, char* string ) {
35         if(!(object && field && string)) return -1;
36         osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Collecing position for field %s", field);
37         int pos = fm_ntop(object->classname, field);
38         if( pos > -1 ) {
39                 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
40                                 "%s at field %s [position %d]", string, field, pos );
41                 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
42                 return 0;
43         }
44         return -1;
45 }
46
47
48 int oilsUtilsIsDBTrue( char* val ) {
49         if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
50         return 0;
51 }
52
53
54 long oilsFMGetObjectId( jsonObject* obj ) {
55         long id = -1;
56         if(!obj) return id;
57         char* ids = oilsFMGetString( obj, "id" );
58         if(ids) { id = atol(ids); free(ids); }
59         return id;
60 }
61
62
63 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
64         if(!permissions) return NULL;
65         int i;
66         oilsEvent* evt = NULL;
67         if(orgid == -1) orgid = 1; /* XXX  */
68
69         for( i = 0; i != size && permissions[i]; i++ ) {
70
71                 char* perm = permissions[i];
72                 jsonObject* params = jsonParseStringFmt("[%d, \"%s\", %d]", userid, perm, orgid);
73                 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage", 
74                         "open-ils.storage.permission.user_has_perm", params );
75
76                 char* r = jsonObjectToSimpleString(o);
77
78                 if(r && !strcmp(r, "0")) 
79                         evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
80
81                 jsonObjectFree(params);
82                 jsonObjectFree(o);
83                 free(r);
84
85                 if(evt) break;
86         }
87
88         return evt;
89 }
90
91 jsonObject* oilsUtilsQuickReq( char* service, char* method, jsonObject* params ) {
92         if(!(service && method)) return NULL;
93         osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
94         osrfAppSession* session = osrfAppSessionClientInit( service ); 
95         int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
96         osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 ); 
97         jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
98         osrfMessageFree(omsg);
99         osrfAppSessionFree(session);
100         return result;
101 }
102
103 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
104         return oilsUtilsQuickReq( "open-ils.storage", method, params );
105 }
106
107 jsonObject* oilsUtilsCStoreReq( char* method, jsonObject* params ) {
108         return oilsUtilsQuickReq("open-ils.cstore", method, params);
109 }
110
111
112
113 jsonObject* oilsUtilsFetchUserByUsername( char* name ) {
114         if(!name) return NULL;
115         jsonObject* params = jsonParseStringFmt("{\"usrname\":\"%s\"}", name);
116         jsonObject* user = oilsUtilsQuickReq( 
117                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
118
119         jsonObjectFree(params);
120         long id = oilsFMGetObjectId(user);
121         osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
122         return user;
123 }
124
125 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
126         if(!barcode) return NULL;
127
128         osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
129
130         jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
131         jsonObject* card = oilsUtilsQuickReq(
132                 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
133
134         if(!card) { jsonObjectFree(params); return NULL; }
135
136         char* usr = oilsFMGetString(card, "usr");
137         if(!usr) return NULL;
138         double iusr = strtod(usr, NULL);
139         free(usr);
140
141         jsonObjectFree(params);
142         params = jsonParseStringFmt("[%lf]", iusr);
143         jsonObject* user = oilsUtilsQuickReq(
144                 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
145
146         jsonObjectFree(params);
147         return user;
148 }
149
150 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
151         if(!setting) return NULL;
152
153         jsonObject* params = jsonParseStringFmt(
154                         "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
155
156         jsonObject* set = oilsUtilsQuickReq(
157                 "open-ils.storage",
158                 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
159
160         jsonObjectFree(params);
161         char* value = oilsFMGetString( set, "value" );
162         jsonObjectFree(set);
163         osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
164         return value;
165
166 }
167
168
169
170 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
171         if(!(uname && passwd)) return NULL;
172
173         osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
174         char* token = NULL;
175
176         jsonObject* params = jsonParseStringFmt("[\"%s\"]", uname);
177
178         jsonObject* o = oilsUtilsQuickReq( 
179                 "open-ils.auth", "open-ils.auth.authenticate.init", params );
180
181         char* seed = jsonObjectGetString(o);
182         char* passhash = md5sum(passwd);
183         char buf[256];
184         bzero(buf, 256);
185         snprintf(buf, 255, "%s%s", seed, passhash);
186         char* fullhash = md5sum(buf);
187
188         jsonObjectFree(o);
189         jsonObjectFree(params);
190         free(passhash);
191
192         params = jsonParseStringFmt( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
193         o = oilsUtilsQuickReq( "open-ils.auth",
194                 "open-ils.auth.authenticate.complete", params );
195
196         if(o) {
197                 char* tok = jsonObjectGetString(
198                         jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
199                 if(tok) token = strdup(tok);
200         }
201
202         free(fullhash);
203         jsonObjectFree(params);
204         jsonObjectFree(o);
205
206         return token;
207 }
208
209
210 jsonObject* oilsUtilsFetchWorkstation( long id ) {
211         jsonObject* p = jsonParseStringFmt("[%ld]", id);
212         jsonObject* r = oilsUtilsQuickReq(
213                 "open-ils.storage", 
214                 "open-ils.storage.direct.actor.workstation.retrieve", p );
215         jsonObjectFree(p);
216         return r;
217 }
218
219 jsonObject* oilsUtilsFetchWorkstationByName( char* name ) {
220         jsonObject* p = jsonParseStringFmt("[\"%s\"]", name);
221         jsonObject* r = oilsUtilsStorageReq(
222                 "open-ils.storage.direct.actor.workstation.search.name", p );
223         jsonObjectFree(p);
224         return r;
225 }
226
227