1 #include "oils_utils.h"
4 // XXX this is entirely untested!!!
5 osrfHash* oilsInitIDL(char* idl_filename) {
8 osrfLogError(OSRF_LOG_MARK, "At least give me a file to try!");
12 if (!oilsIDLInit( idl_filename )) {
13 osrfLogError(OSRF_LOG_MARK, "Problem loading IDL file [%s]!", idl_filename);
20 char* oilsFMGetString( jsonObject* object, char* field ) {
21 return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
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 );
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);
39 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
40 "%s at field %s [position %d]", string, field, pos );
41 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
48 int oilsUtilsIsDBTrue( char* val ) {
49 if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
54 long oilsFMGetObjectId( jsonObject* obj ) {
57 char* ids = oilsFMGetString( obj, "id" );
58 if(ids) { id = atol(ids); free(ids); }
63 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
64 if(!permissions) return NULL;
66 oilsEvent* evt = NULL;
67 if(orgid == -1) orgid = 1; /* XXX */
69 for( i = 0; i != size && permissions[i]; i++ ) {
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 );
76 char* r = jsonObjectToSimpleString(o);
78 if(r && !strcmp(r, "0"))
79 evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
81 jsonObjectFree(params);
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);
103 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
104 return oilsUtilsQuickReq( "open-ils.storage", method, params );
107 jsonObject* oilsUtilsCStoreReq( char* method, jsonObject* params ) {
108 return oilsUtilsQuickReq("open-ils.cstore", method, params);
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 );
119 jsonObjectFree(params);
120 long id = oilsFMGetObjectId(user);
121 osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
125 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
126 if(!barcode) return NULL;
128 osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
130 jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
131 jsonObject* card = oilsUtilsQuickReq(
132 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
134 if(!card) { jsonObjectFree(params); return NULL; }
136 char* usr = oilsFMGetString(card, "usr");
137 if(!usr) return NULL;
138 double iusr = strtod(usr, NULL);
141 jsonObjectFree(params);
142 params = jsonParseStringFmt("[%lf]", iusr);
143 jsonObject* user = oilsUtilsQuickReq(
144 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
146 jsonObjectFree(params);
150 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
151 if(!setting) return NULL;
153 jsonObject* params = jsonParseStringFmt(
154 "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
156 jsonObject* set = oilsUtilsQuickReq(
158 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
160 jsonObjectFree(params);
161 char* value = oilsFMGetString( set, "value" );
163 osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
170 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
171 if(!(uname && passwd)) return NULL;
173 osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
176 jsonObject* params = jsonParseStringFmt("[\"%s\"]", uname);
178 jsonObject* o = oilsUtilsQuickReq(
179 "open-ils.auth", "open-ils.auth.authenticate.init", params );
181 char* seed = jsonObjectGetString(o);
182 char* passhash = md5sum(passwd);
185 snprintf(buf, 255, "%s%s", seed, passhash);
186 char* fullhash = md5sum(buf);
189 jsonObjectFree(params);
192 params = jsonParseStringFmt( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
193 o = oilsUtilsQuickReq( "open-ils.auth",
194 "open-ils.auth.authenticate.complete", params );
197 char* tok = jsonObjectGetString(
198 jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
199 if(tok) token = strdup(tok);
203 jsonObjectFree(params);
210 jsonObject* oilsUtilsFetchWorkstation( long id ) {
211 jsonObject* p = jsonParseStringFmt("[%ld]", id);
212 jsonObject* r = oilsUtilsQuickReq(
214 "open-ils.storage.direct.actor.workstation.retrieve", p );
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 );