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