1 #include "openils/oils_utils.h"
2 #include "openils/oils_idl.h"
4 osrfHash* oilsInitIDL(const char* idl_filename) {
6 char* freeable_filename = NULL;
10 filename = idl_filename;
12 freeable_filename = osrf_settings_host_value("/IDL");
13 filename = freeable_filename;
17 osrfLogError(OSRF_LOG_MARK, "No settings config for '/IDL'");
21 osrfLogInfo(OSRF_LOG_MARK, "Parsing IDL %s", filename);
23 if (!oilsIDLInit( filename )) {
24 osrfLogError(OSRF_LOG_MARK, "Problem loading IDL file [%s]!", filename);
26 free(freeable_filename);
31 free(freeable_filename);
36 @brief Return a const string with the value of a specified column in a row object.
37 @param object Pointer to the row object.
38 @param field Name of the column.
39 @return Pointer to a const string representing the value of the specified column,
40 or NULL in case of error.
42 The row object must be a JSON_ARRAY with a classname. The column value must be a
43 JSON_STRING or a JSON_NUMBER. Any other object type results in a return of NULL.
45 The return value points into the internal contents of the row object, which
48 const char* oilsFMGetStringConst( const jsonObject* object, const char* field ) {
49 return jsonObjectGetString(oilsFMGetObject( object, field ));
53 @brief Return a string with the value of a specified column in a row object.
54 @param object Pointer to the row object.
55 @param field Name of the column.
56 @return Pointer to a newly allocated string representing the value of the specified column,
57 or NULL in case of error.
59 The row object must be a JSON_ARRAY with a classname. The column value must be a
60 JSON_STRING or a JSON_NUMBER. Any other object type results in a return of NULL.
62 The calling code is responsible for freeing the returned string by calling free().
64 char* oilsFMGetString( const jsonObject* object, const char* field ) {
65 return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
69 @brief Return a pointer to the value of a specified column in a row object.
70 @param object Pointer to the row object.
71 @param field Name of the column.
72 @return Pointer to the jsonObject representing the value of the specified column, or NULL
75 The row object must be a JSON_ARRAY with a classname.
77 The return value may point to a JSON_NULL, JSON_STRING, JSON_NUMBER, or JSON_ARRAY. It
78 points into the internal contents of the row object, which retains ownership.
80 const jsonObject* oilsFMGetObject( const jsonObject* object, const char* field ) {
81 if(!(object && field)) return NULL;
82 if( object->type != JSON_ARRAY || !object->classname ) return NULL;
83 int pos = fm_ntop(object->classname, field);
85 return jsonObjectGetIndex( object, pos );
90 int oilsFMSetString( jsonObject* object, const char* field, const char* string ) {
91 if(!(object && field && string)) return -1;
92 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Collecing position for field %s", field);
93 int pos = fm_ntop(object->classname, field);
95 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
96 "%s at field %s [position %d]", string, field, pos );
97 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
104 int oilsUtilsIsDBTrue( const char* val ) {
105 if( val && strcasecmp(val, "f") && strcmp(val, "0") ) return 1;
110 long oilsFMGetObjectId( const jsonObject* obj ) {
113 char* ids = oilsFMGetString( obj, "id" );
122 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
123 if (!permissions) return NULL;
125 oilsEvent* evt = NULL;
127 // Find the root org unit, i.e. the one with no parent.
128 // Assumption: there is only one org unit with no parent.
130 jsonObject* where_clause = jsonParse( "{\"parent_ou\":null}" );
131 jsonObject* org = oilsUtilsQuickReq(
133 "open-ils.cstore.direct.actor.org_unit.search",
136 jsonObjectFree( where_clause );
138 orgid = (int)jsonObjectGetNumber( oilsFMGetObject( org, "id" ) );
143 for( i = 0; i < size && permissions[i]; i++ ) {
145 char* perm = permissions[i];
146 jsonObject* params = jsonParseFmt("[%d, \"%s\", %d]", userid, perm, orgid);
147 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage",
148 "open-ils.storage.permission.user_has_perm", params );
150 char* r = jsonObjectToSimpleString(o);
152 if(r && !strcmp(r, "0"))
153 evt = oilsNewEvent3( OSRF_LOG_MARK, OILS_EVENT_PERM_FAILURE, perm, orgid );
155 jsonObjectFree(params);
167 @brief Perform a remote procedure call.
168 @param service The name of the service to invoke.
169 @param method The name of the method to call.
170 @param params The parameters to be passed to the method, if any.
171 @return A copy of whatever the method returns as a result, or a JSON_NULL if the method
172 doesn't return anything.
174 If the @a params parameter points to a JSON_ARRAY, pass each element of the array
175 as a separate parameter. If it points to any other kind of jsonObject, pass it as a
176 single parameter. If it is NULL, pass no parameters.
178 The calling code is responsible for freeing the returned object by calling jsonObjectFree().
180 jsonObject* oilsUtilsQuickReq( const char* service, const char* method,
181 const jsonObject* params ) {
182 if(!(service && method)) return NULL;
184 osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
186 // Open an application session with the service, and send the request
187 osrfAppSession* session = osrfAppSessionClientInit( service );
188 int reqid = osrfAppSessionSendRequest( session, params, method, 1 );
191 osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 );
192 jsonObject* result = jsonObjectClone( osrfMessageGetResult(omsg) );
195 osrfMessageFree(omsg);
196 osrfAppSessionFree(session);
201 @brief Call a method of the open-ils.storage service.
202 @param method Name of the method.
203 @param params Parameters to be passed to the method, if any.
204 @return A copy of whatever the method returns as a result, or a JSON_NULL if the method
205 doesn't return anything.
207 If the @a params parameter points to a JSON_ARRAY, pass each element of the array
208 as a separate parameter. If it points to any other kind of jsonObject, pass it as a
209 single parameter. If it is NULL, pass no parameters.
211 The calling code is responsible for freeing the returned object by calling jsonObjectFree().
213 jsonObject* oilsUtilsStorageReq( const char* method, const jsonObject* params ) {
214 return oilsUtilsQuickReq( "open-ils.storage", method, params );
218 @brief Call a method of the open-ils.cstore service.
219 @param method Name of the method.
220 @param params Parameters to be passed to the method, if any.
221 @return A copy of whatever the method returns as a result, or a JSON_NULL if the method
222 doesn't return anything.
224 If the @a params parameter points to a JSON_ARRAY, pass each element of the array
225 as a separate parameter. If it points to any other kind of jsonObject, pass it as a
226 single parameter. If it is NULL, pass no parameters.
228 The calling code is responsible for freeing the returned object by calling jsonObjectFree().
230 jsonObject* oilsUtilsCStoreReq( const char* method, const jsonObject* params ) {
231 return oilsUtilsQuickReq("open-ils.cstore", method, params);
237 @brief Given a username, fetch the corresponding row from the actor.usr table, if any.
238 @param name The username for which to search.
239 @return A Fieldmapper object for the relevant row in the actor.usr table, if it exists;
240 or a JSON_NULL if it doesn't.
242 The calling code is responsible for freeing the returned object by calling jsonObjectFree().
244 jsonObject* oilsUtilsFetchUserByUsername( const char* name ) {
245 if(!name) return NULL;
246 jsonObject* params = jsonParseFmt("{\"usrname\":\"%s\"}", name);
247 jsonObject* user = oilsUtilsQuickReq(
248 "open-ils.cstore", "open-ils.cstore.direct.actor.user.search", params );
250 jsonObjectFree(params);
251 long id = oilsFMGetObjectId(user);
252 osrfLogDebug(OSRF_LOG_MARK, "Fetched user %s:%ld", name, id);
257 @brief Given a barcode, fetch the corresponding row from the actor.usr table, if any.
258 @param name The barcode for which to search.
259 @return A Fieldmapper object for the relevant row in the actor.usr table, if it exists;
260 or a JSON_NULL if it doesn't.
262 Look up the barcode in actor.card. Follow a foreign key from there to get a row in
265 The calling code is responsible for freeing the returned object by calling jsonObjectFree().
267 jsonObject* oilsUtilsFetchUserByBarcode(const char* barcode) {
268 if(!barcode) return NULL;
270 osrfLogInfo(OSRF_LOG_MARK, "Fetching user by barcode %s", barcode);
272 jsonObject* params = jsonParseFmt("{\"barcode\":\"%s\"}", barcode);
273 jsonObject* card = oilsUtilsQuickReq(
274 "open-ils.cstore", "open-ils.cstore.direct.actor.card.search", params );
275 jsonObjectFree(params);
278 return NULL; // No such card
280 // Get the user's id as a double
281 char* usr = oilsFMGetString(card, "usr");
282 jsonObjectFree(card);
284 return NULL; // No user id (shouldn't happen)
285 double iusr = strtod(usr, NULL);
288 // Look up the user in actor.usr
289 params = jsonParseFmt("[%f]", iusr);
290 jsonObject* user = oilsUtilsQuickReq(
291 "open-ils.cstore", "open-ils.cstore.direct.actor.user.retrieve", params);
293 jsonObjectFree(params);
297 char* oilsUtilsFetchOrgSetting( int orgid, const char* setting ) {
298 if(!setting) return NULL;
300 jsonObject* params = jsonParseFmt("[%d, \"%s\"]", orgid, setting );
302 jsonObject* set = oilsUtilsQuickReq(
304 "open-ils.actor.ou_setting.ancestor_default", params);
306 char* value = jsonObjectToSimpleString(jsonObjectGetKey(set, "value"));
307 jsonObjectFree(params);
309 osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);
315 char* oilsUtilsLogin( const char* uname, const char* passwd, const char* type, int orgId ) {
316 if(!(uname && passwd)) return NULL;
318 osrfLogDebug(OSRF_LOG_MARK, "Logging in with username %s", uname );
321 jsonObject* params = jsonParseFmt("[\"%s\"]", uname);
323 jsonObject* o = oilsUtilsQuickReq(
324 "open-ils.auth", "open-ils.auth.authenticate.init", params );
326 const char* seed = jsonObjectGetString(o);
327 char* passhash = md5sum(passwd);
329 snprintf(buf, sizeof(buf), "%s%s", seed, passhash);
330 char* fullhash = md5sum(buf);
333 jsonObjectFree(params);
336 params = jsonParseFmt( "[\"%s\", \"%s\", \"%s\", \"%d\"]", uname, fullhash, type, orgId );
337 o = oilsUtilsQuickReq( "open-ils.auth",
338 "open-ils.auth.authenticate.complete", params );
341 const char* tok = jsonObjectGetString(
342 jsonObjectGetKey(jsonObjectGetKey(o,"payload"), "authtoken"));
344 token = strdup( tok );
348 jsonObjectFree(params);
355 jsonObject* oilsUtilsFetchWorkstation( long id ) {
356 jsonObject* p = jsonParseFmt("[%ld]", id);
357 jsonObject* r = oilsUtilsQuickReq(
359 "open-ils.storage.direct.actor.workstation.retrieve", p );
364 jsonObject* oilsUtilsFetchWorkstationByName( const char* name ) {
365 jsonObject* p = jsonParseFmt("{\"name\":\"%s\"}", name);
366 jsonObject* r = oilsUtilsCStoreReq(
367 "open-ils.cstore.direct.actor.workstation.search", p);