1 #include "oils_utils.h"
4 osrfHash* oilsInitIDL(char* idl_filename) {
8 idl_filename = osrf_settings_host_value("/IDL");
13 osrfLogError(OSRF_LOG_MARK, "No settings config for '/IDL'");
17 osrfLogInfo(OSRF_LOG_MARK, "Parsing IDL %s", idl_filename);
19 if (!oilsIDLInit( idl_filename )) {
20 osrfLogError(OSRF_LOG_MARK, "Problem loading IDL file [%s]!", idl_filename);
24 if(freeme) free(idl_filename);
28 char* oilsFMGetString( jsonObject* object, char* field ) {
29 return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
33 jsonObject* oilsFMGetObject( jsonObject* object, char* field ) {
34 if(!(object && field)) return NULL;
35 if( object->type != JSON_ARRAY || !object->classname ) return NULL;
36 int pos = fm_ntop(object->classname, field);
37 if( pos > -1 ) return jsonObjectGetIndex( object, pos );
42 int oilsFMSetString( jsonObject* object, char* field, char* string ) {
43 if(!(object && field && string)) return -1;
44 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Collecing position for field %s", field);
45 int pos = fm_ntop(object->classname, field);
47 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
48 "%s at field %s [position %d]", string, field, pos );
49 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
56 int oilsUtilsIsDBTrue( char* val ) {
57 if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
62 long oilsFMGetObjectId( jsonObject* obj ) {
65 char* ids = oilsFMGetString( obj, "id" );
66 if(ids) { id = atol(ids); free(ids); }
71 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
72 if(!permissions) return NULL;
74 oilsEvent* evt = NULL;
75 if(orgid == -1) orgid = 1; /* XXX */
77 for( i = 0; i != size && permissions[i]; i++ ) {
79 char* perm = permissions[i];
80 jsonObject* params = jsonParseStringFmt("[%d, \"%s\", %d]", userid, perm, orgid);
81 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage",
82 "open-ils.storage.permission.user_has_perm", params );
84 char* r = jsonObjectToSimpleString(o);
86 if(r && !strcmp(r, "0"))
87 evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
89 jsonObjectFree(params);
99 jsonObject* oilsUtilsQuickReq( char* service, char* method, jsonObject* params ) {
100 if(!(service && method)) return NULL;
101 osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
102 osrfAppSession* session = osrfAppSessionClientInit( service );
103 int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
104 osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 );
105 jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
106 osrfMessageFree(omsg);
107 osrfAppSessionFree(session);
111 jsonObject* oilsUtilsStorageReq( char* method, jsonObject* params ) {
112 return oilsUtilsQuickReq( "open-ils.storage", method, params );
115 jsonObject* oilsUtilsCStoreReq( char* method, jsonObject* params ) {
116 return oilsUtilsQuickReq("open-ils.cstore", method, params);
121 jsonObject* oilsUtilsFetchUserByUsername( char* name ) {
122 if(!name) return NULL;
123 jsonObject* params = jsonParseStringFmt("{\"usrname\":\"%s\"}", name);
124 jsonObject* user = oilsUtilsQuickReq(
125 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
127 jsonObjectFree(params);
128 long id = oilsFMGetObjectId(user);
129 osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
133 jsonObject* oilsUtilsFetchUserByBarcode(char* barcode) {
134 if(!barcode) return NULL;
136 osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
138 jsonObject* params = jsonParseStringFmt("{\"barcode\":\"%s\"}", barcode);
139 jsonObject* card = oilsUtilsQuickReq(
140 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
142 if(!card) { jsonObjectFree(params); return NULL; }
144 char* usr = oilsFMGetString(card, "usr");
145 if(!usr) return NULL;
146 double iusr = strtod(usr, NULL);
149 jsonObjectFree(params);
150 params = jsonParseStringFmt("[%f]", iusr);
151 jsonObject* user = oilsUtilsQuickReq(
152 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
154 jsonObjectFree(params);
158 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
159 if(!setting) return NULL;
161 jsonObject* params = jsonParseStringFmt(
162 "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
164 jsonObject* set = oilsUtilsQuickReq(
166 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
168 jsonObjectFree(params);
169 char* value = oilsFMGetString( set, "value" );
171 osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
178 char* oilsUtilsLogin( char* uname, char* passwd, char* type, int orgId ) {
179 if(!(uname && passwd)) return NULL;
181 osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
184 jsonObject* params = jsonParseStringFmt("[\"%s\"]", uname);
186 jsonObject* o = oilsUtilsQuickReq(
187 "open-ils.auth", "open-ils.auth.authenticate.init", params );
189 char* seed = jsonObjectGetString(o);
190 char* passhash = md5sum(passwd);
193 snprintf(buf, 255, "%s%s", seed, passhash);
194 char* fullhash = md5sum(buf);
197 jsonObjectFree(params);
200 params = jsonParseStringFmt( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
201 o = oilsUtilsQuickReq( "open-ils.auth",
202 "open-ils.auth.authenticate.complete", params );
205 char* tok = jsonObjectGetString(
206 jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
207 if(tok) token = strdup(tok);
211 jsonObjectFree(params);
218 jsonObject* oilsUtilsFetchWorkstation( long id ) {
219 jsonObject* p = jsonParseStringFmt("[%ld]", id);
220 jsonObject* r = oilsUtilsQuickReq(
222 "open-ils.storage.direct.actor.workstation.retrieve", p );
227 jsonObject* oilsUtilsFetchWorkstationByName( char* name ) {
228 jsonObject* p = jsonParseStringFmt("[\"%s\"]", name);
229 jsonObject* r = oilsUtilsStorageReq(
230 "open-ils.storage.direct.actor.workstation.search.name", p );