1 #include "oils_utils.h"
3 char* oilsFMGetString( jsonObject* object, char* field ) {
4 return jsonObjectToSimpleString(oilsFMGetObject( object, field ));
8 jsonObject* oilsFMGetObject( jsonObject* object, char* field ) {
9 if(!(object && field)) return NULL;
10 if( object->type != JSON_ARRAY || !object->classname ) return NULL;
11 int pos = fm_ntop(object->classname, field);
12 if( pos > -1 ) return jsonObjectGetIndex( object, pos );
17 int oilsFMSetString( jsonObject* object, char* field, char* string ) {
18 if(!(object && field && string)) return -1;
19 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Collecing position for field %s", field);
20 int pos = fm_ntop(object->classname, field);
22 osrfLogInternal(OSRF_LOG_MARK, "oilsFMSetString(): Setting string "
23 "%s at field %s [position %d]", string, field, pos );
24 jsonObjectSetIndex( object, pos, jsonNewObject(string) );
31 long oilsFMGetObjectId( jsonObject* obj ) {
34 char* ids = oilsFMGetString( obj, "id" );
35 if(ids) { id = atol(ids); free(ids); }
40 oilsEvent* oilsUtilsCheckPerms( int userid, int orgid, char* permissions[], int size ) {
41 if(!permissions) return NULL;
43 oilsEvent* evt = NULL;
44 if(orgid == -1) orgid = 1; /* XXX */
46 for( i = 0; i != size && permissions[i]; i++ ) {
48 char* perm = permissions[i];
49 jsonObject* params = jsonParseString("[%d, \"%s\", %d]", userid, perm, orgid);
50 jsonObject* o = oilsUtilsQuickReq( "open-ils.storage",
51 "open-ils.storage.permission.user_has_perm", params );
53 char* r = jsonObjectToSimpleString(o);
55 if(r && !strcmp(r, "0"))
56 evt = oilsNewEvent3( OILS_EVENT_PERM_FAILURE, perm, orgid );
58 jsonObjectFree(params);
68 jsonObject* oilsUtilsQuickReq( char* service, char* method, jsonObject* params ) {
69 if(!(service && method)) return NULL;
70 osrfLogDebug(OSRF_LOG_MARK, "oilsUtilsQuickReq(): %s - %s", service, method );
71 osrfAppSession* session = osrfAppSessionClientInit( service );
72 int reqid = osrfAppSessionMakeRequest( session, params, method, 1, NULL );
73 osrfMessage* omsg = osrfAppSessionRequestRecv( session, reqid, 60 );
74 jsonObject* result = jsonObjectClone(osrfMessageGetResult(omsg));
75 osrfMessageFree(omsg);
76 osrfAppSessionFree(session);
82 jsonObject* oilsUtilsFetchUserByUsername( char* name ) {
83 if(!name) return NULL;
84 jsonObject* params = jsonParseString("[\"%s\"]", name);
85 jsonObject* r = oilsUtilsQuickReq( "open-ils.storage",
86 "open-ils.storage.direct.actor.user.search.usrname.atomic", params );
87 jsonObject* user = jsonObjectClone(jsonObjectGetIndex( r, 0 ));
92 char* oilsUtilsFetchOrgSetting( int orgid, char* setting ) {
93 if(!setting) return NULL;
95 jsonObject* params = jsonParseString(
96 "[{ \"org_unit\": %d, \"name\":\"%s\" }]", orgid, setting );
98 jsonObject* set = oilsUtilsQuickReq(
100 "open-ils.storage.direct.actor.org_unit_setting.search_where", params );
102 jsonObjectFree(params);
103 char* value = oilsFMGetString( set, "value" );
105 osrfLogDebug(OSRF_LOG_MARK, "Fetched org [%d] setting: %s => %s", orgid, setting, value);